Skip to content

Commit 746352a

Browse files
authored
fix(compiler-core): handle inline comments with undefined bindings (#11217)
close #11216
1 parent ad22879 commit 746352a

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,17 @@ describe('compiler: expression transform', () => {
384384
)
385385
})
386386

387+
test('should not error', () => {
388+
const onError = vi.fn()
389+
parseWithExpressionTransform(
390+
`<p :id="undefined /* force override the id */"/>`,
391+
{
392+
onError,
393+
},
394+
)
395+
expect(onError).not.toHaveBeenCalled()
396+
})
397+
387398
test('should prefix in assignment', () => {
388399
const node = parseWithExpressionTransform(
389400
`{{ x = 1 }}`,

packages/compiler-core/src/babelUtils.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function walkIdentifiers(
1717
root: Node,
1818
onIdentifier: (
1919
node: Identifier,
20-
parent: Node,
20+
parent: Node | null,
2121
parentStack: Node[],
2222
isReference: boolean,
2323
isLocal: boolean,
@@ -36,7 +36,7 @@ export function walkIdentifiers(
3636
: root
3737

3838
walk(root, {
39-
enter(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
39+
enter(node: Node & { scopeIds?: Set<string> }, parent: Node | null) {
4040
parent && parentStack.push(parent)
4141
if (
4242
parent &&
@@ -47,9 +47,9 @@ export function walkIdentifiers(
4747
}
4848
if (node.type === 'Identifier') {
4949
const isLocal = !!knownIds[node.name]
50-
const isRefed = isReferencedIdentifier(node, parent!, parentStack)
50+
const isRefed = isReferencedIdentifier(node, parent, parentStack)
5151
if (includeAll || (isRefed && !isLocal)) {
52-
onIdentifier(node, parent!, parentStack, isRefed, isLocal)
52+
onIdentifier(node, parent, parentStack, isRefed, isLocal)
5353
}
5454
} else if (
5555
node.type === 'ObjectProperty' &&
@@ -79,7 +79,7 @@ export function walkIdentifiers(
7979
}
8080
}
8181
},
82-
leave(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
82+
leave(node: Node & { scopeIds?: Set<string> }, parent: Node | null) {
8383
parent && parentStack.pop()
8484
if (node !== rootExp && node.scopeIds) {
8585
for (const id of node.scopeIds) {

packages/compiler-core/src/transforms/transformExpression.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ export function processExpression(
116116
}
117117

118118
const { inline, bindingMetadata } = context
119-
const rewriteIdentifier = (raw: string, parent?: Node, id?: Identifier) => {
119+
const rewriteIdentifier = (
120+
raw: string,
121+
parent?: Node | null,
122+
id?: Identifier,
123+
) => {
120124
const type = hasOwn(bindingMetadata, raw) && bindingMetadata[raw]
121125
if (inline) {
122126
// x = y
@@ -313,9 +317,10 @@ export function processExpression(
313317
// local scope variable (a v-for alias, or a v-slot prop)
314318
if (
315319
!(needPrefix && isLocal) &&
316-
parent.type !== 'CallExpression' &&
317-
parent.type !== 'NewExpression' &&
318-
parent.type !== 'MemberExpression'
320+
(!parent ||
321+
(parent.type !== 'CallExpression' &&
322+
parent.type !== 'NewExpression' &&
323+
parent.type !== 'MemberExpression'))
319324
) {
320325
;(node as QualifiedId).isConstant = true
321326
}

packages/compiler-sfc/src/compileScript.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ export function compileScript(
616616
) {
617617
const scope: Statement[][] = [scriptSetupAst.body]
618618
walk(node, {
619-
enter(child: Node, parent: Node | undefined) {
619+
enter(child: Node, parent: Node | null) {
620620
if (isFunctionType(child)) {
621621
this.skip()
622622
}

packages/compiler-sfc/src/script/definePropsDestructure.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export function transformDestructuredProps(
239239
const ast = ctx.scriptSetupAst!
240240
walkScope(ast, true)
241241
walk(ast, {
242-
enter(node: Node, parent?: Node) {
242+
enter(node: Node, parent: Node | null) {
243243
parent && parentStack.push(parent)
244244

245245
// skip type nodes
@@ -294,7 +294,7 @@ export function transformDestructuredProps(
294294
}
295295
}
296296
},
297-
leave(node: Node, parent?: Node) {
297+
leave(node: Node, parent: Node | null) {
298298
parent && parentStack.pop()
299299
if (
300300
(node.type === 'BlockStatement' && !isFunctionType(parent!)) ||

packages/global.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ declare module 'estree-walker' {
3838
export function walk<T>(
3939
root: T,
4040
options: {
41-
enter?: (node: T, parent: T | undefined) => any
42-
leave?: (node: T, parent: T | undefined) => any
41+
enter?: (node: T, parent: T | null) => any
42+
leave?: (node: T, parent: T | null) => any
4343
exit?: (node: T) => any
4444
} & ThisType<{ skip: () => void }>,
4545
)

0 commit comments

Comments
 (0)