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

Reintroduce Variance node #333

Merged
merged 2 commits into from
Feb 12, 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
22 changes: 9 additions & 13 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,22 +367,21 @@ pp.flowParseObjectType = function (allowStatic, allowExact) {
isStatic = true;
}

const variancePos = this.state.start;
const variance = this.flowParseVariance();

if (this.match(tt.bracketL)) {
nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
} else if (this.match(tt.parenL) || this.isRelational("<")) {
if (variance) {
this.unexpected(variancePos);
this.unexpected(variance.start);
}
nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, allowStatic));
} else {
propertyKey = this.flowParseObjectPropertyKey();
if (this.isRelational("<") || this.match(tt.parenL)) {
// This is a method property
if (variance) {
this.unexpected(variancePos);
this.unexpected(variance.start);
}
nodeStart.properties.push(this.flowParseObjectTypeMethod(startPos, startLoc, isStatic, propertyKey));
} else {
Expand Down Expand Up @@ -782,12 +781,14 @@ pp.typeCastToParameter = function (node) {
pp.flowParseVariance = function() {
let variance = null;
if (this.match(tt.plusMin)) {
variance = this.startNode();
if (this.state.value === "+") {
variance = "plus";
} else if (this.state.value === "-") {
variance = "minus";
variance.kind = "plus";
} else {
variance.kind = "minus";
}
this.next();
this.finishNode(variance, "Variance");
}
return variance;
};
Expand Down Expand Up @@ -1036,7 +1037,6 @@ export default function (instance) {
// parse class property type annotations
instance.extend("parseClassProperty", function (inner) {
return function (node) {
delete node.variancePos;
if (this.match(tt.colon)) {
node.typeAnnotation = this.flowParseTypeAnnotation();
}
Expand All @@ -1055,10 +1055,9 @@ export default function (instance) {
instance.extend("parseClassMethod", function () {
return function (classBody, method, isGenerator, isAsync) {
if (method.variance) {
this.unexpected(method.variancePos);
this.unexpected(method.variance.start);
}
delete method.variance;
delete method.variancePos;
if (this.isRelational("<")) {
method.typeParameters = this.flowParseTypeParameterDeclaration();
}
Expand Down Expand Up @@ -1093,11 +1092,9 @@ export default function (instance) {

instance.extend("parsePropertyName", function (inner) {
return function (node) {
const variancePos = this.state.start;
const variance = this.flowParseVariance();
const key = inner.call(this, node);
node.variance = variance;
node.variancePos = variancePos;
return key;
};
});
Expand All @@ -1106,10 +1103,9 @@ export default function (instance) {
instance.extend("parseObjPropValue", function (inner) {
return function (prop) {
if (prop.variance) {
this.unexpected(prop.variancePos);
this.unexpected(prop.variance.start);
}
delete prop.variance;
delete prop.variancePos;

let typeParameters;

Expand Down
102 changes: 96 additions & 6 deletions test/fixtures/flow/def-site-variance/1/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,22 @@
"column": 10
}
},
"variance": "plus",
"variance": {
"type": "Variance",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
},
"kind": "plus"
},
"name": "T"
},
{
Expand All @@ -104,7 +119,22 @@
"column": 13
}
},
"variance": "minus",
"variance": {
"type": "Variance",
"start": 11,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 12
}
},
"kind": "minus"
},
"name": "U"
}
]
Expand Down Expand Up @@ -189,7 +219,22 @@
"column": 13
}
},
"variance": "plus",
"variance": {
"type": "Variance",
"start": 29,
"end": 30,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 12
}
},
"kind": "plus"
},
"name": "T"
},
{
Expand All @@ -206,7 +251,22 @@
"column": 16
}
},
"variance": "minus",
"variance": {
"type": "Variance",
"start": 32,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 15
}
},
"kind": "minus"
},
"name": "U"
}
]
Expand Down Expand Up @@ -289,7 +349,22 @@
"column": 9
}
},
"variance": "plus",
"variance": {
"type": "Variance",
"start": 48,
"end": 49,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 8
}
},
"kind": "plus"
},
"name": "T"
},
{
Expand All @@ -306,7 +381,22 @@
"column": 12
}
},
"variance": "minus",
"variance": {
"type": "Variance",
"start": 51,
"end": 52,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 11
}
},
"kind": "minus"
},
"name": "U"
}
]
Expand Down
17 changes: 16 additions & 1 deletion test/fixtures/flow/type-annotations/110/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,22 @@
},
"optional": false,
"static": false,
"variance": "plus"
"variance": {
"type": "Variance",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
}
},
"kind": "plus"
}
}
],
"indexers": [],
Expand Down
17 changes: 16 additions & 1 deletion test/fixtures/flow/type-annotations/111/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,22 @@
},
"optional": false,
"static": false,
"variance": "minus"
"variance": {
"type": "Variance",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
}
},
"kind": "minus"
}
}
],
"indexers": [],
Expand Down
17 changes: 16 additions & 1 deletion test/fixtures/flow/type-annotations/114/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,22 @@
"name": "V"
}
},
"variance": "plus"
"variance": {
"type": "Variance",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
}
},
"kind": "plus"
}
}
],
"exact": false
Expand Down
17 changes: 16 additions & 1 deletion test/fixtures/flow/type-annotations/115/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,22 @@
"name": "V"
}
},
"variance": "minus"
"variance": {
"type": "Variance",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
}
},
"kind": "minus"
}
}
],
"exact": false
Expand Down
19 changes: 17 additions & 2 deletions test/fixtures/flow/type-annotations/118/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,22 @@
},
"name": "p"
},
"variance": "plus",
"variance": {
"type": "Variance",
"start": 9,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
},
"kind": "plus"
},
"static": false,
"typeAnnotation": {
"type": "TypeAnnotation",
Expand Down Expand Up @@ -165,4 +180,4 @@
],
"directives": []
}
}
}
19 changes: 17 additions & 2 deletions test/fixtures/flow/type-annotations/119/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,22 @@
},
"name": "p"
},
"variance": "minus",
"variance": {
"type": "Variance",
"start": 9,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
},
"kind": "minus"
},
"static": false,
"typeAnnotation": {
"type": "TypeAnnotation",
Expand Down Expand Up @@ -165,4 +180,4 @@
],
"directives": []
}
}
}