diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 3aa7323e3e..dea6c42b93 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -367,14 +367,13 @@ 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 { @@ -382,7 +381,7 @@ pp.flowParseObjectType = function (allowStatic, allowExact) { 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 { @@ -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; }; @@ -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(); } @@ -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(); } @@ -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; }; }); @@ -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; diff --git a/test/fixtures/flow/def-site-variance/1/expected.json b/test/fixtures/flow/def-site-variance/1/expected.json index ad7acee460..d5e8a1b7b6 100644 --- a/test/fixtures/flow/def-site-variance/1/expected.json +++ b/test/fixtures/flow/def-site-variance/1/expected.json @@ -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" }, { @@ -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" } ] @@ -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" }, { @@ -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" } ] @@ -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" }, { @@ -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" } ] diff --git a/test/fixtures/flow/type-annotations/110/expected.json b/test/fixtures/flow/type-annotations/110/expected.json index 6556914731..813e1eb0d2 100644 --- a/test/fixtures/flow/type-annotations/110/expected.json +++ b/test/fixtures/flow/type-annotations/110/expected.json @@ -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": [], diff --git a/test/fixtures/flow/type-annotations/111/expected.json b/test/fixtures/flow/type-annotations/111/expected.json index 8dde23737d..e8d4f2f2c1 100644 --- a/test/fixtures/flow/type-annotations/111/expected.json +++ b/test/fixtures/flow/type-annotations/111/expected.json @@ -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": [], diff --git a/test/fixtures/flow/type-annotations/114/expected.json b/test/fixtures/flow/type-annotations/114/expected.json index 6279284056..4a29dfbbee 100644 --- a/test/fixtures/flow/type-annotations/114/expected.json +++ b/test/fixtures/flow/type-annotations/114/expected.json @@ -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 diff --git a/test/fixtures/flow/type-annotations/115/expected.json b/test/fixtures/flow/type-annotations/115/expected.json index 2ed71ae23e..7868c1e169 100644 --- a/test/fixtures/flow/type-annotations/115/expected.json +++ b/test/fixtures/flow/type-annotations/115/expected.json @@ -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 diff --git a/test/fixtures/flow/type-annotations/118/expected.json b/test/fixtures/flow/type-annotations/118/expected.json index 3197ff95a7..6b123d102a 100644 --- a/test/fixtures/flow/type-annotations/118/expected.json +++ b/test/fixtures/flow/type-annotations/118/expected.json @@ -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", @@ -165,4 +180,4 @@ ], "directives": [] } -} +} \ No newline at end of file diff --git a/test/fixtures/flow/type-annotations/119/expected.json b/test/fixtures/flow/type-annotations/119/expected.json index 601ec1663f..3fb9fb001a 100644 --- a/test/fixtures/flow/type-annotations/119/expected.json +++ b/test/fixtures/flow/type-annotations/119/expected.json @@ -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", @@ -165,4 +180,4 @@ ], "directives": [] } -} +} \ No newline at end of file