-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(unminify): add
un-import-rename
rule
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/unminify/src/transformations/__tests__/un-import-rename.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { defineInlineTest } from '@wakaru/test-utils' | ||
import transform from '../un-import-rename' | ||
|
||
const inlineTest = defineInlineTest(transform) | ||
|
||
inlineTest('import rename', | ||
` | ||
import { foo as a, bar as b, code } from '_'; | ||
console.log(a, b, code); | ||
`, | ||
` | ||
import { foo, bar, code } from '_'; | ||
console.log(foo, bar, code); | ||
`, | ||
) | ||
|
||
inlineTest('import rename with naming conflict', | ||
` | ||
import defaultExport, { foo as a, bar as b, code } from 'A'; | ||
import { foo, bar as c } from 'B'; | ||
console.log(a, b, code, foo, c); | ||
`, | ||
` | ||
import defaultExport, { foo as foo_1, bar, code } from 'A'; | ||
import { foo, bar as bar_1 } from 'B'; | ||
console.log(foo_1, bar, code, foo, bar_1); | ||
`, | ||
) | ||
|
||
inlineTest('multiple naming conflicts', | ||
` | ||
import { foo as a, bar as b } from 'A'; | ||
import { foo, bar } from 'B'; | ||
const foo_1 = 'local'; | ||
console.log(a, b, foo, bar, foo_1); | ||
`, | ||
` | ||
import { foo as foo_2, bar as bar_1 } from 'A'; | ||
import { foo, bar } from 'B'; | ||
const foo_1 = 'local'; | ||
console.log(foo_2, bar_1, foo, bar, foo_1); | ||
`, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { assertScopeExists } from '@wakaru/ast-utils/assert' | ||
import { generateName } from '@wakaru/ast-utils/identifier' | ||
import { renameIdentifier } from '@wakaru/ast-utils/reference' | ||
import { createJSCodeshiftTransformationRule } from '@wakaru/shared/rule' | ||
import type { Identifier } from 'jscodeshift' | ||
|
||
/** | ||
* Rename import specifier back to the original name | ||
* | ||
* @example | ||
* import { foo as ab } from 'bar'; | ||
* ab(); | ||
* -> | ||
* import { foo } from 'bar' | ||
* foo(); | ||
*/ | ||
export default createJSCodeshiftTransformationRule({ | ||
name: 'un-import-rename', | ||
transform: (context) => { | ||
const { root, j } = context | ||
|
||
root | ||
.find(j.ImportSpecifier, { | ||
imported: { | ||
type: 'Identifier', | ||
}, | ||
local: { | ||
type: 'Identifier', | ||
}, | ||
}) | ||
.paths() | ||
.forEach((path) => { | ||
const specifier = path.node | ||
const imported = specifier.imported as Identifier | ||
const local = specifier.local as Identifier | ||
|
||
if (imported.name === local.name) return | ||
|
||
const scope = path.scope | ||
assertScopeExists(scope) | ||
|
||
const targetName = generateName(imported.name, scope) | ||
renameIdentifier(j, scope, local.name, targetName) | ||
}) | ||
|
||
/** | ||
* fix all import { foo as foo } to import { foo } | ||
* | ||
* Doing this in a separate loop to avoid modifying the AST structure while iterating over it | ||
* It will cause some weird behavior | ||
*/ | ||
root | ||
.find(j.ImportSpecifier, { | ||
imported: { | ||
type: 'Identifier', | ||
}, | ||
local: { | ||
type: 'Identifier', | ||
}, | ||
}) | ||
.forEach((path) => { | ||
const specifier = path.node | ||
const imported = specifier.imported as Identifier | ||
const local = specifier.local as Identifier | ||
|
||
if (imported.name !== local.name) return | ||
|
||
path.replace(j.importSpecifier(j.identifier(imported.name))) | ||
}) | ||
}, | ||
}) |