Skip to content

Commit 4fcf719

Browse files
committedFeb 2, 2025·
refactor(linter): replace MIME guessing with extension check (#8832)
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.
1 parent 5cfea76 commit 4fcf719

File tree

4 files changed

+13
-31
lines changed

4 files changed

+13
-31
lines changed
 

‎Cargo.lock

-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ markdown = "1.0.0-alpha.21"
169169
memchr = "2.7.4"
170170
miette = { package = "oxc-miette", version = "1.0.2", features = ["fancy-no-syscall"] }
171171
mimalloc = "0.1.43"
172-
mime_guess = "2.0.5"
173172
nonmax = "0.5.5"
174173
num-bigint = "0.4.6"
175174
num-traits = "0.2.19"

‎crates/oxc_linter/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ json-strip-comments = { workspace = true }
4848
language-tags = { workspace = true }
4949
lazy_static = { workspace = true }
5050
memchr = { workspace = true }
51-
mime_guess = { workspace = true }
5251
nonmax = { workspace = true }
5352
phf = { workspace = true, features = ["macros"] }
5453
rayon = { workspace = true }

‎crates/oxc_linter/src/config/oxlintrc.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::path::{Path, PathBuf};
1+
use std::{
2+
ffi::OsStr,
3+
path::{Path, PathBuf},
4+
};
25

36
use schemars::JsonSchema;
47
use serde::{Deserialize, Serialize};
@@ -109,14 +112,14 @@ impl Oxlintrc {
109112
})?;
110113

111114
let json = serde_json::from_str::<serde_json::Value>(&string).map_err(|err| {
112-
let guess = mime_guess::from_path(path);
113-
let err = match guess.first() {
115+
let ext = path.extension().and_then(OsStr::to_str);
116+
let err = match ext {
114117
// syntax error
115-
Some(mime) if mime.subtype() == "json" => err.to_string(),
116-
Some(_) => "Only json configuration is supported".to_string(),
118+
Some(ext) if is_json_ext(ext) => err.to_string(),
119+
Some(_) => "Only JSON configuration files are supported".to_string(),
117120
None => {
118121
format!(
119-
"{err}, if the configuration is not a json file, please use json instead."
122+
"{err}, if the configuration is not a JSON file, please use JSON instead."
120123
)
121124
}
122125
};
@@ -133,6 +136,10 @@ impl Oxlintrc {
133136
}
134137
}
135138

139+
fn is_json_ext(ext: &str) -> bool {
140+
ext == "json" || ext == "jsonc"
141+
}
142+
136143
#[cfg(test)]
137144
mod test {
138145
use serde_json::json;

0 commit comments

Comments
 (0)
Please sign in to comment.