From b853ca4f469e8e43afc1fc10d9485e8a0f4be792 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 11 Jan 2024 22:27:41 +0100 Subject: [PATCH] fix(findExports): support multiple variables in single export (resolves #211) --- src/analyze.ts | 15 ++++++++++++++- test/exports.test.ts | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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)) {