aws-sdk-js-codemod
Version:
Collection of codemod scripts that help update AWS SDK for JavaScript APIs
47 lines (46 loc) • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClientIdNamesFromNewExpr = void 0;
const config_1 = require("../config");
const utils_1 = require("../utils");
const getNamesFromVariableDeclarator = (j, source, newExpression) => source
.find(j.VariableDeclarator, { id: { type: "Identifier" }, init: newExpression })
.nodes()
.map((variableDeclarator) => variableDeclarator.id.name);
const getNamesFromAssignmentPattern = (j, source, newExpression) => source
.find(j.AssignmentPattern, { left: { type: "Identifier" }, right: newExpression })
.nodes()
.map((assignmentPattern) => assignmentPattern.left.name);
const getNamesFromThisMemberExpression = (j, source, newExpression) => source
.find(j.AssignmentExpression, {
left: {
type: "MemberExpression",
object: { type: "ThisExpression" },
property: { type: "Identifier" },
},
right: newExpression,
})
.nodes()
.map((assignmentExpression) => assignmentExpression.left.property.name);
const getClientIdNamesFromNewExpr = (j, source, { v2ClientName, v2ClientLocalName, v2GlobalName }) => {
const namesFromGlobalModule = [];
const namesFromServiceModule = [];
for (const getNames of [
getNamesFromVariableDeclarator,
getNamesFromAssignmentPattern,
getNamesFromThisMemberExpression,
]) {
if (v2GlobalName) {
namesFromGlobalModule.push(...getNames(j, source, (0, utils_1.getClientNewExpressionFromGlobalName)(v2GlobalName, v2ClientName)));
if (v2ClientName === config_1.DYNAMODB) {
namesFromGlobalModule.push(...getNames(j, source, (0, utils_1.getClientNewExpressionFromGlobalName)(v2GlobalName, config_1.DYNAMODB_DOCUMENT_CLIENT)));
}
}
namesFromServiceModule.push(...getNames(j, source, (0, utils_1.getClientNewExpressionFromLocalName)(v2ClientLocalName)));
if (v2ClientName === config_1.DYNAMODB) {
namesFromServiceModule.push(...getNames(j, source, (0, utils_1.getClientNewExpressionFromLocalName)(`${v2ClientLocalName}.${config_1.DOCUMENT_CLIENT}`)));
}
}
return [...new Set([...namesFromGlobalModule, ...namesFromServiceModule])];
};
exports.getClientIdNamesFromNewExpr = getClientIdNamesFromNewExpr;