diff --git a/src/analyze.ts b/src/analyze.ts index 817bf52..90801bc 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -70,7 +70,7 @@ const IMPORT_NAMED_TYPE_RE = /(?<=\s|^|;|})import\s*type\s+([\s"']*(?[\w\t\n\r $*,/{}]+)from\s*)?["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gm; export const EXPORT_DECAL_RE = - /\bexport\s+(?(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?[\w$]+)/g; + /\bexport\s+(?(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?[\w$]+)(?.*,\s*[\w$]+)*/g; export const EXPORT_DECAL_TYPE_RE = /\bexport\s+(?(interface|type|declare (async function|function|let|const enum|const|enum|var|class)))\s+(?[\w$]+)/g; const EXPORT_NAMED_RE = @@ -172,6 +172,19 @@ export function findExports(code: string): ESMExport[] { const declaredExports: DeclarationExport[] = matchAll(EXPORT_DECAL_RE, code, { type: "declaration", }); + // Parse extra names (foo, bar) + for (const declaredExport of declaredExports) { + const extraNamesStr = (declaredExport as any).extraNames as + | string + | undefined; + if (extraNamesStr) { + const extraNames = matchAll(/,\s*(?\w+)/g, extraNamesStr, {}).map( + (m) => m.name, + ); + declaredExport.names = [declaredExport.name, ...extraNames]; + } + delete (declaredExport as any).extraNames; + } // Find named exports const namedExports: NamedExport[] = normalizeNamedExports( diff --git a/test/exports.test.ts b/test/exports.test.ts index c4ed759..7b72bfa 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -82,6 +82,10 @@ describe("findExports", () => { "export const [ a, b ] = foo": { type: "named", names: ["a", "b"] }, "export const [\na\n, b ] = foo": { type: "named", names: ["a", "b"] }, "export const [ a:b,\nc = 1] = foo": { type: "named", names: ["b", "c"] }, + "export const foo = 1, bar,baz=3;": { + type: "declaration", + names: ["foo", "bar", "baz"], + }, }; for (const [input, test] of Object.entries(tests)) {