1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::fmt::Debug;

use serde::{Deserialize, Serialize};
use swc_config::merge::Merge;

#[cfg(feature = "non_critical_lints")]
use crate::rules::non_critical_lints::{
    dot_notation::DotNotationConfig, eqeqeq::EqeqeqConfig, no_bitwise::NoBitwiseConfig,
    no_console::NoConsoleConfig, no_empty_function::NoEmptyFunctionConfig,
    no_param_reassign::NoParamReassignConfig, no_restricted_syntax::NoRestrictedSyntaxConfig,
    no_use_before_define::NoUseBeforeDefineConfig, prefer_const::PreferConstConfig,
    prefer_regex_literals::PreferRegexLiteralsConfig, quotes::QuotesConfig, radix::RadixConfig,
    symbol_description::SymbolDescriptionConfig, use_is_nan::UseIsNanConfig,
    valid_typeof::ValidTypeofConfig, yoda::YodaConfig,
};

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LintRuleReaction {
    Off,
    Warning,
    Error,
}

impl Default for LintRuleReaction {
    fn default() -> Self {
        Self::Off
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(untagged)]
enum LintRuleLevel {
    Str(LintRuleReaction),
    Number(u8),
    Unspecified,
}

impl Default for LintRuleLevel {
    fn default() -> Self {
        Self::Unspecified
    }
}

impl From<LintRuleLevel> for LintRuleReaction {
    fn from(level: LintRuleLevel) -> Self {
        match level {
            LintRuleLevel::Str(level) => level,
            LintRuleLevel::Number(level) => match level {
                1 => LintRuleReaction::Warning,
                2 => LintRuleReaction::Error,
                _ => LintRuleReaction::Off,
            },
            LintRuleLevel::Unspecified => LintRuleReaction::Off,
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RuleConfig<T: Debug + Clone + Serialize + Default>(
    #[serde(default)] LintRuleLevel,
    #[serde(default)] T,
);

impl<T: Debug + Clone + Serialize + Default> RuleConfig<T> {
    pub(crate) fn get_rule_reaction(&self) -> LintRuleReaction {
        self.0.into()
    }

    pub(crate) fn get_rule_config(&self) -> &T {
        &self.1
    }
}

impl<T> Merge for RuleConfig<T>
where
    T: Debug + Clone + Serialize + Default,
{
    fn merge(&mut self, other: Self) {
        if let LintRuleLevel::Unspecified = self.0 {
            self.0 = other.0;
            self.1 = other.1;
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, Merge)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
pub struct LintConfig {
    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noConsole")]
    pub no_console: RuleConfig<NoConsoleConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "preferRegexLiterals")]
    pub prefer_regex_literals: RuleConfig<PreferRegexLiteralsConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noAlert")]
    pub no_alert: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noDebugger")]
    pub no_debugger: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noUseBeforeDefine")]
    pub no_use_before_define: RuleConfig<NoUseBeforeDefineConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "dotNotation")]
    pub dot_notation: RuleConfig<DotNotationConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default)]
    pub quotes: RuleConfig<QuotesConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noEmptyFunction")]
    pub no_empty_function: RuleConfig<NoEmptyFunctionConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noEmptyPattern")]
    pub no_empty_pattern: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default)]
    pub eqeqeq: RuleConfig<EqeqeqConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noLoopFunc")]
    pub no_loop_func: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noNew")]
    pub no_new: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noRestrictedSyntax")]
    pub no_restricted_syntax: RuleConfig<NoRestrictedSyntaxConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default)]
    pub radix: RuleConfig<RadixConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noBitwise")]
    pub no_bitwise: RuleConfig<NoBitwiseConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "defaultParamLast")]
    pub default_param_last: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default)]
    pub yoda: RuleConfig<YodaConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noNewSymbol")]
    pub no_new_symbol: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "useIsNan")]
    pub use_isnan: RuleConfig<UseIsNanConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "validTypeof")]
    pub valid_typeof: RuleConfig<ValidTypeofConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noParamReassign")]
    pub no_param_reassign: RuleConfig<NoParamReassignConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "symbolDescription")]
    pub symbol_description: RuleConfig<SymbolDescriptionConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noObjCalls")]
    pub no_obj_calls: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noVar")]
    pub no_var: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noThrowLiteral")]
    pub no_throw_literal: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "preferConst")]
    pub prefer_const: RuleConfig<PreferConstConfig>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noCompareNegZero")]
    pub no_compare_neg_zero: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "constructorSuper")]
    pub constructor_super: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noSparseArrays")]
    pub no_sparse_arrays: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "defaultCaseLast")]
    pub default_case_last: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noAwaitInLoop")]
    pub no_await_in_loop: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noCondAssign")]
    pub no_cond_assign: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "noPrototypeBuiltins")]
    pub no_prototype_builtins: RuleConfig<()>,
    #[serde(default, alias = "noNewObject")]
    pub no_new_object: RuleConfig<()>,

    #[cfg(feature = "non_critical_lints")]
    #[serde(default, alias = "preferObjectSpread")]
    pub prefer_object_spread: RuleConfig<()>,
}