fix(rome_js_formatter): Match test function names on trimmed text
MODIFY
crates/rome_js_formatter/src/js/expressions/call_arguments.rs
crates/rome_js_formatter/src/js/expressions/call_arguments.rs
@@ -43,16 +43,22 @@ impl FormatNodeRule<JsCallArguments> for FormatJsCallArguments {
let first_argument = first_argument?;
let second_argument = second_argument?;
- if let Some(call_expression) = node.syntax().parent().and_then(JsCallExpression::cast) {
+ let is_framework_test_call = if let Some(call_expression) =
+ node.syntax().parent().and_then(JsCallExpression::cast)
+ {
let callee = call_expression.callee()?;
- let is_framework_test_call = is_framework_test_call(IsTestFrameworkCallPayload {
+ is_framework_test_call(IsTestFrameworkCallPayload {
first_argument: &first_argument,
second_argument: &second_argument,
third_argument: &third_argument,
arguments_len,
callee: &callee,
- })?;
+ })?
+ } else {
+ false
+ };
+
let is_react_hook_with_deps_array =
is_react_hook_with_deps_array(&first_argument, &second_argument)?
&& !node.syntax().first_or_last_token_have_comments();
@@ -66,8 +72,7 @@ impl FormatNodeRule<JsCallArguments> for FormatJsCallArguments {
f.join_with(space_token()).entries(separated).finish()?;
return write!(f, [r_paren_token.format()]);
}
- }
- }
+ };
// we create open a close delimiters
let open_delimiter = format_open_delimiter(&l_paren_token);
@@ -605,14 +610,14 @@ fn matches_test_call(callee: &JsAnyExpression) -> SyntaxResult<Vec<SyntaxTokenTe
for _ in 0..MAX_DEPTH {
if let JsAnyExpression::JsIdentifierExpression(identifier) = &current_node {
let value_token = identifier.name()?.value_token()?;
- let value = value_token.token_text();
+ let value = value_token.token_text_trimmed();
test_call.push(value);
break;
} else if let JsAnyExpression::JsStaticMemberExpression(member_expression) = &current_node {
match member_expression.member()? {
JsAnyName::JsName(name) => {
let value = name.value_token()?;
- test_call.push(value.token_text());
+ test_call.push(value.token_text_trimmed());
current_node = member_expression.object()?;
}
_ => break,
@@ -621,8 +626,8 @@ fn matches_test_call(callee: &JsAnyExpression) -> SyntaxResult<Vec<SyntaxTokenTe
break;
}
}
-
- Ok(test_call.into_iter().rev().collect())
+ test_call.reverse();
+ Ok(test_call)
}
#[cfg(test)]
@@ -662,6 +667,12 @@ mod test {
contains_a_test_pattern(&call_expression.callee().unwrap()),
Ok(true)
);
+
+ let call_expression = extract_call_expression("it();");
+ assert_eq!(
+ contains_a_test_pattern(&call_expression.callee().unwrap()),
+ Ok(true)
+ );
}
#[test]

MODIFY
crates/rome_js_formatter/src/lib.rs
crates/rome_js_formatter/src/lib.rs
@@ -473,9 +473,10 @@ mod test {
fn quick_test() {
let src = r#"
-describe.each([1, 2, 3])("test", (a) => {
- expect(a).toBe(a);
+it(`does something really long and complicated so I have to write a very long name for the test`, function () {
+ console.log("hello!");
});
+
"#;
let syntax = SourceType::tsx();
let tree = parse(src, 0, syntax);

MODIFY
crates/rome_js_formatter/tests/specs/prettier/js/test-declarations/test_declarations.js.snap
crates/rome_js_formatter/tests/specs/prettier/js/test-declarations/test_declarations.js.snap
@@ -324,10 +324,7 @@ xskip(
() => {},
);
-test.describe.only.parallel(
- "does something really long and complicated so I have to write a very long name for the test",
- () => {},
-);
+test.describe.only.parallel("does something really long and complicated so I have to write a very long name for the test", () => {});
test.describe.parallel.serial(
"does something really long and complicated so I have to write a very long name for the testThis is a very",
@@ -414,10 +411,10 @@ it(
135: "does something really long and complicated so I have to write a very long name for the test",
142: "does something really long and complicated so I have to write a very long name for the test",
149: "does something really long and complicated so I have to write a very long name for the test",
- 154: "does something really long and complicated so I have to write a very long name for the test",
- 159: "does something really long and complicated so I have to write a very long name for the testThis is a very",
- 164: "does something really long and complicated so I have to write a very long name for the test",
- 169: "does something really long and complicated so I have to write a very long name for the test",
- 178: does something really long and complicated so I have to write a very long name for the test`, () => {
+ 153: test.describe.only.parallel("does something really long and complicated so I have to write a very long name for the test", () => {});
+ 156: "does something really long and complicated so I have to write a very long name for the testThis is a very",
+ 161: "does something really long and complicated so I have to write a very long name for the test",
+ 166: "does something really long and complicated so I have to write a very long name for the test",
+ 175: does something really long and complicated so I have to write a very long name for the test`, () => {
```

fix(rome_js_formatter): Match test function names on trimmed text (#2777)