From e9d79c99e5b34dc458276b36e4f3785a7ffe4587 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Wed, 18 Oct 2023 09:35:53 -0700 Subject: [PATCH] Add after require property declaration --- .../modules/requireModule/addNamedModule.ts | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts b/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts index 59fb1ddfd..628c9fe53 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts @@ -54,10 +54,26 @@ export const addNamedModule = ( j.callExpression(j.identifier("require"), [j.literal(packageName)]) ); - const v2RequireDeclarations = getRequireDeclarators(j, source, PACKAGE_NAME); - if (v2RequireDeclarations.size()) { - // Insert it after the first require declaration. - v2RequireDeclarations.at(0).insertAfter(v3RequireDeclarator); + const v2RequireDeclarators = getRequireDeclarators(j, source, PACKAGE_NAME); + if (v2RequireDeclarators.size()) { + // Insert it after the first require declarator. + v2RequireDeclarators.at(0).insertAfter(v3RequireDeclarator); + return; + } + + const v2RequirePropertyDeclarators = source.find(j.VariableDeclarator, { + init: { + type: "MemberExpression", + object: { + arguments: [{ value: PACKAGE_NAME }], + callee: { type: "Identifier", name: "require" }, + type: "CallExpression", + }, + }, + }); + if (v2RequirePropertyDeclarators.size()) { + // Insert it after the first require property declarator. + v2RequirePropertyDeclarators.at(0).insertAfter(v3RequireDeclarator); return; }