rubocop-packs 0.0.22 → 0.0.23

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: 29bc317ca9e4c37dca1d9d9c3f2f734fde84eceb37bb3482351d18af19991b5b
4
- data.tar.gz: 6093679682bef57592958637bfc18ad7f310744b0715e4278364d9e74da4a0bf
3
+ metadata.gz: 906e2ee0ee85e6c02849013bd37034fe6f0f3d8e99ba1597767da463e9f864a4
4
+ data.tar.gz: eeec895514ef54dd70846947ecfd37e5577e842938ea93fc220c4d0b086c1247
5
5
  SHA512:
6
- metadata.gz: 8c5727dca42988c966d1bd9e690499284a6426aa99929b6cbec3ff8b5cfc13f8375f03cbdb14a211eb25ac265dc38c2d4e71860507a0474cb4202c8ddd86cc8a
7
- data.tar.gz: 834246c4402b8f969196ece18718d386d94ab3a428d7317b54f4cad8ec72a860c14ffbb633b802c921415f355cfa602fbbd98a8fe9373289e91ab142b6315ab8
6
+ metadata.gz: 481e3a029eaa4e69e0a5f6cf5736356e0f3dd1bb602c7ef24bda112486fb1c1fcfa7e48b40ff2f7bb2eaa38dbf2d3093d2ac4d3603c8d604876efb5213eef53c
7
+ data.tar.gz: 9c0901dd0da0d2365e53488e1d12d281f407cb271db4c35013046548a2bfbc80cc25f0f47e4d4bec79ba043fca4fb0d37d331ad431a947e5dfd4131ce8b2779c
@@ -146,6 +146,23 @@ module RuboCop
146
146
 
147
147
  errors
148
148
  end
149
+
150
+ sig { params(args: T.untyped).returns(String) }
151
+ def self.execute_rubocop(args)
152
+ with_captured_stdout do
153
+ RuboCop::CLI.new.run(args)
154
+ end
155
+ end
156
+
157
+ sig { params(block: T.untyped).returns(String) }
158
+ def self.with_captured_stdout(&block)
159
+ original_stdout = $stdout # capture previous value of $stdout
160
+ $stdout = StringIO.new # assign a string buffer to $stdout
161
+ yield # perform the body of the user code
162
+ $stdout.string # return the contents of the string buffer
163
+ ensure
164
+ $stdout = original_stdout # restore $stdout to its previous value
165
+ end
149
166
  end
150
167
 
151
168
  private_constant :Private
data/lib/rubocop/packs.rb CHANGED
@@ -23,6 +23,40 @@ module RuboCop
23
23
 
24
24
  private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
25
25
 
26
+ class Offense < T::Struct
27
+ extend T::Sig
28
+
29
+ const :cop_name, String
30
+ const :filepath, String
31
+
32
+ sig { returns(ParsePackwerk::Package) }
33
+ def pack
34
+ ParsePackwerk.package_from_path(filepath)
35
+ end
36
+ end
37
+
38
+ sig { params(paths: T::Array[String], cop_names: T::Array[String]).returns(T::Array[Offense]) }
39
+ def self.offenses_for(paths:, cop_names:)
40
+ path_arguments = paths.join(' ')
41
+ cop_arguments = cop_names.join(',')
42
+ # I think we can potentially use `RuboCop::CLI.new(args)` for this to avoid shelling out and starting another process that needs to reload the bundle
43
+ args = [path_arguments, "--only=#{cop_arguments}", '--format=json']
44
+ puts "Executing: bundle exec rubocop #{args.join(' ')}"
45
+ json = JSON.parse(Private.execute_rubocop(args))
46
+ offenses = T.let([], T::Array[Offense])
47
+ json['files'].each do |file_hash|
48
+ filepath = file_hash['path']
49
+ file_hash['offenses'].each do |offense_hash|
50
+ offenses << Offense.new(
51
+ cop_name: offense_hash['cop_name'],
52
+ filepath: filepath
53
+ )
54
+ end
55
+ end
56
+
57
+ offenses
58
+ end
59
+
26
60
  #
27
61
  # Ideally, this is API that is available to us via `rubocop` itself.
28
62
  # That is: the ability to preserve the location of `.rubocop_todo.yml` files and associate
@@ -30,40 +64,22 @@ module RuboCop
30
64
  #
31
65
  sig { params(packs: T::Array[ParsePackwerk::Package]).void }
32
66
  def self.auto_generate_rubocop_todo(packs:)
33
- pack_arguments = packs.map(&:name).join(' ')
34
- cop_arguments = config.permitted_pack_level_cops.join(',')
35
- command = "bundle exec rubocop #{pack_arguments} --only=#{cop_arguments} --format=json"
36
- puts "Executing: #{command}"
37
- json = JSON.parse(`#{command}`)
38
- new_rubocop_todo_exclusions = {}
39
- json['files'].each do |file_hash|
40
- filepath = file_hash['path']
41
- pack = ParsePackwerk.package_from_path(filepath)
42
- next if pack.name == ParsePackwerk::ROOT_PACKAGE_NAME
43
-
44
- file_hash['offenses'].each do |offense_hash|
45
- cop_name = offense_hash['cop_name']
46
- next unless config.permitted_pack_level_cops.include?(cop_name)
67
+ offenses = offenses_for(
68
+ paths: packs.map(&:name).reject { |name| name == ParsePackwerk::ROOT_PACKAGE_NAME },
69
+ cop_names: config.permitted_pack_level_cops
70
+ )
47
71
 
48
- new_rubocop_todo_exclusions[pack.name] ||= {}
49
- new_rubocop_todo_exclusions[pack.name][filepath] ||= []
50
- new_rubocop_todo_exclusions[pack.name][filepath] << cop_name
51
- end
52
- end
53
-
54
- new_rubocop_todo_exclusions.each do |pack_name, file_hash|
55
- pack = T.must(ParsePackwerk.find(pack_name))
72
+ offenses.group_by(&:pack).each do |pack, offenses_for_pack|
56
73
  rubocop_todo_yml = pack.directory.join(PACK_LEVEL_RUBOCOP_TODO_YML)
57
74
  if rubocop_todo_yml.exist?
58
75
  rubocop_todo = YAML.load_file(rubocop_todo_yml)
59
76
  else
60
77
  rubocop_todo = {}
61
78
  end
62
- file_hash.each do |file, failing_cops|
63
- failing_cops.each do |failing_cop|
64
- rubocop_todo[failing_cop] ||= { 'Exclude' => [] }
65
- rubocop_todo[failing_cop]['Exclude'] << file
66
- end
79
+
80
+ offenses_for_pack.each do |offense|
81
+ rubocop_todo[offense.cop_name] ||= { 'Exclude' => [] }
82
+ rubocop_todo[offense.cop_name]['Exclude'] << offense.filepath
67
83
  end
68
84
 
69
85
  next if rubocop_todo.empty?
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.22
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gusto Engineers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-15 00:00:00.000000000 Z
11
+ date: 2022-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport