Skip to content

Commit

Permalink
fix: allow exporting TSDeclareFunction as default (#14763)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxbodya committed Jul 18, 2022
1 parent 354c259 commit d5f4505
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
Expand Up @@ -436,6 +436,7 @@ export default declare<PluginState>((api, options: Options) => {
}
removedPaths.push(path);
} else {
// @ts-expect-error TSDeclareFunction is not expected here
path.replaceWith(buildExportCall("default", declar));
}
} else if (path.isExportNamedDeclaration()) {
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-types/src/ast-types/generated/index.ts
Expand Up @@ -813,7 +813,11 @@ export interface ExportAllDeclaration extends BaseNode {

export interface ExportDefaultDeclaration extends BaseNode {
type: "ExportDefaultDeclaration";
declaration: FunctionDeclaration | ClassDeclaration | Expression;
declaration:
| TSDeclareFunction
| FunctionDeclaration
| ClassDeclaration
| Expression;
exportKind?: "value" | null;
}

Expand Down Expand Up @@ -6722,6 +6726,7 @@ export interface ParentMaps {
TSDeclareFunction:
| BlockStatement
| DoWhileStatement
| ExportDefaultDeclaration
| ExportNamedDeclaration
| ForInStatement
| ForOfStatement
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-types/src/builders/generated/index.ts
Expand Up @@ -639,7 +639,11 @@ export function exportAllDeclaration(
});
}
export function exportDefaultDeclaration(
declaration: t.FunctionDeclaration | t.ClassDeclaration | t.Expression,
declaration:
| t.TSDeclareFunction
| t.FunctionDeclaration
| t.ClassDeclaration
| t.Expression,
): t.ExportDefaultDeclaration {
return validateNode<t.ExportDefaultDeclaration>({
type: "ExportDefaultDeclaration",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/definitions/core.ts
Expand Up @@ -1521,6 +1521,7 @@ defineType("ExportDefaultDeclaration", {
fields: {
declaration: {
validate: assertNodeType(
"TSDeclareFunction",
"FunctionDeclaration",
"ClassDeclaration",
"Expression",
Expand Down
@@ -0,0 +1,23 @@
import * as t from "../../../lib/index.js";

describe("builders", function () {
describe("typescript", function () {
describe("exportDefaultDeclaration", function () {
it("accept TSDeclareFunction as argument for exportDefaultDeclaration", function () {
// this can be used when having function overrides for function exported as default
// export default function test();
// export default function test() {};
expect(() => {
t.exportDefaultDeclaration(
t.tsDeclareFunction(
t.identifier("test"),
null,
[],
t.tsTypeAnnotation(t.tsVoidKeyword()),
),
);
}).not.toThrow();
});
});
});
});

0 comments on commit d5f4505

Please sign in to comment.