UNPKG

aws-sdk-js-codemod

Version:

Collection of codemod scripts that help update AWS SDK for JavaScript APIs

63 lines (62 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addNamedModule = void 0; const config_1 = require("../../config"); const objectPatternPropertyCompareFn_1 = require("../objectPatternPropertyCompareFn"); const requireModule_1 = require("../requireModule"); const addNamedModule = (j, source, options) => { const { localName, importedName = localName, packageName } = options; const clientObjectProperty = j.objectProperty.from({ key: j.identifier(importedName), value: j.identifier(localName), shorthand: true, }); const existingRequires = (0, requireModule_1.getRequireDeclarators)(j, source, packageName); if (existingRequires && existingRequires.nodes().length > 0) { const existingRequireProperties = existingRequires .filter((variableDeclarator) => variableDeclarator.value.id.type === "ObjectPattern") .nodes(); if (existingRequireProperties.length > 0 && existingRequireProperties.find((variableDeclarator) => variableDeclarator.id.type === "ObjectPattern" && variableDeclarator.id.properties.find((property) => { if (property.type !== "Property" && property.type !== "ObjectProperty") { return false; } if (property.key.type !== "Identifier" || property.value.type !== "Identifier") { return false; } return property.key.name === importedName && property.value.name === localName; }))) { return; } if (existingRequireProperties.length > 0) { const firstRequireProperties = existingRequireProperties[0].id.properties; firstRequireProperties.push(clientObjectProperty); firstRequireProperties.sort(objectPatternPropertyCompareFn_1.objectPatternPropertyCompareFn); return; } } // Build a new require declarator. const v3RequireDeclaration = j.variableDeclaration("const", [ j.variableDeclarator(j.objectPattern([clientObjectProperty]), j.callExpression(j.identifier("require"), [j.literal(packageName)])), ]); const v2RequireCallExpressions = source .find(j.CallExpression, { callee: { type: "Identifier", name: "require" }, }) .filter((callExpression) => { const arg = callExpression.value.arguments[0]; if (arg.type !== "Literal" && arg.type !== "StringLiteral") { return false; } return typeof arg.value === "string" && arg.value.startsWith(config_1.PACKAGE_NAME); }); if (v2RequireCallExpressions.size()) { // Insert it after the first v2 require declaration. v2RequireCallExpressions.at(0).closest(j.VariableDeclaration).insertAfter(v3RequireDeclaration); return; } // Insert require declarator at the top of the document. source.get().node.program.body.unshift(v3RequireDeclaration); }; exports.addNamedModule = addNamedModule;