Skip to content

Commit

Permalink
fix(un-indirect-call): better naming conflicts handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Mar 29, 2024
1 parent 74641f5 commit 976d80f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import transform from '../un-indirect-call'

const inlineTest = defineInlineTest(transform)

inlineTest('indirect call from a imported module',
inlineTest('indirect call from imported module',
`
import s from "react";
Expand All @@ -16,6 +16,39 @@ var countRef = useRef(0);
`,
)

inlineTest('indirect call from imported module with existing named import',
`
import s from "react";
import { useRef } from "react";
var countRef = (0, s.useRef)(0);
`,
`
import { useRef } from "react";
var countRef = useRef(0);
`,
)

inlineTest('indirect call from imported module with naming conflicts with local variables',
`
import s from "react";
const fn = () => {
const useRef = 1;
(0, s.useRef)(0);
}
`,
`
import { useRef as useRef_1 } from "react";
const fn = () => {
const useRef = 1;
useRef_1(0);
}
`,
)

inlineTest('multiple indirect call from different sources',
`
import s from "react";
Expand All @@ -38,7 +71,7 @@ var thirdRef = useRef_1(0);
`,
)

inlineTest('indirect call from a required module',
inlineTest('indirect call from required module',
`
const s = require("react");
Expand Down Expand Up @@ -150,17 +183,21 @@ var thirdRef = useRef_1(0);
`,
)

inlineTest.fixme('indirect call with naming conflicts with local variables',
inlineTest('indirect call from required module with naming conflicts with local variables',
`
import s from "react";
const s = require("react");
const fn = () => {
const useRef = 1;
(0, s.useRef)(0);
}
`,
`
import { useRef as useRef_1 } from "react";
const s = require("react");
const {
useRef: useRef_1
} = s;
const fn = () => {
const useRef = 1;
Expand Down
10 changes: 8 additions & 2 deletions packages/unminify/src/transformations/un-indirect-call.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isTopLevel } from '@wakaru/ast-utils'
import { assertScopeExists } from '@wakaru/ast-utils/assert'
import { generateName } from '@wakaru/ast-utils/identifier'
import { ImportManager } from '@wakaru/ast-utils/imports'
import { insertAfter } from '@wakaru/ast-utils/insert'
Expand Down Expand Up @@ -71,6 +72,9 @@ export const transformAST: ASTTransformation = (context) => {
},
})
.forEach((path) => {
const scope = path.scope
assertScopeExists(scope)

const { node } = path
const callee = node.callee as SequenceExpression
const memberExpression = callee.expressions[1] as MemberExpression
Expand Down Expand Up @@ -103,12 +107,14 @@ export const transformAST: ASTTransformation = (context) => {
const namedImportLocalName = [...(importManager.namedImports.get(source)?.get(namedSpecifierName) ?? [])][0]
if (namedImportLocalName) {
replaceMapping.set(key, namedImportLocalName)
processedImportSources.add(source)

const newCallExpression = j.callExpression(j.identifier(namedImportLocalName), node.arguments)
path.replace(newCallExpression)
return
}

const namedSpecifierLocalName = generateName(namedSpecifierName, rootScope, importManager.getAllLocals())
const namedSpecifierLocalName = generateName(namedSpecifierName, scope, importManager.getAllLocals())
importManager.addNamedImport(source, namedSpecifierName, namedSpecifierLocalName)
replaceMapping.set(key, namedSpecifierLocalName)
processedImportSources.add(source)
Expand Down Expand Up @@ -154,7 +160,7 @@ export const transformAST: ASTTransformation = (context) => {
if (propertyDecl.size() === 0) {
// generate `const { useRef: useRef_1 } = react`
const key = j.identifier(property.name)
const valueName = generateName(property.name, rootScope, [...replaceMapping.values()])
const valueName = generateName(property.name, scope, [...replaceMapping.values()])
replaceMapping.set(`${defaultSpecifierName}.${namedSpecifierName}`, valueName)

const value = j.identifier(valueName)
Expand Down

0 comments on commit 976d80f

Please sign in to comment.