pronto-rubocop 0.10.0 → 0.11.0

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: '061479d3c9326adbd73af0ee8e79c5a1f46ed3aa8f8ea8dfa7b7946a899e4aff'
4
- data.tar.gz: 22ab34372429508f391103d663cb462650fac8c08110771ff897400a22662a42
3
+ metadata.gz: '0588bd2b9b7d1c7a702dba2d9a5cdfc74e4d5df1eaa893557aa13445ab82c9d7'
4
+ data.tar.gz: 6bd89ce4bcec0647adc3c62abe770c8f43aff21d77b96a6c9679f6e35ffc84c2
5
5
  SHA512:
6
- metadata.gz: c921d367340e6b725fd6f5c057c0d7ba3fbdab8b03f1ea5a56a559a6336ca646fca67f0f4f6467822db4c300779a015565fb48cf099bf22a38a549f6af8f846e
7
- data.tar.gz: f1711fd2fb7c1bdd9c2374307879eb91e3af059928668a92864def3fea788f3f884075c08b9416623e770aa6309b13a6088d0308a4451047ee5ef529dea55647
6
+ metadata.gz: 2177a1cdf31387028a4dac6aed07b0df8379c33e84d80ac3b1329187beb99ad6b3bb490d1c70b4c098345b5cfac538463beff2767fe0bb28c094e78cbdcd14f1
7
+ data.tar.gz: 48856034490369353ed28755decda13d3e0f225468c8600a2706257ec2a119b88516de1757e7b5a5969ebd76e00e16fd908ca2b0e642901c1f51389a6ce483fc
@@ -0,0 +1,3 @@
1
+ # Order is important. The last matching pattern takes the most precedence.
2
+ # Default owners for everything in the repo.
3
+ * @prontolabs/core
@@ -0,0 +1,22 @@
1
+ name: Checks
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ ruby:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0']
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ bundler-cache: true
21
+ - name: rake spec
22
+ run: bundle exec rake spec
data/README.md CHANGED
@@ -1,12 +1,40 @@
1
- # Pronto runner for Rubocop
1
+ # Pronto runner for RuboCop
2
2
 
3
3
  [![Code Climate](https://codeclimate.com/github/prontolabs/pronto-rubocop.png)](https://codeclimate.com/github/prontolabs/pronto-rubocop)
4
4
  [![Build Status](https://travis-ci.org/prontolabs/pronto-rubocop.svg?branch=master)](https://travis-ci.org/prontolabs/pronto-rubocop)
5
5
  [![Gem Version](https://badge.fury.io/rb/pronto-rubocop.png)](http://badge.fury.io/rb/pronto-rubocop)
6
6
 
7
- Pronto runner for [Rubocop](https://github.com/bbatsov/rubocop), ruby code analyzer. [What is Pronto?](https://github.com/prontolabs/pronto)
7
+ Pronto runner for [RuboCop](https://github.com/bbatsov/rubocop), ruby code
8
+ analyzer. [What is Pronto?](https://github.com/prontolabs/pronto)
8
9
 
9
10
  ## Configuration
10
11
 
11
- Configuring Rubocop via .rubocop.yml will work just fine with pronto-rubocop.
12
- You can also specify a custom `.rubocop.yml` location with the environment variable `RUBOCOP_CONFIG`
12
+ Configuring RuboCop via `.rubocop.yml` will work just fine with
13
+ `pronto-rubocop`.
14
+
15
+ You can also specify a custom `.rubocop.yml` location with the environment
16
+ variable `RUBOCOP_CONFIG`.
17
+
18
+ You can also provide additional configuration via `.pronto.yml`:
19
+
20
+ ```yml
21
+ rubocop:
22
+ # Map of RuboCop severity level to Pronto severity level
23
+ severities:
24
+ refactor: info
25
+ warning: error
26
+
27
+ # Enable suggestions
28
+ suggestions: true
29
+ ```
30
+
31
+ ## Suggestions
32
+
33
+ When suggestions are enabled, the messages will include a line suggesting
34
+ what to change, using [GitHub's](https://twitter.com/wa7son/status/1052326282900443137)
35
+ syntax on Pull Request reviews, that can be approved in one click right from
36
+ the Pull Request.
37
+
38
+ For example:
39
+
40
+ ![GitHub screenshot with suggestion](https://user-images.githubusercontent.com/132/50402757-1bd75b80-0799-11e9-809f-8b8a23ed33f6.png)
@@ -1,14 +1,18 @@
1
1
  require 'pronto'
2
2
  require 'rubocop'
3
3
  require 'pronto/rubocop/patch_cop'
4
+ require 'pronto/rubocop/offense_line'
4
5
 
5
6
  module Pronto
6
7
  class Rubocop < Runner
7
8
  def run
8
9
  ruby_patches
9
10
  .select { |patch| patch.additions > 0 }
10
- .map { |patch| PatchCop.new(patch, self).messages }
11
- .flatten
11
+ .flat_map { |patch| PatchCop.new(patch, self).messages }
12
+ end
13
+
14
+ def pronto_rubocop_config
15
+ @pronto_rubocop_config ||= Pronto::ConfigFile.new.to_h['rubocop'] || {}
12
16
  end
13
17
  end
14
18
  end
@@ -0,0 +1,128 @@
1
+ module Pronto
2
+ class Rubocop < Runner
3
+ class OffenseLine
4
+ def initialize(patch_cop, offense, line)
5
+ @patch_cop = patch_cop
6
+ @offense = offense
7
+ @line = line
8
+ end
9
+
10
+ def message
11
+ Message.new(path, line, level, message_text, nil, Pronto::Rubocop)
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :patch_cop, :offense, :line
17
+
18
+ def path
19
+ line.patch.delta.new_file[:path]
20
+ end
21
+
22
+ def processed_source
23
+ patch_cop.processed_source
24
+ end
25
+
26
+ def message_text
27
+ return offense.message unless suggestion_text
28
+
29
+ "#{offense.message}\n\n```suggestion\n#{suggestion_text}```"
30
+ end
31
+
32
+ def suggestion_text
33
+ return unless patch_cop.runner.pronto_rubocop_config['suggestions']
34
+ return if corrections_count == 0
35
+ return if differing_lines_count != corrections_count
36
+
37
+ @suggestion_text ||= corrected_lines[offense.line - 1]
38
+ end
39
+
40
+ def corrected_lines
41
+ @corrected_lines ||= corrector.rewrite.lines
42
+ end
43
+
44
+ def differing_lines_count
45
+ original_lines.each_with_index.count do |line, index|
46
+ line != corrected_lines[index]
47
+ end
48
+ end
49
+
50
+ def original_lines
51
+ processed_source.lines.join("\n").lines
52
+ end
53
+
54
+ if ::RuboCop::Cop::Team.respond_to?(:mobilize)
55
+ MOBILIZE = :mobilize
56
+
57
+ def report
58
+ @report ||= autocorrect_team.investigate(processed_source).cop_reports.first
59
+ end
60
+
61
+ def corrector
62
+ report.corrector
63
+ end
64
+
65
+ def corrections_count
66
+ report.offenses.size
67
+ end
68
+ else
69
+ # rubocop < v0.85.0
70
+ MOBILIZE = :new
71
+
72
+ def corrector
73
+ @corrector ||= begin
74
+ autocorrect_team.inspect_file(processed_source)
75
+ corrector = RuboCop::Cop::Corrector.new(processed_source.buffer)
76
+ corrector.corrections.concat(autocorrect_team.cops.first.corrections)
77
+ corrector
78
+ end
79
+ end
80
+
81
+ def corrections_count
82
+ @corrections_count ||= corrector.corrections.count
83
+ end
84
+ end
85
+
86
+ def autocorrect_team
87
+ @autocorrect_team ||=
88
+ ::RuboCop::Cop::Team.send(MOBILIZE,
89
+ ::RuboCop::Cop::Registry.new([cop_class]),
90
+ patch_cop.rubocop_config,
91
+ auto_correct: true,
92
+ stdin: true,
93
+ )
94
+ end
95
+
96
+ def cop_class
97
+ patch_cop.registry.find_by_cop_name(offense.cop_name)
98
+ end
99
+
100
+ def level
101
+ severities.fetch(offense.severity.name)
102
+ end
103
+
104
+ def severities
105
+ @severities ||= DEFAULT_SEVERITIES.merge(config_severities)
106
+ end
107
+
108
+ def config_severities
109
+ patch_cop
110
+ .runner
111
+ .pronto_rubocop_config
112
+ .fetch('severities', {})
113
+ .map { |k, v| [k.to_sym, v.to_sym] }
114
+ .to_h
115
+ end
116
+
117
+ DEFAULT_SEVERITIES = {
118
+ refactor: :warning,
119
+ convention: :warning,
120
+ warning: :warning,
121
+ error: :error,
122
+ fatal: :fatal
123
+ }.freeze
124
+
125
+ private_constant :DEFAULT_SEVERITIES
126
+ end
127
+ end
128
+ end
@@ -1,6 +1,8 @@
1
1
  module Pronto
2
2
  class Rubocop < Runner
3
3
  class PatchCop
4
+ attr_reader :runner
5
+
4
6
  def initialize(patch, runner)
5
7
  @patch = patch
6
8
  @runner = runner
@@ -9,21 +11,40 @@ module Pronto
9
11
  def messages
10
12
  return [] unless valid?
11
13
 
12
- offences.map do |offence|
14
+ offenses.flat_map do |offense|
13
15
  patch
14
16
  .added_lines
15
- .select { |line| line.new_lineno == offence.line }
16
- .map { |line| new_message(offence, line) }
17
+ .select { |line| line.new_lineno == offense.line }
18
+ .map { |line| OffenseLine.new(self, offense, line).message }
19
+ end
20
+ end
21
+
22
+ def processed_source
23
+ @processed_source ||= ::RuboCop::ProcessedSource.from_file(
24
+ path,
25
+ rubocop_config.target_ruby_version
26
+ )
27
+ end
28
+
29
+ def registry
30
+ @registry ||= ::RuboCop::Cop::Registry.new(RuboCop::Cop::Cop.all)
31
+ end
32
+
33
+ def rubocop_config
34
+ @rubocop_config ||= begin
35
+ store = ::RuboCop::ConfigStore.new
36
+ store.for(path)
17
37
  end
18
38
  end
19
39
 
20
40
  private
21
41
 
22
- attr_reader :patch, :runner
42
+ attr_reader :patch
23
43
 
24
44
  def valid?
25
- return false if config.file_to_exclude?(path)
26
- return true if config.file_to_include?(path)
45
+ return false if rubocop_config.file_to_exclude?(path)
46
+ return true if rubocop_config.file_to_include?(path)
47
+
27
48
  true
28
49
  end
29
50
 
@@ -31,15 +52,7 @@ module Pronto
31
52
  @path ||= patch.new_file_full_path.to_s
32
53
  end
33
54
 
34
- def config
35
- @config ||= begin
36
- store = ::RuboCop::ConfigStore.new
37
- store.options_config = ENV['RUBOCOP_CONFIG'] if ENV['RUBOCOP_CONFIG']
38
- store.for(path)
39
- end
40
- end
41
-
42
- def offences
55
+ def offenses
43
56
  team
44
57
  .inspect_file(processed_source)
45
58
  .sort
@@ -47,32 +60,13 @@ module Pronto
47
60
  end
48
61
 
49
62
  def team
50
- @team ||= ::RuboCop::Cop::Team.new(registry, config)
51
- end
52
-
53
- def registry
54
- @registry ||= ::RuboCop::Cop::Registry.new(RuboCop::Cop::Cop.all)
55
- end
56
-
57
- def processed_source
58
- @processed_source ||=
59
- ::RuboCop::ProcessedSource.from_file(path, config.target_ruby_version)
60
- end
61
-
62
- def new_message(offence, line)
63
- path = line.patch.delta.new_file[:path]
64
- level = level(offence.severity.name)
65
-
66
- Message.new(path, line, level, offence.message, nil, runner.class)
67
- end
68
-
69
- def level(severity)
70
- case severity
71
- when :refactor, :convention
72
- :warning
73
- when :warning, :error, :fatal
74
- severity
75
- end
63
+ @team ||=
64
+ if ::RuboCop::Cop::Team.respond_to?(:mobilize)
65
+ # rubocop v0.85.0 and later
66
+ ::RuboCop::Cop::Team.mobilize(registry, rubocop_config)
67
+ else
68
+ ::RuboCop::Cop::Team.new(registry, rubocop_config)
69
+ end
76
70
  end
77
71
  end
78
72
  end
@@ -1,5 +1,5 @@
1
1
  module Pronto
2
2
  module RubocopVersion
3
- VERSION = '0.10.0'.freeze
3
+ VERSION = '0.11.0'.freeze
4
4
  end
5
5
  end
@@ -32,8 +32,8 @@ Gem::Specification.new do |s|
32
32
  s.extra_rdoc_files = ['LICENSE', 'README.md']
33
33
  s.require_paths = ['lib']
34
34
 
35
- s.add_runtime_dependency('rubocop', '~> 0.50', '>= 0.49.1')
36
- s.add_runtime_dependency('pronto', '~> 0.10.0')
35
+ s.add_runtime_dependency('pronto', '~> 0.11.0')
36
+ s.add_runtime_dependency('rubocop', '>= 0.49.1', '< 1.0')
37
37
  s.add_development_dependency('rake', '~> 12.0')
38
38
  s.add_development_dependency('rspec', '~> 3.4')
39
39
  s.add_development_dependency('rspec-its', '~> 1.2')
metadata CHANGED
@@ -1,49 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pronto-rubocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mindaugas Mozūras
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-03 00:00:00.000000000 Z
11
+ date: 2021-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rubocop
14
+ name: pronto
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 0.49.1
20
17
  - - "~>"
21
18
  - !ruby/object:Gem::Version
22
- version: '0.50'
19
+ version: 0.11.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: 0.49.1
30
24
  - - "~>"
31
25
  - !ruby/object:Gem::Version
32
- version: '0.50'
26
+ version: 0.11.0
33
27
  - !ruby/object:Gem::Dependency
34
- name: pronto
28
+ name: rubocop
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
- - - "~>"
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.49.1
34
+ - - "<"
38
35
  - !ruby/object:Gem::Version
39
- version: 0.10.0
36
+ version: '1.0'
40
37
  type: :runtime
41
38
  prerelease: false
42
39
  version_requirements: !ruby/object:Gem::Requirement
43
40
  requirements:
44
- - - "~>"
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.49.1
44
+ - - "<"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.10.0
46
+ version: '1.0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -94,9 +94,12 @@ extra_rdoc_files:
94
94
  - LICENSE
95
95
  - README.md
96
96
  files:
97
+ - ".github/CODEOWNERS"
98
+ - ".github/workflows/checks.yml"
97
99
  - LICENSE
98
100
  - README.md
99
101
  - lib/pronto/rubocop.rb
102
+ - lib/pronto/rubocop/offense_line.rb
100
103
  - lib/pronto/rubocop/patch_cop.rb
101
104
  - lib/pronto/rubocop/version.rb
102
105
  - pronto-rubocop.gemspec
@@ -119,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
122
  - !ruby/object:Gem::Version
120
123
  version: '0'
121
124
  requirements: []
122
- rubygems_version: 3.0.1
125
+ rubygems_version: 3.0.3
123
126
  signing_key:
124
127
  specification_version: 4
125
128
  summary: Pronto runner for Rubocop, ruby code analyzer