Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(findExports): find declaration export with inline object or array #247

Merged
merged 2 commits into from
Apr 30, 2024
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.
Jump to
Jump to file
Failed to load files.
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