|
1 | | -import { |
2 | | - Collection, |
3 | | - ImportNamespaceSpecifier, |
4 | | - ImportSpecifier, |
5 | | - JSCodeshift, |
6 | | - Property, |
7 | | - ObjectProperty, |
8 | | -} from "jscodeshift"; |
| 1 | +import { Collection, ImportSpecifier, JSCodeshift } from "jscodeshift"; |
9 | 2 |
|
10 | | -import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config"; |
| 3 | +import { PACKAGE_NAME } from "../../config"; |
11 | 4 | import { getImportSpecifiers } from "../getImportSpecifiers"; |
12 | | -import { getRequireProperty } from "../getRequireProperty"; |
13 | 5 | import { importSpecifierCompareFn } from "../importSpecifierCompareFn"; |
14 | | -import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn"; |
15 | 6 | import { ModulesOptions } from "../types"; |
16 | 7 |
|
17 | 8 | export const addNamedModule = ( |
@@ -41,62 +32,6 @@ export const addNamedModule = ( |
41 | 32 | return; |
42 | 33 | } |
43 | 34 |
|
44 | | - const importNamespaceSpecifiers = importSpecifiers.filter( |
45 | | - (importSpecifier) => importSpecifier?.type === "ImportNamespaceSpecifier" |
46 | | - ) as ImportNamespaceSpecifier[]; |
47 | | - |
48 | | - // If namespace import exists. |
49 | | - if (importNamespaceSpecifiers.length > 0) { |
50 | | - const defaultLocalName = importNamespaceSpecifiers[0].local!.name; |
51 | | - const namedImportObjectProperty = getRequireProperty(j, { importedName, localName }); |
52 | | - |
53 | | - const existingVarDeclarator = source.find(j.VariableDeclarator, { |
54 | | - type: "VariableDeclarator", |
55 | | - init: { type: "Identifier", name: defaultLocalName }, |
56 | | - }); |
57 | | - |
58 | | - // If variable declarator exists. |
59 | | - if (existingVarDeclarator.size()) { |
60 | | - // Return if property exists. |
61 | | - if ( |
62 | | - existingVarDeclarator.some((varDeclarator) => { |
63 | | - const id = varDeclarator.node.id; |
64 | | - if (id.type !== "ObjectPattern") return false; |
65 | | - for (const property of id.properties) { |
66 | | - if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) continue; |
67 | | - const propertyKey = (property as Property | ObjectProperty).key; |
68 | | - if (propertyKey.type !== "Identifier") continue; |
69 | | - if (propertyKey.name === importedName) return true; |
70 | | - } |
71 | | - return false; |
72 | | - }) |
73 | | - ) |
74 | | - return; |
75 | | - |
76 | | - // Add property to the first declarator. |
77 | | - const firstDeclaratorProperties = existingVarDeclarator.get(0).node.id.properties; |
78 | | - firstDeclaratorProperties.push(namedImportObjectProperty); |
79 | | - firstDeclaratorProperties.sort(objectPatternPropertyCompareFn); |
80 | | - return; |
81 | | - } |
82 | | - |
83 | | - const varDeclaration = j.variableDeclaration("const", [ |
84 | | - j.variableDeclarator( |
85 | | - j.objectPattern([namedImportObjectProperty]), |
86 | | - j.identifier(defaultLocalName) |
87 | | - ), |
88 | | - ]); |
89 | | - |
90 | | - source |
91 | | - .find(j.ImportDeclaration, { |
92 | | - type: "ImportDeclaration", |
93 | | - specifiers: [importNamespaceSpecifiers[0]], |
94 | | - source: { value: packageName }, |
95 | | - }) |
96 | | - .insertAfter(varDeclaration); |
97 | | - return; |
98 | | - } |
99 | | - |
100 | 35 | // Add named import to the first import declaration. |
101 | 36 | const firstImportDeclSpecifiers = importDeclarations.nodes()[0].specifiers; |
102 | 37 | if (firstImportDeclSpecifiers) { |
|
0 commit comments