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
This commit is contained in:
jbrown215
2017-08-15 16:44:15 -04:00
committed by Henry Zhu
parent 2dba910b9e
commit c28465e03e
16 changed files with 353 additions and 78 deletions

View File

@@ -35,7 +35,7 @@
"babel-register": "^6.24.1",
"babel-traverse": "^6.25.0",
"babel-types": "^6.25.0",
"babylon": "^6.17.2",
"babylon": "^6.18.0",
"convert-source-map": "^1.1.0",
"debug": "^2.1.1",
"json5": "^0.5.0",

View File

@@ -22,6 +22,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "^6.22.0",
"babylon": "^6.17.2"
"babylon": "^6.18.0"
}
}

View File

@@ -1,3 +1,5 @@
import * as t from "babel-types";
export function AnyTypeAnnotation() {
this.word("any");
}
@@ -20,17 +22,21 @@ export function NullLiteralTypeAnnotation() {
this.word("null");
}
export function DeclareClass(node: Object) {
this.word("declare");
this.space();
export function DeclareClass(node: Object, parent: Object) {
if (!t.isDeclareExportDeclaration(parent)) {
this.word("declare");
this.space();
}
this.word("class");
this.space();
this._interfaceish(node);
}
export function DeclareFunction(node: Object) {
this.word("declare");
this.space();
export function DeclareFunction(node: Object, parent: Object) {
if (!t.isDeclareExportDeclaration(parent)) {
this.word("declare");
this.space();
}
this.word("function");
this.space();
this.print(node.id, node);
@@ -69,9 +75,19 @@ export function DeclareTypeAlias(node: Object) {
this.TypeAlias(node);
}
export function DeclareVariable(node: Object) {
this.word("declare");
this.space();
export function DeclareOpaqueType(node: Object, parent: Object) {
if (!t.isDeclareExportDeclaration(parent)) {
this.word("declare");
this.space();
}
this.OpaqueType(node);
}
export function DeclareVariable(node: Object, parent: Object) {
if (!t.isDeclareExportDeclaration(parent)) {
this.word("declare");
this.space();
}
this.word("var");
this.space();
this.print(node.id, node);
@@ -79,6 +95,44 @@ export function DeclareVariable(node: Object) {
this.semicolon();
}
export function DeclareExportDeclaration(node: Object) {
this.word("declare");
this.space();
this.word("export");
this.space();
if (node.default) {
this.word("default");
this.space();
}
FlowExportDeclaration.apply(this, arguments);
}
function FlowExportDeclaration(node: Object) {
if (node.declaration) {
const declar = node.declaration;
this.print(declar, node);
if (!t.isStatement(declar)) this.semicolon();
} else {
this.token("{");
if (node.specifiers.length) {
this.space();
this.printList(node.specifiers, node);
this.space();
}
this.token("}");
if (node.source) {
this.space();
this.word("from");
this.space();
this.print(node.source, node);
}
this.semicolon();
}
}
export function ExistentialTypeParam() {
this.token("*");
}
@@ -222,6 +276,26 @@ export function TypeAlias(node: Object) {
this.print(node.right, node);
this.semicolon();
}
export function OpaqueType(node: Object) {
this.word("opaque");
this.space();
this.word("type");
this.space();
this.print(node.id, node);
this.print(node.typeParameters, node);
if (node.supertype) {
this.token(":");
this.space();
this.print(node.supertype, node);
}
if (node.impltype) {
this.space();
this.token("=");
this.space();
this.print(node.impltype, node);
}
this.semicolon();
}
export function TypeAnnotation(node: Object) {
this.token(":");

View File

@@ -18,3 +18,7 @@ declare type B = {
declare interface I { foo: string }
declare interface I<T> { foo: T }
declare module.exports: { foo: string }
declare opaque type Foo<T>: Bar<T>;
declare opaque type ID;
declare opaque type num: number;
declare opaque type NumArray;

View File

@@ -18,3 +18,7 @@ declare type B = {
declare interface I { foo: string }
declare interface I<T> { foo: T }
declare module.exports: { foo: string }
declare opaque type Foo<T>: Bar<T>;
declare opaque type ID;
declare opaque type num: number;
declare opaque type NumArray;

View File

@@ -0,0 +1,14 @@
opaque type ID = string;
opaque type Foo<T> = Bar<T>;
opaque type Maybe<T> = _Maybe<T, *>;
export opaque type Foo = number;
opaque type union =
| {type: "A"}
| {type: "B"}
;
opaque type overloads =
& ((x: string) => number)
& ((x: number) => string)
;

View File

@@ -0,0 +1,8 @@
opaque type ID = string;
opaque type Foo<T> = Bar<T>;
opaque type Maybe<T> = _Maybe<T, *>;
export opaque type Foo = number;
opaque type union = { type: "A" } | { type: "B" };
opaque type overloads = (x: string) => number & (x: number) => string;

View File

@@ -0,0 +1,15 @@
function a() {}
opaque type A = number;
opaque type B = {
name: string;
};
opaque type union =
| {type: "A"}
| {type: "B"}
;
opaque type overloads =
& ((x: string) => number)
& ((x: number) => string)
;

View File

@@ -0,0 +1,13 @@
function a() {}
/*:: opaque type A = number;*/
/*:: opaque type B = {
name: string;
};*/
/*:: opaque type union =
| {type: "A"}
| {type: "B"}
;*/
/*:: opaque type overloads =
& ((x: string) => number)
& ((x: number) => string)
;*/

View File

@@ -0,0 +1,16 @@
opaque type ID = string;
opaque type Foo<T> = Bar<T>
export opaque type Foo = number;
opaque type union =
| {type: "A"}
| {type: "B"}
;
opaque type overloads =
& ((x: string) => number)
& ((x: number) => string)
;
declare opaque type Foo: Bar;
declare export opaque type Foo: Bar;

View File

@@ -12,7 +12,7 @@
"babel-messages": "^6.23.0",
"babel-runtime": "^6.22.0",
"babel-types": "^6.25.0",
"babylon": "^6.17.2",
"babylon": "^6.18.0",
"debug": "^2.2.0",
"globals": "^9.0.0",
"invariant": "^2.2.0",

View File

@@ -46,7 +46,7 @@ See also `t.isArrayPattern(node, opts)` and `t.assertArrayPattern(node, opts)`.
Aliases: `Pattern`, `LVal`
- `elements`: `Array<Expression>` (required)
- `elements`: `Array<Identifier | Pattern | RestElement>` (required)
- `typeAnnotation` (required)
- `decorators`: `Array<Decorator>` (default: `null`)
@@ -463,6 +463,21 @@ Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration`
---
### declareOpaqueType
```javascript
t.declareOpaqueType(id, typeParameters, supertype)
```
See also `t.isDeclareOpaqueType(node, opts)` and `t.assertDeclareOpaqueType(node, opts)`.
Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration`
- `id` (required)
- `typeParameters` (required)
- `supertype` (required)
---
### declareTypeAlias
```javascript
t.declareTypeAlias(id, typeParameters, right)
@@ -1368,7 +1383,7 @@ See also `t.isObjectProperty(node, opts)` and `t.assertObjectProperty(node, opts
Aliases: `UserWhitespacable`, `Property`, `ObjectMember`
- `key`if computed then `Expression` else `Identifier | Literal` (required)
- `value`: `Expression` (required)
- `value`: `Expression | Pattern | RestElement` (required)
- `computed`: `boolean` (default: `false`)
- `shorthand`: `boolean` (default: `false`)
- `decorators`: `Array<Decorator>` (default: `null`)
@@ -1432,6 +1447,35 @@ Aliases: `Flow`, `UserWhitespacable`
---
### objectTypeSpreadProperty
```javascript
t.objectTypeSpreadProperty(argument)
```
See also `t.isObjectTypeSpreadProperty(node, opts)` and `t.assertObjectTypeSpreadProperty(node, opts)`.
Aliases: `Flow`, `UserWhitespacable`
- `argument` (required)
---
### opaqueType
```javascript
t.opaqueType(id, typeParameters, impltype, supertype)
```
See also `t.isOpaqueType(node, opts)` and `t.assertOpaqueType(node, opts)`.
Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration`
- `id` (required)
- `typeParameters` (required)
- `impltype` (required)
- `supertype` (required)
---
### parenthesizedExpression
```javascript
t.parenthesizedExpression(expression)

View File

@@ -15,6 +15,6 @@
},
"devDependencies": {
"babel-generator": "^6.22.0",
"babylon": "^6.17.2"
"babylon": "^6.18.0"
}
}

View File

@@ -103,6 +103,14 @@ defineType("DeclareTypeAlias", {
}
});
defineType("DeclareOpaqueType", {
visitor: ["id", "typeParameters", "supertype"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
fields: {
// todo
}
});
defineType("DeclareVariable", {
visitor: ["id"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
@@ -111,6 +119,14 @@ defineType("DeclareVariable", {
}
});
defineType("DeclareExportDeclaration", {
visitor: ["declaration", "specifiers", "source"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
fields: {
// todo
},
});
defineType("ExistentialTypeParam", {
aliases: ["Flow"]
});
@@ -236,6 +252,14 @@ defineType("TypeAlias", {
}
});
defineType("OpaqueType", {
visitor: ["id", "typeParameters", "impltype", "supertype"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
fields: {
// todo
}
});
defineType("TypeAnnotation", {
visitor: ["typeAnnotation"],
aliases: ["Flow"],

View File

@@ -70,6 +70,7 @@ getBindingIdentifiers.keys = {
DeclareVariable: ["id"],
InterfaceDeclaration: ["id"],
TypeAlias: ["id"],
OpaqueType: ["id"],
CatchClause: ["param"],
LabeledStatement: ["label"],