-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix/unremoved-multiline-named-import-issue'
- Loading branch information
Showing
3 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |