Skip to content

Commit

Permalink
chore: use getImportEqualsDeclarations in addDefaultModule
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Feb 29, 2024
1 parent f436346 commit 6a91b13
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { PACKAGE_NAME } from "../../config";
import { getImportSpecifiers } from "../importEqualsModule";
import { getImportEqualsDeclarations, getImportSpecifiers } from "../importEqualsModule";
import { getDefaultName } from "./getDefaultName";

export const addDefaultModule = (
Expand All @@ -26,16 +25,7 @@ export const addDefaultModule = (
j.tsExternalModuleReference(j.stringLiteral(packageName))
);

const v2ImportEquals = source
.find(j.TSImportEqualsDeclaration)
.filter((importEqualsDeclaration) => {
const { moduleReference } = importEqualsDeclaration.value;
if (moduleReference.type !== "TSExternalModuleReference") return false;
const { expression } = moduleReference;
if (expression.type !== "StringLiteral") return false;
const { value } = expression;
return value.startsWith(PACKAGE_NAME);
});
const v2ImportEquals = getImportEqualsDeclarations(j, source);

if (v2ImportEquals.size()) {
// Insert it after the first import equals declaration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@ export const addNamedModule = (

const existingDeclaration = source.find(j.TSImportEqualsDeclaration, {
type: "TSImportEqualsDeclaration",
id: {
type: "Identifier",
name: localName,
},
id: { type: "Identifier", name: localName },
moduleReference: {
type: "TSQualifiedName",
left: {
type: "Identifier",
name: defaultLocalName,
},
right: {
type: "Identifier",
name: importedName,
},
left: { type: "Identifier", name: defaultLocalName },
right: { type: "Identifier", name: importedName },
},
});

Expand Down

0 comments on commit 6a91b13

Please sign in to comment.