Skip to content

Commit

Permalink
fix(rslint_parser): Fix arrow parsing (rome#2284)
Browse files Browse the repository at this point in the history
Allows parsing of single parameter async function where the parameter is named `async`
  • Loading branch information
MichaReiser authored Mar 22, 2022
1 parent 1b303cf commit 001bdaa
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
17 changes: 12 additions & 5 deletions crates/rslint_parser/src/syntax/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,12 +750,19 @@ fn parse_arrow_function_with_single_parameter(p: &mut Parser) -> ParsedSyntax {
}

fn is_arrow_function_with_single_parameter(p: &mut Parser) -> bool {
if p.at(T![async]) && !p.has_nth_preceding_line_break(1) {
is_nth_at_identifier_binding(p, 1)
&& p.nth_at(2, T![=>])
// a => ...
if p.nth_at(1, T![=>]) {
// test single_parameter_arrow_function_with_parameter_named_async
// let id = async => async;
is_at_identifier_binding(p) && !p.has_nth_preceding_line_break(1)
}
// async ident => ...
else {
p.at(T![async])
&& !p.has_nth_preceding_line_break(1)
&& is_nth_at_identifier_binding(p, 1)
&& !p.has_nth_preceding_line_break(2)
} else {
is_at_identifier_binding(p) && p.nth_at(1, T![=>]) && !p.has_nth_preceding_line_break(1)
&& p.nth_at(2, T![=>])
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let id = async => async;
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
JsModule {
interpreter_token: missing (optional),
directives: JsDirectiveList [],
items: JsModuleItemList [
JsVariableStatement {
declaration: JsVariableDeclaration {
kind: [email protected] "let" [] [Whitespace(" ")],
declarators: JsVariableDeclaratorList [
JsVariableDeclarator {
id: JsIdentifierBinding {
name_token: [email protected] "id" [] [Whitespace(" ")],
},
variable_annotation: missing (optional),
initializer: JsInitializerClause {
eq_token: [email protected] "=" [] [Whitespace(" ")],
expression: JsArrowFunctionExpression {
async_token: missing (optional),
type_parameters: missing (optional),
parameters: JsIdentifierBinding {
name_token: [email protected] "async" [] [Whitespace(" ")],
},
return_type_annotation: missing (optional),
fat_arrow_token: [email protected] "=>" [] [Whitespace(" ")],
body: JsIdentifierExpression {
name: JsReferenceIdentifier {
value_token: [email protected] "async" [] [],
},
},
},
},
},
],
},
semicolon_token: [email protected] ";" [] [],
},
],
eof_token: [email protected] "" [Newline("\n")] [],
}

0: [email protected]
0: (empty)
1: [email protected]
2: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "let" [] [Whitespace(" ")]
1: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "id" [] [Whitespace(" ")]
1: (empty)
2: [email protected]
0: [email protected] "=" [] [Whitespace(" ")]
1: [email protected]
0: (empty)
1: (empty)
2: [email protected]
0: [email protected] "async" [] [Whitespace(" ")]
3: (empty)
4: [email protected] "=>" [] [Whitespace(" ")]
5: [email protected]
0: [email protected]
0: [email protected] "async" [] []
1: [email protected] ";" [] []
3: [email protected] "" [Newline("\n")] []

0 comments on commit 001bdaa

Please sign in to comment.