Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"categories": {
"correctness": "off"
},
// This is a comment in JSONC format
"rules": {
// Deny debugger statements
"no-debugger": "error"
}
}
4 changes: 4 additions & 0 deletions apps/oxlint/fixtures/jsonc_config_auto_detection/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
debugger;

// this `console.log` should be allowed, because correctness is turned off, and only `debugger` is enabled
console.log("");
6 changes: 6 additions & 0 deletions apps/oxlint/fixtures/jsonc_config_explicit/config.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
// Test explicit config with comments
"rules": {
"no-console": "warn" // Warn on console usage
}
}
1 change: 1 addition & 0 deletions apps/oxlint/fixtures/jsonc_config_explicit/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("hello");
6 changes: 6 additions & 0 deletions apps/oxlint/fixtures/jsonc_nested_config/.oxlintrc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
// Root config
"rules": {
"no-debugger": "error"
}
}
3 changes: 3 additions & 0 deletions apps/oxlint/fixtures/jsonc_nested_config/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
debugger;

console.log("don't report here as i'm not enabled");
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
// Nested config
"rules": {
"no-console": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("test");
4 changes: 2 additions & 2 deletions apps/oxlint/src/command/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ impl LintCommand {
#[derive(Debug, Clone, Bpaf)]
pub struct BasicOptions {
/// Oxlint configuration file (experimental)
/// * only `.json` extension is supported
/// * `.json` and `.jsonc` extensions are supported
/// * you can use comments in configuration files.
/// * tries to be compatible with the ESLint v8's format
///
/// If not provided, Oxlint will look for `.oxlintrc.json` in the current working directory.
/// If not provided, Oxlint will look for `.oxlintrc.jsonc` or `.oxlintrc.json` in the current working directory.
#[bpaf(long, short, argument("./.oxlintrc.json"))]
pub config: Option<PathBuf>,

Expand Down
55 changes: 46 additions & 9 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ impl CliRunner {

impl CliRunner {
const DEFAULT_OXLINTRC: &'static str = ".oxlintrc.json";
const DEFAULT_OXLINTRC_JSONC: &'static str = ".oxlintrc.jsonc";

#[must_use]
pub fn with_cwd(mut self, cwd: PathBuf) -> Self {
Expand Down Expand Up @@ -572,25 +573,41 @@ impl CliRunner {
// when no config is provided, it will search for the default file names in the current working directory
// when no file is found, the default configuration is returned
fn find_oxlint_config(cwd: &Path, config: Option<&PathBuf>) -> Result<Oxlintrc, OxcDiagnostic> {
let path: &Path = config.map_or(Self::DEFAULT_OXLINTRC.as_ref(), PathBuf::as_ref);
let full_path = cwd.join(path);

if config.is_some() || full_path.exists() {
if let Some(path) = config {
let full_path = cwd.join(path);
return Oxlintrc::from_file(&full_path);
}

// Try .oxlintrc.jsonc first, then .oxlintrc.json
let jsonc_path = cwd.join(Self::DEFAULT_OXLINTRC_JSONC);
if jsonc_path.exists() {
return Oxlintrc::from_file(&jsonc_path);
}

let json_path = cwd.join(Self::DEFAULT_OXLINTRC);
if json_path.exists() {
return Oxlintrc::from_file(&json_path);
}
Comment on lines +581 to +590
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the repeated config file detection logic into a helper function to reduce duplication between find_oxlint_config and find_oxlint_config_in_directory. Both functions implement the same priority order (.jsonc then .json) with similar structure.

Copilot uses AI. Check for mistakes.


Ok(Oxlintrc::default())
}

/// Looks in a directory for an oxlint config file, returns the oxlint config if it exists
/// and returns `Err` if none exists or the file is invalid. Does not apply the default
/// config file.
fn find_oxlint_config_in_directory(dir: &Path) -> Result<Option<Oxlintrc>, OxcDiagnostic> {
let possible_config_path = dir.join(Self::DEFAULT_OXLINTRC);
if possible_config_path.is_file() {
Oxlintrc::from_file(&possible_config_path).map(Some)
} else {
Ok(None)
// Try .oxlintrc.jsonc first, then .oxlintrc.json
let jsonc_path = dir.join(Self::DEFAULT_OXLINTRC_JSONC);
if jsonc_path.is_file() {
return Oxlintrc::from_file(&jsonc_path).map(Some);
}

let json_path = dir.join(Self::DEFAULT_OXLINTRC);
if json_path.is_file() {
return Oxlintrc::from_file(&json_path).map(Some);
}

Comment on lines +581 to +609
Copy link

Copilot AI Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This code duplicates the same logic as in find_oxlint_config. Consider extracting this into a helper function that takes a directory path and returns the first existing config file path, or None if neither exists.

Copilot uses AI. Check for mistakes.

Ok(None)
}
}

Expand Down Expand Up @@ -1276,6 +1293,26 @@ mod test {
.test_and_snapshot(args);
}

#[test]
fn test_jsonc_config_auto_detection() {
let args = &["test.js"];
Tester::new()
.with_cwd("fixtures/jsonc_config_auto_detection".into())
.test_and_snapshot(args);
}

#[test]
fn test_jsonc_config_explicit() {
let args = &["-c", "config.jsonc", "test.js"];
Tester::new().with_cwd("fixtures/jsonc_config_explicit".into()).test_and_snapshot(args);
}

#[test]
fn test_jsonc_nested_config() {
let args = &["."];
Tester::new().with_cwd("fixtures/jsonc_nested_config".into()).test_and_snapshot(args);
}

// ToDo: `tsgolint` does not support `big-endian`?
#[test]
#[cfg(not(target_endian = "big"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: apps/oxlint/src/tester.rs
---
##########
arguments: test.js
working directory: fixtures/jsonc_config_auto_detection
----------

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:1:1]
1 | debugger;
: ^^^^^^^^^
`----
help: Remove the debugger statement

Found 0 warnings and 1 error.
Finished in <variable>ms on 1 file using 1 threads.
----------
CLI result: LintFoundErrors
----------
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: apps/oxlint/src/tester.rs
---
##########
arguments: -c config.jsonc test.js
working directory: fixtures/jsonc_config_explicit
----------

! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-console.html\eslint(no-console)]8;;\: Unexpected console statement.
,-[test.js:1:1]
1 | console.log("hello");
: ^^^^^^^^^^^
`----
help: Delete this console statement.

Found 1 warning and 0 errors.
Finished in <variable>ms on 1 file with 90 rules using 1 threads.
----------
CLI result: LintSucceeded
----------
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: apps/oxlint/src/tester.rs
---
##########
arguments: .
working directory: fixtures/jsonc_nested_config
----------

x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html\eslint(no-debugger)]8;;\: `debugger` statement is not allowed
,-[root.js:1:1]
1 | debugger;
: ^^^^^^^^^
`----
help: Remove the debugger statement

x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-console.html\eslint(no-console)]8;;\: Unexpected console statement.
,-[subdir/nested.js:1:1]
1 | console.log("test");
: ^^^^^^^^^^^
`----
help: Delete this console statement.

Found 0 warnings and 2 errors.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: LintFoundErrors
----------
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/config/oxlintrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{
///
/// ::: danger NOTE
///
/// Only the `.json` format is supported. You can use comments in configuration files.
/// Both `.json` and `.jsonc` formats are supported. You can use comments in configuration files.
///
/// :::
///
Expand Down
Loading