Skip to content

Commit

Permalink
feat(linter): eslint config jsonc support (#2121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Jan 22, 2024
1 parent ce94714 commit 69fecac
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 69 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ url = { workspace = true }
rust-lapper = "1.1.0"
once_cell = "1.19.0"
memchr = "2.7.1"
json-strip-comments = "1.0.1"

[dev-dependencies]
miette = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/fixtures/eslint_config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
// requires jsonc support
"no-console": "off",
"no-bitwise": [
"error",
Expand All @@ -17,6 +18,6 @@
}
],
"@typescript-eslint/ban-types": "error",
"jsx-a11y/alt-text": "warn"
"jsx-a11y/alt-text": "warn",
}
}
5 changes: 5 additions & 0 deletions crates/oxc_linter/src/config/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ pub struct FailedToParseAllowWarnDenyFromNumberError(pub String);
#[error(r#"Failed to parse rule severity, expected a string or a number, but got {0:?}"#)]
#[diagnostic()]
pub struct FailedToParseAllowWarnDenyFromJsonValueError(pub String);

#[derive(Debug, Error, Diagnostic)]
#[error("Failed to parse jsonc file {0:?}")]
#[diagnostic()]
pub struct FailedToParseJsonc(pub PathBuf);
30 changes: 13 additions & 17 deletions crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use serde_json::Value;
use crate::{rules::RuleEnum, settings::Nextjs, AllowWarnDeny, JsxA11y, LintSettings};

use self::errors::{
FailedToParseConfigError, FailedToParseConfigJsonError, FailedToParseRuleValueError,
FailedToParseConfigError, FailedToParseConfigJsonError, FailedToParseJsonc,
FailedToParseRuleValueError,
};

pub struct ESLintConfig {
Expand Down Expand Up @@ -37,18 +38,15 @@ impl ESLintConfig {
}

fn read_json(path: &Path) -> Result<serde_json::Value, Error> {
let file = match std::fs::read_to_string(path) {
Ok(file) => file,
Err(e) => {
return Err(FailedToParseConfigError(vec![Error::new(FailedToOpenFileError(
path.to_path_buf(),
e,
))])
.into());
}
};
let mut string = std::fs::read_to_string(path).map_err(|e| {
FailedToParseConfigError(vec![Error::new(FailedToOpenFileError(path.to_path_buf(), e))])
})?;

// jsonc support
json_strip_comments::strip(&mut string)
.map_err(|_| FailedToParseJsonc(path.to_path_buf()))?;

serde_json::from_str::<serde_json::Value>(&file).map_err(|err| {
serde_json::from_str::<serde_json::Value>(&string).map_err(|err| {
let guess = mime_guess::from_path(path);
let err = match guess.first() {
// syntax error
Expand Down Expand Up @@ -256,15 +254,13 @@ fn resolve_rule_value(value: &serde_json::Value) -> Result<(AllowWarnDeny, Optio

#[cfg(test)]
mod test {
use super::parse_rules;
use super::ESLintConfig;
use std::env;

#[test]
fn test_parse_rules() {
let fixture_path = env::current_dir().unwrap().join("fixtures/eslint_config.json");
let input = std::fs::read_to_string(fixture_path).unwrap();
let file = serde_json::from_str::<serde_json::Value>(&input).unwrap();
let rules = parse_rules(&file).unwrap();
insta::assert_debug_snapshot!(rules);
let config = ESLintConfig::new(&fixture_path).unwrap();
assert!(!config.rules.is_empty());
}
}

This file was deleted.

0 comments on commit 69fecac

Please sign in to comment.