Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Function Expression/Declaration AST Facade #1786

Merged
merged 5 commits into from
Nov 16, 2021
Merged

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Nov 15, 2021

Summary

Updates the facade for function declarations and function expression to match the grammar defined in #1719.

  • Function declaration
    • Rename from FnDecl to JsFunctionDeclaration
    • Rename ParameterList to JsParameterList
    • Introduce new JsIdentifierBinding type for id
    • Parses ...rest as JS_REST_PARAMETER instead of JS_REST_PATTERN.
    • Split out TsReturnType for the : and the return type
  • Function expression
    • Rename from FnExpr to JsFunctionExpression
    • Otherwise the same as for declaration
  • Arrow function expression
    • Rename from ArrowExpr to JsArrowFunctionExpression
    • Rename ExprOrBlock to JsAnyArrowFunctionBody
    • Otherwise the same as for declaration.

This PR cleans up some of the function parsing (extracts helpers rather than using boolean params). This improves error reporting for the func_decl use case if the name is missing.

This PR also removes the support for decorators. The parser had some support for decorators but they haven't been part of the grammar. We should re-add them when we decide to properly support them (and add tests)

Part of #1725

Test Plan

Verified that the coverage isn't changing.

Comment on lines -918 to -928
let m = p.start();
p.bump_remap(T![async]);
let mut complete = function_decl(
&mut *p.with_state(ParserState {
in_async: true,
..p.state.clone()
}),
m,
true,
);
complete.change_kind(p, FN_EXPR);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We repetitively parsed the async keyword. I decided to move the consumption of the async keyword into function_expression/function_declaration. This fixes multiple issues where the async identifier wasn't correctly re-mapped to the async keyword.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are there new tests for this new logic? I get we fixed some bugs, but are they reflected inside some test?

Comment on lines +87 to +91
// TODO 1725 This is probably not ideal (same with the `declare` keyword). We should
// use a different AST type for function declarations. For example, a function declaration should
// never have a body but that would be allowed with this approach. Same for interfaces, interface
// methods should never have a body
/// Either parses a typescript declaration body or the function body
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I only copied this method which is why I didn't address the declare problem.

@MichaReiser MichaReiser changed the title Function AST Facade Function Expression/Declaration AST Facade Nov 15, 2021
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 15, 2021

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 834e34a
Status: ✅  Deploy successful!
Preview URL: https://fd7cfb27.tools-8rn.pages.dev

View logs

p.eat(T![declare]);
}

let in_async = p.at(T![ident]) && p.cur_src() == "async";
Copy link
Contributor

@xunilrj xunilrj Nov 15, 2021

Choose a reason for hiding this comment

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

Do you think makes sense to have eat_if and bump_remap_if methods?

let in_async = p.bump_remap_if(T![async], p.at(T![ident]) && p.cur_src() == "async");
// let in_async = p.eat_if(T![ident], p.cur_src() == "async");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I personally like the explicit control flow of the if, I find this clearer than too inline condition. However, something like that may come in handy when I work on signalling the "missing" slots but I don't know exactly how the API would look like.

That's why I prefer to defer to delay adding any such methods until I have a better understanding of the requirements.

@MichaReiser MichaReiser mentioned this pull request Nov 16, 2021
47 tasks
@MichaReiser MichaReiser force-pushed the feature/function-facade branch from c64a86e to c650a2b Compare November 16, 2021 07:23
Base automatically changed from feature/inline-decl to main November 16, 2021 07:24
* Rename `FnDecl` to `JsFunctionDeclaration`
* Rename `ParameterList` to `JsParameterList`
* Introduce `TsReturnType` to group the `: Type`
* Restructure parsing implementation of function decl/expression
* Introduce `JsRestParameter`
@MichaReiser MichaReiser force-pushed the feature/function-facade branch from c650a2b to 3281bb3 Compare November 16, 2021 07:25
@MichaReiser MichaReiser merged commit 30b8e43 into main Nov 16, 2021
@MichaReiser MichaReiser deleted the feature/function-facade branch November 16, 2021 07:37
Comment on lines -918 to -928
let m = p.start();
p.bump_remap(T![async]);
let mut complete = function_decl(
&mut *p.with_state(ParserState {
in_async: true,
..p.state.clone()
}),
m,
true,
);
complete.change_kind(p, FN_EXPR);
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there new tests for this new logic? I get we fixed some bugs, but are they reflected inside some test?

block_impl(p, JS_FUNCTION_BODY, None)
}

// TODO 1725 This is probably not ideal (same with the `declare` keyword). We should
Copy link
Contributor

Choose a reason for hiding this comment

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

If you add a hash # before the issue number (#1725), it integrates with VSCode/Idea and it will show the issue inside the editor

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh neat!

@MichaReiser
Copy link
Contributor Author

Are there new tests for this new logic? I get we fixed some bugs, but are they reflected inside some test?

Yes, I added new tests

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