mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-03 04:28:04 +08:00
rename ASTElement node.conditions -> node.ifConditions, avoid mutating it during codegen (close #4317)
This commit is contained in:
parent
16e3dae245
commit
1903df4991
@ -98,7 +98,7 @@ declare type ASTElement = {
|
||||
ifProcessed?: boolean;
|
||||
elseif?: string;
|
||||
else?: true;
|
||||
conditions?: ASTIfConditions;
|
||||
ifConditions?: ASTIfConditions;
|
||||
|
||||
for?: string;
|
||||
forProcessed?: boolean;
|
||||
|
@ -112,7 +112,7 @@ function genOnce (el: ASTElement): string {
|
||||
|
||||
function genIf (el: any): string {
|
||||
el.ifProcessed = true // avoid recursion
|
||||
return genIfConditions(el.conditions)
|
||||
return genIfConditions(el.ifConditions.slice())
|
||||
}
|
||||
|
||||
function genIfConditions (conditions: ASTIfConditions): string {
|
||||
@ -127,7 +127,7 @@ function genIfConditions (conditions: ASTIfConditions): string {
|
||||
return `${genTernaryExp(condition.block)}`
|
||||
}
|
||||
|
||||
// v-if with v-once shuold generate code like (a)?_m(0):_m(1)
|
||||
// v-if with v-once should generate code like (a)?_m(0):_m(1)
|
||||
function genTernaryExp (el) {
|
||||
return el.once ? genOnce(el) : genElement(el)
|
||||
}
|
||||
|
@ -80,8 +80,8 @@ function markStaticRoots (node: ASTNode, isInFor: boolean) {
|
||||
markStaticRoots(node.children[i], isInFor || !!node.for)
|
||||
}
|
||||
}
|
||||
if (node.conditions) {
|
||||
walkThroughConditionsBlocks(node.conditions, isInFor)
|
||||
if (node.ifConditions) {
|
||||
walkThroughConditionsBlocks(node.ifConditions, isInFor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -353,10 +353,10 @@ function processIfConditions (el, parent) {
|
||||
}
|
||||
|
||||
function addIfCondition (el, condition) {
|
||||
if (!el.conditions) {
|
||||
el.conditions = []
|
||||
if (!el.ifConditions) {
|
||||
el.ifConditions = []
|
||||
}
|
||||
el.conditions.push(condition)
|
||||
el.ifConditions.push(condition)
|
||||
}
|
||||
|
||||
function processOnce (el) {
|
||||
|
@ -66,7 +66,7 @@ describe('optimizer', () => {
|
||||
optimize(ast, baseOptions)
|
||||
expect(ast.static).toBe(false)
|
||||
expect(ast.children[0].static).toBe(false)
|
||||
expect(ast.children[0].conditions[1].block.static).toBeUndefined()
|
||||
expect(ast.children[0].ifConditions[1].block.static).toBeUndefined()
|
||||
})
|
||||
|
||||
it('v-pre directive', () => {
|
||||
@ -228,13 +228,13 @@ describe('optimizer', () => {
|
||||
</div>
|
||||
`, baseOptions)
|
||||
optimize(ast, baseOptions)
|
||||
expect(ast.conditions[1].block.children[0].children[0].conditions[1].block.staticRoot).toBe(false)
|
||||
expect(ast.conditions[1].block.children[0].children[0].conditions[1].block.staticInFor).toBe(true)
|
||||
expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[1].block.staticRoot).toBe(false)
|
||||
expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[1].block.staticInFor).toBe(true)
|
||||
|
||||
expect(ast.conditions[1].block.children[0].children[0].conditions[2].block.staticRoot).toBe(false)
|
||||
expect(ast.conditions[1].block.children[0].children[0].conditions[2].block.staticInFor).toBe(true)
|
||||
expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[2].block.staticRoot).toBe(false)
|
||||
expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[2].block.staticInFor).toBe(true)
|
||||
|
||||
expect(ast.conditions[2].block.children[0].children[0].conditions[1].block.staticRoot).toBe(false)
|
||||
expect(ast.conditions[2].block.children[0].children[0].conditions[1].block.staticInFor).toBe(true)
|
||||
expect(ast.ifConditions[2].block.children[0].children[0].ifConditions[1].block.staticRoot).toBe(false)
|
||||
expect(ast.ifConditions[2].block.children[0].children[0].ifConditions[1].block.staticInFor).toBe(true)
|
||||
})
|
||||
})
|
||||
|
@ -129,7 +129,7 @@ describe('parser', () => {
|
||||
<p v-else></p>
|
||||
`, baseOptions)
|
||||
expect(ast.tag).toBe('div')
|
||||
expect(ast.conditions[1].block.tag).toBe('p')
|
||||
expect(ast.ifConditions[1].block.tag).toBe('p')
|
||||
})
|
||||
|
||||
it('generate correct ast for 3 or more root elements with v-if and v-else on separate lines', () => {
|
||||
@ -139,9 +139,9 @@ describe('parser', () => {
|
||||
<p v-else></p>
|
||||
`, baseOptions)
|
||||
expect(ast.tag).toBe('div')
|
||||
expect(ast.conditions[0].block.tag).toBe('div')
|
||||
expect(ast.conditions[1].block.tag).toBe('span')
|
||||
expect(ast.conditions[2].block.tag).toBe('p')
|
||||
expect(ast.ifConditions[0].block.tag).toBe('div')
|
||||
expect(ast.ifConditions[1].block.tag).toBe('span')
|
||||
expect(ast.ifConditions[2].block.tag).toBe('p')
|
||||
|
||||
const astMore = parse(`
|
||||
<div v-if="1"></div>
|
||||
@ -151,11 +151,11 @@ describe('parser', () => {
|
||||
<p v-else></p>
|
||||
`, baseOptions)
|
||||
expect(astMore.tag).toBe('div')
|
||||
expect(astMore.conditions[0].block.tag).toBe('div')
|
||||
expect(astMore.conditions[1].block.tag).toBe('span')
|
||||
expect(astMore.conditions[2].block.tag).toBe('div')
|
||||
expect(astMore.conditions[3].block.tag).toBe('span')
|
||||
expect(astMore.conditions[4].block.tag).toBe('p')
|
||||
expect(astMore.ifConditions[0].block.tag).toBe('div')
|
||||
expect(astMore.ifConditions[1].block.tag).toBe('span')
|
||||
expect(astMore.ifConditions[2].block.tag).toBe('div')
|
||||
expect(astMore.ifConditions[3].block.tag).toBe('span')
|
||||
expect(astMore.ifConditions[4].block.tag).toBe('p')
|
||||
})
|
||||
|
||||
it('warn 2 root elements with v-if', () => {
|
||||
@ -267,13 +267,13 @@ describe('parser', () => {
|
||||
it('v-if directive syntax', () => {
|
||||
const ast = parse('<p v-if="show">hello world</p>', baseOptions)
|
||||
expect(ast.if).toBe('show')
|
||||
expect(ast.conditions[0].exp).toBe('show')
|
||||
expect(ast.ifConditions[0].exp).toBe('show')
|
||||
})
|
||||
|
||||
it('v-else-if directive syntax', () => {
|
||||
const ast = parse('<div><p v-if="show">hello</p><span v-else-if="2">elseif</span><p v-else>world</p></div>', baseOptions)
|
||||
const ifAst = ast.children[0]
|
||||
const conditionsAst = ifAst.conditions
|
||||
const conditionsAst = ifAst.ifConditions
|
||||
expect(conditionsAst.length).toBe(3)
|
||||
expect(conditionsAst[1].block.children[0].text).toBe('elseif')
|
||||
expect(conditionsAst[1].block.parent).toBe(ast)
|
||||
@ -284,7 +284,7 @@ describe('parser', () => {
|
||||
it('v-else directive syntax', () => {
|
||||
const ast = parse('<div><p v-if="show">hello</p><p v-else>world</p></div>', baseOptions)
|
||||
const ifAst = ast.children[0]
|
||||
const conditionsAst = ifAst.conditions
|
||||
const conditionsAst = ifAst.ifConditions
|
||||
expect(conditionsAst.length).toBe(2)
|
||||
expect(conditionsAst[1].block.children[0].text).toBe('world')
|
||||
expect(conditionsAst[1].block.parent).toBe(ast)
|
||||
|
Loading…
Reference in New Issue
Block a user