diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 65f71bd54443..b55882f6cdc0 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -2496,17 +2496,30 @@ export default class ExpressionParser extends LValParser { ); this.next(); - if ( - this.match(tt.semi) || - (!this.match(tt.star) && !this.state.type.startsExpr) || - this.hasPrecedingLineBreak() - ) { - node.delegate = false; - node.argument = null; - } else { - node.delegate = this.eat(tt.star); - node.argument = this.parseMaybeAssign(); + let delegating = false; + let argument = null; + if (!this.hasPrecedingLineBreak()) { + delegating = this.eat(tt.star); + switch (this.state.type) { + case tt.semi: + case tt.eof: + case tt.braceR: + case tt.parenR: + case tt.bracketR: + case tt.braceBarR: + case tt.colon: + case tt.comma: + // The above is the complete set of tokens that can + // follow an AssignmentExpression, and none of them + // can start an AssignmentExpression + if (!delegating) break; + /* fallthrough */ + default: + argument = this.parseMaybeAssign(); + } } + node.delegate = delegating; + node.argument = argument; return this.finishNode(node, "YieldExpression"); } diff --git a/packages/babel-parser/test/fixtures/core/categorized/function-expression-division-2/input.js b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division-2/input.js new file mode 100644 index 000000000000..f1a6c53e4496 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division-2/input.js @@ -0,0 +1 @@ +{ foo: function x() {} / 1 } diff --git a/packages/babel-parser/test/fixtures/core/categorized/function-expression-division-2/options.json b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division-2/options.json new file mode 100644 index 000000000000..0ef7eacd9b1a --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division-2/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unterminated regular expression. (1:24)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/categorized/function-expression-division/input.js b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division/input.js new file mode 100644 index 000000000000..92192fed9b1a --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division/input.js @@ -0,0 +1 @@ +({ foo: function x() {} / 1 }) diff --git a/packages/babel-parser/test/fixtures/core/categorized/function-expression-division/output.json b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division/output.json new file mode 100644 index 000000000000..9376d54267d0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/function-expression-division/output.json @@ -0,0 +1,71 @@ +{ + "type": "File", + "start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}}, + "program": { + "type": "Program", + "start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":30,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":30}}, + "expression": { + "type": "ObjectExpression", + "start":1,"end":29,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":29}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + }, + "properties": [ + { + "type": "ObjectProperty", + "start":3,"end":27,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":27}}, + "method": false, + "key": { + "type": "Identifier", + "start":3,"end":6,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":6},"identifierName":"foo"}, + "name": "foo" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "BinaryExpression", + "start":8,"end":27,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":27}}, + "left": { + "type": "FunctionExpression", + "start":8,"end":23,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":23}}, + "id": { + "type": "Identifier", + "start":17,"end":18,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":18},"identifierName":"x"}, + "name": "x" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":21,"end":23,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":23}}, + "body": [], + "directives": [] + } + }, + "operator": "/", + "right": { + "type": "NumericLiteral", + "start":26,"end":27,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":27}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/opts/private-name-tokens-true/output.json b/packages/babel-parser/test/fixtures/core/opts/private-name-tokens-true/output.json index 0e4e206e0001..202a7eac0980 100644 --- a/packages/babel-parser/test/fixtures/core/opts/private-name-tokens-true/output.json +++ b/packages/babel-parser/test/fixtures/core/opts/private-name-tokens-true/output.json @@ -68,7 +68,8 @@ "isAssign": false, "prefix": false, "postfix": false, - "binop": null + "binop": null, + "updateContext": null }, "value": "C", "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}} diff --git a/packages/babel-parser/test/fixtures/es2015/yield/regexp/input.js b/packages/babel-parser/test/fixtures/es2015/yield/regexp/input.js new file mode 100644 index 000000000000..9eca89c39ddc --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/yield/regexp/input.js @@ -0,0 +1,2 @@ +function *f1() { yield / 1 /g } +function *f2() { yield /=2 /i } diff --git a/packages/babel-parser/test/fixtures/es2015/yield/regexp/output.json b/packages/babel-parser/test/fixtures/es2015/yield/regexp/output.json new file mode 100644 index 000000000000..edb80b167846 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/yield/regexp/output.json @@ -0,0 +1,87 @@ +{ + "type": "File", + "start":0,"end":63,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":31}}, + "program": { + "type": "Program", + "start":0,"end":63,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":31}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "id": { + "type": "Identifier", + "start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12},"identifierName":"f1"}, + "name": "f1" + }, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":15,"end":31,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":31}}, + "body": [ + { + "type": "ExpressionStatement", + "start":17,"end":29,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":29}}, + "expression": { + "type": "YieldExpression", + "start":17,"end":29,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":29}}, + "delegate": false, + "argument": { + "type": "RegExpLiteral", + "start":23,"end":29,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":29}}, + "extra": { + "raw": "/ 1 /g" + }, + "pattern": " 1 ", + "flags": "g" + } + } + } + ], + "directives": [] + } + }, + { + "type": "FunctionDeclaration", + "start":32,"end":63,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":31}}, + "id": { + "type": "Identifier", + "start":42,"end":44,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":12},"identifierName":"f2"}, + "name": "f2" + }, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":47,"end":63,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":31}}, + "body": [ + { + "type": "ExpressionStatement", + "start":49,"end":61,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":29}}, + "expression": { + "type": "YieldExpression", + "start":49,"end":61,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":29}}, + "delegate": false, + "argument": { + "type": "RegExpLiteral", + "start":55,"end":61,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":29}}, + "extra": { + "raw": "/=2 /i" + }, + "pattern": "=2 ", + "flags": "i" + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2015/yield/without-argument/input.js b/packages/babel-parser/test/fixtures/es2015/yield/without-argument/input.js new file mode 100644 index 000000000000..bb9b7907d4b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/yield/without-argument/input.js @@ -0,0 +1,8 @@ +function* f() { + (yield); + [yield]; + { yield }; + yield; + true ? yield : 1; + yield, 1; +} diff --git a/packages/babel-parser/test/fixtures/es2015/yield/without-argument/output.json b/packages/babel-parser/test/fixtures/es2015/yield/without-argument/output.json new file mode 100644 index 000000000000..4f10511f9b83 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/yield/without-argument/output.json @@ -0,0 +1,146 @@ +{ + "type": "File", + "start":0,"end":93,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":93,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":93,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, + "id": { + "type": "Identifier", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"f"}, + "name": "f" + }, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":14,"end":93,"loc":{"start":{"line":1,"column":14},"end":{"line":8,"column":1}}, + "body": [ + { + "type": "ExpressionStatement", + "start":18,"end":26,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":10}}, + "expression": { + "type": "YieldExpression", + "start":19,"end":24,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":8}}, + "extra": { + "parenthesized": true, + "parenStart": 18 + }, + "delegate": false, + "argument": null + } + }, + { + "type": "ExpressionStatement", + "start":29,"end":37,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":10}}, + "expression": { + "type": "ArrayExpression", + "start":29,"end":36,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":9}}, + "elements": [ + { + "type": "YieldExpression", + "start":30,"end":35,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":8}}, + "delegate": false, + "argument": null + } + ] + } + }, + { + "type": "BlockStatement", + "start":40,"end":49,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":11}}, + "body": [ + { + "type": "ExpressionStatement", + "start":42,"end":47,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":9}}, + "expression": { + "type": "YieldExpression", + "start":42,"end":47,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":9}}, + "delegate": false, + "argument": null + } + } + ], + "directives": [] + }, + { + "type": "EmptyStatement", + "start":49,"end":50,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":12}} + }, + { + "type": "ExpressionStatement", + "start":53,"end":59,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":8}}, + "expression": { + "type": "YieldExpression", + "start":53,"end":58,"loc":{"start":{"line":5,"column":2},"end":{"line":5,"column":7}}, + "delegate": false, + "argument": null + } + }, + { + "type": "ExpressionStatement", + "start":62,"end":79,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":19}}, + "expression": { + "type": "ConditionalExpression", + "start":62,"end":78,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":18}}, + "test": { + "type": "BooleanLiteral", + "start":62,"end":66,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":6}}, + "value": true + }, + "consequent": { + "type": "YieldExpression", + "start":69,"end":74,"loc":{"start":{"line":6,"column":9},"end":{"line":6,"column":14}}, + "delegate": false, + "argument": null + }, + "alternate": { + "type": "NumericLiteral", + "start":77,"end":78,"loc":{"start":{"line":6,"column":17},"end":{"line":6,"column":18}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + }, + { + "type": "ExpressionStatement", + "start":82,"end":91,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"column":11}}, + "expression": { + "type": "SequenceExpression", + "start":82,"end":90,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"column":10}}, + "expressions": [ + { + "type": "YieldExpression", + "start":82,"end":87,"loc":{"start":{"line":7,"column":2},"end":{"line":7,"column":7}}, + "delegate": false, + "argument": null + }, + { + "type": "NumericLiteral", + "start":89,"end":90,"loc":{"start":{"line":7,"column":9},"end":{"line":7,"column":10}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + ] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/input.js b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/input.js new file mode 100644 index 000000000000..beab36c2ed68 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/input.js @@ -0,0 +1,3 @@ +function *f() { + return {| foo: yield |} +} diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/options.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/options.json new file mode 100644 index 000000000000..bf5a72aaff3c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["recordAndTuple", { "syntaxType": "bar" }]] +} diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/output.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/output.json new file mode 100644 index 000000000000..cf5cf61f42cd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/bar-yield-without-argument/output.json @@ -0,0 +1,60 @@ +{ + "type": "File", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"f"}, + "name": "f" + }, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":14,"end":43,"loc":{"start":{"line":1,"column":14},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ReturnStatement", + "start":18,"end":41,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":25}}, + "argument": { + "type": "RecordExpression", + "start":25,"end":41,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":25}}, + "properties": [ + { + "type": "ObjectProperty", + "start":28,"end":38,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":22}}, + "method": false, + "key": { + "type": "Identifier", + "start":28,"end":31,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":15},"identifierName":"foo"}, + "name": "foo" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "YieldExpression", + "start":33,"end":38,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":22}}, + "delegate": false, + "argument": null + } + } + ] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file