Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Cherry-pick #418 to 6.x #476

Merged
merged 2 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type A = {
...any,
};
117 changes: 117 additions & 0 deletions test/fixtures/flow/type-annotations/135/expected.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type A = {
p: {},
...{},
};
Loading