rubocop-packs 0.0.36 → 0.0.37

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d8ba7aa60a4c1be7d8958da1e4653ec7fc1c6435885ed66bbc5e7c47850043e
4
- data.tar.gz: b7b3a20d9ddfeefc92464b8695d834b4a9fa740ff7683faab48fbe2ff2e437b3
3
+ metadata.gz: 9a6037ff65ca051c970d7eb45ed00b2e9dc65a597abde34891443abc5a3945b3
4
+ data.tar.gz: c6f640ff0041e6cca7e3d04b1c39275ff11bef980d3b5c3703e6d3a057ec5fa7
5
5
  SHA512:
6
- metadata.gz: 53cc68843262c7688c051446ad996d4962e6e1ca0215b9d600d6633e2c8043f2daf6fe24b4315ce7979f01a824332fe25076d862818af5ea01853ed989ee7e45
7
- data.tar.gz: '09a74c8fd5f4e14aa53a8376af093f9ce414b1689e6ac21a22dd3c894a967fbc5a5c8368a50e4e6ced576e8c014f8394e31365f039abc6df8e1ea676573d0e6d'
6
+ metadata.gz: e38e5df2948250c7f7a2c43f784db818644447ba86106c9abc2f482847845b1d2bc5b75e49cc4c23d02962732e9bab8da0495f2c89b84176ef63a104a0505403
7
+ data.tar.gz: 6d865709e9f1d4d9c29f9cc2b1f37c9eedcb2e17013e2736d470dd9d76af8526c6925483f1c5b76d2d3dc83b4ecacc16e0c866ee5866cb674842019720e4c1fc
data/README.md CHANGED
@@ -41,7 +41,7 @@ Packs/RootNamespaceIsPackName:
41
41
  - lib/example.rb
42
42
  ```
43
43
 
44
- ## Pack-Level `.rubocop.yml` and `.rubocop_todo.yml` files
44
+ ## Pack-Level `package_rubocop.yml` and `package_rubocop_todo.yml` files
45
45
  See [ADVANCED_USAGE.md](ADVANCED_USAGE.md)
46
46
 
47
47
  ## Contributing
data/config/default.yml CHANGED
@@ -27,3 +27,11 @@ PackwerkLite/Privacy:
27
27
  PackwerkLite/Dependency:
28
28
  # It is recommended to use packwerk
29
29
  Enabled: false
30
+
31
+ # We do this inherit *after* setting the defaults so that pack-specific rubocops can override the defaults
32
+ # Relevant documentation:
33
+ # - Inheriting config from a gem:
34
+ # - https://docs.rubocop.org/rubocop/configuration.html#inheriting-configuration-from-a-dependency-gem
35
+ # - ERB in a .rubocop.yml file
36
+ # - https://docs.rubocop.org/rubocop/configuration.html#pre-processing
37
+ <%= RuboCop::Packs.pack_based_rubocop_config %>
@@ -87,8 +87,6 @@ module RuboCop
87
87
  end
88
88
 
89
89
  loaded_rubocop_yml.each_key do |key|
90
- next if key == 'inherit_from'
91
-
92
90
  if !Packs.config.permitted_pack_level_cops.include?(key)
93
91
  errors << <<~ERROR_MESSAGE
94
92
  #{rubocop_yml} contains invalid configuration for #{key}.
data/lib/rubocop/packs.rb CHANGED
@@ -7,8 +7,13 @@ module RuboCop
7
7
  module Packs
8
8
  extend T::Sig
9
9
 
10
- PACK_LEVEL_RUBOCOP_YML = '.rubocop.yml'
11
- PACK_LEVEL_RUBOCOP_TODO_YML = '.rubocop_todo.yml'
10
+ # Pack-level rubocop and rubocop_todo YML files are named differently because they are not integrated
11
+ # into rubocop in the standard way. For example, we could call these the standard `.rubocop.yml` and
12
+ # `.rubocop_todo.yml`. However, this introduces a number of path relativity issues (https://docs.rubocop.org/rubocop/configuration.html#path-relativity)
13
+ # that make this approach not possible. Therefore, for pack level rubocops, we name them in a way that mirrors packwerk `package_todo.yml` files
14
+ # for consistency and to ensure that thes are not read by rubocop except via the ERB templating mechanism.
15
+ PACK_LEVEL_RUBOCOP_YML = 'package_rubocop.yml'
16
+ PACK_LEVEL_RUBOCOP_TODO_YML = 'package_rubocop_todo.yml'
12
17
 
13
18
  PROJECT_ROOT = T.let(Pathname.new(__dir__).parent.parent.expand_path.freeze, Pathname)
14
19
  CONFIG_DEFAULT = T.let(PROJECT_ROOT.join('config', 'default.yml').freeze, Pathname)
@@ -53,7 +58,7 @@ module RuboCop
53
58
  offenses_for_pack.group_by(&:filepath).each do |filepath, offenses_by_filepath|
54
59
  offenses_by_filepath.map(&:cop_name).uniq.each do |cop_name|
55
60
  rubocop_todo[cop_name] ||= { 'Exclude' => [] }
56
- rubocop_todo[cop_name]['Exclude'] << Pathname.new(filepath).relative_path_from(pack.relative_path).to_s
61
+ rubocop_todo[cop_name]['Exclude'] << filepath
57
62
  end
58
63
  end
59
64
 
@@ -87,6 +92,46 @@ module RuboCop
87
92
  end
88
93
  end
89
94
 
95
+ # It would be great if rubocop (upstream) could take in a glob for `inherit_from`, which
96
+ # would allow us to delete this method and this additional complexity.
97
+ sig { params(root_pathname: String).returns(String) }
98
+ def self.pack_based_rubocop_config(root_pathname: Bundler.root)
99
+ rubocop_config = {}
100
+ # We do this because when the ERB is evaluated Dir.pwd is at the directory containing the YML.
101
+ # Ideally rubocop wouldn't change the PWD before invoking this method.
102
+ Dir.chdir(root_pathname) do
103
+ ::Packs.all.each do |package|
104
+ rubocop_todo = package.relative_path.join(PACK_LEVEL_RUBOCOP_TODO_YML)
105
+ if rubocop_todo.exist?
106
+ loaded_rubocop_todo = YAML.load_file(rubocop_todo)
107
+ loaded_rubocop_todo.each do |cop_name, key_config|
108
+ rubocop_config[cop_name] ||= {}
109
+ rubocop_config[cop_name]['Exclude'] ||= []
110
+ rubocop_config[cop_name]['Exclude'] += key_config['Exclude']
111
+ end
112
+ end
113
+
114
+ pack_rubocop = package.relative_path.join(PACK_LEVEL_RUBOCOP_YML)
115
+ next unless pack_rubocop.exist?
116
+
117
+ loaded_pack_rubocop = YAML.load_file(pack_rubocop)
118
+ loaded_pack_rubocop.each do |cop_name, key_config|
119
+ rubocop_config[cop_name] ||= {}
120
+
121
+ if key_config['Enabled']
122
+ rubocop_config[cop_name]['Include'] ||= []
123
+ rubocop_config[cop_name]['Include'] << package.relative_path.join('**/*').to_s
124
+ else
125
+ rubocop_config[cop_name]['Exclude'] ||= []
126
+ rubocop_config[cop_name]['Exclude'] << package.relative_path.join('**/*').to_s
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ YAML.dump(rubocop_config)
133
+ end
134
+
90
135
  sig { void }
91
136
  def self.bust_cache!
92
137
  config.bust_cache!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-packs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.36
4
+ version: 0.0.37
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gusto Engineers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-29 00:00:00.000000000 Z
11
+ date: 2023-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport