-
-
Notifications
You must be signed in to change notification settings - Fork 675
feat(linter): support .oxlintrc.jsonc
config files
#14267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
camc314
wants to merge
1
commit into
main
from
c/09-30-feat_linter_support_.oxlintrc.jsonc_config_files
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
apps/oxlint/fixtures/jsonc_config_auto_detection/.oxlintrc.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("hello"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
// Root config | ||
"rules": { | ||
"no-debugger": "error" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
6 changes: 6 additions & 0 deletions
6
apps/oxlint/fixtures/jsonc_nested_config/subdir/.oxlintrc.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
// Nested config | ||
"rules": { | ||
"no-console": "error" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log("test"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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); | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] This code duplicates the same logic as in Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
Ok(None) | ||
} | ||
} | ||
|
||
|
@@ -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"))] | ||
|
20 changes: 20 additions & 0 deletions
20
apps/oxlint/src/snapshots/fixtures__jsonc_config_auto_detection_test.js@oxlint.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
---------- |
20 changes: 20 additions & 0 deletions
20
.../oxlint/src/snapshots/fixtures__jsonc_config_explicit_-c config.jsonc test.js@oxlint.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
---------- |
27 changes: 27 additions & 0 deletions
27
apps/oxlint/src/snapshots/fixtures__jsonc_nested_config_.@oxlint.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andfind_oxlint_config_in_directory
. Both functions implement the same priority order (.jsonc then .json) with similar structure.Copilot uses AI. Check for mistakes.