rrubocop 0.7.5

10x faster RuboCop drop-in. Same output format and configs, without maintaining RuboCop gems.
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! RuboCop-compatible `.rubocop.yml` resolution (ported from nitrocop).
//!
//! Supports `inherit_from`, `inherit_gem`, `require`/`plugins`, `inherit_mode`,
//! nested configs, `DisabledByDefault`, and `NewCops` / `Enabled: pending`.

pub mod gem_path;
mod gem_configs;
mod gem_path_local;
mod gem_path_version;

mod types;
mod yaml_read;
mod globutil;
mod filter;
mod filter_path;
mod filter_match;
mod discover;
mod standard_convert;
mod standard;
mod merge;
mod merge_cop;
mod parse;
mod parse_allcops;
mod parse_cop;
mod load_defaults;
mod load_require;
mod load_gems;
mod load_recursive;
mod load_lockfile;
mod load_resolve;
mod load;
mod ruby_ver;
mod resolved;
mod resolved_state;
mod resolved_enabled;
mod resolved_inject;
mod resolved_cop_cfg;
mod resolved_filters;
mod resolved_effective;
mod resolved_fingerprint;

pub use filter::{CopFilter, CopFilterSet};
pub use load::load_config;
pub use resolved::ResolvedConfig;
pub use types::NewCopsPolicy;

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::{Path, PathBuf};
    use crate::cop::EnabledState;

    fn write_yaml(dir: &Path, name: &str, content: &str) -> PathBuf {
        let path = dir.join(name);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(&path, content).unwrap();
        path
    }

    fn write_config(dir: &Path, content: &str) -> PathBuf {
        write_yaml(dir, ".rubocop.yml", content)
    }

    fn fixtures_dir() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/config")
    }

    #[test]
    fn inherit_from_single_file() {
        let dir = tempfile::tempdir().unwrap();
        write_yaml(
            dir.path(),
            "base.yml",
            "Layout/LineLength:\n  Max: 100\nStyle/Foo:\n  Enabled: true\n",
        );
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: base.yml\nLayout/LineLength:\n  Max: 120\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        let cc = config.cop_config("Layout/LineLength");
        assert_eq!(cc.options.get("Max").and_then(|v| v.as_u64()), Some(120));
        assert!(config.is_cop_enabled("Style/Foo", Path::new("a.rb"), &[], &[]));
    }

    #[test]
    fn inherit_from_child_overrides_base() {
        let dir = tempfile::tempdir().unwrap();
        write_yaml(dir.path(), "base.yml", "Style/Foo:\n  Enabled: true\n");
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: base.yml\nStyle/Foo:\n  Enabled: false\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        assert!(!config.is_cop_enabled("Style/Foo", Path::new("a.rb"), &[], &[]));
    }

    #[test]
    fn inherit_from_exclude_appends() {
        let dir = tempfile::tempdir().unwrap();
        write_yaml(
            dir.path(),
            "base.yml",
            "Style/Foo:\n  Exclude:\n    - 'vendor/**'\n",
        );
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: base.yml\nStyle/Foo:\n  Exclude:\n    - 'tmp/**'\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        let cc = config.cop_config("Style/Foo");
        assert!(cc.exclude.contains(&"vendor/**".to_string()));
        assert!(cc.exclude.contains(&"tmp/**".to_string()));
    }

    #[test]
    fn inherit_from_include_replaces() {
        let dir = tempfile::tempdir().unwrap();
        write_yaml(
            dir.path(),
            "base.yml",
            "Style/Foo:\n  Include:\n    - '**/*.rb'\n",
        );
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: base.yml\nStyle/Foo:\n  Include:\n    - 'app/**'\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        assert_eq!(
            config.cop_config("Style/Foo").include,
            vec!["app/**".to_string()]
        );
    }

    #[test]
    fn allcops_exclude_merges_by_default() {
        // RuboCop default inherit_mode merges Exclude for AllCops.
        let dir = tempfile::tempdir().unwrap();
        write_yaml(
            dir.path(),
            "parent.yml",
            "AllCops:\n  Exclude:\n    - 'vendor/**/*'\nLayout/TrailingWhitespace:\n  Enabled: true\n",
        );
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: parent.yml\nAllCops:\n  Exclude:\n    - 'tmp/**/*'\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        let excludes = config.global_excludes();
        assert!(
            excludes.iter().any(|e| e == "vendor/**/*"),
            "parent Exclude should remain: {excludes:?}"
        );
        assert!(
            excludes.iter().any(|e| e == "tmp/**/*"),
            "child Exclude should apply: {excludes:?}"
        );
    }

    #[test]
    fn allcops_exclude_override_replaces() {
        // Override replaces inherit_from Exclude only; RuboCop built-in defaults
        // (vendor/**/*, …) remain. Use a custom pattern so defaults don't mask it.
        let dir = tempfile::tempdir().unwrap();
        write_yaml(
            dir.path(),
            "parent.yml",
            "AllCops:\n  Exclude:\n    - 'spec/fixtures/**/*'\n",
        );
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: parent.yml\ninherit_mode:\n  override:\n    - Exclude\nAllCops:\n  Exclude:\n    - 'coverage/**/*'\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        let excludes = config.global_excludes();
        assert!(
            !excludes.iter().any(|e| e == "spec/fixtures/**/*"),
            "inherited custom exclude should be replaced: {excludes:?}"
        );
        assert!(
            excludes.iter().any(|e| e == "coverage/**/*"),
            "local coverage exclude missing: {excludes:?}"
        );
    }

    #[test]
    fn inherit_mode_merge_include() {
        let dir = tempfile::tempdir().unwrap();
        write_yaml(
            dir.path(),
            "base.yml",
            "Style/Foo:\n  Include:\n    - '**/*.rb'\n",
        );
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: base.yml\ninherit_mode:\n  merge:\n    - Include\nStyle/Foo:\n  Include:\n    - '**/*.rake'\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        let cc = config.cop_config("Style/Foo");
        assert!(cc.include.contains(&"**/*.rb".to_string()));
        assert!(cc.include.contains(&"**/*.rake".to_string()));
    }

    #[test]
    fn inherit_mode_override_exclude() {
        let dir = tempfile::tempdir().unwrap();
        write_yaml(
            dir.path(),
            "base.yml",
            "Style/Foo:\n  Exclude:\n    - 'vendor/**'\n",
        );
        let path = write_yaml(
            dir.path(),
            ".rubocop.yml",
            "inherit_from: base.yml\ninherit_mode:\n  override:\n    - Exclude\nStyle/Foo:\n  Exclude:\n    - 'tmp/**'\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        let cc = config.cop_config("Style/Foo");
        assert!(!cc.exclude.contains(&"vendor/**".to_string()));
        assert!(cc.exclude.contains(&"tmp/**".to_string()));
    }

    #[test]
    fn disabled_by_default_disables_unset_cops() {
        let dir = tempfile::tempdir().unwrap();
        let path = write_config(
            dir.path(),
            "AllCops:\n  DisabledByDefault: true\nStyle/Foo:\n  Enabled: true\n",
        );
        let config = load_config(Some(&path), None, None).unwrap();
        assert!(config.is_cop_enabled("Style/Foo", Path::new("a.rb"), &[], &[]));
        assert!(!config.is_cop_enabled("Style/Bar", Path::new("a.rb"), &[], &[]));
    }

    #[test]
    fn circular_inherit_from_breaks_cycle() {
        let path = fixtures_dir().join("inherit_from/circular_a.yml");
        let result = load_config(Some(&path), None, None);
        assert!(result.is_ok(), "cycle should be broken: {result:?}");
    }

    #[test]
    fn diamond_dependency_loads() {
        let path = fixtures_dir().join("inherit_from/diamond_root.yml");
        let config = load_config(Some(&path), None, None).unwrap();
        assert!(!config.is_cop_enabled("Style/Foo", Path::new("a.rb"), &[], &[]));
        assert!(config.is_cop_enabled(
            "Style/FrozenStringLiteralComment",
            Path::new("a.rb"),
            &[],
            &[]
        ));
        assert!(config.is_cop_enabled("Style/StringLiterals", Path::new("a.rb"), &[], &[]));
    }

    #[test]
    fn fixture_inherit_from_merges() {
        let path = fixtures_dir().join("inherit_from/child.yml");
        let config = load_config(Some(&path), None, None).unwrap();
        assert_eq!(
            config
                .cop_config("Layout/LineLength")
                .options
                .get("Max")
                .and_then(|v| v.as_u64()),
            Some(120)
        );
        let excludes = config.global_excludes();
        assert!(excludes.contains(&"vendor/**".to_string()));
        assert!(excludes.contains(&"tmp/**".to_string()));
    }

    fn assert_nested_cop_state(config: &ResolvedConfig, dir: &Path) {
        assert!(config.has_dir_overrides());
        assert_eq!(
            config.cop_config("Style/Foo").enabled,
            EnabledState::True
        );
        let nested = config.cop_config_for_file("Style/Foo", &dir.join("spec/a.rb"));
        assert_eq!(nested.enabled, EnabledState::False);
    }

    #[test]
    fn nested_dir_override_disables_cop() {
        let dir = tempfile::tempdir().unwrap();
        write_config(dir.path(), "Style/Foo:\n  Enabled: true\n");
        write_yaml(
            dir.path(),
            "spec/.rubocop.yml",
            "Style/Foo:\n  Enabled: false\n",
        );
        let config = load_config(None, Some(dir.path()), None).unwrap();
        assert_nested_cop_state(&config, dir.path());
    }

    fn parent_config_nested_fixture() -> (tempfile::TempDir, PathBuf) {
        let home = tempfile::tempdir().unwrap();
        let project = home.path().join("project");
        fs::create_dir_all(&project).unwrap();
        write_config(home.path(), "Style/Foo:\n  Enabled: true\n");
        write_yaml(
            &project,
            "spec/.rubocop.yml",
            "Style/Foo:\n  Enabled: false\n",
        );
        write_yaml(
            home.path(),
            "sibling/.rubocop.yml",
            "Style/Foo:\n  Enabled: false\n",
        );
        (home, project)
    }

    fn assert_nested_overrides_scoped_to_scan_root(config: &ResolvedConfig) {
        assert!(config
            .dir_overrides
            .iter()
            .any(|(d, _)| d.ends_with("spec")));
        assert!(!config
            .dir_overrides
            .iter()
            .any(|(d, _)| d.ends_with("sibling")));
        assert_eq!(
            config
                .cop_config_for_file("Style/Foo", Path::new("spec/a.rb"))
                .enabled,
            EnabledState::False
        );
    }

    #[test]
    fn nested_overrides_scoped_to_scan_root_not_config_dir() {
        let (_home, project) = parent_config_nested_fixture();
        let config = load_config(None, Some(&project), None).unwrap();
        assert_nested_overrides_scoped_to_scan_root(&config);
    }

    #[test]
    fn cache_fingerprint_changes_with_config() {
        let dir = tempfile::tempdir().unwrap();
        let a = write_config(dir.path(), "Style/Foo:\n  Enabled: true\n");
        let cfg_a = load_config(Some(&a), None, None).unwrap();
        let b = write_config(dir.path(), "Style/Foo:\n  Enabled: false\n");
        let cfg_b = load_config(Some(&b), None, None).unwrap();
        assert_ne!(cfg_a.cache_fingerprint(), cfg_b.cache_fingerprint());
    }

    #[test]
    fn cache_fingerprint_stable_across_config_reloads() {
        let dir = tempfile::tempdir().unwrap();
        let path = write_config(
            dir.path(),
            "Metrics/MethodLength:\n  Max: 10\n  CountAsOne: array\n",
        );
        let a = load_config(Some(&path), None, None).unwrap();
        let b = load_config(Some(&path), None, None).unwrap();
        assert_eq!(a.cache_fingerprint(), b.cache_fingerprint());
    }

    #[test]
    fn cache_fingerprint_differs_by_scan_root() {
        let home = tempfile::tempdir().unwrap();
        let project_a = home.path().join("a");
        let project_b = home.path().join("b");
        fs::create_dir_all(&project_a).unwrap();
        fs::create_dir_all(&project_b).unwrap();
        write_config(home.path(), "Style/Foo:\n  Enabled: true\n");
        write_yaml(
            &project_a,
            "spec/.rubocop.yml",
            "Style/Foo:\n  Enabled: false\n",
        );
        let cfg_a = load_config(None, Some(&project_a), None).unwrap();
        let cfg_b = load_config(None, Some(&project_b), None).unwrap();
        assert_ne!(cfg_a.cache_fingerprint(), cfg_b.cache_fingerprint());
    }

    #[test]
    fn rails_guides_nested_disables_redundant_percent_q() {
        let root = std::path::Path::new("/tmp/parity/rails");
        if !root.join(".rubocop.yml").exists() {
            return;
        }
        let config = load_config(None, Some(root), None).unwrap();
        assert!(
            config.has_dir_overrides(),
            "expected guides/.rubocop.yml override"
        );
        let abs = root.join("guides/test/epub_test.rb");
        let rel = std::path::Path::new("guides/test/epub_test.rb");
        assert!(
            config.disabled_by_dir_override("Style/RedundantPercentQ", &abs),
            "absolute guides path should disable Style/RedundantPercentQ"
        );
        assert!(
            config.disabled_by_dir_override("Style/RedundantPercentQ", rel),
            "relative guides path should disable Style/RedundantPercentQ"
        );
    }
}