From cc7e4707f6f4881b36ca47b2c80d5e5c96073857 Mon Sep 17 00:00:00 2001 From: Stuart Cook Date: Sat, 1 May 2021 18:21:08 +1000 Subject: [PATCH] Add edge-case parser/generator tests for `for (async of` --- .../fixtures/edgecase/for-async-of/input.js | 2 + .../fixtures/edgecase/for-async-of/output.js | 2 + .../fixtures/es2021/for-async-of/dot/input.js | 1 + .../es2021/for-async-of/dot/output.json | 42 +++++++++++ .../es2021/for-async-of/escaped/input.js | 1 + .../es2021/for-async-of/escaped/output.json | 32 ++++++++ .../for-async-of/for-statement/input.js | 1 + .../for-async-of/for-statement/output.json | 43 +++++++++++ .../forbidden-arrow-function/input.js | 1 + .../forbidden-arrow-function/output.json | 50 +++++++++++++ .../for-async-of/forbidden-equals/input.js | 3 + .../forbidden-equals/options.json | 3 + .../es2021/for-async-of/forbidden/input.js | 5 ++ .../es2021/for-async-of/forbidden/output.json | 74 +++++++++++++++++++ .../es2021/for-async-of/parens/input.js | 1 + .../es2021/for-async-of/parens/output.json | 36 +++++++++ 16 files changed, 297 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/dot/input.js create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/dot/output.json create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/input.js create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/output.json create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/input.js create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/output.json create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/input.js create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/output.json create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/input.js create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/options.json create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/input.js create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/output.json create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/parens/input.js create mode 100644 packages/babel-parser/test/fixtures/es2021/for-async-of/parens/output.json diff --git a/packages/babel-generator/test/fixtures/edgecase/for-async-of/input.js b/packages/babel-generator/test/fixtures/edgecase/for-async-of/input.js index 934747c7b4f9..bd94c08f6a06 100644 --- a/packages/babel-generator/test/fixtures/edgecase/for-async-of/input.js +++ b/packages/babel-generator/test/fixtures/edgecase/for-async-of/input.js @@ -4,4 +4,6 @@ for ((async) of async) async; for (\u0061sync of []); +for (async.x of []); + for (async in []); diff --git a/packages/babel-generator/test/fixtures/edgecase/for-async-of/output.js b/packages/babel-generator/test/fixtures/edgecase/for-async-of/output.js index c5734919e906..99c5ce9e1754 100644 --- a/packages/babel-generator/test/fixtures/edgecase/for-async-of/output.js +++ b/packages/babel-generator/test/fixtures/edgecase/for-async-of/output.js @@ -4,4 +4,6 @@ for ((async) of async) async; for ((async) of []); +for (async.x of []); + for (async in []); \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/dot/input.js b/packages/babel-parser/test/fixtures/es2021/for-async-of/dot/input.js new file mode 100644 index 000000000000..6bb5e9a42ba8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/dot/input.js @@ -0,0 +1 @@ +for (async.x of y); diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/dot/output.json b/packages/babel-parser/test/fixtures/es2021/for-async-of/dot/output.json new file mode 100644 index 000000000000..a80878cd9ff9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/dot/output.json @@ -0,0 +1,42 @@ +{ + "type": "File", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "program": { + "type": "Program", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ForOfStatement", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "await": false, + "left": { + "type": "MemberExpression", + "start":5,"end":12,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":12}}, + "object": { + "type": "Identifier", + "start":5,"end":10,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":10},"identifierName":"async"}, + "name": "async" + }, + "computed": false, + "property": { + "type": "Identifier", + "start":11,"end":12,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":12},"identifierName":"x"}, + "name": "x" + } + }, + "right": { + "type": "Identifier", + "start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17},"identifierName":"y"}, + "name": "y" + }, + "body": { + "type": "EmptyStatement", + "start":18,"end":19,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":19}} + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/input.js b/packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/input.js new file mode 100644 index 000000000000..52ed40e868c0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/input.js @@ -0,0 +1 @@ +for (\u0061sync of x); diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/output.json b/packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/output.json new file mode 100644 index 000000000000..8a71ac139e1c --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/escaped/output.json @@ -0,0 +1,32 @@ +{ + "type": "File", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "program": { + "type": "Program", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ForOfStatement", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "await": false, + "left": { + "type": "Identifier", + "start":5,"end":15,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":15},"identifierName":"async"}, + "name": "async" + }, + "right": { + "type": "Identifier", + "start":19,"end":20,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":20},"identifierName":"x"}, + "name": "x" + }, + "body": { + "type": "EmptyStatement", + "start":21,"end":22,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":22}} + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/input.js b/packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/input.js new file mode 100644 index 000000000000..24b0e2c995e7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/input.js @@ -0,0 +1 @@ +for (async of => {};;); diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/output.json b/packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/output.json new file mode 100644 index 000000000000..52f65bac9bce --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/for-statement/output.json @@ -0,0 +1,43 @@ +{ + "type": "File", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}}, + "program": { + "type": "Program", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ForStatement", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}}, + "init": { + "type": "ArrowFunctionExpression", + "start":5,"end":19,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":19}}, + "id": null, + "generator": false, + "async": true, + "params": [ + { + "type": "Identifier", + "start":11,"end":13,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":13},"identifierName":"of"}, + "name": "of" + } + ], + "body": { + "type": "BlockStatement", + "start":17,"end":19,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":19}}, + "body": [], + "directives": [] + } + }, + "test": null, + "update": null, + "body": { + "type": "EmptyStatement", + "start":22,"end":23,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":23}} + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/input.js b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/input.js new file mode 100644 index 000000000000..588565b75c11 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/input.js @@ -0,0 +1 @@ +for (async of => {} of x); diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/output.json b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/output.json new file mode 100644 index 000000000000..b310f007c448 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-arrow-function/output.json @@ -0,0 +1,50 @@ +{ + "type": "File", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}}, + "errors": [ + "SyntaxError: Invalid left-hand side in for-of statement. (1:5)" + ], + "program": { + "type": "Program", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ForOfStatement", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}}, + "await": false, + "left": { + "type": "ArrowFunctionExpression", + "start":5,"end":19,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":19}}, + "id": null, + "generator": false, + "async": true, + "params": [ + { + "type": "Identifier", + "start":11,"end":13,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":13},"identifierName":"of"}, + "name": "of" + } + ], + "body": { + "type": "BlockStatement", + "start":17,"end":19,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":19}}, + "body": [], + "directives": [] + } + }, + "right": { + "type": "Identifier", + "start":23,"end":24,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":24},"identifierName":"x"}, + "name": "x" + }, + "body": { + "type": "EmptyStatement", + "start":25,"end":26,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":26}} + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/input.js b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/input.js new file mode 100644 index 000000000000..60558a38a0ea --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/input.js @@ -0,0 +1,3 @@ +// Check what happens when the async-unary-arrow-function parser looks ahead +// and finds "=" instead of "=>". +for (async of = x); diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/options.json b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/options.json new file mode 100644 index 000000000000..5afe13040ca0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden-equals/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \"=>\" (3:14)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/input.js b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/input.js new file mode 100644 index 000000000000..bd6aeacece16 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/input.js @@ -0,0 +1,5 @@ +for (async of x); + +async () => { + for (async of x); +} diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/output.json b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/output.json new file mode 100644 index 000000000000..d8fa836717f7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/forbidden/output.json @@ -0,0 +1,74 @@ +{ + "type": "File", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "errors": [ + "SyntaxError: The left-hand side of a for-of loop may not be 'async'. (1:5)", + "SyntaxError: The left-hand side of a for-of loop may not be 'async'. (4:7)" + ], + "program": { + "type": "Program", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ForOfStatement", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "await": false, + "left": { + "type": "Identifier", + "start":5,"end":10,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":10},"identifierName":"async"}, + "name": "async" + }, + "right": { + "type": "Identifier", + "start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15},"identifierName":"x"}, + "name": "x" + }, + "body": { + "type": "EmptyStatement", + "start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}} + } + }, + { + "type": "ExpressionStatement", + "start":19,"end":54,"loc":{"start":{"line":3,"column":0},"end":{"line":5,"column":1}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":19,"end":54,"loc":{"start":{"line":3,"column":0},"end":{"line":5,"column":1}}, + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":31,"end":54,"loc":{"start":{"line":3,"column":12},"end":{"line":5,"column":1}}, + "body": [ + { + "type": "ForOfStatement", + "start":35,"end":52,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":19}}, + "await": false, + "left": { + "type": "Identifier", + "start":40,"end":45,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":12},"identifierName":"async"}, + "name": "async" + }, + "right": { + "type": "Identifier", + "start":49,"end":50,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":17},"identifierName":"x"}, + "name": "x" + }, + "body": { + "type": "EmptyStatement", + "start":51,"end":52,"loc":{"start":{"line":4,"column":18},"end":{"line":4,"column":19}} + } + } + ], + "directives": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/parens/input.js b/packages/babel-parser/test/fixtures/es2021/for-async-of/parens/input.js new file mode 100644 index 000000000000..6b21ec95e519 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/parens/input.js @@ -0,0 +1 @@ +for ((async) of x); diff --git a/packages/babel-parser/test/fixtures/es2021/for-async-of/parens/output.json b/packages/babel-parser/test/fixtures/es2021/for-async-of/parens/output.json new file mode 100644 index 000000000000..758ab36cf51a --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2021/for-async-of/parens/output.json @@ -0,0 +1,36 @@ +{ + "type": "File", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "program": { + "type": "Program", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ForOfStatement", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "await": false, + "left": { + "type": "Identifier", + "start":6,"end":11,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":11},"identifierName":"async"}, + "extra": { + "parenthesized": true, + "parenStart": 5 + }, + "name": "async" + }, + "right": { + "type": "Identifier", + "start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17},"identifierName":"x"}, + "name": "x" + }, + "body": { + "type": "EmptyStatement", + "start":18,"end":19,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":19}} + } + } + ], + "directives": [] + } +} \ No newline at end of file