From 02e3066af9278bb7bf31b50dd43e046c1b456896 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Fri, 14 Oct 2016 05:59:32 +0300 Subject: [PATCH] Annotate more errors with expected token Expanding on #150, this allows `unexpected()` to accept the expected token type instead of a message string. This overload is then used in a couple more places (that independently implement a logic similar to `expect()`'s) to construct an `Unexpected token, expected FOO` message. --- src/parser/util.js | 16 ++++++++++------ .../fixtures/core/uncategorised/388/options.json | 2 +- .../fixtures/core/uncategorised/389/options.json | 2 +- .../fixtures/core/uncategorised/405/options.json | 2 +- .../fixtures/core/uncategorised/407/options.json | 2 +- .../fixtures/core/uncategorised/408/options.json | 2 +- .../arrow-functions/no-callee/options.json | 2 +- .../es2015/uncategorised/201/options.json | 2 +- .../es2015/uncategorised/205/options.json | 2 +- .../es2015/uncategorised/209/options.json | 2 +- .../es2015/uncategorised/210/options.json | 2 +- .../es2015/uncategorised/214/options.json | 2 +- .../es2015/uncategorised/215/options.json | 2 +- .../es2015/uncategorised/224/options.json | 2 +- .../es2015/uncategorised/225/options.json | 2 +- .../es2015/uncategorised/253/options.json | 2 +- .../es2015/uncategorised/254/options.json | 2 +- .../es2015/uncategorised/258/options.json | 2 +- .../es2017/async-functions/10/options.json | 2 +- .../es2017/async-functions/6/options.json | 2 +- .../es2017/async-functions/9/options.json | 2 +- .../invalid-export-default/options.json | 2 +- .../invalid-yield-expression/options.json | 2 +- .../options.json | 2 +- .../invalid-syntax/migrated_0015/options.json | 2 +- .../invalid-syntax/migrated_0020/options.json | 2 +- .../invalid-syntax/migrated_0021/options.json | 2 +- .../invalid-syntax/migrated_0025/options.json | 2 +- .../invalid-syntax/migrated_0026/options.json | 2 +- .../invalid-syntax/migrated_0028/options.json | 2 +- .../invalid-syntax/migrated_0071/options.json | 2 +- .../invalid-syntax/migrated_0072/options.json | 2 +- .../invalid-syntax/migrated_0115/options.json | 2 +- .../invalid-syntax/migrated_0257/options.json | 2 +- .../asi-failure-computed/options.json | 2 +- .../asi-failure-generator/options.json | 2 +- 36 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/parser/util.js b/src/parser/util.js index e5e0a7543b..f4a8dab91a 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -27,7 +27,7 @@ pp.expectRelational = function (op) { if (this.isRelational(op)) { this.next(); } else { - this.unexpected(); + this.unexpected(null, tt.relational); } }; @@ -67,18 +67,22 @@ pp.isLineTerminator = function () { // pretend that there is a semicolon at this position. pp.semicolon = function () { - if (!this.isLineTerminator()) this.unexpected(); + if (!this.isLineTerminator()) this.unexpected(null, tt.semi); }; // Expect a token of a given type. If found, consume it, otherwise, // raise an unexpected token error at given pos. pp.expect = function (type, pos) { - return this.eat(type) || this.unexpected(pos, `Unexpected token, expected ${type.label}`); + return this.eat(type) || this.unexpected(pos, type); }; -// Raise an unexpected token error. +// Raise an unexpected token error. Can take the expected token type +// instead of a message string. -pp.unexpected = function (pos, message = "Unexpected token") { - this.raise(pos != null ? pos : this.state.start, message); +pp.unexpected = function (pos, messageOrType = "Unexpected token") { + if (messageOrType && typeof messageOrType === 'object' && messageOrType.label) { + messageOrType = `Unexpected token, expected ${messageOrType.label}`; + } + this.raise(pos != null ? pos : this.state.start, messageOrType); }; diff --git a/test/fixtures/core/uncategorised/388/options.json b/test/fixtures/core/uncategorised/388/options.json index 515b971673..ba6c2c68ee 100644 --- a/test/fixtures/core/uncategorised/388/options.json +++ b/test/fixtures/core/uncategorised/388/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:6)" + "throws": "Unexpected token, expected ; (1:6)" } \ No newline at end of file diff --git a/test/fixtures/core/uncategorised/389/options.json b/test/fixtures/core/uncategorised/389/options.json index 515b971673..ba6c2c68ee 100644 --- a/test/fixtures/core/uncategorised/389/options.json +++ b/test/fixtures/core/uncategorised/389/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:6)" + "throws": "Unexpected token, expected ; (1:6)" } \ No newline at end of file diff --git a/test/fixtures/core/uncategorised/405/options.json b/test/fixtures/core/uncategorised/405/options.json index e68fbb6aec..3044657f4d 100644 --- a/test/fixtures/core/uncategorised/405/options.json +++ b/test/fixtures/core/uncategorised/405/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:2)" + "throws": "Unexpected token, expected ; (1:2)" } \ No newline at end of file diff --git a/test/fixtures/core/uncategorised/407/options.json b/test/fixtures/core/uncategorised/407/options.json index e68fbb6aec..3044657f4d 100644 --- a/test/fixtures/core/uncategorised/407/options.json +++ b/test/fixtures/core/uncategorised/407/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:2)" + "throws": "Unexpected token, expected ; (1:2)" } \ No newline at end of file diff --git a/test/fixtures/core/uncategorised/408/options.json b/test/fixtures/core/uncategorised/408/options.json index e68fbb6aec..3044657f4d 100644 --- a/test/fixtures/core/uncategorised/408/options.json +++ b/test/fixtures/core/uncategorised/408/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:2)" + "throws": "Unexpected token, expected ; (1:2)" } \ No newline at end of file diff --git a/test/fixtures/es2015/arrow-functions/no-callee/options.json b/test/fixtures/es2015/arrow-functions/no-callee/options.json index 9ce9658f7d..1288082ab0 100644 --- a/test/fixtures/es2015/arrow-functions/no-callee/options.json +++ b/test/fixtures/es2015/arrow-functions/no-callee/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:8)" + "throws": "Unexpected token, expected ; (1:8)" } diff --git a/test/fixtures/es2015/uncategorised/201/options.json b/test/fixtures/es2015/uncategorised/201/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/es2015/uncategorised/201/options.json +++ b/test/fixtures/es2015/uncategorised/201/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/205/options.json b/test/fixtures/es2015/uncategorised/205/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/es2015/uncategorised/205/options.json +++ b/test/fixtures/es2015/uncategorised/205/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/209/options.json b/test/fixtures/es2015/uncategorised/209/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/es2015/uncategorised/209/options.json +++ b/test/fixtures/es2015/uncategorised/209/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/210/options.json b/test/fixtures/es2015/uncategorised/210/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/es2015/uncategorised/210/options.json +++ b/test/fixtures/es2015/uncategorised/210/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/214/options.json b/test/fixtures/es2015/uncategorised/214/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/es2015/uncategorised/214/options.json +++ b/test/fixtures/es2015/uncategorised/214/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/215/options.json b/test/fixtures/es2015/uncategorised/215/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/es2015/uncategorised/215/options.json +++ b/test/fixtures/es2015/uncategorised/215/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/224/options.json b/test/fixtures/es2015/uncategorised/224/options.json index 2506b3d344..2ba5b13372 100644 --- a/test/fixtures/es2015/uncategorised/224/options.json +++ b/test/fixtures/es2015/uncategorised/224/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (2:4)" + "throws": "Unexpected token, expected ; (2:4)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/225/options.json b/test/fixtures/es2015/uncategorised/225/options.json index f86bbd48e9..8495a77815 100644 --- a/test/fixtures/es2015/uncategorised/225/options.json +++ b/test/fixtures/es2015/uncategorised/225/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (2:6)" + "throws": "Unexpected token, expected ; (2:6)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/253/options.json b/test/fixtures/es2015/uncategorised/253/options.json index 515b971673..ba6c2c68ee 100644 --- a/test/fixtures/es2015/uncategorised/253/options.json +++ b/test/fixtures/es2015/uncategorised/253/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:6)" + "throws": "Unexpected token, expected ; (1:6)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/254/options.json b/test/fixtures/es2015/uncategorised/254/options.json index 515b971673..ba6c2c68ee 100644 --- a/test/fixtures/es2015/uncategorised/254/options.json +++ b/test/fixtures/es2015/uncategorised/254/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:6)" + "throws": "Unexpected token, expected ; (1:6)" } \ No newline at end of file diff --git a/test/fixtures/es2015/uncategorised/258/options.json b/test/fixtures/es2015/uncategorised/258/options.json index b81bee7fa2..fce556fdca 100644 --- a/test/fixtures/es2015/uncategorised/258/options.json +++ b/test/fixtures/es2015/uncategorised/258/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:21)" + "throws": "Unexpected token, expected ; (1:21)" } \ No newline at end of file diff --git a/test/fixtures/es2017/async-functions/10/options.json b/test/fixtures/es2017/async-functions/10/options.json index b2f4797ad8..4fbed8c376 100644 --- a/test/fixtures/es2017/async-functions/10/options.json +++ b/test/fixtures/es2017/async-functions/10/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (2:4)" + "throws": "Unexpected token, expected ; (2:4)" } diff --git a/test/fixtures/es2017/async-functions/6/options.json b/test/fixtures/es2017/async-functions/6/options.json index d50e8469b8..111afcd69d 100644 --- a/test/fixtures/es2017/async-functions/6/options.json +++ b/test/fixtures/es2017/async-functions/6/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:14)" + "throws": "Unexpected token, expected ; (1:14)" } diff --git a/test/fixtures/es2017/async-functions/9/options.json b/test/fixtures/es2017/async-functions/9/options.json index c87b29dd4d..62e1e03ac5 100644 --- a/test/fixtures/es2017/async-functions/9/options.json +++ b/test/fixtures/es2017/async-functions/9/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:30)" + "throws": "Unexpected token, expected ; (1:30)" } diff --git a/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json b/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json index aca079ee39..940e0cf2e3 100644 --- a/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json +++ b/test/fixtures/esprima/es2015-export-declaration/invalid-export-default/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:20)" + "throws": "Unexpected token, expected ; (1:20)" } \ No newline at end of file diff --git a/test/fixtures/esprima/es2015-yield/invalid-yield-expression/options.json b/test/fixtures/esprima/es2015-yield/invalid-yield-expression/options.json index aca079ee39..940e0cf2e3 100644 --- a/test/fixtures/esprima/es2015-yield/invalid-yield-expression/options.json +++ b/test/fixtures/esprima/es2015-yield/invalid-yield-expression/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:20)" + "throws": "Unexpected token, expected ; (1:20)" } \ No newline at end of file diff --git a/test/fixtures/esprima/es2015-yield/invalid-yield-generator-member-expression/options.json b/test/fixtures/esprima/es2015-yield/invalid-yield-generator-member-expression/options.json index f55830c1c3..cff9bee079 100644 --- a/test/fixtures/esprima/es2015-yield/invalid-yield-generator-member-expression/options.json +++ b/test/fixtures/esprima/es2015-yield/invalid-yield-generator-member-expression/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:28)" + "throws": "Unexpected token, expected ; (1:28)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0015/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0015/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0015/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0015/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0020/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0020/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0020/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0020/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0021/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0021/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0021/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0021/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0025/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0025/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0025/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0025/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0026/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0026/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0026/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0026/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0028/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0028/options.json index 3b5e811628..478be58865 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0028/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0028/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:3)" + "throws": "Unexpected token, expected ; (1:3)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0071/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0071/options.json index 515b971673..ba6c2c68ee 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0071/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0071/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:6)" + "throws": "Unexpected token, expected ; (1:6)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0072/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0072/options.json index 515b971673..ba6c2c68ee 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0072/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0072/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:6)" + "throws": "Unexpected token, expected ; (1:6)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0115/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0115/options.json index e68fbb6aec..3044657f4d 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0115/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0115/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:2)" + "throws": "Unexpected token, expected ; (1:2)" } \ No newline at end of file diff --git a/test/fixtures/esprima/invalid-syntax/migrated_0257/options.json b/test/fixtures/esprima/invalid-syntax/migrated_0257/options.json index 89e36d9013..c86ee6ab79 100644 --- a/test/fixtures/esprima/invalid-syntax/migrated_0257/options.json +++ b/test/fixtures/esprima/invalid-syntax/migrated_0257/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:16)" + "throws": "Unexpected token, expected ; (1:16)" } \ No newline at end of file diff --git a/test/fixtures/experimental/class-properties/asi-failure-computed/options.json b/test/fixtures/experimental/class-properties/asi-failure-computed/options.json index 0c58f8ded1..3d6afa3a38 100644 --- a/test/fixtures/experimental/class-properties/asi-failure-computed/options.json +++ b/test/fixtures/experimental/class-properties/asi-failure-computed/options.json @@ -1,4 +1,4 @@ { - "throws": "Unexpected token (3:9)", + "throws": "Unexpected token, expected ; (3:9)", "plugins": ["classProperties"] } diff --git a/test/fixtures/experimental/class-properties/asi-failure-generator/options.json b/test/fixtures/experimental/class-properties/asi-failure-generator/options.json index 2a095c57b8..32897f69b1 100644 --- a/test/fixtures/experimental/class-properties/asi-failure-generator/options.json +++ b/test/fixtures/experimental/class-properties/asi-failure-generator/options.json @@ -1,4 +1,4 @@ { - "throws": "Unexpected token (3:8)", + "throws": "Unexpected token, expected ; (3:8)", "plugins": ["classProperties"] }