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( + `<template> cannot be keyed. Place the key on real elements instead.`, + getRawBindingAttr(el, 'key') + ) + } + if (el.for) { + const iterator = el.iterator2 || el.iterator1 + const parent = el.parent + if ( + iterator && + iterator === exp && + parent && + parent.tag === 'transition-group' + ) { warn( - `<template> cannot be keyed. Place the key on real elements instead.`, - getRawBindingAttr(el, 'key') + `Do not use v-for index as key on <transition-group> children, ` + + `this is the same as not using keys.`, + getRawBindingAttr(el, 'key'), + true /* tip */ ) } - if (el.for) { - const iterator = el.iterator2 || el.iterator1 - const parent = el.parent - if ( - iterator && - iterator === exp && - parent && - parent.tag === 'transition-group' - ) { - warn( - `Do not use v-for index as key on <transition-group> children, ` + - `this is the same as not using keys.`, - getRawBindingAttr(el, 'key'), - true /* tip */ - ) - } - } } - el.key = exp } + el.key = exp } function processRef(el) { @@ -508,14 +512,15 @@ function processRef(el) { } export function processFor(el: ASTElement) { - let exp - if ((exp = getAndRemoveAttr(el, 'v-for'))) { - const res = parseFor(exp) - if (res) { - extend(el, res) - } else if (__DEV__) { - warn(`Invalid v-for expression: ${exp}`, el.rawAttrsMap['v-for']) - } + const exp = getAndRemoveAttr(el, 'v-for') + if (!exp) { + return + } + const res = parseFor(exp) + if (res) { + extend(el, res) + } else if (__DEV__) { + warn(`Invalid v-for expression: ${exp}`, el.rawAttrsMap['v-for']) } } @@ -585,16 +590,14 @@ function findPrevElement(children: Array<any>): ASTElement | void { while (i--) { if (children[i].type === 1) { return children[i] - } else { - if (__DEV__ && children[i].text !== ' ') { + } else if (__DEV__ && children[i].text !== ' ') { warn( `text "${children[i].text.trim()}" between v-if and v-else(-if) ` + `will be ignored.`, children[i] ) - } - children.pop() } + children.pop() } } @@ -747,22 +750,23 @@ function getSlotName(binding) { // handle <slot/> outlets function processSlotOutlet(el) { - if (el.tag === 'slot') { - el.slotName = getBindingAttr(el, 'name') - if (__DEV__ && el.key) { - warn( - `\`key\` does not work on <slot> because slots are abstract outlets ` + - `and can possibly expand into multiple elements. ` + - `Use the key on a wrapping element instead.`, - getRawBindingAttr(el, 'key') - ) - } + if (el.tag !== 'slot') { + return + } + el.slotName = getBindingAttr(el, 'name') + if (__DEV__ && el.key) { + warn( + `\`key\` does not work on <slot> because slots are abstract outlets ` + + `and can possibly expand into multiple elements. ` + + `Use the key on a wrapping element instead.`, + getRawBindingAttr(el, 'key') + ) } } function processComponent(el) { - let binding - if ((binding = getBindingAttr(el, 'is'))) { + const binding = getBindingAttr(el, 'is') + if (binding) { el.component = binding } if (getAndRemoveAttr(el, 'inline-template') != null) { @@ -932,13 +936,12 @@ function checkInFor(el: ASTElement): boolean { function parseModifiers(name: string): Object | void { const match = name.match(modifierRE) - if (match) { - const ret = {} - match.forEach(m => { + return match + ? match.reduce((ret,m) => { ret[m.slice(1)] = true - }) - return ret - } + return ret + },{}) + : undefined } function makeAttrsMap(attrs: Array<Record<string, any>>): Record<string, any> { diff --git a/src/compiler/to-function.ts b/src/compiler/to-function.ts index de0b95c1d35..3576db7f820 100644 --- a/src/compiler/to-function.ts +++ b/src/compiler/to-function.ts @@ -100,8 +100,7 @@ export function createCompileToFunctionFn(compile: Function): Function { // this should only happen if there is a bug in the compiler itself. // mostly for codegen development use /* istanbul ignore if */ - if (__DEV__) { - if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) { + if (__DEV__ && (!compiled.errors || !compiled.errors.length) && fnGenErrors.length) { warn( `Failed to generate render function:\n\n` + fnGenErrors @@ -111,7 +110,6 @@ export function createCompileToFunctionFn(compile: Function): Function { .join('\n'), vm ) - } } return (cache[key] = res) From ca924158a883a2499bf6c8f4dd6ab7a094983592 Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Fri, 18 Nov 2022 20:19:20 +0800 Subject: [PATCH 4/6] chore: update --- src/compiler/codegen/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/codegen/index.ts b/src/compiler/codegen/index.ts index c65744fda77..4af1b67130a 100644 --- a/src/compiler/codegen/index.ts +++ b/src/compiler/codegen/index.ts @@ -530,7 +530,7 @@ export function genChildren( child.tag !== 'slot' ) { const normalizationType = checkSkip - ? state.maybeComponent(el) + ? state.maybeComponent(child) ? `,1` : `,0` : `` From 99120bc1a5ec3934fb3d1f1e22061527b89aef34 Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Fri, 18 Nov 2022 22:01:21 +0800 Subject: [PATCH 5/6] chore: update --- src/compiler/parser/index.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/compiler/parser/index.ts b/src/compiler/parser/index.ts index 3f5c86136ca..b67c1889f98 100644 --- a/src/compiler/parser/index.ts +++ b/src/compiler/parser/index.ts @@ -936,12 +936,15 @@ function checkInFor(el: ASTElement): boolean { function parseModifiers(name: string): Object | void { const match = name.match(modifierRE) - return match - ? match.reduce((ret,m) => { - ret[m.slice(1)] = true - return ret - },{}) - : undefined + if(!match){ + return + } + + return match.reduce((ret,m) => { + ret[m.slice(1)] = true + return ret + },{}) + } function makeAttrsMap(attrs: Array<Record<string, any>>): Record<string, any> { From bb38547b3d492b658a93fd8fe39e9e57d5c372f1 Mon Sep 17 00:00:00 2001 From: Simon He <13917107469@163.com> Date: Sat, 19 Nov 2022 12:00:40 +0800 Subject: [PATCH 6/6] chore: update --- src/compiler/parser/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/parser/index.ts b/src/compiler/parser/index.ts index b67c1889f98..33f0ff79db7 100644 --- a/src/compiler/parser/index.ts +++ b/src/compiler/parser/index.ts @@ -936,7 +936,7 @@ function checkInFor(el: ASTElement): boolean { function parseModifiers(name: string): Object | void { const match = name.match(modifierRE) - if(!match){ + if(!match) { return } @@ -944,7 +944,6 @@ function parseModifiers(name: string): Object | void { ret[m.slice(1)] = true return ret },{}) - } function makeAttrsMap(attrs: Array<Record<string, any>>): Record<string, any> {