Skip to content

Commit

Permalink
Cherry-pick babel#418 to 6.x (babel#476)
Browse files Browse the repository at this point in the history
* Add support for flow type spread (babel#418)

* Add support for flow type spread

* Broaden spreadable types from primary to all, more tests

* Eliminate variance sigil for type spreads, better errors, fix tests

# Conflicts:
#	src/plugins/flow.js

* Fix tests
  • Loading branch information
Sebastian McKenzie committed Apr 20, 2017
1 parent fab343e commit a4ca1cc
Show file tree
Hide file tree
Showing 14 changed files with 638 additions and 22 deletions.
54 changes: 35 additions & 19 deletions src/plugins/flow.js
Expand Up @@ -194,7 +194,7 @@ pp.flowParseDeclareInterface = function (node) {

// Interfaces

pp.flowParseInterfaceish = function (node, allowStatic) {
pp.flowParseInterfaceish = function (node) {
node.id = this.parseIdentifier();

if (this.isRelational("<")) {
Expand All @@ -219,7 +219,7 @@ pp.flowParseInterfaceish = function (node, allowStatic) {
} while (this.eat(tt.comma));
}

node.body = this.flowParseObjectType(allowStatic);
node.body = this.flowParseObjectType(true, false, false);
};

pp.flowParseInterfaceExtends = function () {
Expand Down Expand Up @@ -400,7 +400,7 @@ pp.flowParseObjectTypeCallProperty = function (node, isStatic) {
return this.finishNode(node, "ObjectTypeCallProperty");
};

pp.flowParseObjectType = function (allowStatic, allowExact) {
pp.flowParseObjectType = function (allowStatic, allowExact, allowSpread) {
const oldInType = this.state.inType;
this.state.inType = true;

Expand Down Expand Up @@ -448,24 +448,40 @@ pp.flowParseObjectType = function (allowStatic, allowExact) {
}
nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, isStatic));
} else {
propertyKey = this.flowParseObjectPropertyKey();
if (this.isRelational("<") || this.match(tt.parenL)) {
// This is a method property
if (this.match(tt.ellipsis)) {
if (!allowSpread) {
this.unexpected(
null,
"Spread operator cannnot appear in class or interface definitions"
);
}
if (variance) {
this.unexpected(variancePos);
this.unexpected(variance.start, "Spread properties cannot have variance");
}
nodeStart.properties.push(this.flowParseObjectTypeMethod(startPos, startLoc, isStatic, propertyKey));
this.expect(tt.ellipsis);
node.argument = this.flowParseType();
this.flowObjectTypeSemicolon();
nodeStart.properties.push(this.finishNode(node, "ObjectTypeSpreadProperty"));
} else {
if (this.eat(tt.question)) {
optional = true;
propertyKey = this.flowParseObjectPropertyKey();
if (this.isRelational("<") || this.match(tt.parenL)) {
// This is a method property
if (variance) {
this.unexpected(variance.start);
}
nodeStart.properties.push(this.flowParseObjectTypeMethod(startPos, startLoc, isStatic, propertyKey));
} else {
if (this.eat(tt.question)) {
optional = true;
}
node.key = propertyKey;
node.value = this.flowParseTypeInitialiser();
node.optional = optional;
node.static = isStatic;
node.variance = variance;
this.flowObjectTypeSemicolon();
nodeStart.properties.push(this.finishNode(node, "ObjectTypeProperty"));
}
node.key = propertyKey;
node.value = this.flowParseTypeInitialiser();
node.optional = optional;
node.static = isStatic;
node.variance = variance;
this.flowObjectTypeSemicolon();
nodeStart.properties.push(this.finishNode(node, "ObjectTypeProperty"));
}
}

Expand Down Expand Up @@ -627,10 +643,10 @@ pp.flowParsePrimaryType = function () {
return this.flowIdentToTypeAnnotation(startPos, startLoc, node, this.parseIdentifier());

case tt.braceL:
return this.flowParseObjectType(false, false);
return this.flowParseObjectType(false, false, true);

case tt.braceBarL:
return this.flowParseObjectType(false, true);
return this.flowParseObjectType(false, true, true);

case tt.bracketL:
return this.flowParseTupleType();
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/flow/type-annotations/112/options.json
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:10)"
"throws": "Unexpected token (1:12)"
}
2 changes: 1 addition & 1 deletion test/fixtures/flow/type-annotations/113/options.json
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:10)"
"throws": "Unexpected token (1:12)"
}
2 changes: 1 addition & 1 deletion test/fixtures/flow/type-annotations/125/options.json
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:26)"
"throws": "Unexpected token (1:29)"
}
3 changes: 3 additions & 0 deletions test/fixtures/flow/type-annotations/135/actual.js
@@ -0,0 +1,3 @@
type A = {
...any,
};
117 changes: 117 additions & 0 deletions test/fixtures/flow/type-annotations/135/expected.json
@@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 2
}
},
"sourceType": "module",
"body": [
{
"type": "TypeAlias",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 2
}
},
"id": {
"type": "Identifier",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "A"
},
"name": "A"
},
"typeParameters": null,
"right": {
"type": "ObjectTypeAnnotation",
"start": 9,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 3,
"column": 1
}
},
"callProperties": [],
"properties": [
{
"type": "ObjectTypeSpreadProperty",
"start": 12,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 8
}
},
"argument": {
"type": "AnyTypeAnnotation",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 7
}
}
}
}
],
"indexers": [],
"exact": false
}
}
],
"directives": []
}
}
4 changes: 4 additions & 0 deletions test/fixtures/flow/type-annotations/136/actual.js
@@ -0,0 +1,4 @@
type A = {
p: {},
...{},
};

0 comments on commit a4ca1cc

Please sign in to comment.