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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#![recursion_limit = "1000"]
#![cfg_attr(
    feature = "force_exhaustive_checks",
    feature(non_exhaustive_omitted_patterns_lint)
)]
use std::{collections::HashMap, error::Error, fmt};
use full_moon::ast::Ast;
use serde::{
    de::{DeserializeOwned, Deserializer},
    Deserialize,
};
mod ast_util;
mod lint_filtering;
pub mod rules;
pub mod standard_library;
mod util;
#[cfg(test)]
mod test_util;
use rules::{Context, Diagnostic, Rule, Severity};
use standard_library::StandardLibrary;
#[derive(Debug)]
pub struct CheckerError {
    pub name: &'static str,
    pub problem: CheckerErrorProblem,
}
#[derive(Debug)]
pub enum CheckerErrorProblem {
    ConfigDeserializeError(Box<dyn Error>),
    RuleNewError(Box<dyn Error>),
}
impl fmt::Display for CheckerError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        use CheckerErrorProblem::*;
        write!(formatter, "[{}] ", self.name)?;
        match &self.problem {
            ConfigDeserializeError(error) => write!(
                formatter,
                "Configuration was incorrectly formatted: {}",
                error
            ),
            RuleNewError(error) => write!(formatter, "{}", error),
        }
    }
}
impl Error for CheckerError {}
#[derive(Deserialize)]
#[serde(default)]
#[serde(rename_all = "kebab-case")]
pub struct CheckerConfig<V> {
    pub config: HashMap<String, V>,
    pub rules: HashMap<String, RuleVariation>,
    pub std: String,
    
    
    pub roblox_std_source: RobloxStdSource,
}
impl<V> Default for CheckerConfig<V> {
    fn default() -> Self {
        CheckerConfig {
            config: HashMap::new(),
            rules: HashMap::new(),
            std: "lua51".to_owned(),
            roblox_std_source: RobloxStdSource::default(),
        }
    }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RuleVariation {
    Allow,
    Deny,
    Warn,
}
impl RuleVariation {
    pub fn to_severity(self) -> Severity {
        match self {
            RuleVariation::Allow => Severity::Allow,
            RuleVariation::Deny => Severity::Error,
            RuleVariation::Warn => Severity::Warning,
        }
    }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RobloxStdSource {
    Floating,
    Pinned,
}
impl Default for RobloxStdSource {
    fn default() -> Self {
        Self::Floating
    }
}
macro_rules! use_rules {
    {
        $(
            $rule_name:ident: $rule_path:ty,
        )+
        $(
            #[$meta:meta]
            {
                $($meta_rule_name:ident: $meta_rule_path:ty,)+
            },
        )+
    } => {
        lazy_static::lazy_static! {
            static ref ALL_RULES: Vec<&'static str> = vec![
                $(
                    stringify!($rule_name),
                )+
                $(
                    $(
                        #[$meta]
                        stringify!($meta_rule_name),
                    )+
                )+
            ];
        }
        pub struct Checker<V: 'static + DeserializeOwned> {
            config: CheckerConfig<V>,
            context: Context,
            $(
                $rule_name: $rule_path,
            )+
            $(
                $(
                    #[$meta]
                    $meta_rule_name: $meta_rule_path,
                )+
            )+
        }
        impl<V: 'static + DeserializeOwned> Checker<V> {
            
            pub fn new(
                mut config: CheckerConfig<V>,
                standard_library: StandardLibrary,
            ) -> Result<Self, CheckerError> where V: for<'de> Deserializer<'de> {
                macro_rules! rule_field {
                    ($name:ident, $path:ty) => {{
                        let rule_name = stringify!($name);
                        let rule = <$path>::new({
                            match config.config.remove(rule_name) {
                                Some(entry_generic) => {
                                    <$path as Rule>::Config::deserialize(entry_generic).map_err(|error| {
                                        CheckerError {
                                            name: rule_name,
                                            problem: CheckerErrorProblem::ConfigDeserializeError(Box::new(error)),
                                        }
                                    })?
                                }
                                None => {
                                    <$path as Rule>::Config::default()
                                }
                            }
                        }).map_err(|error| {
                            CheckerError {
                                name: stringify!($name),
                                problem: CheckerErrorProblem::RuleNewError(Box::new(error)),
                            }
                        })?;
                        rule
                    }};
                }
                Ok(Self {
                    $(
                        $rule_name: {
                            rule_field!($rule_name, $rule_path)
                        },
                    )+
                    $(
                        $(
                            #[$meta]
                            $meta_rule_name: {
                                rule_field!($meta_rule_name, $meta_rule_path)
                            },
                        )+
                    )+
                    config,
                    context: Context {
                        standard_library,
                    },
                })
            }
            pub fn test_on(&self, ast: &Ast) -> Vec<CheckerDiagnostic> {
                let mut diagnostics = Vec::new();
                macro_rules! check_rule {
                    ($name:ident) => {
                        let rule = &self.$name;
                        diagnostics.extend(&mut rule.pass(ast, &self.context).into_iter().map(|diagnostic| {
                            CheckerDiagnostic {
                                diagnostic,
                                severity: self.get_lint_severity(rule, stringify!($name)),
                            }
                        }));
                    };
                }
                $(
                    check_rule!($rule_name);
                )+
                $(
                    $(
                        #[$meta]
                        {
                            check_rule!($meta_rule_name);
                        }
                    )+
                )+
                diagnostics = lint_filtering::filter_diagnostics(
                    ast,
                    diagnostics,
                    self.get_lint_severity(&self.invalid_lint_filter, "invalid_lint_filter"),
                );
                diagnostics
            }
            fn get_lint_severity<R: Rule>(&self, lint: &R, name: &'static str) -> Severity {
                match self.config.rules.get(name) {
                    Some(variation) => variation.to_severity(),
                    None => lint.severity(),
                }
            }
        }
    };
}
#[derive(Debug)]
pub struct CheckerDiagnostic {
    pub diagnostic: Diagnostic,
    pub severity: Severity,
}
pub fn rule_exists(name: &str) -> bool {
    ALL_RULES.contains(&name)
}
use_rules! {
    almost_swapped: rules::almost_swapped::AlmostSwappedLint,
    bad_string_escape: rules::bad_string_escape::BadStringEscapeLint,
    compare_nan: rules::compare_nan::CompareNanLint,
    deprecated: rules::deprecated::DeprecatedLint,
    divide_by_zero: rules::divide_by_zero::DivideByZeroLint,
    duplicate_keys: rules::duplicate_keys::DuplicateKeysLint,
    empty_if: rules::empty_if::EmptyIfLint,
    global_usage: rules::global_usage::GlobalLint,
    if_same_then_else: rules::if_same_then_else::IfSameThenElseLint,
    ifs_same_cond: rules::ifs_same_cond::IfsSameCondLint,
    incorrect_standard_library_use: rules::standard_library::StandardLibraryLint,
    invalid_lint_filter: rules::invalid_lint_filter::InvalidLintFilterLint,
    mismatched_arg_count: rules::mismatched_arg_count::MismatchedArgCountLint,
    multiple_statements: rules::multiple_statements::MultipleStatementsLint,
    parenthese_conditions: rules::parenthese_conditions::ParentheseConditionsLint,
    shadowing: rules::shadowing::ShadowingLint,
    suspicious_reverse_loop: rules::suspicious_reverse_loop::SuspiciousReverseLoopLint,
    type_check_inside_call: rules::type_check_inside_call::TypeCheckInsideCallLint,
    unbalanced_assignments: rules::unbalanced_assignments::UnbalancedAssignmentsLint,
    undefined_variable: rules::undefined_variable::UndefinedVariableLint,
    unscoped_variables: rules::unscoped_variables::UnscopedVariablesLint,
    unused_variable: rules::unused_variable::UnusedVariableLint,
    #[cfg(feature = "roblox")]
    {
        roblox_incorrect_color3_new_bounds: rules::roblox_incorrect_color3_new_bounds::Color3BoundsLint,
        roblox_incorrect_roact_usage: rules::roblox_incorrect_roact_usage::IncorrectRoactUsageLint,
    },
}