Skip to content

Commit

Permalink
throw error with accurate position
Browse files Browse the repository at this point in the history
  • Loading branch information
lala7573 committed Jul 3, 2021
1 parent adedffd commit 889a19a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
23 changes: 17 additions & 6 deletions packages/babel-parser/src/parser/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,14 @@ export default class UtilParser extends Tokenizer {
}
}

// Raise an unexpected token error. Can take the expected token type
// instead of a message string.

unexpected(
unexpectedError(
pos: ?number,
messageOrType: ErrorTemplate | TokenType = {
code: ErrorCodes.SyntaxError,
reasonCode: "UnexpectedToken",
template: "Unexpected token",
},
): empty {
) {
if (messageOrType instanceof TokenType) {
messageOrType = {
code: ErrorCodes.SyntaxError,
Expand All @@ -178,7 +175,21 @@ export default class UtilParser extends Tokenizer {
}

/* eslint-disable @babel/development-internal/dry-error-messages */
throw this.raise(pos != null ? pos : this.state.start, messageOrType);
return this.raise(pos != null ? pos : this.state.start, messageOrType);
}

// Raise an unexpected token error. Can take the expected token type
// instead of a message string.
unexpected(
pos: ?number,
messageOrType: ErrorTemplate | TokenType = {
code: ErrorCodes.SyntaxError,
reasonCode: "UnexpectedToken",
template: "Unexpected token",
},
): empty {
/* eslint-disable @babel/development-internal/dry-error-messages */
throw this.unexpectedError(pos, messageOrType);
/* eslint-enable @babel/development-internal/dry-error-messages */
}

Expand Down
15 changes: 8 additions & 7 deletions packages/babel-parser/src/plugins/typescript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.match(tt.questionDot) &&
this.lookaheadCharCode() === charCodes.lessThan
) {
isOptionalCall = true;
state.optionalChainMember = isOptionalCall = true;
if (noCalls) {
state.stop = true;
return base;
Expand Down Expand Up @@ -2108,6 +2108,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const typeArguments = this.tsParseTypeArguments();

if (typeArguments) {
if (isOptionalCall && !this.match(tt.parenL)) {
return this.unexpectedError(this.state.pos, tt.parenL);
}

if (!noCalls && this.eat(tt.parenL)) {
// possibleAsync always false here, because we would have handled it above.
// $FlowIgnore (won't be any undefined arguments)
Expand All @@ -2128,10 +2132,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.optional = true;
}

return this.finishCallExpression(
node,
state.optionalChainMember || isOptionalCall,
);
return this.finishCallExpression(node, state.optionalChainMember);
} else if (this.match(tt.backQuote)) {
const result = this.parseTaggedTemplateExpression(
base,
Expand All @@ -2147,8 +2148,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.unexpected();
});

if (!result && isOptionalCall) {
this.unexpected(this.state.pos, tt.parenL);
if (result?.failState) {
throw result.node;
}

if (result) return result;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"errors": [
"SyntaxError: Unexpected token, expected \"(\" (1:12)"
],
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"expression": {
"type": "OptionalMemberExpression",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"object": "SyntaxError: Unexpected token, expected \"(\" (1:12)",
"computed": true,
"property": {
"type": "NumericLiteral",
"start":12,"end":13,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":13}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"optional": false
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"expression": {
"type": "CallExpression",
"type": "OptionalCallExpression",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"callee": {
"type": "Identifier",
Expand Down Expand Up @@ -41,7 +41,7 @@
"type": "ExpressionStatement",
"start":10,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}},
"expression": {
"type": "CallExpression",
"type": "OptionalCallExpression",
"start":10,"end":21,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":11}},
"callee": {
"type": "Identifier",
Expand Down Expand Up @@ -80,7 +80,7 @@
"type": "ExpressionStatement",
"start":23,"end":33,"loc":{"start":{"line":3,"column":0},"end":{"line":4,"column":8}},
"expression": {
"type": "CallExpression",
"type": "OptionalCallExpression",
"start":23,"end":32,"loc":{"start":{"line":3,"column":0},"end":{"line":4,"column":7}},
"callee": {
"type": "Identifier",
Expand Down

0 comments on commit 889a19a

Please sign in to comment.