Skip to content

Commit

Permalink
Add after require property declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 19, 2023
1 parent f826424 commit fda5583
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit fda5583

Please sign in to comment.