Skip to content

Commit

Permalink
fix(linter): output line/column for --format=stylish instead of off…
Browse files Browse the repository at this point in the history
…set + length (#9136)

closes #9098
  • Loading branch information
Sysix committed Feb 15, 2025
1 parent b68e240 commit 47c1649
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
4 changes: 0 additions & 4 deletions apps/oxlint/src/output_formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ mod test {
Tester::new().with_cwd(TEST_CWD.into()).test_and_snapshot(args);
}

/// disabled for windows
/// stylish will output the offset which will be different for windows
/// when there are multiple lines (`\r\n` vs `\n`)
#[cfg(all(test, not(target_os = "windows")))]
#[test]
fn test_output_formatter_diagnostic_stylish() {
let args = &["--format=stylish", "test.js"];
Expand Down
20 changes: 10 additions & 10 deletions apps/oxlint/src/output_formatter/stylish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ fn format_stylish(diagnostics: &[Error]) -> String {
};
let max_len_width = diagnostics
.iter()
.filter_map(|diagnostic| diagnostic.labels())
.flat_map(std::iter::Iterator::collect::<Vec<_>>)
.map(|label| format!("{}:{}", label.offset(), label.len()).len())
.map(|diagnostic| {
let start = Info::new(diagnostic).start;
format!("{}:{}", start.line, start.column).len()
})
.max()
.unwrap_or(0);

Expand All @@ -83,13 +84,12 @@ fn format_stylish(diagnostics: &[Error]) -> String {
"\u{1b}[33mwarning\u{1b}[0m"
};

if let Some(label) = diagnostic.labels().expect("should have labels").next() {
let rule = diagnostic.code().map_or_else(String::new, |code| code.to_string());
let position = format!("{}:{}", label.offset(), label.len());
output.push_str(
&format!(" \u{1b}[2m{position:max_len_width$}\u{1b}[0m {severity_str} {diagnostic} \u{1b}[2m{rule}\u{1b}[0m\n"),
);
}
let info = Info::new(diagnostic);
let rule = diagnostic.code().map_or_else(String::new, |code| code.to_string());
let position = format!("{}:{}", info.start.line, info.start.column);
output.push_str(
&format!(" \u{1b}[2m{position:max_len_width$}\u{1b}[0m {severity_str} {diagnostic} \u{1b}[2m{rule}\u{1b}[0m\n"),
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ working directory: fixtures/output_formatter_diagnostic
----------

test.js
[2m9:3 [0m [33mwarning[0m Function 'foo' is declared but never used. [2meslint(no-unused-vars)[0m
[2m16:1[0m [33mwarning[0m Parameter 'b' is declared but never used. Unused parameters should start with a '_'. [2meslint(no-unused-vars)[0m
[2m38:9[0m [31merror[0m `debugger` statement is not allowed [2meslint(no-debugger)[0m
[2m1:10[0m [33mwarning[0m Function 'foo' is declared but never used. [2meslint(no-unused-vars)[0m
[2m1:17[0m [33mwarning[0m Parameter 'b' is declared but never used. Unused parameters should start with a '_'. [2meslint(no-unused-vars)[0m
[2m5:1 [0m [31merror[0m `debugger` statement is not allowed [2meslint(no-debugger)[0m

3 problems (1 error, 2 warnings)
----------
Expand Down

0 comments on commit 47c1649

Please sign in to comment.