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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion apps/oxlint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ oxc_span = { workspace = true }

bpaf = { workspace = true, features = ["autocomplete", "bright-color", "derive"] }
ignore = { workspace = true, features = ["simd-accel"] }
insta = { workspace = true }
miette = { workspace = true }
rayon = { workspace = true }
rustc-hash = { workspace = true }
Expand All @@ -46,6 +45,10 @@ serde_json = { workspace = true }
tempfile = { workspace = true }
tracing-subscriber = { workspace = true, features = [] } # Omit the `regex` feature

[dev-dependencies]
insta = { workspace = true }
regex = { workspace = true }

[features]
default = []
allocator = ["dep:jemallocator", "dep:mimalloc"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"no-debugger": "error",
"no-unused-vars": "warn"
}
}
5 changes: 5 additions & 0 deletions apps/oxlint/fixtures/output_formatter_diagnostic/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo(a, b) {
return a;
}

debugger;
29 changes: 16 additions & 13 deletions apps/oxlint/src/output_formatter/default.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::time::Duration;

use crate::output_formatter::InternalFormatter;
use oxc_diagnostics::{
reporter::{DiagnosticReporter, DiagnosticResult},
Error, GraphicalReportHandler,
};
use oxc_linter::table::RuleTable;

use crate::output_formatter::InternalFormatter;

#[derive(Debug)]
pub struct DefaultOutputFormatter;

Expand Down Expand Up @@ -59,12 +58,25 @@ struct GraphicalReporter {
handler: GraphicalReportHandler,
}

#[cfg(not(test))]
impl Default for GraphicalReporter {
fn default() -> Self {
Self { handler: GraphicalReportHandler::new() }
}
}

#[cfg(test)]
use oxc_diagnostics::GraphicalTheme;

/// we need to override the GraphicalReport for the tests
/// because our CI can not handle colors output and [`GraphicalReportHandler`] will auto detect the environment
#[cfg(test)]
impl Default for GraphicalReporter {
fn default() -> Self {
Self { handler: GraphicalReportHandler::new_themed(GraphicalTheme::none()) }
}
}

impl DiagnosticReporter for GraphicalReporter {
fn finish(&mut self, result: &DiagnosticResult) -> Option<String> {
let mut output = String::new();
Expand Down Expand Up @@ -103,13 +115,6 @@ impl DiagnosticReporter for GraphicalReporter {
Some(output)
}
}
impl GraphicalReporter {
#[cfg(test)]
pub fn with_handler(mut self, handler: GraphicalReportHandler) -> Self {
self.handler = handler;
self
}
}

#[cfg(test)]
mod test {
Expand All @@ -119,7 +124,7 @@ mod test {
default::{DefaultOutputFormatter, GraphicalReporter},
InternalFormatter, LintCommandInfo,
};
use miette::{GraphicalReportHandler, GraphicalTheme, NamedSource};
use miette::NamedSource;
use oxc_diagnostics::{
reporter::{DiagnosticReporter, DiagnosticResult},
OxcDiagnostic,
Expand Down Expand Up @@ -196,9 +201,7 @@ mod test {

#[test]
fn reporter_error() {
let mut reporter = GraphicalReporter::default().with_handler(
GraphicalReportHandler::new_themed(GraphicalTheme::none()).with_links(false),
);
let mut reporter = GraphicalReporter::default();

let error = OxcDiagnostic::warn("error message")
.with_label(Span::new(0, 8))
Expand Down
50 changes: 50 additions & 0 deletions apps/oxlint/src/output_formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,53 @@ impl OutputFormatter {
self.internal.get_diagnostic_reporter()
}
}

#[cfg(test)]
mod test {
use crate::tester::Tester;

const TEST_CWD: &str = "fixtures/output_formatter_diagnostic";

#[test]
fn test_output_formatter_diagnostic_default() {
let args = &["--format=default", "test.js"];

Tester::new().with_cwd(TEST_CWD.into()).test_and_snapshot(args);
}

/// disabled for windows
/// json 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_json() {
let args = &["--format=json", "test.js"];

Tester::new().with_cwd(TEST_CWD.into()).test_and_snapshot(args);
}

#[test]
fn test_output_formatter_diagnostic_checkstyle() {
let args = &["--format=checkstyle", "test.js"];

Tester::new().with_cwd(TEST_CWD.into()).test_and_snapshot(args);
}

#[test]
fn test_output_formatter_diagnostic_github() {
let args = &["--format=github", "test.js"];

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"];

Tester::new().with_cwd(TEST_CWD.into()).test_and_snapshot(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: apps/oxlint/src/tester.rs
---
##########
--format=checkstyle test.js
----------
<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="test.js"><error line="5" column="1" severity="error" message="`debugger` statement is not allowed" source="" /><error line="1" column="10" severity="warning" message="Function &apos;foo&apos; is declared but never used." source="" /><error line="1" column="17" severity="warning" message="Parameter &apos;b&apos; is declared but never used. Unused parameters should start with a &apos;_&apos;." source="" /></file></checkstyle>
35 changes: 35 additions & 0 deletions apps/oxlint/src/snapshots/--format=default test.js@oxlint.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: apps/oxlint/src/tester.rs
---
##########
--format=default test.js
----------

x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html\eslint(no-debugger)]8;;\: `debugger` statement is not allowed
,-[test.js:5:1]
4 |
5 | debugger;
: ^^^^^^^^^
`----
help: Delete this code.

! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html\eslint(no-unused-vars)]8;;\: Function 'foo' is declared but never used.
,-[test.js:1:10]
1 | function foo(a, b) {
: ^|^
: `-- 'foo' is declared here
2 | return a;
`----
help: Consider removing this declaration.

! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html\eslint(no-unused-vars)]8;;\: Parameter 'b' is declared but never used. Unused parameters should start with a '_'.
,-[test.js:1:17]
1 | function foo(a, b) {
: |
: `-- 'b' is declared here
2 | return a;
`----
help: Consider removing this parameter.

Found 2 warnings and 1 error.
Finished in <variable> on 1 file with 97 rules using <variable>.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: apps/oxlint/src/tester.rs
---
##########
--format=github test.js
----------
::error file=test.js,line=5,endLine=5,col=1,endColumn=10,title=oxlint::`debugger` statement is not allowed
::warning file=test.js,line=1,endLine=1,col=10,endColumn=13,title=oxlint::Function 'foo' is declared but never used.
::warning file=test.js,line=1,endLine=1,col=17,endColumn=18,title=oxlint::Parameter 'b' is declared but never used. Unused parameters should start with a '_'.
11 changes: 11 additions & 0 deletions apps/oxlint/src/snapshots/--format=json test.js@oxlint.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: apps/oxlint/src/tester.rs
---
##########
--format=json test.js
----------
[
{"message": "`debugger` statement is not allowed","code": "eslint(no-debugger)","severity": "error","causes": [],"url": "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html","help": "Delete this code.","filename": "test.js","labels": [{"span": {"offset": 38,"length": 9}}],"related": []},
{"message": "Function 'foo' is declared but never used.","code": "eslint(no-unused-vars)","severity": "warning","causes": [],"url": "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html","help": "Consider removing this declaration.","filename": "test.js","labels": [{"label": "'foo' is declared here","span": {"offset": 9,"length": 3}}],"related": []},
{"message": "Parameter 'b' is declared but never used. Unused parameters should start with a '_'.","code": "eslint(no-unused-vars)","severity": "warning","causes": [],"url": "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html","help": "Consider removing this parameter.","filename": "test.js","labels": [{"label": "'b' is declared here","span": {"offset": 16,"length": 1}}],"related": []}
]
13 changes: 13 additions & 0 deletions apps/oxlint/src/snapshots/--format=stylish test.js@oxlint.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: apps/oxlint/src/tester.rs
---
##########
--format=stylish test.js
----------

test.js
9:3  warning Function 'foo' is declared but never used. eslint(no-unused-vars)
16:1 warning Parameter 'b' is declared but never used. Unused parameters should start with a '_'. eslint(no-unused-vars)
38:9 error `debugger` statement is not allowed eslint(no-debugger)

✖ 3 problems (1 error, 2 warnings)
10 changes: 8 additions & 2 deletions apps/oxlint/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::cli::{lint_command, CliRunResult, LintResult, LintRunner};
#[cfg(test)]
use crate::runner::Runner;
#[cfg(test)]
use regex::Regex;
#[cfg(test)]
use std::{env, path::PathBuf};

#[cfg(test)]
pub struct Tester {
cwd: PathBuf,
Expand Down Expand Up @@ -64,8 +65,13 @@ impl Tester {
settings.set_omit_expression(true);
settings.set_snapshot_suffix("oxlint");

let regex = Regex::new(r"\d+ms|\d+ threads?").unwrap();

let output_string = &String::from_utf8(output).unwrap();
let output_string = regex.replace_all(output_string, "<variable>");

settings.bind(|| {
insta::assert_snapshot!(format!("{}", args_string), String::from_utf8(output).unwrap());
insta::assert_snapshot!(format!("{}", args_string), output_string);
});
}
}
Loading