Skip to content

Commit

Permalink
use exact error message
Browse files Browse the repository at this point in the history
  • Loading branch information
lala7573 committed Jul 7, 2021
1 parent adedffd commit dac5990
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
17 changes: 10 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 All @@ -2086,6 +2086,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

if (this.isRelational("<")) {
let error;
// tsTryParseAndCatch is expensive, so avoid if not necessary.
// There are number of things we are going to "maybe" parse, like type arguments on
// tagged template expressions. If any of them fail, walk it back and continue.
Expand All @@ -2108,6 +2109,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const typeArguments = this.tsParseTypeArguments();

if (typeArguments) {
if (isOptionalCall && !this.match(tt.parenL)) {
error = [this.state.pos, tt.parenL];
this.unexpected();
}

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 +2134,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 +2150,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.unexpected();
});

if (!result && isOptionalCall) {
this.unexpected(this.state.pos, tt.parenL);
if (error) {
this.unexpected(...error);
}

if (result) return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"plugins": [
"typescript"
],
"throws": "Unexpected token, expected \"(\" (1:4)"
"throws": "Unexpected token, expected \"(\" (1:12)"
}
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 dac5990

Please sign in to comment.