-
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 importModule getImportSpecifiers (#786)
- Loading branch information
Showing
10 changed files
with
93 additions
and
67 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 importModule 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
14 changes: 14 additions & 0 deletions
14
src/transforms/v2-to-v3/modules/importModule/getImportDeclarations.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,14 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
import { PACKAGE_NAME } from "../../config"; | ||
|
||
export const getImportDeclarations = (j: JSCodeshift, source: Collection<unknown>, path?: string) => | ||
source.find(j.ImportDeclaration).filter((importDeclaration) => { | ||
const sourceValue = importDeclaration.value.source.value; | ||
if (typeof sourceValue !== "string") { | ||
return false; | ||
} | ||
if (path) { | ||
return sourceValue === path; | ||
} | ||
return sourceValue.startsWith(PACKAGE_NAME); | ||
}); |
36 changes: 36 additions & 0 deletions
36
src/transforms/v2-to-v3/modules/importModule/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,36 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
import { ImportSpecifierType } from "../types"; | ||
import { getImportDeclarations } from "./getImportDeclarations"; | ||
|
||
export const getImportSpecifiers = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
path?: string | ||
): ImportSpecifierType[] => { | ||
const importSpecifiers = new Set<ImportSpecifierType>(); | ||
|
||
getImportDeclarations(j, source, path).forEach((importDeclaration) => { | ||
const specifiers = importDeclaration.value.specifiers || []; | ||
for (const specifier of specifiers) { | ||
switch (specifier.type) { | ||
case "ImportSpecifier": { | ||
const importedName = specifier.imported.name; | ||
importSpecifiers.add({ | ||
importedName, | ||
localName: specifier.local!.name || importedName, | ||
}); | ||
break; | ||
} | ||
case "ImportNamespaceSpecifier": | ||
case "ImportDefaultSpecifier": { | ||
if (specifier.local) { | ||
importSpecifiers.add(specifier.local.name); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return Array.from(importSpecifiers); | ||
}; |
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 +1,3 @@ | ||
export * from "./addNamedModule"; | ||
export * from "./getImportSpecifiers"; | ||
export * from "./getImportDeclarations"; |
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