From 55e293808c98aa91e38acd706559a0d561b44764 Mon Sep 17 00:00:00 2001 From: Pionxzh Date: Fri, 16 Feb 2024 21:59:11 +0800 Subject: [PATCH] fix(smart-inline): fix missing renaming in property descturing closes #117 --- .../__tests__/smart-inline.spec.ts | 19 +++++++++++++++++++ .../src/transformations/smart-inline.ts | 11 +++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts b/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts index 7b74c77c..d07a1692 100644 --- a/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts +++ b/packages/unminify/src/transformations/__tests__/smart-inline.spec.ts @@ -250,6 +250,25 @@ function foo() { `, ) +inlineTest('property destructuring - consecutive assignments', + ` +const t = s.target; +const p = t.parentElement; +const v = p.value; +`, + ` +const { + target +} = s; +const { + parentElement +} = target; +const { + value +} = parentElement; +`, +) + inlineTest('array destructuring', ` const t = e[0]; diff --git a/packages/unminify/src/transformations/smart-inline.ts b/packages/unminify/src/transformations/smart-inline.ts index 6d517b62..510e85c1 100644 --- a/packages/unminify/src/transformations/smart-inline.ts +++ b/packages/unminify/src/transformations/smart-inline.ts @@ -1,7 +1,7 @@ import { mergeComments } from '@wakaru/ast-utils/comments' import { generateName } from '@wakaru/ast-utils/identifier' import { createObjectProperty } from '@wakaru/ast-utils/object' -import { findReferences } from '@wakaru/ast-utils/reference' +import { findReferences, renameIdentifier } from '@wakaru/ast-utils/reference' import { MultiMap } from '@wakaru/ds' import { nonNullable } from '@wakaru/shared/array' import { createJSCodeshiftTransformationRule } from '@wakaru/shared/rule' @@ -218,7 +218,12 @@ function handleDestructuring(j: JSCodeshift, body: StatementKind[], scope: Scope body.splice(insertIndex, 0, destructuring) }) + const renameMap = new Map() objectAccessDeclarationMap.forEach((declarations, objectName) => { + if (renameMap.has(objectName)) { + objectName = renameMap.get(objectName)! + } + // Rename all variables to their property names let insertIndex = body.length const destructuringPropertyMap = new Map() @@ -252,7 +257,8 @@ function handleDestructuring(j: JSCodeshift, body: StatementKind[], scope: Scope const newPropertyName = destructuringPropertyMap.get(propertyName) || generateName(propertyName, scope, declaredNames) - scope.rename(variableName, newPropertyName) + renameIdentifier(j, scope, variableName, newPropertyName) + renameMap.set(variableName, newPropertyName) // TODO: should we move this into `scope.rename`? scope.markAsStale() destructuringPropertyMap.set(propertyName, newPropertyName) @@ -270,6 +276,7 @@ function handleDestructuring(j: JSCodeshift, body: StatementKind[], scope: Scope const kinds = [...destructuringPropertyMap.keys()].map(n => variableKindMap.get(n)).filter(nonNullable) const kind = getMostRestrictiveKind(kinds) if (!kind) return + const properties = [...destructuringPropertyMap.entries()] .map(([propertyName, newPropertyName]) => { const property = createObjectProperty(j, propertyName, j.identifier(newPropertyName))