aws-sdk-js-codemod
Version:
Collection of codemod scripts that help update AWS SDK for JavaScript APIs
40 lines (39 loc) • 2.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeImport = void 0;
const removeDeclaration_1 = require("../removeDeclaration");
const getImportDeclarations_1 = require("./getImportDeclarations");
const isAnotherSpecifier = (j, source, localName) => source
.find(j.ImportDeclaration, { specifiers: [{ local: { name: localName } }] })
.filter((importDeclaration) => {
const sourceValue = importDeclaration.value.source.value;
if (typeof sourceValue !== "string") {
return false;
}
return sourceValue.startsWith("@aws-sdk/");
})
.size() > 0;
const removeImport = (j, source) => (0, getImportDeclarations_1.getImportDeclarations)(j, source).forEach((importDeclaration) => {
importDeclaration.value.specifiers = (importDeclaration.value.specifiers || []).filter((specifier) => {
const localName = specifier.local?.name;
if (!localName) {
return true;
}
if (typeof localName !== "string") {
throw new Error("Please report your use case on https://github.com/aws/aws-sdk-js-codemod");
}
const identifiers = source.find(j.Identifier, { name: localName });
const importedName = specifier.type === "ImportSpecifier" && specifier.imported?.name;
// For default or namespace import, there's only one occurrence of local identifier.
// For named import, there can be two occurrences: one imported identifier and one local identifier.
const identifierNum = importedName && importedName === localName ? 2 : 1;
// Either the identifiers are the only occurences on the page.
// Or there's another specifier with the same name imported from JS SDK v3.
return !(identifiers.size() === identifierNum || isAnotherSpecifier(j, source, localName));
});
// Remove ImportDeclaration if there are no import specifiers.
if (importDeclaration.value.specifiers.length === 0) {
(0, removeDeclaration_1.removeDeclaration)(j, source, importDeclaration);
}
});
exports.removeImport = removeImport;