Skip to content

Commit

Permalink
fix: fix function name reference
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Aug 11, 2024
1 parent 091c68c commit 48feaba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/ast-utils/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export function findReferences(
): Collection<Identifier> {
const targetScope = 'bindings' in nodeOrScope ? nodeOrScope : j(nodeOrScope).get().scope as Scope
const range = 'bindings' in nodeOrScope ? nodeOrScope.path : nodeOrScope
const rangeNode = 'node' in range ? range.node : range

return j(range)
.find(j.Identifier, { name: identifierName })
Expand All @@ -179,6 +180,11 @@ export function findReferences(
// ignore properties (e.g. in MemberExpression
if (path.name === 'property' && j.MemberExpression.check(path.parent.node) && !path.parent.node.computed) return false

// ignore function name that is at the top level
if (path.parent.node === rangeNode && j.FunctionDeclaration.check(path.parent.node) && path.parent.node.id === path.node) {
return false
}

if (!path.scope) return false

let scope: Scope | null = path.scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,25 @@ function foo() {
`,
)

inlineTest('property destructuring - resolve naming conflicts #4',
`
function J(U) {
const B = U.children;
const G = U.className;
const J = U.description;
}
`,
`
function J(U) {
const {
children,
className,
description
} = U;
}
`,
)

inlineTest('array destructuring',
`
const t = e[0];
Expand Down

0 comments on commit 48feaba

Please sign in to comment.