Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow exporting TSDeclareFunction as default #14763

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
Comment on lines +439 to 440
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from my understanding TSDeclareFunction is not expected to get there(I would assume it is to be removed somewhere earlier, and it is fine to ignore this)

}
} 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();
});
});
});
});