Skip to content

Commit 12ac972

Browse files
committed
refactor(linter): replace MIME guessing with extension check
1 parent 0568210 commit 12ac972

File tree

7 files changed

+51
-31
lines changed

7 files changed

+51
-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"

apps/oxlint/src/lint.rs

+7
Original file line numberDiff line numberDiff line change
@@ -856,4 +856,11 @@ mod test {
856856
vec![String::from("src/target"), String::from("!src/dist"), String::from("!!src/dist")]
857857
);
858858
}
859+
860+
#[test]
861+
fn test_non_json_config() {
862+
let args = &["-c", "test.js"];
863+
std::env::set_var("NO_COLOR", "1");
864+
Tester::new().with_cwd("fixtures/eslintrc_env".into()).test_and_snapshot(args);
865+
}
859866
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
arguments: -c test.js
6+
working directory: fixtures/eslintrc_env
7+
----------
8+
Failed to parse configuration file.
9+
10+
× Failed to parse eslint config "<cwd>/fixtures/eslintrc_env/test.js".
11+
│ Only JSON configuration files are supported
12+
13+
----------
14+
CLI result: InvalidOptionConfig
15+
----------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
assertion_line: 95
4+
---
5+
##########
6+
arguments: -c test.js
7+
working directory: fixtures/eslintrc_env
8+
----------
9+
Failed to parse configuration file.
10+
11+
× Failed to parse eslint config "<cwd>/fixtures/eslintrc_env/test.js".
12+
│ Only JSON configuration files are supported
13+
14+
----------
15+
CLI result: InvalidOptionConfig
16+
----------

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)