Fix(rome_js_formatter):regex flag order
MODIFY
crates/rome_js_formatter/src/js/expressions/regex_literal_expression.rs
crates/rome_js_formatter/src/js/expressions/regex_literal_expression.rs
use crate::{Format, FormatElement, FormatNode, Formatter};
use rome_formatter::FormatResult;
+use rome_formatter::Token;
use rome_js_syntax::JsRegexLiteralExpression;
use rome_js_syntax::JsRegexLiteralExpressionFields;
impl FormatNode for JsRegexLiteralExpression {
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
let JsRegexLiteralExpressionFields { value_token } = self.as_fields();
+ let value_token = value_token?;
+ let trimmed_raw_string = value_token.text_trimmed();
+ // find end slash, so we could split our regex literal to two part `body`: raw_string[0..end_slash_pos + 1] and `flags`: raw_string[end_slash_pos + 1..]
+ // reference https://tc39.es/ecma262/#prod-RegularExpressionLiteral
+ let ends_with_slash = trimmed_raw_string.ends_with('/');
+ // this means that we have a regex literal with no flags
+ if ends_with_slash {
+ return value_token.format(formatter);
+ }
+ // SAFETY: a valid regex literal must have a end slash
+ let end_slash_pos = trimmed_raw_string.rfind('/').unwrap();
+ let mut flag_char_vec = trimmed_raw_string[end_slash_pos + 1..]
+ .chars()
+ .collect::<Vec<_>>();
+ flag_char_vec.sort_unstable();
+ let sorted_flag_string = flag_char_vec.iter().collect::<String>();
- value_token.format(formatter)
+ let sorted_regex_literal = Token::from_syntax_token_cow_slice(
+ std::borrow::Cow::Owned(format!(
+ "{}{}",
+ &trimmed_raw_string[0..end_slash_pos + 1],
+ sorted_flag_string
+ )),
+ &value_token,
+ value_token.text_trimmed_range().start(),
+ );
+ Ok(formatter.format_replaced(&value_token, sorted_regex_literal.into()))
}
}

MODIFY
crates/rome_js_formatter/tests/specs/js/module/expression/literal_expression.js
crates/rome_js_formatter/tests/specs/js/module/expression/literal_expression.js
true
false
null
+;/[/]\/\u0aBc/mgi;
\ No newline at end of file

MODIFY
crates/rome_js_formatter/tests/specs/js/module/expression/literal_expression.js.snap
crates/rome_js_formatter/tests/specs/js/module/expression/literal_expression.js.snap
---
source: crates/rome_js_formatter/tests/spec_test.rs
+assertion_line: 242
expression: literal_expression.js
+
---
# Input
"a"
@@ -9,7 +11,7 @@ expression: literal_expression.js
true
false
null
-
+;/[/]\/\u0aBc/mgi;
=============================
# Outputs
## Output 1
@@ -24,4 +26,5 @@ Quote style: Double Quotes
true;
false;
null;
+/[/]\/\u0aBc/gim;

MODIFY
crates/rome_js_formatter/tests/specs/prettier/js/regex/test.js.snap
crates/rome_js_formatter/tests/specs/prettier/js/regex/test.js.snap
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
-assertion_line: 57
+assertion_line: 175
expression: test.js
---
@@ -12,7 +12,7 @@ expression: test.js
# Output
```js
-/[/]\/\u0aBc/mgi;
+/[/]\/\u0aBc/gim;
```

Fix(rome_js_formatter):regex flag order (#2505)