Add support for flow type spread (#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
This commit is contained in:
Conrad Buck
2017-04-03 13:05:05 -07:00
committed by Daniel Tschinder
parent 4d18221098
commit 213fdab063
11 changed files with 635 additions and 19 deletions

View File

@@ -219,7 +219,7 @@ pp.flowParseInterfaceish = function (node) {
} while (this.eat(tt.comma));
}
node.body = this.flowParseObjectType(true);
node.body = this.flowParseObjectType(true, false, false);
};
pp.flowParseInterfaceExtends = function () {
@@ -403,7 +403,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;
@@ -450,24 +450,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(variance.start);
this.unexpected(variance.start, "Spread properties cannot have variance");
}
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.expect(tt.ellipsis);
node.argument = this.flowParseType();
this.flowObjectTypeSemicolon();
nodeStart.properties.push(this.finishNode(node, "ObjectTypeProperty"));
nodeStart.properties.push(this.finishNode(node, "ObjectTypeSpreadProperty"));
} else {
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"));
}
}
}
@@ -629,10 +645,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();