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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! Configuration file support.

// FIXME: Workaround for https://github.com/GREsau/schemars/pull/65
#![allow(clippy::field_reassign_with_default)]

mod de;
use dirs_next::config_dir;
use rslint_core::{get_group_rules_by_name, CstRule, CstRuleStore, Diagnostic, RuleLevel};
use rslint_errors::file::{Files, SimpleFile};
use serde::{Deserialize, Serialize};
use std::{
    env,
    fs::read_to_string,
    path::{Path, PathBuf},
};

/// The name of the config files to search for.
pub const CONFIG_NAMES: [&str; 2] = ["rslintrc.json", "rslintrc.toml"];

/// A list of boxed rule implementations.
pub type RuleList = Vec<Box<dyn CstRule>>;

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Deserialize, Serialize)]
pub struct ConfigRepr {
    rules: Option<RulesConfigRepr>,
    #[serde(default)]
    errors: ErrorsConfigRepr,
}

impl Default for ConfigRepr {
    fn default() -> Self {
        Self {
            rules: None,
            errors: Default::default(),
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(default)]
struct RulesConfigRepr {
    #[serde(deserialize_with = "de::from_rule_objects")]
    errors: RuleList,

    #[serde(deserialize_with = "de::from_rule_objects")]
    warnings: RuleList,

    groups: Vec<String>,
    allowed: Vec<String>,
}

#[cfg(feature = "schema")]
impl schemars::JsonSchema for RulesConfigRepr {
    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        use schemars::schema::*;
        use schemars::*;

        macro_rules! string_schema {
            ($val:expr) => {
                Schema::Object(SchemaObject {
                    string: Some(Box::new(StringValidation {
                        pattern: Some($val.to_string()),
                        ..Default::default()
                    })),
                    ..Default::default()
                })
            };
            ($val:expr, $rule_desc:expr) => {{
                let mut split = $rule_desc.split('\n');
                let header = split.next().unwrap_or("");
                let body = split.next().unwrap_or("").to_string();

                Schema::Object(SchemaObject {
                    string: Some(Box::new(StringValidation {
                        pattern: Some($val.to_string()),
                        ..Default::default()
                    })),
                    metadata: Some(Box::new(Metadata {
                        title: Some(header.to_string()),
                        description: Some(body),
                        ..Default::default()
                    })),
                    ..Default::default()
                })
            }};
        }

        let rules = CstRuleStore::new().builtins().rules;
        let mut rule_items = vec![];

        for rule in &rules {
            rule_items.push(string_schema!(rule.name(), rule.docs()));
        }
        let rule_items_schema = Schema::Object(SchemaObject {
            array: Some(Box::new(ArrayValidation {
                items: Some(SingleOrVec::Vec(rule_items)),
                ..Default::default()
            })),
            ..Default::default()
        });

        // TODO(RDambrosio016): dont hardcode it like this
        let group_items = vec![
            string_schema!("errors"),
            string_schema!("style"),
            string_schema!("regex"),
        ];

        let groups_schema = Schema::Object(SchemaObject {
            array: Some(Box::new(ArrayValidation {
                items: Some(SingleOrVec::Vec(group_items)),
                ..Default::default()
            })),
            ..Default::default()
        });

        let mut rule_obj_items = Map::new();
        for rule in &rules {
            if let Some(schema) = rule.schema() {
                rule_obj_items.insert(rule.name().to_string(), Schema::Object(schema.schema));
            }
        }
        let rules_schema = Schema::Object(SchemaObject {
            object: Some(Box::new(ObjectValidation {
                properties: rule_obj_items,
                ..Default::default()
            })),
            ..Default::default()
        });

        let mut map = Map::new();
        map.insert("groups".to_string(), groups_schema);
        map.insert("allowed".to_string(), rule_items_schema);
        map.insert("errors".to_string(), rules_schema.clone());
        map.insert("warnings".to_string(), rules_schema);

        Schema::Object(SchemaObject {
            object: Some(Box::new(ObjectValidation {
                properties: map,
                ..Default::default()
            })),
            ..Default::default()
        })
    }

    fn schema_name() -> String {
        "rules".to_string()
    }
}

#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Deserialize, Serialize)]
struct ErrorsConfigRepr {
    formatter: String,
}

impl Default for ErrorsConfigRepr {
    fn default() -> Self {
        Self {
            formatter: "long".to_string(),
        }
    }
}

#[derive(Debug, Default)]
pub struct Config {
    repr: ConfigRepr,
}

enum ConfigStyle {
    Toml,
    Json,
}

impl Config {
    /// Creates a new config by first searching for a config in the current
    /// dir and all of it ancestors, and if `no_global_config` is `false`,
    /// look in the systems config directory.
    pub fn new(no_global_config: bool) -> Result<Self, (SimpleFile, Diagnostic)> {
        let path = Self::find_config(no_global_config);
        let (source, (path, style)) = match path
            .as_ref()
            .and_then(|(path, _)| read_to_string(path).ok())
        {
            Some(source) => (source, path.unwrap()),
            None => return Ok(Self::default()),
        };

        match style {
            ConfigStyle::Json => match serde_json::from_str::<ConfigRepr>(&source) {
                Ok(repr) => Ok(Self { repr }),
                Err(err) => {
                    let config_file = SimpleFile::new(path.to_string_lossy().into(), source);
                    let (line, col) = (err.line() - 1, err.column() - 1);
                    let idx = config_file
                        .line_range(0, line)
                        .expect("serde_json yielded an invalid line range")
                        .start
                        + col;

                    let diag =
                        Diagnostic::error(1, "config", err.to_string()).primary(idx..idx, "");
                    Err((config_file, diag))
                }
            },
            ConfigStyle::Toml => match toml::from_str::<ConfigRepr>(&source) {
                Ok(repr) => Ok(Self { repr }),

                Err(err) => {
                    let config_file = SimpleFile::new(path.to_string_lossy().into(), source);
                    let d = if let Some(idx) = err
                        .line_col()
                        .and_then(|(line, col)| Some(config_file.line_range(0, line)?.start + col))
                    {
                        let pos_regex = regex::Regex::new(" at line \\d+ column \\d+$").unwrap();
                        let msg = err.to_string();
                        let msg = pos_regex.replace(&msg, "");
                        Diagnostic::error(1, "config", msg).primary(idx..idx, "")
                    } else {
                        Diagnostic::error(1, "config", err.to_string())
                    };
                    Err((config_file, d))
                }
            },
        }
    }

    fn find_config(global_config: bool) -> Option<(PathBuf, ConfigStyle)> {
        let path = env::current_dir().ok()?;
        fn search_path(path: &Path) -> Option<(PathBuf, ConfigStyle)> {
            for config_name in CONFIG_NAMES.iter() {
                let new_path = path.join(config_name);
                let style = if config_name.ends_with("json") {
                    ConfigStyle::Json
                } else {
                    ConfigStyle::Toml
                };

                if new_path.exists() {
                    return Some((new_path, style));
                }
            }
            None
        }

        for path in path.ancestors() {
            if let Some(res) = search_path(path) {
                return Some(res);
            }
        }

        let path = config_dir()?;
        if let Some(res) = search_path(&path).filter(|_| global_config) {
            return Some(res);
        }

        None
    }

    /// Returns the formatter that should be used.
    pub fn formatter(&self) -> String {
        self.repr.errors.formatter.clone()
    }

    pub fn warning_rule_names(&self) -> impl Iterator<Item = &str> {
        self.repr
            .rules
            .iter()
            .flat_map(|rules| &rules.warnings)
            .map(|rule| rule.name())
    }

    pub fn rule_level_by_name(&self, rule_name: &str) -> RuleLevel {
        if self.warning_rule_names().any(|name| name == rule_name) {
            RuleLevel::Warning
        } else {
            RuleLevel::Error
        }
    }

    /// Collects all rules and creates a `CstRuleStore`.
    ///
    /// # Returns
    ///
    /// The rule store, and any warnings that may have
    /// occurred while collecting the groups.
    pub fn rules_store(&self) -> (CstRuleStore, Vec<Diagnostic>) {
        let rule_cfg = match &self.repr.rules {
            Some(rules) => rules,
            None => return (CstRuleStore::new().recommended(), Vec::new()),
        };

        let mut warnings = vec![];
        let rules = unique_rules(rule_cfg.errors.clone(), rule_cfg.warnings.clone());
        let mut rules = self
            .intersect_allowed(rules, &mut warnings)
            .collect::<Vec<_>>();

        for group in &rule_cfg.groups {
            if let Some(group_rules) = get_group_rules_by_name(group) {
                let list = self.intersect_allowed(group_rules.into_iter(), &mut warnings);
                let list = list.collect::<Vec<_>>();
                rules = unique_rules(rules, list).collect();
            } else {
                let d = Diagnostic::warning(1, "config", format!("unknown rule group '{}'", group));
                warnings.push(d);
            }
        }

        let mut store = CstRuleStore::new();
        store.load_rules(rules);
        (store, warnings)
    }

    /// Remove any rules which are explicitly allowed by the `allowed` field.
    fn intersect_allowed<'s>(
        &'s self,
        rules: impl Iterator<Item = Box<dyn CstRule>> + 's,
        warnings: &'s mut Vec<Diagnostic>,
    ) -> impl Iterator<Item = Box<dyn CstRule>> + 's {
        rules.filter(move |rule| {
            let rule_cfg = match self.repr.rules.as_ref() {
                Some(rule_cfg) => rule_cfg,
                None => return true,
            };

            let res = rule_cfg
                .allowed
                .iter()
                .any(|allowed| allowed == rule.name());

            if res {
                let d = Diagnostic::warning(
                    1,
                    "config",
                    format!(
                        "ignoring configuration for '{}' because it is explicitly allowed",
                        rule.name()
                    ),
                );
                warnings.push(d)
            }

            !res
        })
    }
}

fn unique_rules(first: RuleList, mut second: RuleList) -> impl Iterator<Item = Box<dyn CstRule>> {
    second.retain(|rule| !first.iter().any(|prev| prev.name() == rule.name()));
    first.into_iter().chain(second)
}