Skip to content

Commit

Permalink
refactor: expose as defaultName
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 11, 2024
1 parent 1784a75 commit e602bc1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
28 changes: 3 additions & 25 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface ESMExport {
start: number;
end: number;
name?: string;
defaultName?: string;
names: string[];
specifier?: string;
}
Expand Down Expand Up @@ -80,12 +81,8 @@ const EXPORT_NAMED_DESTRUCT =
/\bexport\s+(let|var|const)\s+(?:{(?<exports1>[^}]+?)[\s,]*}|\[(?<exports2>[^\]]+?)[\s,]*])\s+=/gm;
const EXPORT_STAR_RE =
/\bexport\s*(\*)(\s*as\s+(?<name>[\w$]+)\s+)?\s*(\s*from\s*["']\s*(?<specifier>(?<="\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)(?<defaultName>\w+)/g;
const TYPE_RE = /^\s*?type\s/;

export function findStaticImports(code: string): StaticImport[] {
Expand Down Expand Up @@ -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",
Expand All @@ -236,7 +219,6 @@ export function findExports(code: string): ESMExport[] {
...namedExports,
...destructuredExports,
...defaultExport,
...namedDefaultExport,
...starExports,
]);

Expand Down Expand Up @@ -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";
}
Expand Down
11 changes: 10 additions & 1 deletion test/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("findExports", () => {
type: "named",
},
"export default foo": {
type: "namedDefault",
type: "default",
name: "default",
names: ["default"],
},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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", () => {
Expand Down

0 comments on commit e602bc1

Please sign in to comment.