Add support for flow predicates in babel-generator (#5984)

This commit is contained in:
Brian Ng
2017-07-25 10:38:17 -05:00
committed by Henry Zhu
parent 55aea26f13
commit 9a1b8ea443
10 changed files with 122 additions and 0 deletions

View File

@@ -42,9 +42,28 @@ export function DeclareFunction(node: Object, parent: Object) {
this.space();
this.print(node.id, node);
this.print(node.id.typeAnnotation.typeAnnotation, node);
if (node.predicate) {
this.space();
this.print(node.predicate, node);
}
this.semicolon();
}
export function InferredPredicate(/*node: Object*/) {
this.token("%");
this.word("checks");
}
export function DeclaredPredicate(node: Object) {
this.token("%");
this.word("checks");
this.token("(");
this.print(node.value, node);
this.token(")");
}
export function DeclareInterface(node: Object) {
this.word("declare");
this.space();

View File

@@ -49,6 +49,16 @@ export function _method(node: Object) {
this.print(node.body, node);
}
export function _predicate(node: Object) {
if (node.predicate) {
if (!node.returnType) {
this.token(":");
}
this.space();
this.print(node.predicate, node);
}
}
export function FunctionExpression(node: Object) {
if (node.async) {
this.word("async");
@@ -65,6 +75,8 @@ export function FunctionExpression(node: Object) {
}
this._params(node);
this._predicate(node);
this.space();
this.print(node.body, node);
}
@@ -89,6 +101,8 @@ export function ArrowFunctionExpression(node: Object) {
this._params(node);
}
this._predicate(node);
this.space();
this.token("=>");
this.space();

View File

@@ -0,0 +1,17 @@
declare function foo(x: mixed): boolean %checks(x !== null);
declare function my_filter<T, P: $Pred<1>>(v: Array<T>, cb: P): Array<$Refine<T,P,1>>;
declare function f2(x: mixed): string %checks(Array.isArray(x));
function foo(x: mixed): %checks { return typeof x === "string"; }
function is_string(x): boolean %checks {
return typeof x === "string";
}
var f = (x: mixed): %checks => typeof x === "string";
const foo = (x: mixed): boolean %checks => typeof x === "string";
(x): %checks => x !== null;

View File

@@ -0,0 +1,17 @@
declare function foo(x: mixed): boolean %checks(x !== null);
declare function my_filter<T, P: $Pred<1>>(v: Array<T>, cb: P): Array<$Refine<T, P, 1>>;
declare function f2(x: mixed): string %checks(Array.isArray(x));
function foo(x: mixed): %checks {
return typeof x === "string";
}
function is_string(x): boolean %checks {
return typeof x === "string";
}
var f = (x: mixed): %checks => typeof x === "string";
const foo = (x: mixed): boolean %checks => typeof x === "string";
x: %checks => x !== null;

View File

@@ -67,6 +67,8 @@ export default function({ types: t }) {
const param = node.params[i];
param.optional = false;
}
node.predicate = null;
},
TypeCastExpression(path) {

View File

@@ -0,0 +1 @@
declare function foo(x: mixed): boolean %checks(typeof x === "string");

View File

@@ -0,0 +1,5 @@
var f = (x): %checks => typeof x === "string";
var g = (x: mixed): boolean %checks => typeof x === "string";
function h(x: mixed): %checks {
return typeof x === "string";
}

View File

@@ -0,0 +1,7 @@
var f = x => typeof x === "string";
var g = x => typeof x === "string";
function h(x) {
return typeof x === "string";
}

View File

@@ -520,6 +520,19 @@ Aliases: `Flow`, `FlowDeclaration`, `Statement`, `Declaration`
---
### declaredPredicate
```javascript
t.declaredPredicate(value)
```
See also `t.isDeclaredPredicate(node, opts)` and `t.assertDeclaredPredicate(node, opts)`.
Aliases: `Flow`, `FlowPredicate`
- `value` (required)
---
### decorator
```javascript
t.decorator(expression)
@@ -947,6 +960,18 @@ Aliases: `ModuleSpecifier`
- `imported`: `Identifier` (required)
- `importKind`: `null | 'type' | 'typeof'` (default: `null`)
---
### inferredPredicate
```javascript
t.inferredPredicate()
```
See also `t.isInferredPredicate(node, opts)` and `t.assertInferredPredicate(node, opts)`.
Aliases: `Flow`, `FlowPredicate`
---
### interfaceDeclaration

View File

@@ -125,6 +125,14 @@ defineType("DeclareExportAllDeclaration", {
},
});
defineType("DeclaredPredicate", {
visitor: ["value"],
aliases: ["Flow", "FlowPredicate"],
fields: {
// todo
},
});
defineType("ExistsTypeAnnotation", {
aliases: ["Flow"],
});
@@ -153,6 +161,13 @@ defineType("GenericTypeAnnotation", {
},
});
defineType("InferredPredicate", {
aliases: ["Flow", "FlowPredicate"],
fields: {
// todo
},
});
defineType("InterfaceExtends", {
visitor: ["id", "typeParameters"],
aliases: ["Flow"],