Skip to content

Commit 4079ee5

Browse files
authored
Support flow function type annotation with no parent (#14014)
1 parent f4236f4 commit 4079ee5

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎packages/babel-generator/src/generators/flow.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export function ExistsTypeAnnotation(this: Printer) {
290290
export function FunctionTypeAnnotation(
291291
this: Printer,
292292
node: t.FunctionTypeAnnotation,
293-
parent: any,
293+
parent: t.Node | void,
294294
) {
295295
this.print(node.typeParameters, node);
296296
this.token("(");
@@ -321,9 +321,10 @@ export function FunctionTypeAnnotation(
321321

322322
// this node type is overloaded, not sure why but it makes it EXTREMELY annoying
323323
if (
324-
parent.type === "ObjectTypeCallProperty" ||
325-
parent.type === "DeclareFunction" ||
326-
(parent.type === "ObjectTypeProperty" && parent.method)
324+
parent &&
325+
(parent.type === "ObjectTypeCallProperty" ||
326+
parent.type === "DeclareFunction" ||
327+
(parent.type === "ObjectTypeProperty" && parent.method))
327328
) {
328329
this.token(":");
329330
} else {

‎packages/babel-generator/test/index.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ describe("programmatic generation", function () {
497497
expect(output).toBe("interface A {}");
498498
});
499499

500+
it("flow function type annotation with no parent", () => {
501+
const functionTypeAnnotation = t.functionTypeAnnotation(
502+
null,
503+
[],
504+
null,
505+
t.voidTypeAnnotation(),
506+
);
507+
const output = generate(functionTypeAnnotation).code;
508+
expect(output).toBe("() => void");
509+
});
510+
500511
describe("directives", function () {
501512
it("preserves escapes", function () {
502513
const directive = t.directive(

0 commit comments

Comments
 (0)