Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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