mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
fix(formatter_json): normalize CRLF for suppressed text (#23702)
The same as #23701, for JSON formatter.
This commit is contained in:
1 parent
7cd17370b0
commit
f21ed2c112
5 files changed
+84
-2
No files matched your search
@@ -1,5 +1,7 @@
|
||||
crates/oxc_formatter/tests/fixtures/** text=auto eol=lf
|
||||
crates/oxc_formatter/tests/fixtures/js/crlf/** text=auto eol=crlf
|
||||
crates/oxc_formatter_json/tests/fixtures/** text=auto eol=lf
|
||||
crates/oxc_formatter_json/tests/fixtures/jsonc/crlf/** text=auto eol=crlf
|
||||
apps/oxfmt/test/**/fixtures/** text=auto eol=lf
|
||||
apps/oxlint/test/fixtures/** text=auto eol=lf
|
||||
|
||||
|
||||
@@ -3,8 +3,9 @@ use std::cell::Cell;
|
||||
use oxc_allocator::StringBuilder;
|
||||
use oxc_ast::Comment;
|
||||
use oxc_formatter_core::{
|
||||
Buffer, Format, SourceText,
|
||||
Buffer, Format, LINE_TERMINATORS, SourceText, arena_cow_str,
|
||||
builders::{empty_line, expand_parent, hard_line_break, line_suffix, space, text},
|
||||
normalize_newlines,
|
||||
spec::is_suppression_marker,
|
||||
write,
|
||||
};
|
||||
@@ -310,7 +311,11 @@ pub struct FormatSuppressedNode(pub Span);
|
||||
impl<'a> Format<'a, JsonFormatContext<'a>> for FormatSuppressedNode {
|
||||
fn fmt(&self, f: &mut JsonFormatter<'_, 'a>) {
|
||||
write!(f, FormatLeadingComments(self.0));
|
||||
write!(f, text(f.context().source_text().text_for(&self.0)));
|
||||
// The IR only supports `\n` as a line break. Normalize CRLF / CR / LS / PS to LF;
|
||||
// the printer will re-emit the configured `LineEnding` at the final stage.
|
||||
let raw = f.context().source_text().text_for(&self.0);
|
||||
let normalized = normalize_newlines(raw, LINE_TERMINATORS);
|
||||
write!(f, text(arena_cow_str(&normalized, f)));
|
||||
// The verbatim text already includes inside-span comments;
|
||||
// advance the cursor so they aren't re-emitted as leading comments of a later node.
|
||||
let _ = f.context().comments().take_before(self.0.end);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"a": 1,
|
||||
// prettier-ignore
|
||||
"b": {
|
||||
"c": 2
|
||||
},
|
||||
"d": [1, 2, 3]
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
---
|
||||
source: crates/oxc_formatter_json/tests/fixtures/mod.rs
|
||||
---
|
||||
==================== Input ====================
|
||||
{
|
||||
"a": 1,
|
||||
// prettier-ignore
|
||||
"b": {
|
||||
"c": 2
|
||||
},
|
||||
"d": [1, 2, 3]
|
||||
}
|
||||
|
||||
==================== Output ====================
|
||||
-----------------------------------------------------
|
||||
{ endOfLine: "lf", printWidth: 80, variant: "jsonc" }
|
||||
-----------------------------------------------------
|
||||
{
|
||||
"a": 1,
|
||||
// prettier-ignore
|
||||
"b": {
|
||||
"c": 2
|
||||
},
|
||||
"d": [1, 2, 3],
|
||||
}
|
||||
|
||||
------------------------------------------------------
|
||||
{ endOfLine: "lf", printWidth: 100, variant: "jsonc" }
|
||||
------------------------------------------------------
|
||||
{
|
||||
"a": 1,
|
||||
// prettier-ignore
|
||||
"b": {
|
||||
"c": 2
|
||||
},
|
||||
"d": [1, 2, 3],
|
||||
}
|
||||
|
||||
-------------------------------------------------------
|
||||
{ endOfLine: "crlf", printWidth: 80, variant: "jsonc" }
|
||||
-------------------------------------------------------
|
||||
{
|
||||
"a": 1,
|
||||
// prettier-ignore
|
||||
"b": {
|
||||
"c": 2
|
||||
},
|
||||
"d": [1, 2, 3],
|
||||
}
|
||||
|
||||
--------------------------------------------------------
|
||||
{ endOfLine: "crlf", printWidth: 100, variant: "jsonc" }
|
||||
--------------------------------------------------------
|
||||
{
|
||||
"a": 1,
|
||||
// prettier-ignore
|
||||
"b": {
|
||||
"c": 2
|
||||
},
|
||||
"d": [1, 2, 3],
|
||||
}
|
||||
|
||||
===================== End =====================
|
||||
@@ -0,0 +1,4 @@
|
||||
[
|
||||
{ "variant": "jsonc", "endOfLine": "lf" },
|
||||
{ "variant": "jsonc", "endOfLine": "crlf" }
|
||||
]
|
||||
Reference in new issue
Block a user