From ffcf461fbedf4074d4343babc7dec9747dafba60 Mon Sep 17 00:00:00 2001 From: "William C. Johnson" Date: Mon, 2 Oct 2017 21:00:50 -0400 Subject: [PATCH] More detailed error message in ambiguous cases. --- src/parser/statement.js | 11 +++++++++-- test/fixtures/core/uncategorised/345/options.json | 11 +++++++++-- test/fixtures/core/uncategorised/386/options.json | 11 +++++++++-- .../invalid-syntax/migrated_0000/options.json | 11 +++++++++-- .../invalid-syntax/migrated_0069/options.json | 11 +++++++++-- .../comprehension/object-paren-free/options.json | 2 +- .../object-block-ambiguity/pattern-block/actual.js | 4 ++++ .../pattern-block/options.json | 13 +++++++++++++ 8 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/lightscript/object-block-ambiguity/pattern-block/actual.js create mode 100644 test/fixtures/lightscript/object-block-ambiguity/pattern-block/options.json diff --git a/src/parser/statement.js b/src/parser/statement.js index 247bdd5228..2eac9aa23c 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -116,8 +116,15 @@ pp.parseStatement = function (declaration, topLevel) { try { return this.parseBlock(); } catch (err) { - if (objParseError && objParseError.pos >= err.pos) { - throw objParseError; + if (objParseError) { + if (objParseError.pos > err.pos) { + throw objParseError; + } else if (objParseError.pos < err.pos) { + throw err; + } else { + objParseError.message = "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: " + objParseError.message; + throw objParseError; + } } else { throw err; } diff --git a/test/fixtures/core/uncategorised/345/options.json b/test/fixtures/core/uncategorised/345/options.json index 04b967a2ee..45c6541808 100644 --- a/test/fixtures/core/uncategorised/345/options.json +++ b/test/fixtures/core/uncategorised/345/options.json @@ -1,3 +1,10 @@ { - "throws": "Unexpected token (1:1)" -} \ No newline at end of file + "alternatives": { + "all": { + "throws": "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: Unexpected token (1:1)" + }, + "noLsc": { + "throws": "Unexpected token (1:1)" + } + } +} diff --git a/test/fixtures/core/uncategorised/386/options.json b/test/fixtures/core/uncategorised/386/options.json index 476d7fff91..9d89b4fed8 100644 --- a/test/fixtures/core/uncategorised/386/options.json +++ b/test/fixtures/core/uncategorised/386/options.json @@ -1,3 +1,10 @@ { - "throws": "Unexpected token (4:1)" -} \ No newline at end of file + "alternatives": { + "all": { + "throws": "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: Unexpected token (4:1)" + }, + "noLsc": { + "throws": "Unexpected token (4:1)" + } + } +} diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0000/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0000/options.json index 04b967a2ee..45c6541808 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0000/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0000/options.json @@ -1,3 +1,10 @@ { - "throws": "Unexpected token (1:1)" -} \ No newline at end of file + "alternatives": { + "all": { + "throws": "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: Unexpected token (1:1)" + }, + "noLsc": { + "throws": "Unexpected token (1:1)" + } + } +} diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0069/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0069/options.json index 04b967a2ee..45c6541808 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0069/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0069/options.json @@ -1,3 +1,10 @@ { - "throws": "Unexpected token (1:1)" -} \ No newline at end of file + "alternatives": { + "all": { + "throws": "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: Unexpected token (1:1)" + }, + "noLsc": { + "throws": "Unexpected token (1:1)" + } + } +} diff --git a/test/fixtures/lightscript/comprehension/object-paren-free/options.json b/test/fixtures/lightscript/comprehension/object-paren-free/options.json index 22fb4f261c..feeefdb2cc 100644 --- a/test/fixtures/lightscript/comprehension/object-paren-free/options.json +++ b/test/fixtures/lightscript/comprehension/object-paren-free/options.json @@ -1,7 +1,7 @@ { "alternatives": { "all": { - "throws": "Unexpected token, expected } (1:26)" + "throws": "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: Unexpected token, expected } (1:26)" }, "noSeqExprParens": { "allPlugins": true, diff --git a/test/fixtures/lightscript/object-block-ambiguity/pattern-block/actual.js b/test/fixtures/lightscript/object-block-ambiguity/pattern-block/actual.js new file mode 100644 index 0000000000..941464a8f4 --- /dev/null +++ b/test/fixtures/lightscript/object-block-ambiguity/pattern-block/actual.js @@ -0,0 +1,4 @@ +{ + a = b + x& +} diff --git a/test/fixtures/lightscript/object-block-ambiguity/pattern-block/options.json b/test/fixtures/lightscript/object-block-ambiguity/pattern-block/options.json new file mode 100644 index 0000000000..f2a7da2e70 --- /dev/null +++ b/test/fixtures/lightscript/object-block-ambiguity/pattern-block/options.json @@ -0,0 +1,13 @@ +{ + "alternatives": { + "all": { + "throws": "Cannot parse brace-delimited construct as an object or as a block. When parsed as an object, the error is: Unexpected token, expected , (3:3)" + }, + "whiteblockOnly": { + "throws": "Unexpected token, expected , (3:3)" + }, + "braceblock": { + "throws": "Unexpected token, expected , (3:3)" + } + } +}