Skip to content

Improve arrow function parsing #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

andy-hanson
Copy link
Contributor

@andy-hanson andy-hanson commented Sep 10, 2018

Fixes parsing for #255. Does not fix the issue since a conditional expression is still unsupported in the resolver.

Previously at (a ? b : c)(), we would assume that a ? b : c were the parameters of an arrow function because every token involved was a valid token to appear in a signature -- but that wasn't strict enough. Now we will verify that ? is always followed by :, ,, or ), none of which are legal in expressions.
Also correctly parses x => x now, though it still won't compile.

@andy-hanson andy-hanson force-pushed the arrow-function-parsing branch from 24d6822 to 2cff7e0 Compare September 10, 2018 00:34
src/parser.ts Outdated
@@ -1420,6 +1420,11 @@ export class Parser extends DiagnosticEmitter {
return Node.createFunctionExpression(declaration);
}

private parseArrowFunctionBody(tn: Tokenizer): Statement | null {
// TODO: should parse an expression unless the next token is `{`.
Copy link
Member

Choose a reason for hiding this comment

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

tn.peek() can check the next token, would that help?

src/compiler.ts Outdated
@@ -5445,7 +5444,9 @@ export class Compiler extends DiagnosticEmitter {
}
}
} else {
body.push(this.compileStatement(bodyStatement));
body.push(bodyStatement instanceof Expression
Copy link
Member

Choose a reason for hiding this comment

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

This should probably be bodyStatement.kind != NodeKind.BLOCK, as instanceof isn't supported (yet, without a GC).

dcodeIO added a commit that referenced this pull request Oct 4, 2018
Quite similar to #256 and also uses its test, but also fixes the serializer and doesn't try to parse an untyped 'x => x'.
@dcodeIO
Copy link
Member

dcodeIO commented Oct 4, 2018

The commit above does something very similar with a somewhat reduced refactoring footprint. Also fixes related serializer issues but doesn't tackle x => x. We should probably support this as well eventually, even if just for parsing and erroring out (no type information the compiler could utilize).

@andy-hanson
Copy link
Contributor Author

Closing in favor of referenced commit.

@andy-hanson andy-hanson deleted the arrow-function-parsing branch October 12, 2018 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants