From 3a761e64f237e118c56d88b6c9c5fb8dae32ebc4 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Mon, 20 Apr 2020 14:28:27 -0500 Subject: [PATCH] Set exprAllowed to false for star token --- .../babel-parser/src/tokenizer/context.js | 4 + .../categorized/regex-after-star/input.js | 1 + .../categorized/regex-after-star/output.json | 36 ++++++ .../es2015/generators/yield-regex/input.js | 1 + .../es2015/generators/yield-regex/output.json | 50 ++++++++ .../input.ts | 3 + .../options.json | 5 + .../anonymous-function-generator/input.ts | 3 + .../anonymous-function-generator/output.json | 118 ++++++++++++++++++ .../function/anonymous-generator/input.ts | 3 + .../function/anonymous-generator/output.json | 115 +++++++++++++++++ .../input.ts | 3 + .../options.json | 5 + .../tsx/anonymous-function-generator/input.ts | 3 + .../anonymous-function-generator/output.json | 115 +++++++++++++++++ 15 files changed, 465 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/core/categorized/regex-after-star/input.js create mode 100644 packages/babel-parser/test/fixtures/core/categorized/regex-after-star/output.json create mode 100644 packages/babel-parser/test/fixtures/es2015/generators/yield-regex/input.js create mode 100644 packages/babel-parser/test/fixtures/es2015/generators/yield-regex/output.json create mode 100644 packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/input.ts create mode 100644 packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/options.json create mode 100644 packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/input.ts create mode 100644 packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json diff --git a/packages/babel-parser/src/tokenizer/context.js b/packages/babel-parser/src/tokenizer/context.js index 83e21443a955..7bcaf1da4416 100644 --- a/packages/babel-parser/src/tokenizer/context.js +++ b/packages/babel-parser/src/tokenizer/context.js @@ -133,3 +133,7 @@ tt.backQuote.updateContext = function() { } this.state.exprAllowed = false; }; + +tt.star.updateContext = function() { + this.state.exprAllowed = false; +}; diff --git a/packages/babel-parser/test/fixtures/core/categorized/regex-after-star/input.js b/packages/babel-parser/test/fixtures/core/categorized/regex-after-star/input.js new file mode 100644 index 000000000000..9a85649c5bfc --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/regex-after-star/input.js @@ -0,0 +1 @@ +a * /b/ diff --git a/packages/babel-parser/test/fixtures/core/categorized/regex-after-star/output.json b/packages/babel-parser/test/fixtures/core/categorized/regex-after-star/output.json new file mode 100644 index 000000000000..2e01bf74071e --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/categorized/regex-after-star/output.json @@ -0,0 +1,36 @@ +{ + "type": "File", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "program": { + "type": "Program", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "expression": { + "type": "BinaryExpression", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "left": { + "type": "Identifier", + "start":0,"end":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":1},"identifierName":"a"}, + "name": "a" + }, + "operator": "*", + "right": { + "type": "RegExpLiteral", + "start":4,"end":7,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":7}}, + "extra": { + "raw": "/b/" + }, + "pattern": "b", + "flags": "" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2015/generators/yield-regex/input.js b/packages/babel-parser/test/fixtures/es2015/generators/yield-regex/input.js new file mode 100644 index 000000000000..fad58539e472 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/generators/yield-regex/input.js @@ -0,0 +1 @@ +function* x() { yield* /z/ } diff --git a/packages/babel-parser/test/fixtures/es2015/generators/yield-regex/output.json b/packages/babel-parser/test/fixtures/es2015/generators/yield-regex/output.json new file mode 100644 index 000000000000..4fb61256be14 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/generators/yield-regex/output.json @@ -0,0 +1,50 @@ +{ + "type": "File", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "program": { + "type": "Program", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}}, + "id": { + "type": "Identifier", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"}, + "name": "x" + }, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":14,"end":28,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":28}}, + "body": [ + { + "type": "ExpressionStatement", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "expression": { + "type": "YieldExpression", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "delegate": true, + "argument": { + "type": "RegExpLiteral", + "start":23,"end":26,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":26}}, + "extra": { + "raw": "/z/" + }, + "pattern": "z", + "flags": "" + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/input.ts b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/input.ts new file mode 100644 index 000000000000..109fe67a21e0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/input.ts @@ -0,0 +1,3 @@ +const fn = function* <,>(input: T): Generator { + yield 2; +} diff --git a/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/options.json b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/options.json new file mode 100644 index 000000000000..a12f51bf6fe4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator-invalid/options.json @@ -0,0 +1,5 @@ +{ + "sourceType": "module", + "plugins": ["jsx", "flow"], + "throws": "Unexpected token (1:22)" +} diff --git a/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/input.ts b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/input.ts new file mode 100644 index 000000000000..33ce49c1f4a6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/input.ts @@ -0,0 +1,3 @@ +const fn = function* (input: T): Generator { + yield 2; +} diff --git a/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json new file mode 100644 index 000000000000..064d5577f360 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-generics/anonymous-function-generator/output.json @@ -0,0 +1,118 @@ +{ + "type": "File", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":68,"loc":{"start":{"line":1,"column":6},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":8,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":8},"identifierName":"fn"}, + "name": "fn" + }, + "init": { + "type": "FunctionExpression", + "start":11,"end":68,"loc":{"start":{"line":1,"column":11},"end":{"line":3,"column":1}}, + "id": null, + "generator": true, + "async": false, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":21,"end":24,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":24}}, + "params": [ + { + "type": "TypeParameter", + "start":22,"end":23,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":23}}, + "name": "T", + "variance": null + } + ] + }, + "params": [ + { + "type": "Identifier", + "start":25,"end":33,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":33},"identifierName":"input"}, + "name": "input", + "typeAnnotation": { + "type": "TypeAnnotation", + "start":30,"end":33,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":33}}, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start":32,"end":33,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":33}}, + "typeParameters": null, + "id": { + "type": "Identifier", + "start":32,"end":33,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":33},"identifierName":"T"}, + "name": "T" + } + } + } + } + ], + "predicate": null, + "returnType": { + "type": "TypeAnnotation", + "start":34,"end":53,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":53}}, + "typeAnnotation": { + "type": "GenericTypeAnnotation", + "start":36,"end":53,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":53}}, + "typeParameters": { + "type": "TypeParameterInstantiation", + "start":45,"end":53,"loc":{"start":{"line":1,"column":45},"end":{"line":1,"column":53}}, + "params": [ + { + "type": "NumberTypeAnnotation", + "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}} + } + ] + }, + "id": { + "type": "Identifier", + "start":36,"end":45,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":45},"identifierName":"Generator"}, + "name": "Generator" + } + } + }, + "body": { + "type": "BlockStatement", + "start":54,"end":68,"loc":{"start":{"line":1,"column":54},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ExpressionStatement", + "start":58,"end":66,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":10}}, + "expression": { + "type": "YieldExpression", + "start":58,"end":65,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":9}}, + "delegate": false, + "argument": { + "type": "NumericLiteral", + "start":64,"end":65,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + } + ], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/input.ts b/packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/input.ts new file mode 100644 index 000000000000..33ce49c1f4a6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/input.ts @@ -0,0 +1,3 @@ +const fn = function* (input: T): Generator { + yield 2; +} diff --git a/packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/output.json b/packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/output.json new file mode 100644 index 000000000000..b48b432ce19a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/function/anonymous-generator/output.json @@ -0,0 +1,115 @@ +{ + "type": "File", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":68,"loc":{"start":{"line":1,"column":6},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":8,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":8},"identifierName":"fn"}, + "name": "fn" + }, + "init": { + "type": "FunctionExpression", + "start":11,"end":68,"loc":{"start":{"line":1,"column":11},"end":{"line":3,"column":1}}, + "id": null, + "generator": true, + "async": false, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":21,"end":24,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":24}}, + "params": [ + { + "type": "TSTypeParameter", + "start":22,"end":23,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":23}}, + "name": "T" + } + ] + }, + "params": [ + { + "type": "Identifier", + "start":25,"end":33,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":33},"identifierName":"input"}, + "name": "input", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":30,"end":33,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":33}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":32,"end":33,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":33}}, + "typeName": { + "type": "Identifier", + "start":32,"end":33,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":33},"identifierName":"T"}, + "name": "T" + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start":34,"end":53,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":53}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":36,"end":53,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":53}}, + "typeName": { + "type": "Identifier", + "start":36,"end":45,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":45},"identifierName":"Generator"}, + "name": "Generator" + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "start":45,"end":53,"loc":{"start":{"line":1,"column":45},"end":{"line":1,"column":53}}, + "params": [ + { + "type": "TSNumberKeyword", + "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}} + } + ] + } + } + }, + "body": { + "type": "BlockStatement", + "start":54,"end":68,"loc":{"start":{"line":1,"column":54},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ExpressionStatement", + "start":58,"end":66,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":10}}, + "expression": { + "type": "YieldExpression", + "start":58,"end":65,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":9}}, + "delegate": false, + "argument": { + "type": "NumericLiteral", + "start":64,"end":65,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + } + ], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/input.ts b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/input.ts new file mode 100644 index 000000000000..109fe67a21e0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/input.ts @@ -0,0 +1,3 @@ +const fn = function* <,>(input: T): Generator { + yield 2; +} diff --git a/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/options.json b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/options.json new file mode 100644 index 000000000000..be27f9417b1b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator-invalid/options.json @@ -0,0 +1,5 @@ +{ + "plugins": ["jsx", "typescript"], + "sourceType": "module", + "throws": "Unexpected token (1:22)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/input.ts b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/input.ts new file mode 100644 index 000000000000..33ce49c1f4a6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/input.ts @@ -0,0 +1,3 @@ +const fn = function* (input: T): Generator { + yield 2; +} diff --git a/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json new file mode 100644 index 000000000000..937ca57900ca --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/tsx/anonymous-function-generator/output.json @@ -0,0 +1,115 @@ +{ + "type": "File", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":68,"loc":{"start":{"line":1,"column":6},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":8,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":8},"identifierName":"fn"}, + "name": "fn" + }, + "init": { + "type": "FunctionExpression", + "start":11,"end":68,"loc":{"start":{"line":1,"column":11},"end":{"line":3,"column":1}}, + "id": null, + "generator": true, + "async": false, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":21,"end":24,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":24}}, + "params": [ + { + "type": "TSTypeParameter", + "start":22,"end":23,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":23}}, + "name": "T" + } + ] + }, + "params": [ + { + "type": "Identifier", + "start":25,"end":33,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":33},"identifierName":"input"}, + "name": "input", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start":30,"end":33,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":33}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":32,"end":33,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":33}}, + "typeName": { + "type": "Identifier", + "start":32,"end":33,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":33},"identifierName":"T"}, + "name": "T" + } + } + } + } + ], + "returnType": { + "type": "TSTypeAnnotation", + "start":34,"end":53,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":53}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":36,"end":53,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":53}}, + "typeName": { + "type": "Identifier", + "start":36,"end":45,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":45},"identifierName":"Generator"}, + "name": "Generator" + }, + "typeParameters": { + "type": "TSTypeParameterInstantiation", + "start":45,"end":53,"loc":{"start":{"line":1,"column":45},"end":{"line":1,"column":53}}, + "params": [ + { + "type": "TSNumberKeyword", + "start":46,"end":52,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":52}} + } + ] + } + } + }, + "body": { + "type": "BlockStatement", + "start":54,"end":68,"loc":{"start":{"line":1,"column":54},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ExpressionStatement", + "start":58,"end":66,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":10}}, + "expression": { + "type": "YieldExpression", + "start":58,"end":65,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":9}}, + "delegate": false, + "argument": { + "type": "NumericLiteral", + "start":64,"end":65,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + } + ], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +}