Skip to content

Commit

Permalink
refactor(linter): replace MIME guessing with extension check
Browse files Browse the repository at this point in the history
  • Loading branch information
camchenry committed Feb 1, 2025
1 parent 0568210 commit 580e5e6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 31 deletions.
23 changes: 0 additions & 23 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ markdown = "1.0.0-alpha.21"
memchr = "2.7.4"
miette = { package = "oxc-miette", version = "1.0.2", features = ["fancy-no-syscall"] }
mimalloc = "0.1.43"
mime_guess = "2.0.5"
nonmax = "0.5.5"
num-bigint = "0.4.6"
num-traits = "0.2.19"
Expand Down
6 changes: 6 additions & 0 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,4 +856,10 @@ mod test {
vec![String::from("src/target"), String::from("!src/dist"), String::from("!!src/dist")]
);
}

#[test]
fn test_non_json_config() {
let args = &["-c", "test.js"];
Tester::new().with_cwd("fixtures/eslintrc_env".into()).test_and_snapshot(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: apps/oxlint/src/tester.rs
---
##########
arguments: -c test.js
working directory: fixtures/eslintrc_env
----------
Failed to parse configuration file.

× Failed to parse eslint config "<cwd>/fixtures/eslintrc_env/test.js".
│ Only JSON configuration files are supported

----------
CLI result: InvalidOptionConfig
----------
1 change: 0 additions & 1 deletion crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ json-strip-comments = { workspace = true }
language-tags = { workspace = true }
lazy_static = { workspace = true }
memchr = { workspace = true }
mime_guess = { workspace = true }
nonmax = { workspace = true }
phf = { workspace = true, features = ["macros"] }
rayon = { workspace = true }
Expand Down
19 changes: 13 additions & 6 deletions crates/oxc_linter/src/config/oxlintrc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::{Path, PathBuf};
use std::{
ffi::OsStr,
path::{Path, PathBuf},
};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -109,14 +112,14 @@ impl Oxlintrc {
})?;

let json = serde_json::from_str::<serde_json::Value>(&string).map_err(|err| {
let guess = mime_guess::from_path(path);
let err = match guess.first() {
let ext = path.extension().and_then(OsStr::to_str);
let err = match ext {
// syntax error
Some(mime) if mime.subtype() == "json" => err.to_string(),
Some(_) => "Only json configuration is supported".to_string(),
Some(ext) if is_json_ext(ext) => err.to_string(),
Some(_) => "Only JSON configuration files are supported".to_string(),
None => {
format!(
"{err}, if the configuration is not a json file, please use json instead."
"{err}, if the configuration is not a JSON file, please use JSON instead."
)
}
};
Expand All @@ -133,6 +136,10 @@ impl Oxlintrc {
}
}

fn is_json_ext(ext: &str) -> bool {
ext == "json" || ext == "jsonc"
}

#[cfg(test)]
mod test {
use serde_json::json;
Expand Down

0 comments on commit 580e5e6

Please sign in to comment.