Skip to content

Commit

Permalink
fix: check for names in getRequireDeclarator
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 19, 2023
1 parent 1e8d93e commit 196a9db
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/transforms/v2-to-v3/modules/getRequireDeclarator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ export interface GetRequireDeclaratorOptions {
export const getRequireDeclarator = (
j: JSCodeshift,
source: Collection<unknown>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options: GetRequireDeclaratorOptions
) => {
const { v2ClientName, v2GlobalName } = options;

// Support DynamoDB.DocumentClient
const v2ClientLocalName = options.v2ClientLocalName.split(".")[0];

// Temporary fix, will be removed in https://github.com/awslabs/aws-sdk-js-codemod/pull/622
const v2RequireCallExpressions = source.find(j.VariableDeclaration).filter(
(variableDeclaration) =>
Expand All @@ -21,6 +25,10 @@ export const getRequireDeclarator = (
(declaration: VariableDeclarator | Identifier) => {
if (declaration.type === "Identifier") return true;

const id = declaration.id;
if (id.type !== "Identifier") return true;
if (![v2GlobalName, v2ClientName, v2ClientLocalName].includes(id.name)) return true;

const init = declaration.init;
if (!init) return true;
if (init.type !== "CallExpression") return true;
Expand Down Expand Up @@ -70,6 +78,7 @@ export const getRequireDeclarator = (

const property = init.property;
if (property.type !== "Identifier") return true;
if (![v2GlobalName, v2ClientName, v2ClientLocalName].includes(property.name)) return true;

return false;
}
Expand Down

0 comments on commit 196a9db

Please sign in to comment.