Skip to content

Commit c28465e

Browse files
jbrown215hzoo
authored andcommitted
Flow opaque type 6.x backport (#6081)
* Flow opaque type backport * Add tests for strip types, comments, and babel-generator * Fix failing tests, run scripts * Bump babylon to 6.18.0
1 parent 2dba910 commit c28465e

File tree

16 files changed

+353
-78
lines changed

16 files changed

+353
-78
lines changed

lib/types.js

Lines changed: 121 additions & 63 deletions
Large diffs are not rendered by default.

packages/babel-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"babel-register": "^6.24.1",
3636
"babel-traverse": "^6.25.0",
3737
"babel-types": "^6.25.0",
38-
"babylon": "^6.17.2",
38+
"babylon": "^6.18.0",
3939
"convert-source-map": "^1.1.0",
4040
"debug": "^2.1.1",
4141
"json5": "^0.5.0",

packages/babel-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
},
2323
"devDependencies": {
2424
"babel-helper-fixtures": "^6.22.0",
25-
"babylon": "^6.17.2"
25+
"babylon": "^6.18.0"
2626
}
2727
}

packages/babel-generator/src/generators/flow.js

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as t from "babel-types";
2+
13
export function AnyTypeAnnotation() {
24
this.word("any");
35
}
@@ -20,17 +22,21 @@ export function NullLiteralTypeAnnotation() {
2022
this.word("null");
2123
}
2224

23-
export function DeclareClass(node: Object) {
24-
this.word("declare");
25-
this.space();
25+
export function DeclareClass(node: Object, parent: Object) {
26+
if (!t.isDeclareExportDeclaration(parent)) {
27+
this.word("declare");
28+
this.space();
29+
}
2630
this.word("class");
2731
this.space();
2832
this._interfaceish(node);
2933
}
3034

31-
export function DeclareFunction(node: Object) {
32-
this.word("declare");
33-
this.space();
35+
export function DeclareFunction(node: Object, parent: Object) {
36+
if (!t.isDeclareExportDeclaration(parent)) {
37+
this.word("declare");
38+
this.space();
39+
}
3440
this.word("function");
3541
this.space();
3642
this.print(node.id, node);
@@ -69,16 +75,64 @@ export function DeclareTypeAlias(node: Object) {
6975
this.TypeAlias(node);
7076
}
7177

72-
export function DeclareVariable(node: Object) {
73-
this.word("declare");
74-
this.space();
78+
export function DeclareOpaqueType(node: Object, parent: Object) {
79+
if (!t.isDeclareExportDeclaration(parent)) {
80+
this.word("declare");
81+
this.space();
82+
}
83+
this.OpaqueType(node);
84+
}
85+
86+
export function DeclareVariable(node: Object, parent: Object) {
87+
if (!t.isDeclareExportDeclaration(parent)) {
88+
this.word("declare");
89+
this.space();
90+
}
7591
this.word("var");
7692
this.space();
7793
this.print(node.id, node);
7894
this.print(node.id.typeAnnotation, node);
7995
this.semicolon();
8096
}
8197

98+
export function DeclareExportDeclaration(node: Object) {
99+
this.word("declare");
100+
this.space();
101+
this.word("export");
102+
this.space();
103+
if (node.default) {
104+
this.word("default");
105+
this.space();
106+
}
107+
108+
FlowExportDeclaration.apply(this, arguments);
109+
}
110+
111+
function FlowExportDeclaration(node: Object) {
112+
if (node.declaration) {
113+
const declar = node.declaration;
114+
this.print(declar, node);
115+
if (!t.isStatement(declar)) this.semicolon();
116+
} else {
117+
this.token("{");
118+
if (node.specifiers.length) {
119+
this.space();
120+
this.printList(node.specifiers, node);
121+
this.space();
122+
}
123+
this.token("}");
124+
125+
if (node.source) {
126+
this.space();
127+
this.word("from");
128+
this.space();
129+
this.print(node.source, node);
130+
}
131+
132+
this.semicolon();
133+
}
134+
}
135+
82136
export function ExistentialTypeParam() {
83137
this.token("*");
84138
}
@@ -222,6 +276,26 @@ export function TypeAlias(node: Object) {
222276
this.print(node.right, node);
223277
this.semicolon();
224278
}
279+
export function OpaqueType(node: Object) {
280+
this.word("opaque");
281+
this.space();
282+
this.word("type");
283+
this.space();
284+
this.print(node.id, node);
285+
this.print(node.typeParameters, node);
286+
if (node.supertype) {
287+
this.token(":");
288+
this.space();
289+
this.print(node.supertype, node);
290+
}
291+
if (node.impltype) {
292+
this.space();
293+
this.token("=");
294+
this.space();
295+
this.print(node.impltype, node);
296+
}
297+
this.semicolon();
298+
}
225299

226300
export function TypeAnnotation(node: Object) {
227301
this.token(":");

packages/babel-generator/test/fixtures/flow/declare-statements/actual.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ declare type B = {
1818
declare interface I { foo: string }
1919
declare interface I<T> { foo: T }
2020
declare module.exports: { foo: string }
21+
declare opaque type Foo<T>: Bar<T>;
22+
declare opaque type ID;
23+
declare opaque type num: number;
24+
declare opaque type NumArray;

packages/babel-generator/test/fixtures/flow/declare-statements/expected.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ declare type B = {
1818
declare interface I { foo: string }
1919
declare interface I<T> { foo: T }
2020
declare module.exports: { foo: string }
21+
declare opaque type Foo<T>: Bar<T>;
22+
declare opaque type ID;
23+
declare opaque type num: number;
24+
declare opaque type NumArray;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
opaque type ID = string;
2+
opaque type Foo<T> = Bar<T>;
3+
opaque type Maybe<T> = _Maybe<T, *>;
4+
export opaque type Foo = number;
5+
6+
opaque type union =
7+
| {type: "A"}
8+
| {type: "B"}
9+
;
10+
11+
opaque type overloads =
12+
& ((x: string) => number)
13+
& ((x: number) => string)
14+
;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
opaque type ID = string;
2+
opaque type Foo<T> = Bar<T>;
3+
opaque type Maybe<T> = _Maybe<T, *>;
4+
export opaque type Foo = number;
5+
6+
opaque type union = { type: "A" } | { type: "B" };
7+
8+
opaque type overloads = (x: string) => number & (x: number) => string;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function a() {}
2+
opaque type A = number;
3+
opaque type B = {
4+
name: string;
5+
};
6+
7+
opaque type union =
8+
| {type: "A"}
9+
| {type: "B"}
10+
;
11+
12+
opaque type overloads =
13+
& ((x: string) => number)
14+
& ((x: number) => string)
15+
;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function a() {}
2+
/*:: opaque type A = number;*/
3+
/*:: opaque type B = {
4+
name: string;
5+
};*/
6+
/*:: opaque type union =
7+
| {type: "A"}
8+
| {type: "B"}
9+
;*/
10+
/*:: opaque type overloads =
11+
& ((x: string) => number)
12+
& ((x: number) => string)
13+
;*/

0 commit comments

Comments
 (0)