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:
parent
4d18221098
commit
213fdab063
@ -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();
|
||||
|
||||
3
test/fixtures/flow/type-annotations/135/actual.js
vendored
Normal file
3
test/fixtures/flow/type-annotations/135/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
type A = {
|
||||
...any,
|
||||
};
|
||||
117
test/fixtures/flow/type-annotations/135/expected.json
vendored
Normal file
117
test/fixtures/flow/type-annotations/135/expected.json
vendored
Normal file
@ -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
test/fixtures/flow/type-annotations/136/actual.js
vendored
Normal file
4
test/fixtures/flow/type-annotations/136/actual.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
type A = {
|
||||
p: {},
|
||||
...{},
|
||||
};
|
||||
175
test/fixtures/flow/type-annotations/136/expected.json
vendored
Normal file
175
test/fixtures/flow/type-annotations/136/expected.json
vendored
Normal file
@ -0,0 +1,175 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"body": [
|
||||
{
|
||||
"type": "TypeAlias",
|
||||
"start": 0,
|
||||
"end": 29,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"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": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 1
|
||||
}
|
||||
},
|
||||
"callProperties": [],
|
||||
"properties": [
|
||||
{
|
||||
"type": "ObjectTypeProperty",
|
||||
"start": 12,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start": 12,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 2
|
||||
},
|
||||
"identifierName": "p"
|
||||
},
|
||||
"name": "p"
|
||||
},
|
||||
"value": {
|
||||
"type": "ObjectTypeAnnotation",
|
||||
"start": 15,
|
||||
"end": 17,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 6
|
||||
}
|
||||
},
|
||||
"callProperties": [],
|
||||
"properties": [],
|
||||
"indexers": [],
|
||||
"exact": false
|
||||
},
|
||||
"optional": false,
|
||||
"static": false,
|
||||
"variance": null
|
||||
},
|
||||
{
|
||||
"type": "ObjectTypeSpreadProperty",
|
||||
"start": 20,
|
||||
"end": 26,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 3,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"argument": {
|
||||
"type": "ObjectTypeAnnotation",
|
||||
"start": 23,
|
||||
"end": 25,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 3,
|
||||
"column": 6
|
||||
}
|
||||
},
|
||||
"callProperties": [],
|
||||
"properties": [],
|
||||
"indexers": [],
|
||||
"exact": false
|
||||
}
|
||||
}
|
||||
],
|
||||
"indexers": [],
|
||||
"exact": false
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
3
test/fixtures/flow/type-annotations/137/actual.js
vendored
Normal file
3
test/fixtures/flow/type-annotations/137/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
interface A {
|
||||
...any,
|
||||
};
|
||||
3
test/fixtures/flow/type-annotations/137/options.json
vendored
Normal file
3
test/fixtures/flow/type-annotations/137/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Spread operator cannnot appear in class or interface definitions (2:1)"
|
||||
}
|
||||
5
test/fixtures/flow/type-annotations/138/actual.js
vendored
Normal file
5
test/fixtures/flow/type-annotations/138/actual.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
class A {}
|
||||
class B {}
|
||||
type C = {
|
||||
...A&B
|
||||
};
|
||||
283
test/fixtures/flow/type-annotations/138/expected.json
vendored
Normal file
283
test/fixtures/flow/type-annotations/138/expected.json
vendored
Normal file
@ -0,0 +1,283 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 43,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 43,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"body": [
|
||||
{
|
||||
"type": "ClassDeclaration",
|
||||
"start": 0,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 6,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "A"
|
||||
},
|
||||
"name": "A"
|
||||
},
|
||||
"superClass": null,
|
||||
"body": {
|
||||
"type": "ClassBody",
|
||||
"start": 8,
|
||||
"end": 10,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"body": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ClassDeclaration",
|
||||
"start": 11,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 17,
|
||||
"end": 18,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "B"
|
||||
},
|
||||
"name": "B"
|
||||
},
|
||||
"superClass": null,
|
||||
"body": {
|
||||
"type": "ClassBody",
|
||||
"start": 19,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 8
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 10
|
||||
}
|
||||
},
|
||||
"body": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "TypeAlias",
|
||||
"start": 22,
|
||||
"end": 43,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 2
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 27,
|
||||
"end": 28,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 3,
|
||||
"column": 6
|
||||
},
|
||||
"identifierName": "C"
|
||||
},
|
||||
"name": "C"
|
||||
},
|
||||
"typeParameters": null,
|
||||
"right": {
|
||||
"type": "ObjectTypeAnnotation",
|
||||
"start": 31,
|
||||
"end": 42,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 9
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 1
|
||||
}
|
||||
},
|
||||
"callProperties": [],
|
||||
"properties": [
|
||||
{
|
||||
"type": "ObjectTypeSpreadProperty",
|
||||
"start": 34,
|
||||
"end": 40,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"argument": {
|
||||
"type": "IntersectionTypeAnnotation",
|
||||
"start": 37,
|
||||
"end": 40,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"types": [
|
||||
{
|
||||
"type": "GenericTypeAnnotation",
|
||||
"start": 37,
|
||||
"end": 38,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 5
|
||||
}
|
||||
},
|
||||
"typeParameters": null,
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 37,
|
||||
"end": 38,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 4
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 5
|
||||
},
|
||||
"identifierName": "A"
|
||||
},
|
||||
"name": "A"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "GenericTypeAnnotation",
|
||||
"start": 39,
|
||||
"end": 40,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"typeParameters": null,
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 39,
|
||||
"end": 40,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "B"
|
||||
},
|
||||
"name": "B"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"indexers": [],
|
||||
"exact": false
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
4
test/fixtures/flow/type-annotations/139/actual.js
vendored
Normal file
4
test/fixtures/flow/type-annotations/139/actual.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
class A {}
|
||||
type C = {
|
||||
-...A
|
||||
};
|
||||
3
test/fixtures/flow/type-annotations/139/options.json
vendored
Normal file
3
test/fixtures/flow/type-annotations/139/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Spread properties cannot have variance (3:1)"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user