Skip to content

Commit

Permalink
Merge branch 'fix/unremoved-multiline-named-import-issue'
Browse files Browse the repository at this point in the history
  • Loading branch information
omermecitoglu committed Nov 7, 2024
2 parents 92fa26b + 24de60f commit b7dfdf6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/core/transpile.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { transpile as tsTranspile } from "typescript";

function removeImports(code: string) {
return code.replace(/^import\s.+\sfrom\s.+;?$/gm, "").trim();
}
import removeImports from "~/utils/removeImports";

function fixExports(code: string) {
const validMethods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
Expand Down
78 changes: 78 additions & 0 deletions src/utils/removeImports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { describe, expect, it } from "@jest/globals";
import removeImports from "./removeImports";

describe("removeImports", () => {
it("removes a default import", () => {
const code = 'import defaultExport from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a namespace import", () => {
const code = 'import * as name from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a named import", () => {
const code = 'import { export1 } from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a named import with alias", () => {
const code = 'import { export1 as alias1 } from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a default import with alias", () => {
const code = 'import { default as alias } from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes multiple named imports", () => {
const code = 'import { export1, export2 } from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes multiple named imports with alias", () => {
const code = 'import { export1, export2 as alias2 } from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a named import with a string alias", () => {
const code = 'import { "string name" as alias } from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a combined default and named import", () => {
const code = 'import defaultExport, { export1 } from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a combined default and namespace import", () => {
const code = 'import defaultExport, * as name from "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a side-effect only import", () => {
const code = 'import "module-name";';
expect(removeImports(code)).toBe("");
});

it("removes a default import with single quotes", () => {
const code = "import defaultExport from 'module-name';";
expect(removeImports(code)).toBe("");
});

it("removes a default import missing semicolon", () => {
const code = 'import defaultExport from "module-name"';
expect(removeImports(code)).toBe("");
});

it("removes a multiline named import", () => {
const code = [
"import {",
" myAwesomeFunction",
'} from "my-awesome-module";',
];
expect(removeImports(code.join("\n"))).toBe("");
});
});
6 changes: 6 additions & 0 deletions src/utils/removeImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function removeImports(code: string) {
return code
.replace(/(^import\s+[^;]+;?$|^import\s+[^;]*\sfrom\s.+;?$)/gm, "") // matches single-line imports and side-effect imports
.replace(/(^import\s+{[\s\S]+?}\s+from\s+["'][^"']+["'];?)/gm, "") // matches multiline named imports
.trim();
}

0 comments on commit b7dfdf6

Please sign in to comment.