rubocop 0.14.1 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -3
- data/CHANGELOG.md +245 -198
- data/README.md +7 -0
- data/Rakefile +5 -1
- data/config/default.yml +27 -4
- data/config/enabled.yml +18 -4
- data/lib/rubocop.rb +13 -1
- data/lib/rubocop/cli.rb +83 -23
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/cop/cop.rb +31 -6
- data/lib/rubocop/cop/lint/block_alignment.rb +11 -8
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +21 -14
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/rails/output.rb +35 -0
- data/lib/rubocop/cop/style/{access_control.rb → access_modifier_indentation.rb} +18 -15
- data/lib/rubocop/cop/style/alias.rb +14 -2
- data/lib/rubocop/cop/style/align_hash.rb +174 -109
- data/lib/rubocop/cop/style/autocorrect_alignment.rb +38 -18
- data/lib/rubocop/cop/style/blocks.rb +4 -6
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +3 -3
- data/lib/rubocop/cop/style/cyclomatic_complexity.rb +46 -0
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +48 -0
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +62 -0
- data/lib/rubocop/cop/style/end_of_line.rb +6 -2
- data/lib/rubocop/cop/style/favor_modifier.rb +11 -1
- data/lib/rubocop/cop/style/final_newline.rb +10 -4
- data/lib/rubocop/cop/style/hash_syntax.rb +32 -21
- data/lib/rubocop/cop/style/leading_comment_space.rb +9 -0
- data/lib/rubocop/cop/style/method_call_parentheses.rb +11 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +11 -15
- data/lib/rubocop/cop/style/redundant_return.rb +7 -4
- data/lib/rubocop/cop/style/redundant_self.rb +3 -3
- data/lib/rubocop/cop/style/signal_exception.rb +4 -2
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +7 -1
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +6 -0
- data/lib/rubocop/cop/style/space_after_method_name.rb +7 -1
- data/lib/rubocop/cop/style/space_after_not.rb +6 -2
- data/lib/rubocop/cop/style/space_around_block_braces.rb +149 -0
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +33 -0
- data/lib/rubocop/cop/style/space_around_operators.rb +169 -0
- data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +6 -0
- data/lib/rubocop/cop/style/space_inside.rb +35 -0
- data/lib/rubocop/cop/style/space_inside_brackets.rb +18 -0
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +99 -0
- data/lib/rubocop/cop/style/space_inside_parens.rb +18 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +52 -25
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/surrounding_space.rb +1 -344
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +17 -5
- data/lib/rubocop/cop/style/trailing_whitespace.rb +9 -5
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -2
- data/lib/rubocop/cop/style/word_array.rb +16 -1
- data/lib/rubocop/cop/team.rb +5 -5
- data/lib/rubocop/cop/util.rb +1 -0
- data/lib/rubocop/formatter/offence_count_formatter.rb +0 -1
- data/lib/rubocop/options.rb +76 -111
- data/lib/rubocop/rake_task.rb +4 -2
- data/lib/rubocop/target_finder.rb +3 -3
- data/lib/rubocop/version.rb +1 -1
- data/spec/rubocop/cli_spec.rb +123 -13
- data/spec/rubocop/config_spec.rb +2 -2
- data/spec/rubocop/cop/lint/rescue_exception_spec.rb +10 -0
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +13 -0
- data/spec/rubocop/cop/offence_spec.rb +2 -0
- data/spec/rubocop/cop/rails/output_spec.rb +40 -0
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +243 -0
- data/spec/rubocop/cop/style/alias_spec.rb +8 -0
- data/spec/rubocop/cop/style/align_array_spec.rb +12 -0
- data/spec/rubocop/cop/style/align_hash_spec.rb +15 -0
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +7 -4
- data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +203 -0
- data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +56 -0
- data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +87 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +17 -8
- data/spec/rubocop/cop/style/favor_modifier_spec.rb +34 -0
- data/spec/rubocop/cop/style/final_newline_spec.rb +5 -0
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +22 -2
- data/spec/rubocop/cop/style/leading_comment_space_spec.rb +5 -0
- data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +39 -4
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +5 -0
- data/spec/rubocop/cop/style/signal_exception_spec.rb +11 -0
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +7 -0
- data/spec/rubocop/cop/style/space_after_comma_spec.rb +5 -0
- data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +29 -8
- data/spec/rubocop/cop/style/space_after_method_name_spec.rb +15 -0
- data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +5 -0
- data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +68 -0
- data/spec/rubocop/cop/style/space_around_equals_in_default_parameter_spec.rb +5 -0
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +43 -0
- data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +23 -0
- data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +7 -0
- data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +65 -23
- data/spec/rubocop/cop/style/space_inside_parens_spec.rb +7 -0
- data/spec/rubocop/cop/style/special_global_vars_spec.rb +12 -2
- data/spec/rubocop/cop/style/string_literals_spec.rb +6 -0
- data/spec/rubocop/cop/style/symbol_array_spec.rb +5 -7
- data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +26 -1
- data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +7 -0
- data/spec/rubocop/cop/style/trivial_accessors_spec.rb +8 -0
- data/spec/rubocop/cop/style/word_array_spec.rb +33 -2
- data/spec/rubocop/cop/team_spec.rb +4 -4
- data/spec/rubocop/formatter/json_formatter_spec.rb +1 -1
- data/spec/rubocop/options_spec.rb +5 -96
- data/spec/rubocop/processed_source_spec.rb +3 -3
- data/spec/spec_helper.rb +28 -23
- data/spec/support/mri_syntax_checker.rb +20 -16
- metadata +24 -5
- data/spec/rubocop/cop/style/access_control_spec.rb +0 -164
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
[![Gem Version](https://badge.fury.io/rb/rubocop.png)](http://badge.fury.io/rb/rubocop)
|
2
|
+
[![Dependency Status](https://gemnasium.com/bbatsov/rubocop.png)](https://gemnasium.com/bbatsov/rubocop)
|
2
3
|
[![Build Status](https://travis-ci.org/bbatsov/rubocop.png?branch=master)](https://travis-ci.org/bbatsov/rubocop)
|
3
4
|
[![Coverage Status](https://coveralls.io/repos/bbatsov/rubocop/badge.png?branch=master)](https://coveralls.io/r/bbatsov/rubocop)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/bbatsov/rubocop.png)](https://codeclimate.com/github/bbatsov/rubocop)
|
4
6
|
|
5
7
|
# RuboCop
|
6
8
|
|
@@ -447,6 +449,11 @@ If you're a ST2 user you might find the
|
|
447
449
|
[Sublime RuboCop plugin](https://github.com/pderichs/sublime_rubocop)
|
448
450
|
useful.
|
449
451
|
|
452
|
+
### Brackets
|
453
|
+
The [brackets-rubocop](https://github.com/smockle/brackets-rubocop)
|
454
|
+
extension displays RuboCop results in Brackets.
|
455
|
+
It can be installed via the extension manager in Brackets.
|
456
|
+
|
450
457
|
### Other Editors
|
451
458
|
|
452
459
|
Here's one great opportunity to contribute to RuboCop - implement
|
data/Rakefile
CHANGED
@@ -13,6 +13,7 @@ end
|
|
13
13
|
require 'rake'
|
14
14
|
require 'rspec/core'
|
15
15
|
require 'rspec/core/rake_task'
|
16
|
+
require 'rubocop/rake_task'
|
16
17
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
17
18
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
18
19
|
end
|
@@ -23,7 +24,10 @@ task :coverage do
|
|
23
24
|
Rake::Task['spec'].execute
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
|
+
desc 'Run RuboCop over itself'
|
28
|
+
Rubocop::RakeTask.new(:internal_investigation)
|
29
|
+
|
30
|
+
task default: [:spec, :internal_investigation]
|
27
31
|
|
28
32
|
require 'yard'
|
29
33
|
YARD::Rake::YardocTask.new
|
data/config/default.yml
CHANGED
@@ -16,6 +16,11 @@ AllCops:
|
|
16
16
|
# directory .rubocop.yml files, or by giving the -R/--rails option.
|
17
17
|
RunRailsCops: false
|
18
18
|
|
19
|
+
# Indent private/protected/public as deep as method definitions
|
20
|
+
AccessModifierIndentation:
|
21
|
+
# supported styles are "indent" and "outdent"
|
22
|
+
EnforcedStyle: indent
|
23
|
+
|
19
24
|
# Align the elements of a hash literal if they span more than one line.
|
20
25
|
AlignHash:
|
21
26
|
# Alignment of entries using hash rocket as separator. Valid values are:
|
@@ -50,6 +55,10 @@ AssignmentInCondition:
|
|
50
55
|
BlockNesting:
|
51
56
|
Max: 3
|
52
57
|
|
58
|
+
BracesAroundHashParameters:
|
59
|
+
# Valid values are: braces, no_braces
|
60
|
+
EnforcedStyle: no_braces
|
61
|
+
|
53
62
|
ClassLength:
|
54
63
|
CountComments: false # count full line comments?
|
55
64
|
Max: 100
|
@@ -72,6 +81,10 @@ CommentAnnotation:
|
|
72
81
|
- HACK
|
73
82
|
- REVIEW
|
74
83
|
|
84
|
+
# Avoid complex methods.
|
85
|
+
CyclomaticComplexity:
|
86
|
+
Max: 6
|
87
|
+
|
75
88
|
# Multi-line method chaining should be done with leading dots.
|
76
89
|
DotPosition:
|
77
90
|
Style: 'leading'
|
@@ -108,6 +121,13 @@ MethodName:
|
|
108
121
|
NumericLiterals:
|
109
122
|
MinDigits: 5
|
110
123
|
|
124
|
+
Output:
|
125
|
+
Ignore:
|
126
|
+
- '^.*\.rake$'
|
127
|
+
- '^.*/script/.*$'
|
128
|
+
- '^.*/tasks/.*$'
|
129
|
+
- 'Rakefile$'
|
130
|
+
|
111
131
|
ParameterLists:
|
112
132
|
Max: 5
|
113
133
|
CountKeywordArgs: true
|
@@ -148,11 +168,15 @@ StringLiterals:
|
|
148
168
|
SpaceAroundBlockBraces:
|
149
169
|
# Valid values are: space_inside_braces, no_space_inside_braces
|
150
170
|
EnforcedStyle: space_inside_braces
|
171
|
+
# Valid values are: space, no_space
|
172
|
+
EnforcedStyleForEmptyBraces: no_space
|
151
173
|
# Space between { and |. Overrides EnforcedStyle if there is a conflict.
|
152
174
|
SpaceBeforeBlockParameters: true
|
153
175
|
|
154
176
|
SpaceInsideHashLiteralBraces:
|
155
|
-
|
177
|
+
# Valid values (for both parameters) are: space, no_space
|
178
|
+
EnforcedStyle: space
|
179
|
+
EnforcedStyleForEmptyBraces: no_space
|
156
180
|
|
157
181
|
SymbolName:
|
158
182
|
AllowCamelCase: true
|
@@ -185,6 +209,5 @@ VariableName:
|
|
185
209
|
# Valid values are: snake_case, camelCase
|
186
210
|
EnforcedStyle: snake_case
|
187
211
|
|
188
|
-
|
189
|
-
|
190
|
-
EnforcedStyle: no_braces
|
212
|
+
WordArray:
|
213
|
+
MinSize: 0
|
data/config/enabled.yml
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
# These are all the cops that are enabled in the default configuration.
|
2
2
|
|
3
|
-
|
4
|
-
Description:
|
5
|
-
Indent private/protected as deep as defs and keep blank
|
6
|
-
lines around them.
|
3
|
+
AccessModifierIndentation:
|
4
|
+
Description: Check indentation of private/protected visibility modifiers.
|
7
5
|
Enabled: true
|
8
6
|
|
9
7
|
Alias:
|
@@ -103,6 +101,10 @@ ConstantName:
|
|
103
101
|
Description: 'Constants should use SCREAMING_SNAKE_CASE.'
|
104
102
|
Enabled: true
|
105
103
|
|
104
|
+
CyclomaticComplexity:
|
105
|
+
Description: 'Avoid complex methods.'
|
106
|
+
Enabled: true
|
107
|
+
|
106
108
|
DefWithParentheses:
|
107
109
|
Description: 'Use def with parentheses when there are arguments.'
|
108
110
|
Enabled: true
|
@@ -125,6 +127,14 @@ EmptyLines:
|
|
125
127
|
Description: "Don't use several empty lines in a row."
|
126
128
|
Enabled: true
|
127
129
|
|
130
|
+
EmptyLinesAroundAccessModifier:
|
131
|
+
Description: "Keep blank lines around access modifiers."
|
132
|
+
Enabled: true
|
133
|
+
|
134
|
+
EmptyLinesAroundBody:
|
135
|
+
Description: "Keeps track of blank lines around expression bodies."
|
136
|
+
Enabled: true
|
137
|
+
|
128
138
|
EmptyLineBetweenDefs:
|
129
139
|
Description: 'Use empty lines between defs.'
|
130
140
|
Enabled: true
|
@@ -569,6 +579,10 @@ HasAndBelongsToMany:
|
|
569
579
|
Description: 'Prefer has_many :through to has_and_belongs_to_many.'
|
570
580
|
Enabled: true
|
571
581
|
|
582
|
+
Output:
|
583
|
+
Description: 'Checks for calls to puts, print, etc.'
|
584
|
+
Enabled: true
|
585
|
+
|
572
586
|
ReadAttribute:
|
573
587
|
Description: 'Prefer self[:attribute] over read_attribute(:attribute).'
|
574
588
|
Enabled: true
|
data/lib/rubocop.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'rainbow'
|
4
4
|
require 'English'
|
5
|
+
require 'set'
|
5
6
|
require 'parser/current'
|
6
7
|
require 'ast/sexp'
|
7
8
|
require 'powerpack'
|
@@ -44,8 +45,9 @@ require 'rubocop/cop/lint/void'
|
|
44
45
|
|
45
46
|
require 'rubocop/cop/style/autocorrect_alignment'
|
46
47
|
require 'rubocop/cop/style/configurable_naming'
|
48
|
+
require 'rubocop/cop/style/cyclomatic_complexity'
|
47
49
|
require 'rubocop/cop/style/string_help'
|
48
|
-
require 'rubocop/cop/style/
|
50
|
+
require 'rubocop/cop/style/access_modifier_indentation'
|
49
51
|
require 'rubocop/cop/style/alias'
|
50
52
|
require 'rubocop/cop/style/align_array'
|
51
53
|
require 'rubocop/cop/style/align_hash'
|
@@ -74,6 +76,8 @@ require 'rubocop/cop/style/documentation'
|
|
74
76
|
require 'rubocop/cop/style/dot_position'
|
75
77
|
require 'rubocop/cop/style/empty_line_between_defs'
|
76
78
|
require 'rubocop/cop/style/empty_lines'
|
79
|
+
require 'rubocop/cop/style/empty_lines_around_body'
|
80
|
+
require 'rubocop/cop/style/empty_lines_around_access_modifier'
|
77
81
|
require 'rubocop/cop/style/empty_literal'
|
78
82
|
require 'rubocop/cop/style/encoding'
|
79
83
|
require 'rubocop/cop/style/end_block'
|
@@ -130,6 +134,13 @@ require 'rubocop/cop/style/special_global_vars'
|
|
130
134
|
require 'rubocop/cop/style/space_before_modifier_keyword'
|
131
135
|
require 'rubocop/cop/style/string_literals'
|
132
136
|
require 'rubocop/cop/style/surrounding_space'
|
137
|
+
require 'rubocop/cop/style/space_around_block_braces'
|
138
|
+
require 'rubocop/cop/style/space_around_equals_in_parameter_default'
|
139
|
+
require 'rubocop/cop/style/space_around_operators'
|
140
|
+
require 'rubocop/cop/style/space_inside'
|
141
|
+
require 'rubocop/cop/style/space_inside_hash_literal_braces'
|
142
|
+
require 'rubocop/cop/style/space_inside_brackets'
|
143
|
+
require 'rubocop/cop/style/space_inside_parens'
|
133
144
|
require 'rubocop/cop/style/symbol_array'
|
134
145
|
require 'rubocop/cop/style/symbol_name'
|
135
146
|
require 'rubocop/cop/style/tab'
|
@@ -146,6 +157,7 @@ require 'rubocop/cop/style/word_array'
|
|
146
157
|
require 'rubocop/cop/style/braces_around_hash_parameters'
|
147
158
|
|
148
159
|
require 'rubocop/cop/rails/has_and_belongs_to_many'
|
160
|
+
require 'rubocop/cop/rails/output'
|
149
161
|
require 'rubocop/cop/rails/read_attribute'
|
150
162
|
require 'rubocop/cop/rails/validation'
|
151
163
|
|
data/lib/rubocop/cli.rb
CHANGED
@@ -24,10 +24,44 @@ module Rubocop
|
|
24
24
|
def run(args = ARGV)
|
25
25
|
trap_interrupt
|
26
26
|
|
27
|
-
@options,
|
27
|
+
@options, remaining_args = Options.new.parse(args)
|
28
|
+
target_files = target_finder.find(remaining_args)
|
28
29
|
|
29
|
-
|
30
|
+
act_on_options(remaining_args)
|
31
|
+
|
32
|
+
any_failed = process_files(target_files)
|
33
|
+
|
34
|
+
display_error_summary(@errors)
|
35
|
+
|
36
|
+
!any_failed && !wants_to_quit ? 0 : 1
|
37
|
+
rescue => e
|
38
|
+
$stderr.puts e.message
|
39
|
+
return 1
|
40
|
+
end
|
41
|
+
|
42
|
+
def trap_interrupt
|
43
|
+
Signal.trap('INT') do
|
44
|
+
exit!(1) if wants_to_quit?
|
45
|
+
self.wants_to_quit = true
|
46
|
+
$stderr.puts
|
47
|
+
$stderr.puts 'Exiting... Interrupt again to exit immediately.'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def display_error_summary(errors)
|
52
|
+
return if errors.empty?
|
53
|
+
plural = errors.count > 1 ? 's' : ''
|
54
|
+
warn "\n#{errors.count} error#{plural} occurred:".color(:red)
|
55
|
+
errors.each { |error| warn error }
|
56
|
+
warn 'Errors are usually caused by RuboCop bugs.'
|
57
|
+
warn 'Please, report your problems to RuboCop\'s issue tracker.'
|
58
|
+
warn 'Mention the following information in the issue report:'
|
59
|
+
warn Rubocop::Version.version(true)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
30
63
|
|
64
|
+
def process_files(target_files)
|
31
65
|
target_files.each(&:freeze).freeze
|
32
66
|
inspected_files = []
|
33
67
|
any_failed = false
|
@@ -49,13 +83,34 @@ module Rubocop
|
|
49
83
|
|
50
84
|
formatter_set.finished(inspected_files.freeze)
|
51
85
|
formatter_set.close_output_files
|
86
|
+
any_failed
|
87
|
+
end
|
52
88
|
|
53
|
-
|
89
|
+
def act_on_options(args)
|
90
|
+
if @options[:show_cops]
|
91
|
+
print_available_cops
|
92
|
+
exit(0)
|
93
|
+
end
|
54
94
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
95
|
+
@config_store.set_options_config(@options[:config]) if @options[:config]
|
96
|
+
|
97
|
+
Sickill::Rainbow.enabled = false if @options[:no_color]
|
98
|
+
|
99
|
+
puts Rubocop::Version.version(false) if @options[:version]
|
100
|
+
puts Rubocop::Version.version(true) if @options[:verbose_version]
|
101
|
+
exit(0) if @options[:version] || @options[:verbose_version]
|
102
|
+
|
103
|
+
ConfigLoader.debug = @options[:debug]
|
104
|
+
|
105
|
+
if @options[:auto_gen_config]
|
106
|
+
target_finder.find(args).each do |file|
|
107
|
+
config = @config_store.for(file)
|
108
|
+
if config.contains_auto_generated_config
|
109
|
+
fail "Remove #{ConfigLoader::AUTO_GENERATED_FILE} from the " +
|
110
|
+
'current configuration before generating it again.'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
59
114
|
end
|
60
115
|
|
61
116
|
def mobilized_cop_classes(config)
|
@@ -85,27 +140,32 @@ module Rubocop
|
|
85
140
|
offences
|
86
141
|
end
|
87
142
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
143
|
+
def print_available_cops
|
144
|
+
cops = Cop::Cop.all
|
145
|
+
puts "Available cops (#{cops.length}) + config for #{Dir.pwd.to_s}: "
|
146
|
+
dirconf = @config_store.for(Dir.pwd.to_s)
|
147
|
+
cops.types.sort!.each do |type|
|
148
|
+
coptypes = cops.with_type(type).sort_by!(&:cop_name)
|
149
|
+
puts "Type '#{type.to_s.capitalize}' (#{coptypes.size}):"
|
150
|
+
coptypes.each do |cop|
|
151
|
+
puts " - #{cop.cop_name}"
|
152
|
+
cnf = dirconf.for_cop(cop).dup
|
153
|
+
print_conf_option('Description',
|
154
|
+
cnf.delete('Description') { 'None' })
|
155
|
+
cnf.each { |k, v| print_conf_option(k, v) }
|
156
|
+
print_conf_option('SupportsAutoCorrection',
|
157
|
+
cop.new.support_autocorrect?.to_s)
|
158
|
+
end
|
94
159
|
end
|
95
160
|
end
|
96
161
|
|
97
|
-
def
|
98
|
-
|
99
|
-
plural = errors.count > 1 ? 's' : ''
|
100
|
-
warn "\n#{errors.count} error#{plural} occurred:".color(:red)
|
101
|
-
errors.each { |error| warn error }
|
102
|
-
warn 'Errors are usually caused by RuboCop bugs.'
|
103
|
-
warn 'Please, report your problems to RuboCop\'s issue tracker.'
|
104
|
-
warn 'Mention the following information in the issue report:'
|
105
|
-
warn Rubocop::Version.version(true)
|
162
|
+
def print_conf_option(option, value)
|
163
|
+
puts " - #{option}: #{value}"
|
106
164
|
end
|
107
165
|
|
108
|
-
|
166
|
+
def target_finder
|
167
|
+
@target_finder ||= TargetFinder.new(@config_store, @options[:debug])
|
168
|
+
end
|
109
169
|
|
110
170
|
def run_rails_cops?(config)
|
111
171
|
@options[:rails] || config['AllCops']['RunRailsCops']
|
data/lib/rubocop/config.rb
CHANGED
data/lib/rubocop/cop/cop.rb
CHANGED
@@ -2,9 +2,10 @@
|
|
2
2
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
|
+
class CorrectionNotPossible < Exception; end
|
6
|
+
|
5
7
|
# Store for all cops with helper functions
|
6
8
|
class CopStore < ::Array
|
7
|
-
|
8
9
|
# @return [Array<String>] list of types for current cops.
|
9
10
|
def types
|
10
11
|
@types = map(&:cop_type).uniq! unless defined? @types
|
@@ -15,6 +16,11 @@ module Rubocop
|
|
15
16
|
def with_type(type)
|
16
17
|
select { |c| c.cop_type == type }
|
17
18
|
end
|
19
|
+
|
20
|
+
# @return [Array<Cop>] Cops not for a specific type.
|
21
|
+
def without_type(type)
|
22
|
+
reject { |c| c.cop_type == type }
|
23
|
+
end
|
18
24
|
end
|
19
25
|
|
20
26
|
# A scaffold for concrete cops.
|
@@ -55,6 +61,10 @@ module Rubocop
|
|
55
61
|
@all.clone
|
56
62
|
end
|
57
63
|
|
64
|
+
def self.non_rails
|
65
|
+
@all.without_type(:rails)
|
66
|
+
end
|
67
|
+
|
58
68
|
def self.inherited(subclass)
|
59
69
|
@all << subclass
|
60
70
|
end
|
@@ -81,7 +91,7 @@ module Rubocop
|
|
81
91
|
|
82
92
|
def initialize(config = nil, options = nil)
|
83
93
|
@config = config || Config.new
|
84
|
-
@options = options || {
|
94
|
+
@options = options || { auto_correct: false, debug: false }
|
85
95
|
|
86
96
|
@offences = []
|
87
97
|
@corrections = []
|
@@ -93,7 +103,7 @@ module Rubocop
|
|
93
103
|
end
|
94
104
|
|
95
105
|
def autocorrect?
|
96
|
-
@options[:
|
106
|
+
@options[:auto_correct] && support_autocorrect?
|
97
107
|
end
|
98
108
|
|
99
109
|
def debug?
|
@@ -115,10 +125,14 @@ module Rubocop
|
|
115
125
|
|
116
126
|
message = message ? message : message(node)
|
117
127
|
message = debug? ? "#{name}: #{message}" : message
|
118
|
-
@offences <<
|
119
|
-
Offence.new(severity, location, message, name, autocorrect?)
|
120
128
|
|
121
|
-
|
129
|
+
corrected = begin
|
130
|
+
autocorrect(node) if autocorrect?
|
131
|
+
autocorrect?
|
132
|
+
rescue CorrectionNotPossible
|
133
|
+
false
|
134
|
+
end
|
135
|
+
@offences << Offence.new(severity, location, message, name, corrected)
|
122
136
|
end
|
123
137
|
|
124
138
|
def convention(node, location, message = nil)
|
@@ -194,6 +208,17 @@ module Rubocop
|
|
194
208
|
Parser::Source::Range.new(source_buffer, begin_pos,
|
195
209
|
begin_pos + column_count)
|
196
210
|
end
|
211
|
+
|
212
|
+
def range_with_surrounding_space(range, side = :both)
|
213
|
+
src = @processed_source.buffer.source
|
214
|
+
go_left = side == :left || side == :both
|
215
|
+
go_right = side == :right || side == :both
|
216
|
+
begin_pos = range.begin_pos
|
217
|
+
begin_pos -= 1 while go_left && src[begin_pos - 1] =~ /[ \t]/
|
218
|
+
end_pos = range.end_pos
|
219
|
+
end_pos += 1 while go_right && src[end_pos] =~ /[ \t]/
|
220
|
+
Parser::Source::Range.new(@processed_source.buffer, begin_pos, end_pos)
|
221
|
+
end
|
197
222
|
end
|
198
223
|
end
|
199
224
|
end
|
@@ -129,23 +129,26 @@ module Rubocop
|
|
129
129
|
match = /\S.*/.match(do_loc.source_line)
|
130
130
|
indentation_of_do_line = match.begin(0)
|
131
131
|
if end_loc.column != indentation_of_do_line
|
132
|
-
alt_start_msg = if start_loc.line == do_loc.line &&
|
133
|
-
start_loc.column == indentation_of_do_line
|
134
|
-
''
|
135
|
-
else
|
136
|
-
" or #{match[0]} at #{do_loc.line}, " +
|
137
|
-
"#{indentation_of_do_line}"
|
138
|
-
end
|
139
132
|
warning(nil,
|
140
133
|
end_loc,
|
141
134
|
sprintf(MSG, end_loc.line, end_loc.column,
|
142
135
|
start_loc.source.lines.to_a.first.chomp,
|
143
136
|
start_loc.line, start_loc.column,
|
144
|
-
alt_start_msg
|
137
|
+
alt_start_msg(match, start_loc, do_loc,
|
138
|
+
indentation_of_do_line)))
|
145
139
|
end
|
146
140
|
end
|
147
141
|
end
|
148
142
|
|
143
|
+
def alt_start_msg(match, start_loc, do_loc, indentation_of_do_line)
|
144
|
+
if start_loc.line == do_loc.line &&
|
145
|
+
start_loc.column == indentation_of_do_line
|
146
|
+
''
|
147
|
+
else
|
148
|
+
" or #{match[0]} at #{do_loc.line}, #{indentation_of_do_line}"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
149
152
|
def message
|
150
153
|
end
|
151
154
|
|