diff --git a/src/analyze.ts b/src/analyze.ts index 6fb2012..c6708ac 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -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+(?(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?[\w$]+)(?.*,\s*[\w$]+)*/g; - + /\bexport\s+(?(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?[\w$]+)(?.*,\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; };` @@ -403,9 +402,13 @@ export function findExports(code: string): ESMExport[] { | string | undefined; if (extraNamesStr) { - const extraNames = matchAll(/,\s*(?\w+)/g, extraNamesStr, {}).map( - (m) => m.name, - ); + const extraNames = matchAll( + /({.*?})|(\[.*?])|(,\s*(?\w+))/g, + extraNamesStr, + {}, + ) + .map((m) => m.name) + .filter(Boolean); declaredExport.names = [declaredExport.name, ...extraNames]; } delete (declaredExport as any).extraNames; diff --git a/test/exports.test.ts b/test/exports.test.ts index 61c2840..20cf9a7 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -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)) {