From e602bc1ac9adaa9b8cdab99b8eff650e2ea46b99 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 11 Jan 2024 12:31:22 +0100 Subject: [PATCH] refactor: expose as `defaultName` --- src/analyze.ts | 28 +++------------------------- test/exports.test.ts | 11 ++++++++++- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index 6a38524..f53017e 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -40,6 +40,7 @@ export interface ESMExport { start: number; end: number; name?: string; + defaultName?: string; names: string[]; specifier?: string; } @@ -80,12 +81,8 @@ const EXPORT_NAMED_DESTRUCT = /\bexport\s+(let|var|const)\s+(?:{(?[^}]+?)[\s,]*}|\[(?[^\]]+?)[\s,]*])\s+=/gm; const EXPORT_STAR_RE = /\bexport\s*(\*)(\s*as\s+(?[\w$]+)\s+)?\s*(\s*from\s*["']\s*(?(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][^\n;]*)?/g; -// updated export default to prevent duplication when named export deafult happen const EXPORT_DEFAULT_RE = - /\bexport\s+default\s+(async function|function|class|true|false|(\W\D)|\d)/g; -// named export default -const EXPORT_NAMED_DEFAULT_RE = - /\bexport\s+default\s+(?!async function|function|class|true|false|\W|\d)\w*/g; + /\bexport\s+default\s+(async function|function|class|true|false|(\W\D)|\d)|\bexport\s+default\s+(?!async function|function|class|true|false|\W|\d)(?\w+)/g; const TYPE_RE = /^\s*?type\s/; export function findStaticImports(code: string): StaticImport[] { @@ -210,20 +207,6 @@ export function findExports(code: string): ESMExport[] { name: "default", }); - // Find export default - const namedDefaultExport: DefaultExport[] = matchAll( - EXPORT_NAMED_DEFAULT_RE, - code, - { - type: "namedDefault", - code, - }, - ).map((exp) => { - exp.name = "default"; - - return exp; - }); - // Find export star const starExports: ESMExport[] = matchAll(EXPORT_STAR_RE, code, { type: "star", @@ -236,7 +219,6 @@ export function findExports(code: string): ESMExport[] { ...namedExports, ...destructuredExports, ...defaultExport, - ...namedDefaultExport, ...starExports, ]); @@ -316,11 +298,7 @@ function normalizeExports(exports: ESMExport[]) { if (!exp.name && exp.names && exp.names.length === 1) { exp.name = exp.names[0]; } - if ( - exp.name === "default" && - exp.type !== "default" && - exp.type !== "namedDefault" - ) { + if (exp.name === "default" && exp.type !== "default") { exp._type = exp.type; exp.type = "default"; } diff --git a/test/exports.test.ts b/test/exports.test.ts index e8f6f85..c4ed759 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -22,7 +22,7 @@ describe("findExports", () => { type: "named", }, "export default foo": { - type: "namedDefault", + type: "default", name: "default", names: ["default"], }, @@ -53,6 +53,12 @@ describe("findExports", () => { "export async function* foo ()": { type: "declaration", names: ["foo"] }, "export async function *foo ()": { type: "declaration", names: ["foo"] }, "export const $foo = () => {}": { type: "declaration", names: ["$foo"] }, + "export default something": { + type: "default", + name: "default", + defaultName: "something", + names: ["default"], + }, "export { foo as default }": { type: "default", name: "default", @@ -95,6 +101,9 @@ describe("findExports", () => { if (test.specifier) { expect(match.specifier).toEqual(test.specifier); } + if (test.defaultName) { + expect(match.defaultName).toEqual(test.defaultName); + } }); } it("handles multiple exports", () => {