Skip to content

Commit

Permalink
Annotate more errors with expected token (babel#172)
Browse files Browse the repository at this point in the history
Expanding on babel#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.
  • Loading branch information
motiz88 authored and Kristof degrave committed Oct 27, 2016
1 parent 8b7c5ca commit 990c67e
Show file tree
Hide file tree
Showing 36 changed files with 45 additions and 41 deletions.
16 changes: 10 additions & 6 deletions src/parser/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pp.expectRelational = function (op) {
if (this.isRelational(op)) {
this.next();
} else {
this.unexpected();
this.unexpected(null, tt.relational);
}
};

Expand Down Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/388/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/389/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/405/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/407/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}
2 changes: 1 addition & 1 deletion test/fixtures/core/uncategorised/408/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:8)"
"throws": "Unexpected token, expected ; (1:8)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/201/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/205/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/209/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/210/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/214/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/215/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/224/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (2:4)"
"throws": "Unexpected token, expected ; (2:4)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/225/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (2:6)"
"throws": "Unexpected token, expected ; (2:6)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/253/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/254/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/uncategorised/258/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:21)"
"throws": "Unexpected token, expected ; (1:21)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2017/async-functions/10/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (2:4)"
"throws": "Unexpected token, expected ; (2:4)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2017/async-functions/6/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:14)"
"throws": "Unexpected token, expected ; (1:14)"
}
2 changes: 1 addition & 1 deletion test/fixtures/es2017/async-functions/9/options.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:30)"
"throws": "Unexpected token, expected ; (1:30)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:20)"
"throws": "Unexpected token, expected ; (1:20)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:20)"
"throws": "Unexpected token, expected ; (1:20)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:28)"
"throws": "Unexpected token, expected ; (1:28)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:3)"
"throws": "Unexpected token, expected ; (1:3)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:6)"
"throws": "Unexpected token, expected ; (1:6)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:2)"
"throws": "Unexpected token, expected ; (1:2)"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Unexpected token (1:16)"
"throws": "Unexpected token, expected ; (1:16)"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:9)",
"throws": "Unexpected token, expected ; (3:9)",
"plugins": ["classProperties"]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"throws": "Unexpected token (3:8)",
"throws": "Unexpected token, expected ; (3:8)",
"plugins": ["classProperties"]
}

0 comments on commit 990c67e

Please sign in to comment.