diff --git a/src/tokenizer/context.js b/src/tokenizer/context.js index f46eafc8c4..7a345726cf 100644 --- a/src/tokenizer/context.js +++ b/src/tokenizer/context.js @@ -61,6 +61,11 @@ tt.parenR.updateContext = tt.braceR.updateContext = function() { }; tt.name.updateContext = function(prevType) { + if (this.state.value === "of" && this.curContext() === types.parenStatement) { + this.state.exprAllowed = !prevType.beforeExpr; + return; + } + this.state.exprAllowed = false; if (prevType === tt._let || prevType === tt._const || prevType === tt._var) { diff --git a/test/fixtures/es2015/for-of/right-regex/actual.js b/test/fixtures/es2015/for-of/right-regex/actual.js new file mode 100644 index 0000000000..0673bc7e5a --- /dev/null +++ b/test/fixtures/es2015/for-of/right-regex/actual.js @@ -0,0 +1,3 @@ +for (var x of /foo/g); + +for (var x = of /foo/g;;); \ No newline at end of file diff --git a/test/fixtures/es2015/for-of/right-regex/expected.json b/test/fixtures/es2015/for-of/right-regex/expected.json new file mode 100644 index 0000000000..d387a90779 --- /dev/null +++ b/test/fixtures/es2015/for-of/right-regex/expected.json @@ -0,0 +1,301 @@ +{ + "type": "File", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ForOfStatement", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "await": false, + "left": { + "type": "VariableDeclaration", + "start": 5, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": null + } + ], + "kind": "var" + }, + "right": { + "type": "RegExpLiteral", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "extra": { + "raw": "/foo/g" + }, + "pattern": "foo", + "flags": "g" + }, + "body": { + "type": "EmptyStatement", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + } + } + }, + { + "type": "ForStatement", + "start": 24, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "init": { + "type": "VariableDeclaration", + "start": 29, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 33, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "BinaryExpression", + "start": 37, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "left": { + "type": "BinaryExpression", + "start": 37, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "left": { + "type": "Identifier", + "start": 37, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "of" + }, + "name": "of" + }, + "operator": "/", + "right": { + "type": "Identifier", + "start": 41, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "foo" + }, + "name": "foo" + } + }, + "operator": "/", + "right": { + "type": "Identifier", + "start": 45, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + }, + "identifierName": "g" + }, + "name": "g" + } + } + } + ], + "kind": "var" + }, + "test": null, + "update": null, + "body": { + "type": "EmptyStatement", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 25 + }, + "end": { + "line": 3, + "column": 26 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file