Skip to content

Commit

Permalink
refactor(linter): replace MIME guessing with extension check (#8832)
Browse files Browse the repository at this point in the history
I noticed that we were using this `mime_guess` crate, when it all it really does internally is check the extension of the file and map that to known MIME types: https://github.com/abonander/mime_guess/blob/805964fb54871f0154ee155bf21729b77ffd4a1c/src/lib.rs#L87-L90

Since we only expect JSON configuration files, I've replaced the MIME guessing with a simple extension check.
  • Loading branch information
camchenry committed Feb 2, 2025
1 parent 5cfea76 commit 4fcf719
Show file tree
Hide file tree
Showing 4 changed files with 13 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
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 4fcf719

Please sign in to comment.