Skip to content

Commit 5991522

Browse files
committed
feat(compiler): Switch from rawAttrsList to rawAttrsMap
1 parent ca71c0d commit 5991522

File tree

13 files changed

+33
-54
lines changed

13 files changed

+33
-54
lines changed

Diff for: flow/compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ declare type ASTElement = {
8686
type: 1;
8787
tag: string;
8888
attrsList: Array<ASTAttr>;
89-
rawAttrsList?: Array<ASTAttr>;
9089
attrsMap: { [key: string]: string | null };
90+
rawAttrsMap: { [key: string]: ASTAttr };
9191
parent: ASTElement | void;
9292
children: Array<ASTNode>;
9393

Diff for: src/compiler/codegen/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { genHandlers } from './events'
44
import baseDirectives from '../directives/index'
55
import { camelize, no, extend } from 'shared/util'
6-
import { baseWarn, getRawAttr, pluckModuleFunction } from '../helpers'
6+
import { baseWarn, pluckModuleFunction } from '../helpers'
77

88
type TransformFunction = (el: ASTElement, code: string) => string;
99
type DataGenFunction = (el: ASTElement) => string;
@@ -116,7 +116,7 @@ function genOnce (el: ASTElement, state: CodegenState): string {
116116
if (!key) {
117117
process.env.NODE_ENV !== 'production' && state.warn(
118118
`v-once can only be used inside v-for that is keyed. `,
119-
getRawAttr(el, 'v-once')
119+
el.rawAttrsMap['v-once']
120120
)
121121
return genElement(el, state)
122122
}
@@ -188,7 +188,7 @@ export function genFor (
188188
`<${el.tag} v-for="${alias} in ${exp}">: component lists rendered with ` +
189189
`v-for should have explicit keys. ` +
190190
`See https://vuejs.org/guide/list.html#key for more info.`,
191-
getRawAttr(el, 'v-for'),
191+
el.rawAttrsMap['v-for'],
192192
true /* tip */
193193
)
194194
}

Diff for: src/compiler/error-detector.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* @flow */
22

3-
import { getRawAttr } from './helpers'
43
import { dirRE, onRE } from './parser/index'
54

65
type Range = { start?: number, end?: number };
@@ -34,7 +33,7 @@ function checkNode (node: ASTNode, warn: Function) {
3433
if (dirRE.test(name)) {
3534
const value = node.attrsMap[name]
3635
if (value) {
37-
const range = getRawAttr(node, name)
36+
const range = node.rawAttrsMap[name]
3837
if (name === 'v-for') {
3938
checkFor(node, `v-for="${value}"`, warn, range)
4039
} else if (onRE.test(name)) {

Diff for: src/compiler/helpers.js

+3-21
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,13 @@ export function addHandler (
112112
}
113113
}
114114

115-
export function getRawAttr (
116-
el: ASTElement,
117-
name: string
118-
) {
119-
const list = el.rawAttrsList
120-
if (!list) {
121-
return
122-
}
123-
for (let i = list.length - 1; i >= 0; i--) {
124-
if (list[i].name === name) {
125-
return list[i]
126-
}
127-
}
128-
}
129-
130115
export function getRawBindingAttr (
131116
el: ASTElement,
132117
name: string
133118
) {
134-
if (!el.rawAttrsList) {
135-
return
136-
}
137-
return getRawAttr(el, ':' + name) ||
138-
getRawAttr(el, 'v-bind:' + name) ||
139-
getRawAttr(el, name)
119+
return el.rawAttrsMap[':' + name] ||
120+
el.rawAttrsMap['v-bind:' + name] ||
121+
el.rawAttrsMap[name]
140122
}
141123

142124
export function getBindingAttr (

Diff for: src/compiler/optimizer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function optimize (root: ?ASTElement, options: CompilerOptions) {
3030

3131
function genStaticKeys (keys: string): Function {
3232
return makeMap(
33-
'type,tag,attrsList,attrsMap,plain,parent,children,attrs,start,end,rawAttrsList' +
33+
'type,tag,attrsList,attrsMap,plain,parent,children,attrs,start,end,rawAttrsMap' +
3434
(keys ? ',' + keys : '')
3535
)
3636
}

Diff for: src/compiler/parser/index.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
baseWarn,
1515
addHandler,
1616
addDirective,
17-
getRawAttr,
1817
getBindingAttr,
1918
getAndRemoveAttr,
2019
getRawBindingAttr,
@@ -53,6 +52,7 @@ export function createASTElement (
5352
tag,
5453
attrsList: attrs,
5554
attrsMap: makeAttrsMap(attrs),
55+
rawAttrsMap: {},
5656
parent,
5757
children: []
5858
}
@@ -128,8 +128,11 @@ export function parse (
128128
}
129129

130130
if (options.outputSourceRange) {
131-
element.rawAttrsList = element.attrsList.slice()
132131
element.start = start
132+
element.rawAttrsMap = element.attrsList.reduce((cumulated, attr) => {
133+
cumulated[attr.name] = attr
134+
return cumulated
135+
}, {})
133136
}
134137

135138
if (isForbiddenTag(element) && !isServerRendering()) {
@@ -180,7 +183,7 @@ export function parse (
180183
warnOnce(
181184
'Cannot use v-for on stateful component root element because ' +
182185
'it renders multiple elements.',
183-
getRawAttr(el, 'v-for')
186+
el.rawAttrsMap['v-for']
184187
)
185188
}
186189
}
@@ -388,7 +391,7 @@ export function processFor (el: ASTElement) {
388391
if (!inMatch) {
389392
process.env.NODE_ENV !== 'production' && warn(
390393
`Invalid v-for expression: ${exp}`,
391-
getRawAttr(el, 'v-for')
394+
el.rawAttrsMap['v-for']
392395
)
393396
return
394397
}
@@ -437,7 +440,7 @@ function processIfConditions (el, parent) {
437440
warn(
438441
`v-${el.elseif ? ('else-if="' + el.elseif + '"') : 'else'} ` +
439442
`used on element <${el.tag}> without corresponding v-if.`,
440-
getRawAttr(el, el.elseif ? 'v-else-if' : 'v-else')
443+
el.rawAttrsMap[el.elseif ? 'v-else-if' : 'v-else']
441444
)
442445
}
443446
}
@@ -496,7 +499,7 @@ function processSlot (el) {
496499
`replaced by "slot-scope" since 2.5. The new "slot-scope" attribute ` +
497500
`can also be used on plain elements in addition to <template> to ` +
498501
`denote scoped slots.`,
499-
getRawAttr(el, 'scope'),
502+
el.rawAttrsMap['scope'],
500503
true
501504
)
502505
}
@@ -508,7 +511,7 @@ function processSlot (el) {
508511
`Ambiguous combined usage of slot-scope and v-for on <${el.tag}> ` +
509512
`(v-for takes higher priority). Use a wrapper <template> for the ` +
510513
`scoped slot to make it clearer.`,
511-
getRawAttr(el, 'slot-scope'),
514+
el.rawAttrsMap['slot-scope'],
512515
true
513516
)
514517
}
@@ -693,7 +696,7 @@ function checkForAliasModel (el, value) {
693696
`This will not be able to modify the v-for source array because ` +
694697
`writing to the alias is like modifying a function local variable. ` +
695698
`Consider using an array of objects and use v-model on an object property instead.`,
696-
getRawAttr(el, 'v-model')
699+
el.rawAttrsMap['v-model']
697700
)
698701
}
699702
_el = _el.parent

Diff for: src/platforms/web/compiler/directives/model.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import config from 'core/config'
4-
import { addHandler, addProp, getRawAttr, getBindingAttr } from 'compiler/helpers'
4+
import { addHandler, addProp, getBindingAttr } from 'compiler/helpers'
55
import { genComponentModel, genAssignmentCode } from 'compiler/directives/model'
66

77
let warn
@@ -29,7 +29,7 @@ export default function model (
2929
warn(
3030
`<${el.tag} v-model="${value}" type="file">:\n` +
3131
`File inputs are read only. Use a v-on:change listener instead.`,
32-
getRawAttr(el, 'v-model')
32+
el.rawAttrsMap['v-model']
3333
)
3434
}
3535
}
@@ -56,7 +56,7 @@ export default function model (
5656
`v-model is not supported on this element type. ` +
5757
'If you are working with contenteditable, it\'s recommended to ' +
5858
'wrap a library dedicated for that purpose inside a custom component.',
59-
getRawAttr(el, 'v-model')
59+
el.rawAttrsMap['v-model']
6060
)
6161
}
6262

@@ -139,7 +139,7 @@ function genDefaultModel (
139139
warn(
140140
`${binding}="${value}" conflicts with v-model on the same element ` +
141141
'because the latter already expands to a value binding internally',
142-
getRawAttr(el, binding)
142+
el.rawAttrsMap[binding]
143143
)
144144
}
145145
}

Diff for: src/platforms/web/compiler/modules/class.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { parseText } from 'compiler/parser/text-parser'
44
import {
55
getAndRemoveAttr,
66
getBindingAttr,
7-
getRawAttr,
87
baseWarn
98
} from 'compiler/helpers'
109

@@ -19,7 +18,7 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
1918
'Interpolation inside attributes has been removed. ' +
2019
'Use v-bind or the colon shorthand instead. For example, ' +
2120
'instead of <div class="{{ val }}">, use <div :class="val">.',
22-
getRawAttr(el, 'class')
21+
el.rawAttrsMap['class']
2322
)
2423
}
2524
}

Diff for: src/platforms/web/compiler/modules/style.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { parseStyleText } from 'web/util/style'
55
import {
66
getAndRemoveAttr,
77
getBindingAttr,
8-
getRawAttr,
98
baseWarn
109
} from 'compiler/helpers'
1110

@@ -22,7 +21,7 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
2221
'Interpolation inside attributes has been removed. ' +
2322
'Use v-bind or the colon shorthand instead. For example, ' +
2423
'instead of <div style="{{ val }}">, use <div :style="val">.',
25-
getRawAttr(el, 'style')
24+
el.rawAttrsMap['style']
2625
)
2726
}
2827
}

Diff for: src/platforms/weex/compiler/modules/class.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { parseText } from 'compiler/parser/text-parser'
44
import {
55
getAndRemoveAttr,
66
getBindingAttr,
7-
getRawAttr,
87
baseWarn
98
} from 'compiler/helpers'
109

@@ -22,7 +21,7 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
2221
`class="${staticClass}": ` +
2322
'Interpolation inside attributes has been deprecated. ' +
2423
'Use v-bind or the colon shorthand instead.',
25-
getRawAttr(el, 'class')
24+
el.rawAttrsMap['class']
2625
)
2726
}
2827
if (!dynamic && classResult) {

Diff for: src/platforms/weex/compiler/modules/props.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
2222
el.attrsMap[realName] = el.attrsMap[attr.name]
2323
delete el.attrsMap[attr.name]
2424
}
25-
const rawAttrsList = el.rawAttrsList || []
26-
for (let i = rawAttrsList.length - 1; i >= 0; i--) {
27-
if (rawAttrsList[i].name === attr.name) {
28-
rawAttrsList[i].name = realName
29-
break
30-
}
25+
if (el.rawAttrsMap && el.rawAttrsMap[attr.name]) {
26+
el.rawAttrsMap[realName] = el.rawAttrsMap[attr.name]
27+
// $flow-disable-line
28+
delete el.rawAttrsMap[attr.name]
3129
}
3230
attr.name = realName
3331
}

Diff for: src/platforms/weex/compiler/modules/style.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { parseText } from 'compiler/parser/text-parser'
55
import {
66
getAndRemoveAttr,
77
getBindingAttr,
8-
getRawAttr,
98
baseWarn
109
} from 'compiler/helpers'
1110

@@ -25,7 +24,7 @@ function transformNode (el: ASTElement, options: CompilerOptions) {
2524
`style="${String(staticStyle)}": ` +
2625
'Interpolation inside attributes has been deprecated. ' +
2726
'Use v-bind or the colon shorthand instead.',
28-
getRawAttr(el, 'style')
27+
el.rawAttrsMap['style']
2928
)
3029
}
3130
if (!dynamic && styleResult) {

Diff for: src/server/optimizing-compiler/optimizer.js

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function optimizeSiblings (el) {
8484
tag: 'template',
8585
attrsList: [],
8686
attrsMap: {},
87+
rawAttrsMap: {},
8788
children: currentOptimizableGroup,
8889
ssrOptimizability: optimizability.FULL
8990
})

0 commit comments

Comments
 (0)