Fix type annotation for optional function type properties

This commit is contained in:
James Long committed 2016-12-30 00:08:03 -05:00
1 parent 11a01552a4
commit 73755a686d
3 files changed
+9 -7

No files matched your search

+4 -2
View File
@@ -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 " : "",
@@ -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) {
@@ -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;
"
`;