fix(rome_js_formatter): number with trailing whitespace
MODIFY
crates/rome_js_formatter/src/js/expressions/static_member_expression.rs
crates/rome_js_formatter/src/js/expressions/static_member_expression.rs
-use crate::{format_elements, group_elements, Format, FormatElement, FormatNode, Formatter};
+use rome_js_syntax::JsSyntaxKind;
+use rome_rowan::AstNode;
+
+use crate::{format_elements, group_elements, token, Format, FormatElement, FormatNode, Formatter};
use rome_formatter::FormatResult;
use rome_js_syntax::JsStaticMemberExpression;
@@ -12,10 +15,36 @@ impl FormatNode for JsStaticMemberExpression {
member,
} = self.as_fields();
+ let object_syntax = object.clone()?.syntax().clone();
+
+ let is_object_number_literal =
+ object_syntax.kind() == JsSyntaxKind::JS_NUMBER_LITERAL_EXPRESSION;
+
+ let has_object_trailing_trivia =
+ object_syntax.last_trailing_trivia().unwrap().pieces().len() > 0;
+ let has_operator_leading_trivia =
+ operator_token.clone()?.leading_trivia().pieces().len() > 0;
+
+ let formatted_object = object?.format(formatter)?;
+
+ if is_object_number_literal && (has_object_trailing_trivia || has_operator_leading_trivia) {
+ let (object_leading, object_content, object_trailing) = formatted_object.split_trivia();
+
Ok(group_elements(format_elements![
- object.format(formatter)?,
+ object_leading,
+ token("("),
+ object_content,
+ token(")"),
+ object_trailing,
operator_token.format(formatter)?,
member.format(formatter)?,
]))
+ } else {
+ Ok(group_elements(format_elements![
+ formatted_object,
+ operator_token.format(formatter)?,
+ member.format(formatter)?,
+ ]))
+ }
}
}

ADD
crates/rome_js_formatter/tests/specs/js/module/number/number-with-space.js
+123 .toString
+123
+/****/.toString
+123/**/.toString
+123
+.toString

ADD
crates/rome_js_formatter/tests/specs/js/module/number/number-with-space.js.snap
+---
+source: crates/rome_js_formatter/tests/spec_test.rs
+expression: number-with-space.js
+---
+# Input
+123 .toString
+123
+/****/.toString
+123/**/.toString
+123
+.toString
+
+=============================
+# Outputs
+## Output 1
+-----
+Indent style: Tab
+Line width: 80
+Quote style: Double Quotes
+-----
+(123).toString;
+(123)
+/****/ .toString;
+(123) /**/ .toString;
+(123).toString;
+

fix(rome_js_formatter): number with trailing whitespace (#2519)

Co-authored-by: Mark <mark.ladyshau@olx.pl>