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

Property variance type annotations for Flow plugin #161

Merged
merged 4 commits into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
61 changes: 50 additions & 11 deletions src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,7 @@ pp.flowParseTypeAlias = function (node) {
pp.flowParseTypeParameter = function () {
let node = this.startNode();

let variance;
if (this.match(tt.plusMin)) {
if (this.state.value === "+") {
variance = "plus";
} else if (this.state.value === "-") {
variance = "minus";
}
this.eat(tt.plusMin);
}
let variance = this.flowParseVariance();

let ident = this.flowParseTypeAnnotatableIdentifier(false, false);
node.name = ident.name;
Expand Down Expand Up @@ -278,14 +270,15 @@ pp.flowParseObjectPropertyKey = function () {
return (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true);
};

pp.flowParseObjectTypeIndexer = function (node, isStatic) {
pp.flowParseObjectTypeIndexer = function (node, isStatic, variance) {
node.static = isStatic;

this.expect(tt.bracketL);
node.id = this.flowParseObjectPropertyKey();
node.key = this.flowParseTypeInitialiser();
this.expect(tt.bracketR);
node.value = this.flowParseTypeInitialiser();
node.variance = variance;

this.flowObjectTypeSemicolon();
return this.finishNode(node, "ObjectTypeIndexer");
Expand Down Expand Up @@ -368,9 +361,15 @@ pp.flowParseObjectType = function (allowStatic, allowExact) {
isStatic = true;
}

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

if (this.match(tt.bracketL)) {
nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic));
nodeStart.indexers.push(this.flowParseObjectTypeIndexer(node, isStatic, variance));
} else if (this.match(tt.parenL) || this.isRelational("<")) {
if (variance) {
this.unexpected(variancePos);
}
nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, allowStatic));
} else {
if (isStatic && this.match(tt.colon)) {
Expand All @@ -380,6 +379,9 @@ pp.flowParseObjectType = function (allowStatic, allowExact) {
}
if (this.isRelational("<") || this.match(tt.parenL)) {
// This is a method property
if (variance) {
this.unexpected(variancePos);
}
nodeStart.properties.push(this.flowParseObjectTypeMethod(startPos, startLoc, isStatic, propertyKey));
} else {
if (this.eat(tt.question)) {
Expand All @@ -389,6 +391,7 @@ pp.flowParseObjectType = function (allowStatic, allowExact) {
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 @@ -726,6 +729,19 @@ pp.typeCastToParameter = function (node) {
);
};

pp.flowParseVariance = function() {
let variance = null;
if (this.match(tt.plusMin)) {
if (this.state.value === "+") {
variance = "plus";
} else if (this.state.value === "-") {
variance = "minus";
}
this.next();
}
return variance;
};

export default function (instance) {
// plain function return types: function name(): string {}
instance.extend("parseFunctionBody", function (inner) {
Expand Down Expand Up @@ -969,6 +985,7 @@ 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 @@ -986,6 +1003,11 @@ export default function (instance) {
// parse type parameters for class methods
instance.extend("parseClassMethod", function () {
return function (classBody, method, isGenerator, isAsync) {
if (method.variance) {
this.unexpected(method.variancePos);
}
delete method.variance;
delete method.variancePos;
if (this.isRelational("<")) {
method.typeParameters = this.flowParseTypeParameterDeclaration();
}
Expand Down Expand Up @@ -1018,9 +1040,26 @@ export default function (instance) {
};
});

instance.extend("parsePropertyName", function (inner) {
return function (node) {
let variancePos = this.state.start;
let variance = this.flowParseVariance();
let key = inner.call(this, node);
node.variance = variance;
node.variancePos = variancePos;
return key;
};
});

// parse type parameters for object method shorthand
instance.extend("parseObjPropValue", function (inner) {
return function (prop) {
if (prop.variance) {
this.unexpected(prop.start);
Copy link
Member

@danez danez Oct 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be prop.variancePos?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Yup. In this case they are one-in-the-same, but this should definitely be variancePos for clarity. We wouldn't need variancePos except for static properties on classes, which share the same AST building code.

}
delete prop.variance;
delete prop.variancePos;

let typeParameters;

// method shorthand
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/flow/call-properties/3/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@
}
}
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": []
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/flow/declare-statements/10/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@
}
},
"optional": false,
"static": true
"static": true,
"variance": null
}
],
"indexers": []
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/flow/declare-statements/11/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@
"column": 51
}
}
}
},
"variance": null
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/flow/declare-statements/14/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@
},
"name": "U"
}
}
},
"variance": null
}
]
}
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/flow/declare-statements/15/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
}
}
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": []
Expand Down Expand Up @@ -271,7 +272,8 @@
"name": "T"
}
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": []
Expand Down
9 changes: 6 additions & 3 deletions test/fixtures/flow/declare-statements/17/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
}
},
"optional": false,
"static": false
"static": false,
"variance": null
},
{
"type": "ObjectTypeProperty",
Expand Down Expand Up @@ -174,7 +175,8 @@
}
},
"optional": false,
"static": true
"static": true,
"variance": null
},
{
"type": "ObjectTypeProperty",
Expand Down Expand Up @@ -223,7 +225,8 @@
}
},
"optional": false,
"static": false
"static": false,
"variance": null
}
],
"indexers": [],
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/flow/declare-statements/9/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@
}
}
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": []
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/flow/def-site-variance/1/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,4 @@
],
"directives": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@
},
"typeParameters": null
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
}
}
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": [
Expand Down Expand Up @@ -184,7 +185,8 @@
"column": 46
}
}
}
},
"variance": null
}
]
}
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/flow/type-alias/4/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@
"raw": "\"A\""
}
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": []
Expand Down Expand Up @@ -211,7 +212,8 @@
"raw": "\"B\""
}
},
"optional": false
"optional": false,
"variance": null
}
],
"indexers": []
Expand Down
Loading