Use arrow syntax for ObjectTypeProperty FunctionTypeAnnotations (#3663)

This fixes an issue where optional properties would get transformed to
an invalid syntax. The easiest solution was to make all FunctionTypes
get transformed to the arrow syntax (previously everything went the
other way).
This commit is contained in:
Paul O’Shannessy 2016-08-20 07:37:56 -07:00 committed by Henry Zhu
parent 07b3dc18a0
commit 55f37a1b1e
7 changed files with 17 additions and 13 deletions

View File

@ -1,7 +1,5 @@
/* eslint max-len: 0 */
import * as t from "babel-types";
export function AnyTypeAnnotation() {
this.word("any");
}
@ -104,7 +102,7 @@ export function FunctionTypeAnnotation(node: Object, parent: Object) {
this.token(")");
// this node type is overloaded, not sure why but it makes it EXTREMELY annoying
if (parent.type === "ObjectTypeProperty" || parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
if (parent.type === "ObjectTypeCallProperty" || parent.type === "DeclareFunction") {
this.token(":");
} else {
this.space();
@ -306,10 +304,8 @@ export function ObjectTypeProperty(node: Object) {
}
this.print(node.key, node);
if (node.optional) this.token("?");
if (!t.isFunctionTypeAnnotation(node.value)) {
this.token(":");
this.space();
}
this.token(":");
this.space();
this.print(node.value, node);
}

View File

@ -7,8 +7,8 @@ declare module A {
declare function foo(): number;
}
declare module A {
declare class B { foo(): number }
declare class B { foo: () => number }
}
declare module A {
declare module.exports: { foo(): number }
declare module.exports: { foo: () => number }
}

View File

@ -12,6 +12,9 @@ declare class A { static () : number }
declare class A mixins B<T>, C {}
declare type A = string
declare type T<U> = { [k:string]: U }
declare type B = {
fn?: (foo: string) => void,
}
declare interface I { foo: string }
declare interface I<T> { foo: T }
declare module.exports: { foo: string }

View File

@ -6,12 +6,15 @@ declare function foo<T>(): void;
declare function foo(x: number, y: string): void;
declare class A {}
declare class A<T> extends B<T> { x: number }
declare class A { static foo(): number; static x: string; }
declare class A { static foo: () => number; static x: string; }
declare class A { static [indexer: number]: string }
declare class A { static (): number }
declare class A mixins B<T>, C {}
declare type A = string;
declare type T<U> = { [k: string]: U };
declare type B = {
fn?: (foo: string) => void
};
declare interface I { foo: string }
declare interface I<T> { foo: T }
declare module.exports: { foo: string }

View File

@ -1,7 +1,7 @@
interface A {};
interface A extends B {};
interface A<T> extends B<T>, C<T> {};
interface A { foo(): number };
interface A { foo: () => number };
interface Dictionary { length: number; [index: string]: string; };
class Foo implements Bar {}
class Foo extends Bar implements Bat, Man<number> {}

View File

@ -47,6 +47,7 @@ var a: { param1: number; param2: string }
var a: { param1: number; param2?: string }
var a: { [a: number]: string; [b: number]: string; };
var a: { add(x: number, ...y: Array<string>): void };
var a: { subtract: (x: number, ...y: Array<string>) => void };
var a: { id<T>(x: T): T; };
var a:Array<number> = [1, 2, 3]
a = class Foo<T> {}

View File

@ -46,8 +46,9 @@ var a: { subObj: ?{ strVal: string } };
var a: { param1: number; param2: string; };
var a: { param1: number; param2?: string; };
var a: { [a: number]: string; [b: number]: string; };
var a: { add(x: number, ...y: Array<string>): void };
var a: { id<T>(x: T): T };
var a: { add: (x: number, ...y: Array<string>) => void };
var a: { subtract: (x: number, ...y: Array<string>) => void };
var a: { id: <T>(x: T) => T };
var a: Array<number> = [1, 2, 3];
a = class Foo<T> {};
a = class Foo<T> extends Bar<T> {};