From 3267e5f3659219fcf525e9605f8a0ac5a969598d Mon Sep 17 00:00:00 2001 From: Vladimir Kurchatkin Date: Thu, 5 Jan 2017 15:25:57 +0400 Subject: [PATCH] Don't set inType flag when parsing property names (#266) * Don't set inType flag when parsing property names * Add inPropertyName type definition * Move inPropertyName check before this.curContext() --- src/parser/expression.js | 7 +- src/plugins/flow.js | 11 - src/plugins/jsx/index.js | 4 +- src/tokenizer/state.js | 2 + .../flow/regression/issue-264/actual.js | 3 + .../flow/regression/issue-264/expected.json | 192 ++++++++++++++++++ 6 files changed, 205 insertions(+), 14 deletions(-) create mode 100644 test/fixtures/flow/regression/issue-264/actual.js create mode 100644 test/fixtures/flow/regression/issue-264/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index ed312d9d5c..1e9f4197e3 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -850,11 +850,14 @@ pp.parsePropertyName = function (prop) { prop.computed = true; prop.key = this.parseMaybeAssign(); this.expect(tt.bracketR); - return prop.key; } else { prop.computed = false; - return prop.key = (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true); + const oldInPropertyName = this.state.inPropertyName; + this.state.inPropertyName = true; + prop.key = (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true); + this.state.inPropertyName = oldInPropertyName; } + return prop.key; }; // Initialize empty function node. diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 0c0486b1c9..cc4c7a1e76 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -949,17 +949,6 @@ export default function (instance) { }; }); - // ensure that inside property names, < isn't interpreted as JSX, but as a type parameter - instance.extend("parsePropertyName", function (inner) { - return function (prop) { - const oldInType = this.state.inType; - this.state.inType = true; - const out = inner.call(this, prop); - this.state.inType = oldInType; - return out; - }; - }); - // ensure that inside flow types, we bypass the jsx parser plugin instance.extend("readToken", function (inner) { return function (code) { diff --git a/src/plugins/jsx/index.js b/src/plugins/jsx/index.js index bb5dc2f8a5..bd4633a095 100644 --- a/src/plugins/jsx/index.js +++ b/src/plugins/jsx/index.js @@ -416,7 +416,9 @@ export default function(instance) { instance.extend("readToken", function(inner) { return function(code) { - let context = this.curContext(); + if (this.state.inPropertyName) return inner.call(this, code); + + const context = this.curContext(); if (context === tc.j_expr) { return this.jsxReadToken(); diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index 1436eb9fee..b526ca22eb 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -16,6 +16,7 @@ export default class State { this.inFunction = this.inGenerator = this.inAsync = + this.inPropertyName = this.inType = this.noAnonFunctionType = false; @@ -69,6 +70,7 @@ export default class State { inMethod: boolean; inAsync: boolean; inType: boolean; + inPropertyName: boolean; // Labels in scope. labels: Array; diff --git a/test/fixtures/flow/regression/issue-264/actual.js b/test/fixtures/flow/regression/issue-264/actual.js new file mode 100644 index 0000000000..fe3121ebcb --- /dev/null +++ b/test/fixtures/flow/regression/issue-264/actual.js @@ -0,0 +1,3 @@ +const map = { + [age <= 17] : 'Too young' +}; diff --git a/test/fixtures/flow/regression/issue-264/expected.json b/test/fixtures/flow/regression/issue-264/expected.json new file mode 100644 index 0000000000..ca0f1791c7 --- /dev/null +++ b/test/fixtures/flow/regression/issue-264/expected.json @@ -0,0 +1,192 @@ +{ + "type": "File", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "map" + }, + "name": "map" + }, + "init": { + "type": "ObjectExpression", + "start": 12, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 16, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "method": false, + "shorthand": false, + "computed": true, + "key": { + "type": "BinaryExpression", + "start": 17, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "left": { + "type": "Identifier", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "age" + }, + "name": "age" + }, + "operator": "<=", + "right": { + "type": "NumericLiteral", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "extra": { + "rawValue": 17, + "raw": "17" + }, + "value": 17 + } + }, + "value": { + "type": "StringLiteral", + "start": 30, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "extra": { + "rawValue": "Too young", + "raw": "'Too young'" + }, + "value": "Too young" + } + } + ] + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file