From 746352a14d62e9d3d9a38c359d2c54d418c1e0ac Mon Sep 17 00:00:00 2001 From: Tycho Date: Fri, 28 Jun 2024 09:48:23 +0800 Subject: [PATCH] fix(compiler-core): handle inline comments with undefined bindings (#11217) close #11216 --- .../transforms/transformExpressions.spec.ts | 11 +++++++++++ packages/compiler-core/src/babelUtils.ts | 10 +++++----- .../src/transforms/transformExpression.ts | 13 +++++++++---- packages/compiler-sfc/src/compileScript.ts | 2 +- .../src/script/definePropsDestructure.ts | 4 ++-- packages/global.d.ts | 4 ++-- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts index ffd93d791ca..7ca831f0ce8 100644 --- a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts @@ -384,6 +384,17 @@ describe('compiler: expression transform', () => { ) }) + test('should not error', () => { + const onError = vi.fn() + parseWithExpressionTransform( + `

`, + { + onError, + }, + ) + expect(onError).not.toHaveBeenCalled() + }) + test('should prefix in assignment', () => { const node = parseWithExpressionTransform( `{{ x = 1 }}`, diff --git a/packages/compiler-core/src/babelUtils.ts b/packages/compiler-core/src/babelUtils.ts index 7482494e17a..67997798864 100644 --- a/packages/compiler-core/src/babelUtils.ts +++ b/packages/compiler-core/src/babelUtils.ts @@ -17,7 +17,7 @@ export function walkIdentifiers( root: Node, onIdentifier: ( node: Identifier, - parent: Node, + parent: Node | null, parentStack: Node[], isReference: boolean, isLocal: boolean, @@ -36,7 +36,7 @@ export function walkIdentifiers( : root walk(root, { - enter(node: Node & { scopeIds?: Set }, parent: Node | undefined) { + enter(node: Node & { scopeIds?: Set }, parent: Node | null) { parent && parentStack.push(parent) if ( parent && @@ -47,9 +47,9 @@ export function walkIdentifiers( } if (node.type === 'Identifier') { const isLocal = !!knownIds[node.name] - const isRefed = isReferencedIdentifier(node, parent!, parentStack) + const isRefed = isReferencedIdentifier(node, parent, parentStack) if (includeAll || (isRefed && !isLocal)) { - onIdentifier(node, parent!, parentStack, isRefed, isLocal) + onIdentifier(node, parent, parentStack, isRefed, isLocal) } } else if ( node.type === 'ObjectProperty' && @@ -79,7 +79,7 @@ export function walkIdentifiers( } } }, - leave(node: Node & { scopeIds?: Set }, parent: Node | undefined) { + leave(node: Node & { scopeIds?: Set }, parent: Node | null) { parent && parentStack.pop() if (node !== rootExp && node.scopeIds) { for (const id of node.scopeIds) { diff --git a/packages/compiler-core/src/transforms/transformExpression.ts b/packages/compiler-core/src/transforms/transformExpression.ts index 35aa9a373a4..de450491e7c 100644 --- a/packages/compiler-core/src/transforms/transformExpression.ts +++ b/packages/compiler-core/src/transforms/transformExpression.ts @@ -116,7 +116,11 @@ export function processExpression( } const { inline, bindingMetadata } = context - const rewriteIdentifier = (raw: string, parent?: Node, id?: Identifier) => { + const rewriteIdentifier = ( + raw: string, + parent?: Node | null, + id?: Identifier, + ) => { const type = hasOwn(bindingMetadata, raw) && bindingMetadata[raw] if (inline) { // x = y @@ -313,9 +317,10 @@ export function processExpression( // local scope variable (a v-for alias, or a v-slot prop) if ( !(needPrefix && isLocal) && - parent.type !== 'CallExpression' && - parent.type !== 'NewExpression' && - parent.type !== 'MemberExpression' + (!parent || + (parent.type !== 'CallExpression' && + parent.type !== 'NewExpression' && + parent.type !== 'MemberExpression')) ) { ;(node as QualifiedId).isConstant = true } diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 8a0aaeaf717..2fa2241a7de 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -616,7 +616,7 @@ export function compileScript( ) { const scope: Statement[][] = [scriptSetupAst.body] walk(node, { - enter(child: Node, parent: Node | undefined) { + enter(child: Node, parent: Node | null) { if (isFunctionType(child)) { this.skip() } diff --git a/packages/compiler-sfc/src/script/definePropsDestructure.ts b/packages/compiler-sfc/src/script/definePropsDestructure.ts index e4a59aca7d5..dd54ab85ba9 100644 --- a/packages/compiler-sfc/src/script/definePropsDestructure.ts +++ b/packages/compiler-sfc/src/script/definePropsDestructure.ts @@ -239,7 +239,7 @@ export function transformDestructuredProps( const ast = ctx.scriptSetupAst! walkScope(ast, true) walk(ast, { - enter(node: Node, parent?: Node) { + enter(node: Node, parent: Node | null) { parent && parentStack.push(parent) // skip type nodes @@ -294,7 +294,7 @@ export function transformDestructuredProps( } } }, - leave(node: Node, parent?: Node) { + leave(node: Node, parent: Node | null) { parent && parentStack.pop() if ( (node.type === 'BlockStatement' && !isFunctionType(parent!)) || diff --git a/packages/global.d.ts b/packages/global.d.ts index 38320b81e04..1bae0b929fb 100644 --- a/packages/global.d.ts +++ b/packages/global.d.ts @@ -38,8 +38,8 @@ declare module 'estree-walker' { export function walk( root: T, options: { - enter?: (node: T, parent: T | undefined) => any - leave?: (node: T, parent: T | undefined) => any + enter?: (node: T, parent: T | null) => any + leave?: (node: T, parent: T | null) => any exit?: (node: T) => any } & ThisType<{ skip: () => void }>, )