Skip to content

Commit

Permalink
fix(compiler-core): handle inline comments with undefined bindings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-leong authored Jun 28, 2024
1 parent ad22879 commit 746352a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,17 @@ describe('compiler: expression transform', () => {
)
})

test('should not error', () => {
const onError = vi.fn()
parseWithExpressionTransform(
`<p :id="undefined /* force override the id */"/>`,
{
onError,
},
)
expect(onError).not.toHaveBeenCalled()
})

test('should prefix in assignment', () => {
const node = parseWithExpressionTransform(
`{{ x = 1 }}`,
Expand Down
10 changes: 5 additions & 5 deletions packages/compiler-core/src/babelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function walkIdentifiers(
root: Node,
onIdentifier: (
node: Identifier,
parent: Node,
parent: Node | null,
parentStack: Node[],
isReference: boolean,
isLocal: boolean,
Expand All @@ -36,7 +36,7 @@ export function walkIdentifiers(
: root

walk(root, {
enter(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
enter(node: Node & { scopeIds?: Set<string> }, parent: Node | null) {
parent && parentStack.push(parent)
if (
parent &&
Expand All @@ -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' &&
Expand Down Expand Up @@ -79,7 +79,7 @@ export function walkIdentifiers(
}
}
},
leave(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
leave(node: Node & { scopeIds?: Set<string> }, parent: Node | null) {
parent && parentStack.pop()
if (node !== rootExp && node.scopeIds) {
for (const id of node.scopeIds) {
Expand Down
13 changes: 9 additions & 4 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-sfc/src/script/definePropsDestructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!)) ||
Expand Down
4 changes: 2 additions & 2 deletions packages/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ declare module 'estree-walker' {
export function walk<T>(
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 }>,
)
Expand Down

0 comments on commit 746352a

Please sign in to comment.