From 3eb4b9b1c40175acd42eadeeace65e74c615d18b Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 4 Nov 2023 23:41:16 +0000 Subject: [PATCH] feat(linter) parse configuration for unicorn/filename-case (#1145) --- .../src/rules/unicorn/filename_case.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/oxc_linter/src/rules/unicorn/filename_case.rs b/crates/oxc_linter/src/rules/unicorn/filename_case.rs index f3608f11c..b796f3cd0 100644 --- a/crates/oxc_linter/src/rules/unicorn/filename_case.rs +++ b/crates/oxc_linter/src/rules/unicorn/filename_case.rs @@ -5,6 +5,7 @@ use oxc_diagnostics::{ }; use oxc_macros::declare_oxc_lint; use oxc_span::Span; +use serde_json::Value; use crate::{context::LintContext, rule::Rule}; @@ -46,6 +47,36 @@ declare_oxc_lint!( ); impl Rule for FilenameCase { + fn from_configuration(value: serde_json::Value) -> Self { + let Some(case_type) = value.get("cases") else { return Self::default() }; + + match case_type { + Value::String(s) => match s.as_str() { + "kebabCase" => Self { kebab_case: true, ..Self::default() }, + "camelCase" => Self { camel_case: true, ..Self::default() }, + "snakeCase" => Self { snake_case: true, ..Self::default() }, + "pascalCase" => Self { pascal_case: true, ..Self::default() }, + "underscoreCase" => Self { underscore_case: true, ..Self::default() }, + _ => Self::default(), + }, + Value::Object(map) => { + let mut filename_case = Self::default(); + for (key, value) in map { + match (key.as_str(), value) { + ("kebabCase", Value::Bool(b)) => filename_case.kebab_case = *b, + ("camelCase", Value::Bool(b)) => filename_case.camel_case = *b, + ("snakeCase", Value::Bool(b)) => filename_case.snake_case = *b, + ("pascalCase", Value::Bool(b)) => filename_case.pascal_case = *b, + ("underscoreCase", Value::Bool(b)) => filename_case.underscore_case = *b, + _ => (), + } + } + filename_case + } + _ => Self::default(), + } + } + fn run_once<'a>(&self, ctx: &LintContext<'_>) { let Some(filename) = ctx.file_path().file_stem().and_then(|s| s.to_str()) else { return };