Skip to content

Commit

Permalink
Flow opaque type 6.x backport (#6081)
Browse files Browse the repository at this point in the history
* Flow opaque type backport

* Add tests for strip types, comments, and babel-generator

* Fix failing tests, run scripts

* Bump babylon to 6.18.0
  • Loading branch information
jbrown215 authored and hzoo committed Aug 15, 2017
1 parent 2dba910 commit c28465e
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 78 deletions.
184 changes: 121 additions & 63 deletions lib/types.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Expand Up @@ -22,6 +22,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "^6.22.0",
"babylon": "^6.17.2"
"babylon": "^6.18.0"
}
}
92 changes: 83 additions & 9 deletions packages/babel-generator/src/generators/flow.js
@@ -1,3 +1,5 @@
import * as t from "babel-types";

export function AnyTypeAnnotation() {
this.word("any");
}
Expand All @@ -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);
Expand Down Expand Up @@ -69,16 +75,64 @@ 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);
this.print(node.id.typeAnnotation, node);
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("*");
}
Expand Down Expand Up @@ -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(":");
Expand Down
Expand Up @@ -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;
Expand Up @@ -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;
@@ -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)
;
@@ -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;
@@ -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)
;
@@ -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)
;*/
@@ -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;
2 changes: 1 addition & 1 deletion packages/babel-traverse/package.json
Expand Up @@ -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",
Expand Down
48 changes: 46 additions & 2 deletions packages/babel-types/README.md
Expand Up @@ -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`)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/package.json
Expand Up @@ -15,6 +15,6 @@
},
"devDependencies": {
"babel-generator": "^6.22.0",
"babylon": "^6.17.2"
"babylon": "^6.18.0"
}
}
24 changes: 24 additions & 0 deletions packages/babel-types/src/definitions/flow.js
Expand Up @@ -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"],
Expand All @@ -111,6 +119,14 @@ defineType("DeclareVariable", {
}
});

defineType("DeclareExportDeclaration", {
visitor: ["declaration", "specifiers", "source"],
aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"],
fields: {
// todo
},
});

defineType("ExistentialTypeParam", {
aliases: ["Flow"]
});
Expand Down Expand Up @@ -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"],
Expand Down
1 change: 1 addition & 0 deletions packages/babel-types/src/retrievers.js
Expand Up @@ -70,6 +70,7 @@ getBindingIdentifiers.keys = {
DeclareVariable: ["id"],
InterfaceDeclaration: ["id"],
TypeAlias: ["id"],
OpaqueType: ["id"],

CatchClause: ["param"],
LabeledStatement: ["label"],
Expand Down

0 comments on commit c28465e

Please sign in to comment.