Skip to content

Commit

Permalink
fix(findExports): support multiple variables in single export (resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 11, 2024
1 parent 5d23c98 commit b853ca4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const IMPORT_NAMED_TYPE_RE =
/(?<=\s|^|;|})import\s*type\s+([\s"']*(?<imports>[\w\t\n\r $*,/{}]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gm;

export const EXPORT_DECAL_RE =
/\bexport\s+(?<declaration>(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?<name>[\w$]+)/g;
/\bexport\s+(?<declaration>(async function\s*\*?|function\s*\*?|let|const enum|const|enum|var|class))\s+\*?(?<name>[\w$]+)(?<extraNames>.*,\s*[\w$]+)*/g;
export const EXPORT_DECAL_TYPE_RE =
/\bexport\s+(?<declaration>(interface|type|declare (async function|function|let|const enum|const|enum|var|class)))\s+(?<name>[\w$]+)/g;
const EXPORT_NAMED_RE =
Expand Down Expand Up @@ -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*(?<name>\w+)/g, extraNamesStr, {}).map(
(m) => m.name,
);
declaredExport.names = [declaredExport.name, ...extraNames];
}
delete (declaredExport as any).extraNames;
}

// Find named exports
const namedExports: NamedExport[] = normalizeNamedExports(
Expand Down
4 changes: 4 additions & 0 deletions test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit b853ca4

Please sign in to comment.