swc_ecma_lints/
config.rs

1use std::fmt::Debug;
2
3use serde::{Deserialize, Serialize};
4use swc_config::merge::Merge;
5
6#[cfg(feature = "non_critical_lints")]
7use crate::rules::non_critical_lints::{
8    dot_notation::DotNotationConfig, eqeqeq::EqeqeqConfig, no_bitwise::NoBitwiseConfig,
9    no_console::NoConsoleConfig, no_empty_function::NoEmptyFunctionConfig,
10    no_param_reassign::NoParamReassignConfig, no_restricted_syntax::NoRestrictedSyntaxConfig,
11    no_use_before_define::NoUseBeforeDefineConfig, prefer_const::PreferConstConfig,
12    prefer_regex_literals::PreferRegexLiteralsConfig, quotes::QuotesConfig, radix::RadixConfig,
13    symbol_description::SymbolDescriptionConfig, use_is_nan::UseIsNanConfig,
14    valid_typeof::ValidTypeofConfig, yoda::YodaConfig,
15};
16
17#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
18#[serde(rename_all = "lowercase")]
19pub enum LintRuleReaction {
20    Off,
21    Warning,
22    Error,
23}
24
25impl Default for LintRuleReaction {
26    fn default() -> Self {
27        Self::Off
28    }
29}
30
31#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
32#[serde(untagged)]
33enum LintRuleLevel {
34    Str(LintRuleReaction),
35    Number(u8),
36    Unspecified,
37}
38
39impl Default for LintRuleLevel {
40    fn default() -> Self {
41        Self::Unspecified
42    }
43}
44
45impl From<LintRuleLevel> for LintRuleReaction {
46    fn from(level: LintRuleLevel) -> Self {
47        match level {
48            LintRuleLevel::Str(level) => level,
49            LintRuleLevel::Number(level) => match level {
50                1 => LintRuleReaction::Warning,
51                2 => LintRuleReaction::Error,
52                _ => LintRuleReaction::Off,
53            },
54            LintRuleLevel::Unspecified => LintRuleReaction::Off,
55        }
56    }
57}
58
59#[derive(Debug, Clone, Default, Serialize, Deserialize)]
60pub struct RuleConfig<T: Debug + Clone + Serialize + Default>(
61    #[serde(default)] LintRuleLevel,
62    #[serde(default)] T,
63);
64
65impl<T: Debug + Clone + Serialize + Default> RuleConfig<T> {
66    pub(crate) fn get_rule_reaction(&self) -> LintRuleReaction {
67        self.0.into()
68    }
69
70    pub(crate) fn get_rule_config(&self) -> &T {
71        &self.1
72    }
73}
74
75impl<T> Merge for RuleConfig<T>
76where
77    T: Debug + Clone + Serialize + Default,
78{
79    fn merge(&mut self, other: Self) {
80        if let LintRuleLevel::Unspecified = self.0 {
81            self.0 = other.0;
82            self.1 = other.1;
83        }
84    }
85}
86
87#[derive(Debug, Clone, Default, Serialize, Deserialize, Merge)]
88#[non_exhaustive]
89#[serde(rename_all = "kebab-case")]
90pub struct LintConfig {
91    #[cfg(feature = "non_critical_lints")]
92    #[serde(default, alias = "noConsole")]
93    pub no_console: RuleConfig<NoConsoleConfig>,
94
95    #[cfg(feature = "non_critical_lints")]
96    #[serde(default, alias = "preferRegexLiterals")]
97    pub prefer_regex_literals: RuleConfig<PreferRegexLiteralsConfig>,
98
99    #[cfg(feature = "non_critical_lints")]
100    #[serde(default, alias = "noAlert")]
101    pub no_alert: RuleConfig<()>,
102
103    #[cfg(feature = "non_critical_lints")]
104    #[serde(default, alias = "noDebugger")]
105    pub no_debugger: RuleConfig<()>,
106
107    #[cfg(feature = "non_critical_lints")]
108    #[serde(default, alias = "noUseBeforeDefine")]
109    pub no_use_before_define: RuleConfig<NoUseBeforeDefineConfig>,
110
111    #[cfg(feature = "non_critical_lints")]
112    #[serde(default, alias = "dotNotation")]
113    pub dot_notation: RuleConfig<DotNotationConfig>,
114
115    #[cfg(feature = "non_critical_lints")]
116    #[serde(default)]
117    pub quotes: RuleConfig<QuotesConfig>,
118
119    #[cfg(feature = "non_critical_lints")]
120    #[serde(default, alias = "noEmptyFunction")]
121    pub no_empty_function: RuleConfig<NoEmptyFunctionConfig>,
122
123    #[cfg(feature = "non_critical_lints")]
124    #[serde(default, alias = "noEmptyPattern")]
125    pub no_empty_pattern: RuleConfig<()>,
126
127    #[cfg(feature = "non_critical_lints")]
128    #[serde(default)]
129    pub eqeqeq: RuleConfig<EqeqeqConfig>,
130
131    #[cfg(feature = "non_critical_lints")]
132    #[serde(default, alias = "noLoopFunc")]
133    pub no_loop_func: RuleConfig<()>,
134
135    #[cfg(feature = "non_critical_lints")]
136    #[serde(default, alias = "noNew")]
137    pub no_new: RuleConfig<()>,
138
139    #[cfg(feature = "non_critical_lints")]
140    #[serde(default, alias = "noRestrictedSyntax")]
141    pub no_restricted_syntax: RuleConfig<NoRestrictedSyntaxConfig>,
142
143    #[cfg(feature = "non_critical_lints")]
144    #[serde(default)]
145    pub radix: RuleConfig<RadixConfig>,
146
147    #[cfg(feature = "non_critical_lints")]
148    #[serde(default, alias = "noBitwise")]
149    pub no_bitwise: RuleConfig<NoBitwiseConfig>,
150
151    #[cfg(feature = "non_critical_lints")]
152    #[serde(default, alias = "defaultParamLast")]
153    pub default_param_last: RuleConfig<()>,
154
155    #[cfg(feature = "non_critical_lints")]
156    #[serde(default)]
157    pub yoda: RuleConfig<YodaConfig>,
158
159    #[cfg(feature = "non_critical_lints")]
160    #[serde(default, alias = "noNewSymbol")]
161    pub no_new_symbol: RuleConfig<()>,
162
163    #[cfg(feature = "non_critical_lints")]
164    #[serde(default, alias = "useIsNan")]
165    pub use_isnan: RuleConfig<UseIsNanConfig>,
166
167    #[cfg(feature = "non_critical_lints")]
168    #[serde(default, alias = "validTypeof")]
169    pub valid_typeof: RuleConfig<ValidTypeofConfig>,
170
171    #[cfg(feature = "non_critical_lints")]
172    #[serde(default, alias = "noParamReassign")]
173    pub no_param_reassign: RuleConfig<NoParamReassignConfig>,
174
175    #[cfg(feature = "non_critical_lints")]
176    #[serde(default, alias = "symbolDescription")]
177    pub symbol_description: RuleConfig<SymbolDescriptionConfig>,
178
179    #[cfg(feature = "non_critical_lints")]
180    #[serde(default, alias = "noObjCalls")]
181    pub no_obj_calls: RuleConfig<()>,
182
183    #[cfg(feature = "non_critical_lints")]
184    #[serde(default, alias = "noVar")]
185    pub no_var: RuleConfig<()>,
186
187    #[cfg(feature = "non_critical_lints")]
188    #[serde(default, alias = "noThrowLiteral")]
189    pub no_throw_literal: RuleConfig<()>,
190
191    #[cfg(feature = "non_critical_lints")]
192    #[serde(default, alias = "preferConst")]
193    pub prefer_const: RuleConfig<PreferConstConfig>,
194
195    #[cfg(feature = "non_critical_lints")]
196    #[serde(default, alias = "noCompareNegZero")]
197    pub no_compare_neg_zero: RuleConfig<()>,
198
199    #[cfg(feature = "non_critical_lints")]
200    #[serde(default, alias = "constructorSuper")]
201    pub constructor_super: RuleConfig<()>,
202
203    #[cfg(feature = "non_critical_lints")]
204    #[serde(default, alias = "noSparseArrays")]
205    pub no_sparse_arrays: RuleConfig<()>,
206
207    #[cfg(feature = "non_critical_lints")]
208    #[serde(default, alias = "defaultCaseLast")]
209    pub default_case_last: RuleConfig<()>,
210
211    #[cfg(feature = "non_critical_lints")]
212    #[serde(default, alias = "noAwaitInLoop")]
213    pub no_await_in_loop: RuleConfig<()>,
214
215    #[cfg(feature = "non_critical_lints")]
216    #[serde(default, alias = "noCondAssign")]
217    pub no_cond_assign: RuleConfig<()>,
218
219    #[cfg(feature = "non_critical_lints")]
220    #[serde(default, alias = "noPrototypeBuiltins")]
221    pub no_prototype_builtins: RuleConfig<()>,
222    #[serde(default, alias = "noNewObject")]
223    pub no_new_object: RuleConfig<()>,
224
225    #[cfg(feature = "non_critical_lints")]
226    #[serde(default, alias = "preferObjectSpread")]
227    pub prefer_object_spread: RuleConfig<()>,
228}