Skip to content
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
crates/oxc_formatter/tests/fixtures/** text=auto eol=lf
crates/oxc_formatter/tests/fixtures/js/crlf text=auto eol=crlf
apps/oxfmt/test/fixtures/** text=auto eol=lf
apps/oxlint/test/fixtures/** text=auto eol=lf
47 changes: 28 additions & 19 deletions crates/oxc_formatter/src/formatter/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
//! 2. Applies indentation based on container type (block, soft, none)
//! 3. Preserves comment relationships and spacing
//! 4. Advances cursor for processed comments
use oxc_allocator::StringBuilder;
use oxc_ast::{Comment, CommentContent, CommentKind};
use oxc_span::Span;
use oxc_syntax::line_terminator::LineTerminatorSplitter;

use crate::write;

Expand Down Expand Up @@ -427,20 +429,32 @@ impl<'a> Format<'a> for FormatDanglingComments<'a> {

impl<'a> Format<'a> for Comment {
fn fmt(&self, f: &mut Formatter<'_, 'a>) {
let source_text = f.source_text().text_for(&self.span).trim_end();
if is_alignable_comment(source_text) {
let mut lines = source_text.lines();

// `is_alignable_comment` only returns `true` for multiline comments
let first_line = lines.next().unwrap();
write!(f, [text(first_line.trim_end())]);

// Indent the remaining lines by one space so that all `*` are aligned.
for line in lines {
write!(f, [hard_line_break(), " ", text(line.trim())]);
let content = f.source_text().text_for(&self.span);
if content.bytes().any(|b| b == b'\n' || b == b'\r') {
let mut lines = LineTerminatorSplitter::new(content);
if is_alignable_comment(content) {
// `unwrap` is safe because `content` contains at least one line.
let first_line = lines.next().unwrap();
write!(f, [text(first_line.trim_end())]);

// Indent the remaining lines by one space so that all `*` are aligned.
for line in lines {
write!(f, [hard_line_break(), " ", text(line.trim())]);
}
} else {
// Normalize line endings `\r\n` to `\n`
let mut string = StringBuilder::with_capacity_in(content.len(), f.allocator());
// `unwrap` is safe because `content` contains at least one line.
string.push_str(lines.next().unwrap().trim_end());

for str in lines {
string.push('\n');
string.push_str(str);
}
write!(f, [text(string.into_str())]);
}
} else {
write!(f, [text(source_text)]);
write!(f, [text(content.trim_end())]);
}
}
}
Expand Down Expand Up @@ -475,11 +489,6 @@ impl<'a> Format<'a> for Comment {
/// */
/// "#)));
/// ```
pub fn is_alignable_comment(source_text: &str) -> bool {
if !source_text.contains('\n') {
return false;
}
source_text.lines().enumerate().all(|(index, line)| {
if index == 0 { line.starts_with("/*") } else { line.trim_start().starts_with('*') }
})
pub fn is_alignable_comment(lines: &str) -> bool {
LineTerminatorSplitter::new(lines).skip(1).all(|line| line.trim_start().starts_with('*'))
}
13 changes: 13 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/crlf/block-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
multi
line
block
*/

/**
* js doc
* @type {string}
* @description This is a description.
*/

// single line comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
/*
multi
line
block
*/

/**
* js doc
* @type {string}
* @description This is a description.
*/

// single line comment

==================== Output ====================
-----------------------------------
{ endOfLine: "lf", printWidth: 80 }
-----------------------------------
/*
multi
line
block
*/

/**
* js doc
* @type {string}
* @description This is a description.
*/

// single line comment

------------------------------------
{ endOfLine: "lf", printWidth: 100 }
------------------------------------
/*
multi
line
block
*/

/**
* js doc
* @type {string}
* @description This is a description.
*/

// single line comment

-------------------------------------
{ endOfLine: "crlf", printWidth: 80 }
-------------------------------------
/*
multi
line
block
*/

/**
* js doc
* @type {string}
* @description This is a description.
*/

// single line comment

--------------------------------------
{ endOfLine: "crlf", printWidth: 100 }
--------------------------------------
/*
multi
line
block
*/

/**
* js doc
* @type {string}
* @description This is a description.
*/

// single line comment

===================== End =====================
8 changes: 8 additions & 0 deletions crates/oxc_formatter/tests/fixtures/js/crlf/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"endOfLine": "lf"
},
{
"endOfLine": "crlf"
}
]
12 changes: 11 additions & 1 deletion crates/oxc_formatter/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{env::current_dir, fs, path::Path};
use oxc_allocator::Allocator;
use oxc_formatter::{
ArrowParentheses, BracketSameLine, BracketSpacing, FormatOptions, Formatter, IndentStyle,
IndentWidth, LineWidth, QuoteStyle, Semicolons, TrailingCommas, get_parse_options,
IndentWidth, LineEnding, LineWidth, QuoteStyle, Semicolons, TrailingCommas, get_parse_options,
};
use oxc_parser::Parser;
use oxc_span::SourceType;
Expand Down Expand Up @@ -108,6 +108,16 @@ fn parse_format_options(json: &OptionSet) -> FormatOptions {
options.bracket_same_line = BracketSameLine::from(b);
}
}
"endOfLine" => {
if let Some(s) = value.as_str() {
options.line_ending = match s {
"lf" => LineEnding::Lf,
"crlf" => LineEnding::Crlf,
"cr" => LineEnding::Cr,
_ => LineEnding::default(),
};
}
}
_ => {}
}
}
Expand Down
Loading