From 73755a686df537efd82334f5fa1096215ddbde86 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Dec 2016 00:08:03 -0500 Subject: [PATCH] Fix type annotation for optional function type properties --- src/printer.js | 6 ++++-- tests/method_properties/__snapshots__/jsfmt.spec.js.snap | 6 +++--- tests/refinements/__snapshots__/jsfmt.spec.js.snap | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/printer.js b/src/printer.js index 53fa9dd6f..ed9774f5e 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1332,7 +1332,7 @@ function genericPrintNoParens(path, options, print) { // var A: (a: B) => void; var parent = path.getParentNode(0); var isArrowFunctionTypeAnnotation = !( - (!parent.variance && namedTypes.ObjectTypeProperty.check(parent)) || + (!parent.variance && !parent.optional && namedTypes.ObjectTypeProperty.check(parent)) || namedTypes.ObjectTypeCallProperty.check(parent) || namedTypes.DeclareFunction.check(path.getParentNode(2)) ); @@ -1444,7 +1444,9 @@ function genericPrintNoParens(path, options, print) { var variance = n.variance === "plus" ? "+" : n.variance === "minus" ? "-" : ""; - var isFunction = !n.variance && n.value.type === "FunctionTypeAnnotation"; + // TODO: This is a bad hack and we need a better way to know + // when to emit an arrow function or not. + var isFunction = !n.variance && !n.optional && n.value.type === "FunctionTypeAnnotation"; return concat([ n.static ? "static " : "", diff --git a/tests/method_properties/__snapshots__/jsfmt.spec.js.snap b/tests/method_properties/__snapshots__/jsfmt.spec.js.snap index d06574ab6..77059867c 100644 --- a/tests/method_properties/__snapshots__/jsfmt.spec.js.snap +++ b/tests/method_properties/__snapshots__/jsfmt.spec.js.snap @@ -8,7 +8,7 @@ declare class Foo { export {Foo}; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // @flow -declare class Foo { bar?(): string } +declare class Foo { bar?: () => string } export { Foo } " `; @@ -24,7 +24,7 @@ C.x; (new C).foo.x; C.bar.x; -import {Foo} from './exports_optional_prop'; +import {Foo} from \'./exports_optional_prop\'; const foo = new Foo(); (foo.bar(): string); // error, could be undefined @@ -52,7 +52,7 @@ class C { C.x; new C().foo.x; C.bar.x; -import { Foo } from "./exports_optional_prop"; +import { Foo } from \"./exports_optional_prop\"; const foo = new Foo(); (foo.bar(): string); function f(x) { diff --git a/tests/refinements/__snapshots__/jsfmt.spec.js.snap b/tests/refinements/__snapshots__/jsfmt.spec.js.snap index c1205d0e2..74220dc56 100644 --- a/tests/refinements/__snapshots__/jsfmt.spec.js.snap +++ b/tests/refinements/__snapshots__/jsfmt.spec.js.snap @@ -490,7 +490,7 @@ function bar(x:Object) { // treated as \`any\`, so allowed // still treated as \`any\`, so allowed // also treated as \`any\`, so allowed -function foo(x: { y?(): void }) { +function foo(x: { y?: () => void }) { x.y(); if (x.hasOwnProperty(\"y\")) { x.y(); @@ -708,7 +708,7 @@ exports[`test lib.js 1`] = ` declare var BAZ: {stuff?: (x: number) => void} | void; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* @flow */ -declare var BAZ: { stuff?(x: number): void } | void; +declare var BAZ: { stuff?: (x: number) => void } | void; " `;