From beca7e2d8e664ec681fcc7481daec171d962cae2 Mon Sep 17 00:00:00 2001 From: Jorge Henriquez Date: Mon, 22 Jun 2020 15:12:52 -0700 Subject: [PATCH] Add better parser error when using jsx (#11722) * Add "<" parser tests * No {jsx,flow,typescript} plugin * Type parameter * Valid JS Code * Add: better parser error when using jsx Address #11499 * Add: babel parser test Test parser with no plugins and when jsx is given with a js expression * Add: no flow but with typescript test * Add: type paramter test with no plugins (no flow) * Add: unclosed jsx element test --- .../babel-parser/src/parser/expression.js | 6 +++ .../errors/_no-plugin-jsx-expression/input.js | 1 + .../_no-plugin-jsx-expression/options.json | 4 ++ .../jsx/errors/_no-plugin-no-jsx/input.js | 2 + .../jsx/errors/_no-plugin-no-jsx/output.json | 36 ++++++++++++++++ .../_no-plugin-ts-type-param-no-flow/input.js | 1 + .../options.json | 3 ++ .../output.json | 42 ++++++++++++++++++ .../errors/_no-plugin-ts-type-param/input.js | 1 + .../_no-plugin-ts-type-param/output.json | 43 +++++++++++++++++++ .../jsx/errors/_no-plugin-type-param/input.js | 1 + .../errors/_no-plugin-type-param/options.json | 4 ++ .../fixtures/jsx/errors/_no_plugin/input.js | 1 + .../jsx/errors/_no_plugin/options.json | 4 ++ .../jsx/errors/unclosed-jsx-element/input.js | 1 + .../errors/unclosed-jsx-element/options.json | 4 ++ 16 files changed, 154 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/options.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-no-jsx/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-no-jsx/output.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/options.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/output.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/output.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/options.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/options.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/options.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 875d71e465ef..8c4dc7bd191b 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1177,6 +1177,12 @@ export default class ExpressionParser extends LValParser { } } // fall through + case tt.relational: { + if (this.state.value === "<") { + throw this.expectOnePlugin(["jsx", "flow", "typescript"]); + } + } + // fall through default: throw this.unexpected(); } diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/input.js b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/input.js new file mode 100644 index 000000000000..f56238150ab2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/input.js @@ -0,0 +1 @@ +
{name}
diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/options.json b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/options.json new file mode 100644 index 000000000000..f11e7c5d0451 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-jsx-expression/options.json @@ -0,0 +1,4 @@ +{ + "plugins": [], + "throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (1:0)" +} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-no-jsx/input.js b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-no-jsx/input.js new file mode 100644 index 000000000000..fbc4c110bd81 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-no-jsx/input.js @@ -0,0 +1,2 @@ +"use strict" +
() => {} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/options.json b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/options.json new file mode 100644 index 000000000000..5047d6993fe8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["typescript"] +} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/output.json b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/output.json new file mode 100644 index 000000000000..d1ca421c201b --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param-no-flow/output.json @@ -0,0 +1,42 @@ +{ + "type": "File", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "program": { + "type": "Program", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":11,"end":13,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":13}}, + "body": [], + "directives": [] + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "params": [ + { + "type": "TSTypeParameter", + "start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}}, + "name": "div" + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/input.js b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/input.js new file mode 100644 index 000000000000..2791c2c7a3a0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/input.js @@ -0,0 +1 @@ +
() => {} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/output.json b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/output.json new file mode 100644 index 000000000000..44b47bc7920b --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-ts-type-param/output.json @@ -0,0 +1,43 @@ +{ + "type": "File", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "program": { + "type": "Program", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":11,"end":13,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":13}}, + "body": [], + "directives": [] + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "params": [ + { + "type": "TypeParameter", + "start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}}, + "name": "div", + "variance": null + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/input.js b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/input.js new file mode 100644 index 000000000000..2791c2c7a3a0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/input.js @@ -0,0 +1 @@ +
() => {} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/options.json b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/options.json new file mode 100644 index 000000000000..f11e7c5d0451 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-type-param/options.json @@ -0,0 +1,4 @@ +{ + "plugins": [], + "throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (1:0)" +} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/input.js b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/input.js new file mode 100644 index 000000000000..7c89b545c5ac --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/input.js @@ -0,0 +1 @@ +
diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/options.json b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/options.json new file mode 100644 index 000000000000..7e0538ba8978 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin/options.json @@ -0,0 +1,4 @@ +{ + "throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (1:0)", + "plugins": [] +} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/input.js b/packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/input.js new file mode 100644 index 000000000000..2791c2c7a3a0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/input.js @@ -0,0 +1 @@ +
() => {} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/options.json b/packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/options.json new file mode 100644 index 000000000000..05048c2000ee --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/unclosed-jsx-element/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["jsx"], + "throws": "Unexpected token (1:13)" +}