From 7fb045420bd4c6ad854886c9763163dac3907a18 Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Wed, 9 Nov 2022 18:19:24 +0800 Subject: [PATCH 1/6] refactor:tidy up compiler/codegen --- src/compiler/codegen/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/compiler/codegen/index.ts b/src/compiler/codegen/index.ts index b0daf352f82..6d4f63f8dd6 100644 --- a/src/compiler/codegen/index.ts +++ b/src/compiler/codegen/index.ts @@ -477,13 +477,13 @@ function hash(str) { } function containsSlotChild(el: ASTNode): boolean { - if (el.type === 1) { - if (el.tag === 'slot') { - return true - } - return el.children.some(containsSlotChild) + if (el.type !== 1) { + return false + } + if (el.tag === 'slot') { + return true } - return false + return el.children.some(containsSlotChild) } function genScopedSlot(el: ASTElement, state: CodegenState): string { From 992999e8fccb7631ccd7e3b7c3527670fe72c95f Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Fri, 18 Nov 2022 19:26:52 +0800 Subject: [PATCH 2/6] chore: update --- src/compiler/codegen/index.ts | 47 ++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/compiler/codegen/index.ts b/src/compiler/codegen/index.ts index 6d4f63f8dd6..c65744fda77 100644 --- a/src/compiler/codegen/index.ts +++ b/src/compiler/codegen/index.ts @@ -518,30 +518,31 @@ export function genChildren( altGenNode?: Function ): string | void { const children = el.children - if (children.length) { - const el: any = children[0] - // optimize single v-for - if ( - children.length === 1 && - el.for && - el.tag !== 'template' && - el.tag !== 'slot' - ) { - const normalizationType = checkSkip - ? state.maybeComponent(el) - ? `,1` - : `,0` - : `` - return `${(altGenElement || genElement)(el, state)}${normalizationType}` - } - const normalizationType = checkSkip - ? getNormalizationType(children, state.maybeComponent) - : 0 - const gen = altGenNode || genNode - return `[${children.map(c => gen(c, state)).join(',')}]${ - normalizationType ? `,${normalizationType}` : '' - }` + if (!children.length) { + return } + const child: any = children[0] + // optimize single v-for + if ( + children.length === 1 && + child.for && + child.tag !== 'template' && + child.tag !== 'slot' + ) { + const normalizationType = checkSkip + ? state.maybeComponent(el) + ? `,1` + : `,0` + : `` + return `${(altGenElement || genElement)(child, state)}${normalizationType}` + } + const normalizationType = checkSkip + ? getNormalizationType(children, state.maybeComponent) + : 0 + const gen = altGenNode || genNode + return `[${children.map(c => gen(c, state)).join(',')}]${ + normalizationType ? `,${normalizationType}` : '' + }` } // determine the normalization needed for the children array. From df32350220080dd264f9af5c9e4ff2a5140a4a11 Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Fri, 18 Nov 2022 20:08:41 +0800 Subject: [PATCH 3/6] chore: update --- src/compiler/optimizer.ts | 51 ++++----- src/compiler/parser/index.ts | 205 ++++++++++++++++++----------------- src/compiler/to-function.ts | 4 +- 3 files changed, 131 insertions(+), 129 deletions(-) diff --git a/src/compiler/optimizer.ts b/src/compiler/optimizer.ts index 49ef6aee2f1..fe854b57d95 100644 --- a/src/compiler/optimizer.ts +++ b/src/compiler/optimizer.ts @@ -70,32 +70,33 @@ function markStatic(node: ASTNode) { } function markStaticRoots(node: ASTNode, isInFor: boolean) { - if (node.type === 1) { - if (node.static || node.once) { - node.staticInFor = isInFor - } - // For a node to qualify as a static root, it should have children that - // are not just static text. Otherwise the cost of hoisting out will - // outweigh the benefits and it's better off to just always render it fresh. - if ( - node.static && - node.children.length && - !(node.children.length === 1 && node.children[0].type === 3) - ) { - node.staticRoot = true - return - } else { - node.staticRoot = false - } - if (node.children) { - for (let i = 0, l = node.children.length; i < l; i++) { - markStaticRoots(node.children[i], isInFor || !!node.for) - } + if (node.type !== 1) { + return + } + if (node.static || node.once) { + node.staticInFor = isInFor + } + // For a node to qualify as a static root, it should have children that + // are not just static text. Otherwise the cost of hoisting out will + // outweigh the benefits and it's better off to just always render it fresh. + if ( + node.static && + node.children.length && + !(node.children.length === 1 && node.children[0].type === 3) + ) { + node.staticRoot = true + return + } else { + node.staticRoot = false + } + if (node.children) { + for (let i = 0, l = node.children.length; i < l; i++) { + markStaticRoots(node.children[i], isInFor || !!node.for) } - if (node.ifConditions) { - for (let i = 1, l = node.ifConditions.length; i < l; i++) { - markStaticRoots(node.ifConditions[i].block, isInFor) - } + } + if (node.ifConditions) { + for (let i = 1, l = node.ifConditions.length; i < l; i++) { + markStaticRoots(node.ifConditions[i].block, isInFor) } } } diff --git a/src/compiler/parser/index.ts b/src/compiler/parser/index.ts index c02379d0f14..3f5c86136ca 100644 --- a/src/compiler/parser/index.ts +++ b/src/compiler/parser/index.ts @@ -183,15 +183,16 @@ export function parse(template: string, options: CompilerOptions): ASTElement { function trimEndingWhitespace(el) { // remove trailing whitespace node - if (!inPre) { - let lastNode - while ( - (lastNode = el.children[el.children.length - 1]) && - lastNode.type === 3 && - lastNode.text === ' ' - ) { - el.children.pop() - } + if (inPre) { + return + } + let lastNode + while ( + (lastNode = el.children[el.children.length - 1]) && + lastNode.type === 3 && + lastNode.text === ' ' + ) { + el.children.pop() } } @@ -368,54 +369,56 @@ export function parse(template: string, options: CompilerOptions): ASTElement { } else { text = preserveWhitespace ? ' ' : '' } - if (text) { - if (!inPre && whitespaceOption === 'condense') { - // condense consecutive whitespaces into single space - text = text.replace(whitespaceRE, ' ') + if (!text) { + return + } + if (!inPre && whitespaceOption === 'condense') { + // condense consecutive whitespaces into single space + text = text.replace(whitespaceRE, ' ') + } + let res + let child: ASTNode | undefined + if (!inVPre && text !== ' ' && (res = parseText(text, delimiters))) { + child = { + type: 2, + expression: res.expression, + tokens: res.tokens, + text } - let res - let child: ASTNode | undefined - if (!inVPre && text !== ' ' && (res = parseText(text, delimiters))) { - child = { - type: 2, - expression: res.expression, - tokens: res.tokens, - text - } - } else if ( - text !== ' ' || - !children.length || - children[children.length - 1].text !== ' ' - ) { - child = { - type: 3, - text - } + } else if ( + text !== ' ' || + !children.length || + children[children.length - 1].text !== ' ' + ) { + child = { + type: 3, + text } - if (child) { - if (__DEV__ && options.outputSourceRange) { - child.start = start - child.end = end - } - children.push(child) + } + if (child) { + if (__DEV__ && options.outputSourceRange) { + child.start = start + child.end = end } + children.push(child) } }, comment(text: string, start, end) { // adding anything as a sibling to the root node is forbidden // comments should still be allowed, but ignored - if (currentParent) { - const child: ASTText = { - type: 3, - text, - isComment: true - } - if (__DEV__ && options.outputSourceRange) { - child.start = start - child.end = end - } - currentParent.children.push(child) + if (!currentParent) { + return } + const child: ASTText = { + type: 3, + text, + isComment: true + } + if (__DEV__ && options.outputSourceRange) { + child.start = start + child.end = end + } + currentParent.children.push(child) } }) return root @@ -469,34 +472,35 @@ export function processElement(element: ASTElement, options: CompilerOptions) { function processKey(el) { const exp = getBindingAttr(el, 'key') - if (exp) { - if (__DEV__) { - if (el.tag === 'template') { + if (!exp) { + return + } + if (__DEV__) { + if (el.tag === 'template') { + warn( + `