-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility requireModule getImportSpecifiers (#781)
- Loading branch information
Showing
20 changed files
with
172 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": patch | ||
--- | ||
|
||
Add utility requireModule getImportSpecifiers |
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
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
3 changes: 1 addition & 2 deletions
3
src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithIdentifier.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
2 changes: 1 addition & 1 deletion
2
src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithObjectPattern.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
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
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 |
---|---|---|
@@ -1,13 +1,5 @@ | ||
import { Collection, JSCodeshift, Literal } from "jscodeshift"; | ||
import { PACKAGE_NAME } from "../config"; | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
import { getRequireDeclarators } from "./requireModule"; | ||
|
||
export const hasRequire = (j: JSCodeshift, source: Collection<unknown>) => | ||
source | ||
.find(j.CallExpression, { | ||
callee: { type: "Identifier", name: "require" }, | ||
}) | ||
.filter((callExpression) => { | ||
const { value: sourceValue } = callExpression.value.arguments[0] as Literal; | ||
return typeof sourceValue === "string" && sourceValue.startsWith(PACKAGE_NAME); | ||
}) | ||
.size() > 0; | ||
getRequireDeclarators(j, source).size() > 0; |
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
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
62 changes: 62 additions & 0 deletions
62
src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.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,62 @@ | ||
import { Collection, Identifier, JSCodeshift, ObjectProperty, Property } from "jscodeshift"; | ||
import { OBJECT_PROPERTY_TYPE_LIST } from "../../config"; | ||
import { ImportSpecifierPattern, ImportSpecifierType } from "../types"; | ||
import { getRequireDeclarators } from "./getRequireDeclarators"; | ||
|
||
const getImportSpecifiersFromObjectPattern = (properties: (Property | ObjectProperty)[]) => { | ||
const importSpecifiers = new Set<ImportSpecifierPattern>(); | ||
|
||
for (const property of properties) { | ||
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) { | ||
continue; | ||
} | ||
const objectProperty = property as Property | ObjectProperty; | ||
const key = objectProperty.key; | ||
const value = objectProperty.value; | ||
if (key.type === "Identifier" && value.type === "Identifier") { | ||
importSpecifiers.add({ | ||
importedName: key.name, | ||
localName: value.name, | ||
}); | ||
} | ||
} | ||
|
||
return Array.from(importSpecifiers); | ||
}; | ||
|
||
export const getImportSpecifiers = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
path?: string | ||
): ImportSpecifierType[] => { | ||
const importSpecifiers = new Set<ImportSpecifierType>(); | ||
|
||
for (const varDeclarator of getRequireDeclarators(j, source, path).nodes()) { | ||
const declaratorId = varDeclarator.id; | ||
const declaratorInit = varDeclarator.init; | ||
|
||
if (declaratorId.type === "Identifier") { | ||
const declaratorIdName = declaratorId.name; | ||
if (declaratorInit!.type === "MemberExpression") { | ||
importSpecifiers.add({ | ||
importedName: (declaratorInit.property as Identifier).name, | ||
localName: declaratorIdName, | ||
}); | ||
} else { | ||
importSpecifiers.add(declaratorIdName); | ||
} | ||
} | ||
|
||
if (declaratorId.type === "ObjectPattern") { | ||
if (declaratorInit!.type !== "CallExpression") { | ||
continue; | ||
} | ||
const properties = declaratorId.properties as (Property | ObjectProperty)[]; | ||
for (const importSpecifier of getImportSpecifiersFromObjectPattern(properties)) { | ||
importSpecifiers.add(importSpecifier); | ||
} | ||
} | ||
} | ||
|
||
return Array.from(importSpecifiers); | ||
}; |
Oops, something went wrong.