improve error info for arrow function at right of binary expression#26085
improve error info for arrow function at right of binary expression#26085Kingwl wants to merge 1 commit intomicrosoft:masterfrom
Conversation
| return nextToken() === SyntaxKind.SlashToken; | ||
| } | ||
|
|
||
| function isStartOfSimpleArrowFunction () { |
There was a problem hiding this comment.
Nit: not space before parentheses
| function isStartOfSimpleArrowFunction () { | ||
| if (token() === SyntaxKind.AsyncKeyword) { | ||
| nextToken(); | ||
| return isIdentifier() && nextToken() === SyntaxKind.EqualsGreaterThanToken || token() === SyntaxKind.EqualsGreaterThanToken; |
There was a problem hiding this comment.
Can you leave a comment on the intent here? Is this supposed to cover async => 100?
| "category": "Error", | ||
| "code": 17017 | ||
| }, | ||
| "Invalid arrow-function arguments, parentheses around the arrow-function may help.": { |
There was a problem hiding this comment.
Arrow functions are not syntactically valid here. Consider wrapping the function in parentheses.
| ~ | ||
| !!! error TS1109: Expression expected. | ||
| true || async => 1; | ||
| ~~~~~~~~~~~ |
There was a problem hiding this comment.
You really don't want the error span to include whitespace.
| const maybeArrow = tryParseAsyncSimpleArrowFunctionExpression() || lookAhead(() => parseSimpleArrowFunctionExpression(<Identifier>parseBinaryExpressionOrHigher(/*precedence*/ 0))); | ||
| parseErrorAtRange(maybeArrow, Diagnostics.Invalid_arrow_function_arguments_parentheses_around_the_arrow_function_may_help); | ||
| } | ||
| leftOperand = makeBinaryExpression(leftOperand, op, parseBinaryExpressionOrHigher(newPrecedence)); |
There was a problem hiding this comment.
You're currently ignoring it if it's present, but I think that you actually want to gracefully parse the arrow functions you've been able to find above.
Throwing away the result and trying to parse as before actually provides a slightly more confusing error experience.
| } | ||
| else { | ||
| leftOperand = makeBinaryExpression(leftOperand, parseTokenNode(), parseBinaryExpressionOrHigher(newPrecedence)); | ||
| const op = parseTokenNode<Token<BinaryOperator>>(); |
There was a problem hiding this comment.
Leave a comment like
// Much of the time, users will write code along the lines of
//
// let x = foo || () => { /*...*/ }
//
// However, arrow functions aren't valid in those positions in the ECMAScript grammar.
// Despite that, we'll try to parse it out anyway and give a decent error message.
|
Many unaddressed comments - please open a fresh PR if desired. Thanks! |
Fixes #25898