|
1 |
| -import type { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift"; |
2 |
| -import { OBJECT_PROPERTY_TYPE_LIST } from "../../config"; |
| 1 | +import type { Collection, JSCodeshift } from "jscodeshift"; |
3 | 2 | import type { ImportSpecifierType } from "../types";
|
4 | 3 | import { getRequireDeclarators } from "./getRequireDeclarators";
|
5 | 4 |
|
6 |
| -const getImportSpecifiersFromObjectPattern = (properties: (Property | ObjectProperty)[]) => { |
7 |
| - const importSpecifiers = new Set<ImportSpecifierType>(); |
8 |
| - |
9 |
| - for (const property of properties) { |
10 |
| - if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) { |
11 |
| - continue; |
12 |
| - } |
13 |
| - const objectProperty = property as Property | ObjectProperty; |
14 |
| - const key = objectProperty.key; |
15 |
| - const value = objectProperty.value; |
16 |
| - if (key.type === "Identifier" && value.type === "Identifier") { |
17 |
| - importSpecifiers.add({ |
18 |
| - importedName: key.name, |
19 |
| - localName: value.name, |
20 |
| - }); |
21 |
| - } |
22 |
| - } |
23 |
| - |
24 |
| - return Array.from(importSpecifiers); |
25 |
| -}; |
26 |
| - |
27 | 5 | export const getImportSpecifiers = (
|
28 | 6 | j: JSCodeshift,
|
29 | 7 | source: Collection<unknown>,
|
@@ -54,9 +32,16 @@ export const getImportSpecifiers = (
|
54 | 32 | if (declaratorInit?.type !== "CallExpression") {
|
55 | 33 | continue;
|
56 | 34 | }
|
57 |
| - const properties = declaratorId.properties as (Property | ObjectProperty)[]; |
58 |
| - for (const importSpecifier of getImportSpecifiersFromObjectPattern(properties)) { |
59 |
| - importSpecifiers.add(importSpecifier); |
| 35 | + for (const property of declaratorId.properties) { |
| 36 | + if (property.type !== "Property" && property.type !== "ObjectProperty") { |
| 37 | + continue; |
| 38 | + } |
| 39 | + if (property.key.type === "Identifier" && property.value.type === "Identifier") { |
| 40 | + importSpecifiers.add({ |
| 41 | + importedName: property.key.name, |
| 42 | + localName: property.value.name, |
| 43 | + }); |
| 44 | + } |
60 | 45 | }
|
61 | 46 | }
|
62 | 47 | }
|
|
0 commit comments