Skip to content

Commit 09c0ac6

Browse files
committedMar 29, 2025·
refactor(linter): improve unicorn/filename-case (#10117)
- Better code style
1 parent dbe0e46 commit 09c0ac6

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed
 

‎crates/oxc_linter/src/rules/unicorn/filename_case.rs

+26-34
Original file line numberDiff line numberDiff line change
@@ -132,55 +132,47 @@ impl Rule for FilenameCase {
132132
fn from_configuration(value: serde_json::Value) -> Self {
133133
let mut config = FilenameCaseConfig::default();
134134

135-
let Some(case_type) = value.get(0) else {
136-
config.kebab_case = true;
137-
return Self(Box::new(config));
138-
};
139-
140-
if let Some(Value::String(pat)) = case_type.get("ignore") {
141-
config.ignore = RegexBuilder::new(pat).build().ok();
142-
}
143-
144-
if let Some(Value::String(s)) = case_type.get("case") {
145-
match s.as_str() {
146-
"camelCase" => config.camel_case = true,
147-
"snakeCase" => config.snake_case = true,
148-
"pascalCase" => config.pascal_case = true,
149-
_ => config.kebab_case = true,
135+
if let Some(value) = value.get(0) {
136+
if let Some(Value::String(pat)) = value.get("ignore") {
137+
config.ignore = RegexBuilder::new(pat).build().ok();
150138
}
151139

152-
return Self(Box::new(config));
153-
}
140+
if let Some(Value::String(s)) = value.get("case") {
141+
match s.as_str() {
142+
"camelCase" => config.camel_case = true,
143+
"snakeCase" => config.snake_case = true,
144+
"pascalCase" => config.pascal_case = true,
145+
_ => config.kebab_case = true,
146+
}
147+
return Self(Box::new(config));
148+
}
154149

155-
if let Some(Value::Object(map)) = case_type.get("cases") {
156-
for (key, value) in map {
157-
match (key.as_str(), value) {
158-
("kebabCase", Value::Bool(b)) => config.kebab_case = *b,
159-
("camelCase", Value::Bool(b)) => config.camel_case = *b,
160-
("snakeCase", Value::Bool(b)) => config.snake_case = *b,
161-
("pascalCase", Value::Bool(b)) => config.pascal_case = *b,
162-
_ => (),
150+
if let Some(Value::Object(map)) = value.get("cases") {
151+
for (key, value) in map {
152+
match (key.as_str(), value) {
153+
("kebabCase", Value::Bool(b)) => config.kebab_case = *b,
154+
("camelCase", Value::Bool(b)) => config.camel_case = *b,
155+
("snakeCase", Value::Bool(b)) => config.snake_case = *b,
156+
("pascalCase", Value::Bool(b)) => config.pascal_case = *b,
157+
_ => (),
158+
}
163159
}
160+
return Self(Box::new(config));
164161
}
165-
return Self(Box::new(config));
166162
}
167163

168164
config.kebab_case = true;
169165
Self(Box::new(config))
170166
}
171167

172168
fn run_once<'a>(&self, ctx: &LintContext<'_>) {
173-
let file_path = ctx.file_path();
174-
let Some(raw_filename) = file_path.file_name().and_then(|s| s.to_str()) else {
169+
let Some(raw_filename) = ctx.file_path().file_name().and_then(|s| s.to_str()) else {
175170
return;
176171
};
177172

178-
if self.ignore.as_ref().is_some_and(|regex| regex.is_match(raw_filename)) {
179-
return;
180-
}
181-
182-
// Get valid filename
183-
if raw_filename.as_bytes() == b".." || raw_filename.starts_with('.') {
173+
if raw_filename.starts_with('.')
174+
|| self.ignore.as_ref().is_some_and(|r| r.is_match(raw_filename))
175+
{
184176
return;
185177
}
186178

0 commit comments

Comments
 (0)
Please sign in to comment.