Skip to content

improve error info for arrow function at right of binary expression#26085

Closed
Kingwl wants to merge 1 commit intomicrosoft:masterfrom
Kingwl:improve-arrow-function
Closed

improve error info for arrow function at right of binary expression#26085
Kingwl wants to merge 1 commit intomicrosoft:masterfrom
Kingwl:improve-arrow-function

Conversation

@Kingwl
Copy link
Copy Markdown
Contributor

@Kingwl Kingwl commented Jul 31, 2018

Fixes #25898

Comment thread src/compiler/parser.ts
return nextToken() === SyntaxKind.SlashToken;
}

function isStartOfSimpleArrowFunction () {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not space before parentheses

Comment thread src/compiler/parser.ts
function isStartOfSimpleArrowFunction () {
if (token() === SyntaxKind.AsyncKeyword) {
nextToken();
return isIdentifier() && nextToken() === SyntaxKind.EqualsGreaterThanToken || token() === SyntaxKind.EqualsGreaterThanToken;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrow functions are not syntactically valid here. Consider wrapping the function in parentheses.

~
!!! error TS1109: Expression expected.
true || async => 1;
~~~~~~~~~~~
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really don't want the error span to include whitespace.

Comment thread src/compiler/parser.ts
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));
Copy link
Copy Markdown
Member

@DanielRosenwasser DanielRosenwasser Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/compiler/parser.ts
}
else {
leftOperand = makeBinaryExpression(leftOperand, parseTokenNode(), parseBinaryExpressionOrHigher(newPrecedence));
const op = parseTokenNode<Token<BinaryOperator>>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@RyanCavanaugh
Copy link
Copy Markdown
Member

Many unaddressed comments - please open a fresh PR if desired. Thanks!

@Kingwl Kingwl deleted the improve-arrow-function branch April 25, 2019 10:08
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants