Annotate more errors with expected token (#172)
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.
This commit is contained in:
parent
99fbcefa65
commit
d3af158ba5
@ -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);
|
||||
};
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:6)"
|
||||
"throws": "Unexpected token, expected ; (1:6)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:6)"
|
||||
"throws": "Unexpected token, expected ; (1:6)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:2)"
|
||||
"throws": "Unexpected token, expected ; (1:2)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:2)"
|
||||
"throws": "Unexpected token, expected ; (1:2)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:2)"
|
||||
"throws": "Unexpected token, expected ; (1:2)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:8)"
|
||||
"throws": "Unexpected token, expected ; (1:8)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (2:4)"
|
||||
"throws": "Unexpected token, expected ; (2:4)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (2:6)"
|
||||
"throws": "Unexpected token, expected ; (2:6)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:6)"
|
||||
"throws": "Unexpected token, expected ; (1:6)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:6)"
|
||||
"throws": "Unexpected token, expected ; (1:6)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:21)"
|
||||
"throws": "Unexpected token, expected ; (1:21)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (2:4)"
|
||||
"throws": "Unexpected token, expected ; (2:4)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:14)"
|
||||
"throws": "Unexpected token, expected ; (1:14)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:30)"
|
||||
"throws": "Unexpected token, expected ; (1:30)"
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:20)"
|
||||
"throws": "Unexpected token, expected ; (1:20)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:20)"
|
||||
"throws": "Unexpected token, expected ; (1:20)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:28)"
|
||||
"throws": "Unexpected token, expected ; (1:28)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:3)"
|
||||
"throws": "Unexpected token, expected ; (1:3)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:6)"
|
||||
"throws": "Unexpected token, expected ; (1:6)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:6)"
|
||||
"throws": "Unexpected token, expected ; (1:6)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:2)"
|
||||
"throws": "Unexpected token, expected ; (1:2)"
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token (1:16)"
|
||||
"throws": "Unexpected token, expected ; (1:16)"
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
{
|
||||
"throws": "Unexpected token (3:9)",
|
||||
"throws": "Unexpected token, expected ; (3:9)",
|
||||
"plugins": ["classProperties"]
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
{
|
||||
"throws": "Unexpected token (3:8)",
|
||||
"throws": "Unexpected token, expected ; (3:8)",
|
||||
"plugins": ["classProperties"]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user