Skip to content

Commit

Permalink
feat(findExports): extract name of default exports (#179)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
mateenagy and pi0 authored Jan 11, 2024
1 parent d212da8 commit 5213717
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const EXPORT_NAMED_DESTRUCT =
/\bexport\s+(let|var|const)\s+(?:{(?<exports1>[^}]+?)[\s,]*}|\[(?<exports2>[^\]]+?)[\s,]*])\s+=/gm;
const EXPORT_STAR_RE =
/\bexport\s*(\*)(\s*as\s+(?<name>[\w$]+)\s+)?\s*(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g;
const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g;
const EXPORT_DEFAULT_RE =
/\bexport\s+default\s+(async function|function|class|true|false|\W|\d)|\bexport\s+default\s+(?<defaultName>.*)/g;
const TYPE_RE = /^\s*?type\s/;

export function findStaticImports(code: string): StaticImport[] {
Expand Down
9 changes: 9 additions & 0 deletions test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe("findExports", () => {
"export async function* foo ()": { type: "declaration", names: ["foo"] },
"export async function *foo ()": { type: "declaration", names: ["foo"] },
"export const $foo = () => {}": { type: "declaration", names: ["$foo"] },
"export default something": {
type: "default",
name: "default",
defaultName: "something",
names: ["default"],
},
"export { foo as default }": {
type: "default",
name: "default",
Expand Down Expand Up @@ -95,6 +101,9 @@ describe("findExports", () => {
if (test.specifier) {
expect(match.specifier).toEqual(test.specifier);
}
if (test.defaultName) {
expect(match.defaultName).toEqual(test.defaultName);
}
});
}
it("handles multiple exports", () => {
Expand Down

0 comments on commit 5213717

Please sign in to comment.