From 78b03c6064c6631223e028c8cb3ea86c5075b1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 1 Jul 2020 10:58:04 -0400 Subject: [PATCH 1/3] fix: throw expect jsx plugin error when an idStart or > is seen --- packages/babel-parser/src/parser/expression.js | 8 +++++++- .../fixtures/jsx/{errors => basic}/html-comment/input.js | 0 .../jsx/{errors => basic}/html-comment/options.json | 0 .../jsx/{errors => basic}/html-comment/output.json | 0 .../test/fixtures/jsx/errors/_no-plugin-fragment/input.js | 5 +++++ .../fixtures/jsx/errors/_no-plugin-fragment/options.json | 4 ++++ .../jsx/errors/_no_plugin-non-BMP-identifier/input.js | 5 +++++ .../jsx/errors/_no_plugin-non-BMP-identifier/options.json | 4 ++++ .../test/fixtures/jsx/errors/html-comment-module/input.js | 1 + .../fixtures/jsx/errors/html-comment-module/options.json | 5 +++++ 10 files changed, 31 insertions(+), 1 deletion(-) rename packages/babel-parser/test/fixtures/jsx/{errors => basic}/html-comment/input.js (100%) rename packages/babel-parser/test/fixtures/jsx/{errors => basic}/html-comment/options.json (100%) rename packages/babel-parser/test/fixtures/jsx/{errors => basic}/html-comment/output.json (100%) create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/options.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/options.json create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/html-comment-module/input.js create mode 100644 packages/babel-parser/test/fixtures/jsx/errors/html-comment-module/options.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index cc6278c2f532..f4f3c733b6fd 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1179,7 +1179,13 @@ export default class ExpressionParser extends LValParser { // fall through case tt.relational: { if (this.state.value === "<") { - throw this.expectOnePlugin(["jsx", "flow", "typescript"]); + const lookaheadCh = this.input.codePointAt(this.nextTokenStart()); + if ( + isIdentifierStart(lookaheadCh) || // Element/Type Parameter + lookaheadCh === charCodes.greaterThan // Fragment <> + ) { + throw this.expectOnePlugin(["jsx", "flow", "typescript"]); + } } } // fall through diff --git a/packages/babel-parser/test/fixtures/jsx/errors/html-comment/input.js b/packages/babel-parser/test/fixtures/jsx/basic/html-comment/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/jsx/errors/html-comment/input.js rename to packages/babel-parser/test/fixtures/jsx/basic/html-comment/input.js diff --git a/packages/babel-parser/test/fixtures/jsx/errors/html-comment/options.json b/packages/babel-parser/test/fixtures/jsx/basic/html-comment/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/jsx/errors/html-comment/options.json rename to packages/babel-parser/test/fixtures/jsx/basic/html-comment/options.json diff --git a/packages/babel-parser/test/fixtures/jsx/errors/html-comment/output.json b/packages/babel-parser/test/fixtures/jsx/basic/html-comment/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/jsx/errors/html-comment/output.json rename to packages/babel-parser/test/fixtures/jsx/basic/html-comment/output.json diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/input.js b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/input.js new file mode 100644 index 000000000000..c6f7d341b11f --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/input.js @@ -0,0 +1,5 @@ +function foo () { + return ( + <>Hello + ); +} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/options.json b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/options.json new file mode 100644 index 000000000000..fca5528e7115 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no-plugin-fragment/options.json @@ -0,0 +1,4 @@ +{ + "throws": "This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript' (3:4)", + "plugins": [] +} diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/input.js b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/input.js new file mode 100644 index 000000000000..f89d068d2554 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/input.js @@ -0,0 +1,5 @@ +< + 𠮷 +> diff --git a/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/options.json b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/options.json new file mode 100644 index 000000000000..7e0538ba8978 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/_no_plugin-non-BMP-identifier/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/html-comment-module/input.js b/packages/babel-parser/test/fixtures/jsx/errors/html-comment-module/input.js new file mode 100644 index 000000000000..5190fb0e1d65 --- /dev/null +++ b/packages/babel-parser/test/fixtures/jsx/errors/html-comment-module/input.js @@ -0,0 +1 @@ + diff --git a/packages/babel-parser/test/fixtures/es2015/modules/invalid-xml-comment-in-module/options.json b/packages/babel-parser/test/fixtures/es2015/modules/invalid-xml-comment-in-module/options.json new file mode 100644 index 000000000000..acc28c552b9e --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/modules/invalid-xml-comment-in-module/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "throws": "Unexpected token (1:0)" +}