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

fix(rome_js_formatter): Fix arrow binding parameter in test call #3399

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions crates/rome_js_formatter/src/js/bindings/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::js::lists::parameter_list::{
AnyParameter, FormatJsAnyParameterList, JsAnyParameterList,
};

use crate::utils::test_call::is_test_call_expression;
use crate::utils::test_call::is_test_call_argument;
use rome_js_syntax::{
JsAnyConstructorParameter, JsAnyFormalParameter, JsCallExpression, JsConstructorParameters,
JsParameters, JsSyntaxKind, JsSyntaxToken, TsType,
JsAnyConstructorParameter, JsAnyFormalParameter, JsConstructorParameters, JsParameters,
JsSyntaxToken, TsType,
};
use rome_rowan::{declare_node_union, SyntaxResult};

Expand Down Expand Up @@ -129,29 +129,10 @@ impl FormatJsAnyParameters {
/// Returns `true` for function parameters if the function is an argument of a [test `CallExpression`](is_test_call_expression).
fn is_in_test_call(&self) -> SyntaxResult<bool> {
let result = match self {
FormatJsAnyParameters::JsParameters(parameters) => {
match parameters.syntax().grand_parent() {
Some(function_parent) => match function_parent.kind() {
JsSyntaxKind::JS_CALL_ARGUMENT_LIST => {
let arguments = function_parent.parent();
let call_expression = arguments.and_then(|args| args.parent());

match call_expression {
Some(call_expression)
if JsCallExpression::can_cast(call_expression.kind()) =>
{
is_test_call_expression(&JsCallExpression::unwrap_cast(
call_expression,
))?
}
_ => false,
}
}
_ => false,
},
None => false,
}
}
FormatJsAnyParameters::JsParameters(parameters) => match parameters.syntax().parent() {
Some(function) => is_test_call_argument(&function)?,
None => false,
},
FormatJsAnyParameters::JsConstructorParameters(_) => false,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::parentheses::{
update_or_lower_expression_needs_parentheses, NeedsParentheses,
};
use crate::utils::function_body::{FormatMaybeCachedFunctionBody, FunctionBodyCacheMode};
use crate::utils::test_call::is_test_call_argument;
use crate::utils::{
resolve_left_most_expression, AssignmentLikeLayout, JsAnyBinaryLikeLeftExpression,
};
Expand Down Expand Up @@ -222,17 +223,25 @@ fn format_signature(
write!(f, [arrow.type_parameters().format()])?;

match arrow.parameters()? {
JsAnyArrowFunctionParameters::JsAnyBinding(binding) => write!(
f,
[
text("("),
&soft_block_indent(&format_args![
binding.format(),
if_group_breaks(&text(","))
]),
text(")")
]
)?,
JsAnyArrowFunctionParameters::JsAnyBinding(binding) => {
let should_hug = is_test_call_argument(arrow.syntax())?;

write!(f, [text("(")])?;

if should_hug {
write!(f, [binding.format()])?;
} else {
write!(
f,
[&soft_block_indent(&format_args![
binding.format(),
if_group_breaks(&text(","))
])]
)?
}

write!(f, [text(")")])?;
}
JsAnyArrowFunctionParameters::JsParameters(params) => {
write!(f, [params.format()])?;
}
Expand Down
12 changes: 12 additions & 0 deletions crates/rome_js_formatter/src/utils/test_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ use crate::prelude::*;
use rome_js_syntax::{
JsAnyArrowFunctionParameters, JsAnyCallArgument, JsAnyExpression, JsAnyFunctionBody,
JsAnyLiteralExpression, JsAnyName, JsCallArgumentList, JsCallArguments, JsCallExpression,
JsSyntaxNode,
};
use rome_rowan::{SyntaxResult, SyntaxTokenText};

/// Returns `Ok(true)` if `maybe_argument` is an argument of a [test call expression](is_test_call_expression).
pub(crate) fn is_test_call_argument(maybe_argument: &JsSyntaxNode) -> SyntaxResult<bool> {
let call_expression = maybe_argument
.parent()
.and_then(JsCallArgumentList::cast)
.and_then(|args| args.syntax().grand_parent())
.and_then(JsCallExpression::cast);

call_expression.map_or(Ok(false), |call| is_test_call_expression(&call))
}

/// This is a specialised function that checks if the current [call expression]
/// resembles a call expression usually used by a testing frameworks.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
it('should have the default duration when using the onClose arguments', done => {
expect(true);
done();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
source: crates/rome_js_formatter/tests/spec_test.rs
expression: arrow_test_callback.js
---
# Input
it('should have the default duration when using the onClose arguments', done => {
expect(true);
done();
});

=============================
# Outputs
## Output 1
-----
Indent style: Tab
Line width: 80
Quote style: Double Quotes
Quote properties: As needed
-----
it("should have the default duration when using the onClose arguments", (done) => {
expect(true);
done();
});


## Lines exceeding width of 80 characters

1: it("should have the default duration when using the onClose arguments", (done) => {

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载