diff --git a/packages/ast-utils/src/reference.ts b/packages/ast-utils/src/reference.ts index 3db57a82..75d51f08 100644 --- a/packages/ast-utils/src/reference.ts +++ b/packages/ast-utils/src/reference.ts @@ -171,6 +171,7 @@ export function findReferences( ): Collection { 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 }) @@ -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 diff --git a/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts b/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts index d8717a0a..a8f2ecb4 100644 --- a/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts +++ b/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts @@ -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];