Skip to content

Commit

Permalink
fix(findExports): find declaration export with inline object or array (
Browse files Browse the repository at this point in the history
  • Loading branch information
Shunjun committed Apr 30, 2024
1 parent d34fda4 commit d880b1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ const IMPORT_NAMED_TYPE_RE =
* @example `export const num = 1, str = 'hello'; export class Example {}`
*/
export const EXPORT_DECAL_RE =
/\bexport\s+(?<declaration>(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?<name>[\w$]+)(?<extraNames>.*,\s*[\w$]+)*/g;

/\bexport\s+(?<declaration>(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?<name>[\w$]+)(?<extraNames>.*,\s*[\s\w:[\]{}]*[\w$\]}]+)*/g;
/**
* Regular expression to match export declarations specifically for types, interfaces, and type aliases in TypeScript.
* @example `export type Result = { success: boolean; }; export interface User { name: string; age: number; };`
Expand Down Expand Up @@ -403,9 +402,13 @@ export function findExports(code: string): ESMExport[] {
| string
| undefined;
if (extraNamesStr) {
const extraNames = matchAll(/,\s*(?<name>\w+)/g, extraNamesStr, {}).map(
(m) => m.name,
);
const extraNames = matchAll(
/({.*?})|(\[.*?])|(,\s*(?<name>\w+))/g,
extraNamesStr,
{},
)
.map((m) => m.name)
.filter(Boolean);
declaredExport.names = [declaredExport.name, ...extraNames];
}
delete (declaredExport as any).extraNames;
Expand Down
12 changes: 12 additions & 0 deletions test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ describe("findExports", () => {
type: "declaration",
names: ["foo", "bar", "baz"],
},
"export const foo = [ 1, bar, baz ];": {
type: "declaration",
names: ["foo"],
},
"export const foo = [ 1, bar ], baz = 2;": {
type: "declaration",
names: ["foo", "baz"],
},
"export const foo = { bar, bar1: [ qux , qux1 ] }, baz = 2;": {
type: "declaration",
names: ["foo", "baz"],
},
};

for (const [input, test] of Object.entries(tests)) {
Expand Down

0 comments on commit d880b1e

Please sign in to comment.