Skip to content

Commit

Permalink
fix(grit): fix matching function body (#3810)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Sep 6, 2024
1 parent 5f4a8c5 commit 4e681d1
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
`function $name ($args) {}` as $func where {
$func => `const $name = ($args) => {}`,
`function $name ($args) { $body }` as $func where {
$func => `const $name = ($args) => { $body }`,
$args <: contains `apple` => `mango`
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SnapshotResult {
messages: [],
matched_ranges: [
"1:1-2:2",
"4:1-5:2",
"4:1-6:2",
],
rewritten_files: [],
created_files: [],
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_grit_patterns/tests/specs/ts/functionToArrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ function foo(apple) {
}

function bar(apple, pear) {
console.log("fruits");
}

function baz() {
function baz(pear) {
}
1 change: 1 addition & 0 deletions crates/biome_js_formatter/src/js/any/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl FormatRule<AnyJsStatement> for FormatAnyJsStatement {
AnyJsStatement::JsFunctionDeclaration(node) => node.format().fmt(f),
AnyJsStatement::JsIfStatement(node) => node.format().fmt(f),
AnyJsStatement::JsLabeledStatement(node) => node.format().fmt(f),
AnyJsStatement::JsMetavariable(node) => node.format().fmt(f),
AnyJsStatement::JsReturnStatement(node) => node.format().fmt(f),
AnyJsStatement::JsSwitchStatement(node) => node.format().fmt(f),
AnyJsStatement::JsThrowStatement(node) => node.format().fmt(f),
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_js_parser/src/syntax/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ pub(super) fn parse_parameters_list(
progress.assert_progressing(p);

if parse_metavariable(p).is_present() {
break;
continue;
}

let parameter = parse_parameter(
Expand Down
6 changes: 5 additions & 1 deletion crates/biome_js_parser/src/syntax/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::binding::*;
use super::class::is_at_ts_abstract_class_declaration;
use super::expr::parse_expression;
use super::metavariable::{is_at_metavariable, is_nth_at_metavariable};
use super::metavariable::{is_at_metavariable, is_nth_at_metavariable, parse_metavariable};
use super::module::parse_export;
use super::typescript::*;
use crate::parser::RecoveryResult;
Expand Down Expand Up @@ -909,6 +909,10 @@ pub(crate) fn parse_statements(p: &mut JsParser, stop_on_r_curly: bool, statemen
break;
}

if parse_metavariable(p).is_present() {
continue;
}

if parse_statement(p, StatementContext::StatementList)
.or_recover_with_token_set(
p,
Expand Down
18 changes: 9 additions & 9 deletions crates/biome_js_parser/test_data/inline/ok/metavar.rast
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ JsModule {
l_curly_token: L_CURLY@126..127 "{" [] [],
directives: JsDirectiveList [],
statements: JsStatementList [
JsExpressionStatement {
expression: JsMetavariable {
value_token: GRIT_METAVARIABLE@127..143 "µstatement" [Newline("\n"), Whitespace(" ")] [],
},
JsMetavariable {
value_token: GRIT_METAVARIABLE@127..143 "µstatement" [Newline("\n"), Whitespace(" ")] [],
},
JsEmptyStatement {
semicolon_token: SEMICOLON@143..144 ";" [] [],
},
JsVariableStatement {
Expand Down Expand Up @@ -269,11 +269,11 @@ JsModule {
0: L_CURLY@126..127 "{" [] []
1: JS_DIRECTIVE_LIST@127..127
2: JS_STATEMENT_LIST@127..174
0: JS_EXPRESSION_STATEMENT@127..144
0: JS_METAVARIABLE@127..143
0: GRIT_METAVARIABLE@127..143 "µstatement" [Newline("\n"), Whitespace(" ")] []
1: SEMICOLON@143..144 ";" [] []
1: JS_VARIABLE_STATEMENT@144..174
0: JS_METAVARIABLE@127..143
0: GRIT_METAVARIABLE@127..143 "µstatement" [Newline("\n"), Whitespace(" ")] []
1: JS_EMPTY_STATEMENT@143..144
0: SEMICOLON@143..144 ";" [] []
2: JS_VARIABLE_STATEMENT@144..174
0: JS_VARIABLE_DECLARATION@144..173
0: (empty)
1: CONST_KW@144..155 "const" [Newline("\n"), Whitespace(" ")] [Whitespace(" ")]
Expand Down
19 changes: 19 additions & 0 deletions crates/biome_js_syntax/src/generated/nodes.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xtask/codegen/js.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ AnyJsStatement =
| TsExternalModuleDeclaration
| TsGlobalDeclaration
| TsImportEqualsDeclaration
| JsMetavariable

JsBlockStatement =
'{'
Expand Down

0 comments on commit 4e681d1

Please sign in to comment.