rubocop 0.52.0 → 0.52.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -5
- data/config/default.yml +1 -11
- data/config/disabled.yml +5 -0
- data/config/enabled.yml +6 -8
- data/lib/rubocop.rb +13 -2
- data/lib/rubocop/ast/node.rb +23 -15
- data/lib/rubocop/cli.rb +25 -2
- data/lib/rubocop/config.rb +23 -8
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -2
- data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -0
- data/lib/rubocop/cop/commissioner.rb +1 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +121 -0
- data/lib/rubocop/cop/correctors/condition_corrector.rb +28 -0
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +26 -0
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +62 -0
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +44 -0
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +31 -0
- data/lib/rubocop/cop/correctors/punctuation_corrector.rb +29 -0
- data/lib/rubocop/cop/correctors/space_corrector.rb +34 -0
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +25 -0
- data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +31 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -0
- data/lib/rubocop/cop/generator.rb +18 -87
- data/lib/rubocop/cop/generator/require_file_injector.rb +78 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +5 -1
- data/lib/rubocop/cop/layout/align_array.rb +5 -1
- data/lib/rubocop/cop/layout/align_hash.rb +1 -1
- data/lib/rubocop/cop/layout/align_parameters.rb +5 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/class_structure.rb +2 -2
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +5 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +5 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +3 -3
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +17 -19
- data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +4 -0
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +4 -0
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +4 -0
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +4 -0
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +4 -0
- data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +4 -0
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +4 -0
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +4 -0
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +4 -0
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +4 -0
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -2
- data/lib/rubocop/cop/layout/indent_array.rb +6 -2
- data/lib/rubocop/cop/layout/indent_assignment.rb +6 -2
- data/lib/rubocop/cop/layout/indent_hash.rb +5 -1
- data/lib/rubocop/cop/layout/indentation_consistency.rb +5 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +5 -1
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +4 -0
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +3 -3
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +4 -0
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +4 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +6 -2
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +4 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -2
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -1
- data/lib/rubocop/cop/layout/space_after_comma.rb +4 -0
- data/lib/rubocop/cop/layout/space_after_semicolon.rb +4 -0
- data/lib/rubocop/cop/layout/space_before_comma.rb +4 -0
- data/lib/rubocop/cop/layout/space_before_semicolon.rb +4 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +3 -2
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +16 -7
- data/lib/rubocop/cop/layout/tab.rb +1 -1
- data/lib/rubocop/cop/lint/block_alignment.rb +1 -1
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -2
- data/lib/rubocop/cop/lint/end_alignment.rb +3 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/unused_block_argument.rb +4 -0
- data/lib/rubocop/cop/lint/unused_method_argument.rb +6 -0
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -1
- data/lib/rubocop/cop/mixin/alignment.rb +70 -0
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -0
- data/lib/rubocop/cop/mixin/array_syntax.rb +2 -0
- data/lib/rubocop/cop/mixin/code_length.rb +2 -0
- data/lib/rubocop/cop/mixin/configurable_max.rb +2 -0
- data/lib/rubocop/cop/mixin/def_node.rb +3 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -15
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -25
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +0 -6
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +5 -9
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +2 -2
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +2 -0
- data/lib/rubocop/cop/mixin/integer_node.rb +2 -0
- data/lib/rubocop/cop/mixin/match_range.rb +2 -0
- data/lib/rubocop/cop/mixin/min_body_length.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +9 -48
- data/lib/rubocop/cop/mixin/negative_conditional.rb +2 -16
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +12 -31
- data/lib/rubocop/cop/mixin/parentheses.rb +2 -19
- data/lib/rubocop/cop/mixin/percent_literal.rb +3 -3
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +2 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +2 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +2 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -3
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -3
- data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -2
- data/lib/rubocop/cop/mixin/string_help.rb +2 -0
- data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -13
- data/lib/rubocop/cop/mixin/surrounding_space.rb +4 -21
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -10
- data/lib/rubocop/cop/mixin/unused_argument.rb +2 -15
- data/lib/rubocop/cop/performance/case_when_splat.rb +1 -1
- data/lib/rubocop/cop/rails/action_filter.rb +3 -2
- data/lib/rubocop/cop/rails/active_support_aliases.rb +3 -2
- data/lib/rubocop/cop/rails/application_job.rb +6 -0
- data/lib/rubocop/cop/rails/application_record.rb +6 -0
- data/lib/rubocop/cop/rails/blank.rb +10 -9
- data/lib/rubocop/cop/rails/date.rb +22 -14
- data/lib/rubocop/cop/rails/delegate.rb +1 -1
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +3 -2
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +4 -2
- data/lib/rubocop/cop/rails/environment_comparison.rb +2 -2
- data/lib/rubocop/cop/rails/file_path.rb +1 -1
- data/lib/rubocop/cop/rails/find_by.rb +2 -2
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +15 -7
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +2 -2
- data/lib/rubocop/cop/rails/inverse_of.rb +130 -8
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +3 -3
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +3 -2
- data/lib/rubocop/cop/rails/presence.rb +31 -18
- data/lib/rubocop/cop/rails/present.rb +11 -8
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +52 -10
- data/lib/rubocop/cop/rails/request_referer.rb +2 -3
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +9 -2
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +38 -10
- data/lib/rubocop/cop/style/class_and_module_children.rb +76 -0
- data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/format_string_token.rb +24 -4
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +46 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +4 -3
- data/lib/rubocop/cop/style/if_unless_modifier.rb +14 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +79 -0
- data/lib/rubocop/cop/style/mixin_usage.rb +13 -2
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +19 -0
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_while.rb +6 -4
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +4 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
- data/lib/rubocop/cop/style/string_literals.rb +4 -0
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +4 -0
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -0
- data/lib/rubocop/cop/style/trailing_comma_in_literal.rb +4 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +33 -24
- data/lib/rubocop/options.rb +33 -10
- data/lib/rubocop/path_util.rb +7 -0
- data/lib/rubocop/token.rb +4 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +14 -4
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +0 -149
- data/lib/rubocop/cop/style/extend_self.rb +0 -92
@@ -79,14 +79,21 @@ module RuboCop
|
|
79
79
|
output.puts
|
80
80
|
cfg = self.class.config_to_allow_offenses[cop_name] || {}
|
81
81
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
82
|
+
# To avoid malformed YAML when potentially reading the config in
|
83
|
+
# #excludes, we use an output buffer and append it to the actual output
|
84
|
+
# only when it results in valid YAML.
|
85
|
+
output_buffer = StringIO.new
|
86
|
+
output_cop_comments(output_buffer, cfg, cop_name, offense_count)
|
87
|
+
output_cop_config(output_buffer, cfg, cop_name)
|
88
|
+
output.puts(output_buffer.string)
|
89
|
+
end
|
90
|
+
|
91
|
+
def output_cop_comments(output_buffer, cfg, cop_name, offense_count)
|
92
|
+
if @show_offense_counts
|
93
|
+
output_buffer.puts "# Offense count: #{offense_count}"
|
94
|
+
end
|
88
95
|
if COPS[cop_name] && COPS[cop_name].first.new.support_autocorrect?
|
89
|
-
|
96
|
+
output_buffer.puts '# Cop supports --auto-correct.'
|
90
97
|
end
|
91
98
|
|
92
99
|
default_cfg = default_config(cop_name)
|
@@ -95,7 +102,7 @@ module RuboCop
|
|
95
102
|
params = cop_config_params(default_cfg, cfg)
|
96
103
|
return if params.empty?
|
97
104
|
|
98
|
-
output_cop_param_comments(params, default_cfg)
|
105
|
+
output_cop_param_comments(output_buffer, params, default_cfg)
|
99
106
|
end
|
100
107
|
|
101
108
|
def cop_config_params(default_cfg, cfg)
|
@@ -104,15 +111,17 @@ module RuboCop
|
|
104
111
|
cfg.keys
|
105
112
|
end
|
106
113
|
|
107
|
-
def output_cop_param_comments(params, default_cfg)
|
114
|
+
def output_cop_param_comments(output_buffer, params, default_cfg)
|
108
115
|
config_params = params.reject { |p| p.start_with?('Supported') }
|
109
|
-
|
116
|
+
output_buffer.puts(
|
117
|
+
"# Configuration parameters: #{config_params.join(', ')}."
|
118
|
+
)
|
110
119
|
|
111
120
|
params.each do |param|
|
112
121
|
value = default_cfg[param]
|
113
122
|
if value.is_a?(Array)
|
114
123
|
next if value.empty?
|
115
|
-
|
124
|
+
output_buffer.puts "# #{param}: #{value.join(', ')}"
|
116
125
|
end
|
117
126
|
end
|
118
127
|
end
|
@@ -121,38 +130,38 @@ module RuboCop
|
|
121
130
|
RuboCop::ConfigLoader.default_configuration[cop_name]
|
122
131
|
end
|
123
132
|
|
124
|
-
def output_cop_config(
|
133
|
+
def output_cop_config(output_buffer, cfg, cop_name)
|
125
134
|
# 'Enabled' option will be put into file only if exclude
|
126
135
|
# limit is exceeded.
|
127
136
|
cfg_without_enabled = cfg.reject { |key| key == 'Enabled' }
|
128
137
|
|
129
|
-
|
138
|
+
output_buffer.puts "#{cop_name}:"
|
130
139
|
cfg_without_enabled.each do |key, value|
|
131
140
|
value = value[0] if value.is_a?(Array)
|
132
|
-
|
141
|
+
output_buffer.puts " #{key}: #{value}"
|
133
142
|
end
|
134
143
|
|
135
|
-
output_offending_files(
|
144
|
+
output_offending_files(output_buffer, cfg_without_enabled, cop_name)
|
136
145
|
end
|
137
146
|
|
138
|
-
def output_offending_files(
|
147
|
+
def output_offending_files(output_buffer, cfg, cop_name)
|
139
148
|
return unless cfg.empty?
|
140
149
|
|
141
150
|
offending_files = @files_with_offenses[cop_name].uniq.sort
|
142
151
|
if offending_files.count > @exclude_limit
|
143
|
-
|
152
|
+
output_buffer.puts ' Enabled: false'
|
144
153
|
else
|
145
|
-
output_exclude_list(
|
154
|
+
output_exclude_list(output_buffer, offending_files, cop_name)
|
146
155
|
end
|
147
156
|
end
|
148
157
|
|
149
|
-
def output_exclude_list(
|
158
|
+
def output_exclude_list(output_buffer, offending_files, cop_name)
|
150
159
|
require 'pathname'
|
151
160
|
parent = Pathname.new(Dir.pwd)
|
152
161
|
|
153
|
-
|
162
|
+
output_buffer.puts ' Exclude:'
|
154
163
|
excludes(offending_files, cop_name, parent).each do |file|
|
155
|
-
output_exclude_path(
|
164
|
+
output_exclude_path(output_buffer, file, parent)
|
156
165
|
end
|
157
166
|
end
|
158
167
|
|
@@ -168,12 +177,12 @@ module RuboCop
|
|
168
177
|
((cfg['Exclude'] || []) + offending_files).uniq
|
169
178
|
end
|
170
179
|
|
171
|
-
def output_exclude_path(
|
180
|
+
def output_exclude_path(output_buffer, file, parent)
|
172
181
|
file_path = Pathname.new(file)
|
173
182
|
relative = file_path.relative_path_from(parent)
|
174
|
-
|
183
|
+
output_buffer.puts " - '#{relative}'"
|
175
184
|
rescue ArgumentError
|
176
|
-
|
185
|
+
output_buffer.puts " - '#{file}'"
|
177
186
|
end
|
178
187
|
end
|
179
188
|
end
|
data/lib/rubocop/options.rb
CHANGED
@@ -4,6 +4,8 @@ require 'optparse'
|
|
4
4
|
require 'shellwords'
|
5
5
|
|
6
6
|
module RuboCop
|
7
|
+
class IncorrectCopNameError < StandardError; end
|
8
|
+
|
7
9
|
# This class handles command line options.
|
8
10
|
class Options
|
9
11
|
EXITING_OPTIONS = %i[version verbose_version show_cops].freeze
|
@@ -183,19 +185,40 @@ module RuboCop
|
|
183
185
|
|
184
186
|
# Validates option arguments and the options' compatibility with each other.
|
185
187
|
class OptionsValidator
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
188
|
+
class << self
|
189
|
+
# Cop name validation must be done later than option parsing, so it's not
|
190
|
+
# called from within Options.
|
191
|
+
def validate_cop_list(names)
|
192
|
+
return unless names
|
193
|
+
|
194
|
+
cop_names = Cop::Cop.registry.names
|
195
|
+
departments = Cop::Cop.registry.departments.map(&:to_s)
|
196
|
+
|
197
|
+
names.each do |name|
|
198
|
+
next if cop_names.include?(name)
|
199
|
+
next if departments.include?(name)
|
200
|
+
next if %w[Syntax Lint/Syntax].include?(name)
|
201
|
+
|
202
|
+
raise IncorrectCopNameError, format_message_from(name, cop_names)
|
203
|
+
end
|
204
|
+
end
|
190
205
|
|
191
|
-
|
206
|
+
private
|
192
207
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
208
|
+
def format_message_from(name, cop_names)
|
209
|
+
message = 'Unrecognized cop or department: %{name}.'
|
210
|
+
message_with_candidate = "#{message}\nDid you mean? %{candidate}"
|
211
|
+
corrections = cop_names.select do |cn|
|
212
|
+
score = StringUtil.similarity(cn, name)
|
213
|
+
score >= NameSimilarity::MINIMUM_SIMILARITY_TO_SUGGEST
|
214
|
+
end.sort
|
197
215
|
|
198
|
-
|
216
|
+
if corrections.empty?
|
217
|
+
format message, name: name
|
218
|
+
else
|
219
|
+
format message_with_candidate, name: name,
|
220
|
+
candidate: corrections.join(', ')
|
221
|
+
end
|
199
222
|
end
|
200
223
|
end
|
201
224
|
|
data/lib/rubocop/path_util.rb
CHANGED
@@ -43,6 +43,13 @@ module RuboCop
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
def find_file_upwards(filename, start_dir = PathUtil.pwd)
|
47
|
+
Pathname(File.expand_path(start_dir)).ascend do |dir|
|
48
|
+
file = File.join(dir, filename)
|
49
|
+
return file if File.exist?(file)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
46
53
|
# Returns true for an absolute Unix or Windows path.
|
47
54
|
def absolute?(path)
|
48
55
|
path =~ %r{\A([A-Z]:)?/}
|
data/lib/rubocop/token.rb
CHANGED
data/lib/rubocop/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.52.
|
4
|
+
version: 0.52.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-12-
|
13
|
+
date: 2017-12-27 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: parallel
|
@@ -200,11 +200,22 @@ files:
|
|
200
200
|
- lib/rubocop/cop/commissioner.rb
|
201
201
|
- lib/rubocop/cop/cop.rb
|
202
202
|
- lib/rubocop/cop/corrector.rb
|
203
|
+
- lib/rubocop/cop/correctors/alignment_corrector.rb
|
204
|
+
- lib/rubocop/cop/correctors/condition_corrector.rb
|
205
|
+
- lib/rubocop/cop/correctors/empty_line_corrector.rb
|
206
|
+
- lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb
|
207
|
+
- lib/rubocop/cop/correctors/ordered_gem_corrector.rb
|
208
|
+
- lib/rubocop/cop/correctors/parentheses_corrector.rb
|
209
|
+
- lib/rubocop/cop/correctors/punctuation_corrector.rb
|
210
|
+
- lib/rubocop/cop/correctors/space_corrector.rb
|
211
|
+
- lib/rubocop/cop/correctors/string_literal_corrector.rb
|
212
|
+
- lib/rubocop/cop/correctors/unused_arg_corrector.rb
|
203
213
|
- lib/rubocop/cop/force.rb
|
204
214
|
- lib/rubocop/cop/gemspec/duplicated_assignment.rb
|
205
215
|
- lib/rubocop/cop/gemspec/ordered_dependencies.rb
|
206
216
|
- lib/rubocop/cop/gemspec/required_ruby_version.rb
|
207
217
|
- lib/rubocop/cop/generator.rb
|
218
|
+
- lib/rubocop/cop/generator/require_file_injector.rb
|
208
219
|
- lib/rubocop/cop/ignored_node.rb
|
209
220
|
- lib/rubocop/cop/internal_affairs.rb
|
210
221
|
- lib/rubocop/cop/internal_affairs/node_destructuring.rb
|
@@ -370,11 +381,11 @@ files:
|
|
370
381
|
- lib/rubocop/cop/metrics/module_length.rb
|
371
382
|
- lib/rubocop/cop/metrics/parameter_lists.rb
|
372
383
|
- lib/rubocop/cop/metrics/perceived_complexity.rb
|
384
|
+
- lib/rubocop/cop/mixin/alignment.rb
|
373
385
|
- lib/rubocop/cop/mixin/annotation_comment.rb
|
374
386
|
- lib/rubocop/cop/mixin/array_hash_indentation.rb
|
375
387
|
- lib/rubocop/cop/mixin/array_min_size.rb
|
376
388
|
- lib/rubocop/cop/mixin/array_syntax.rb
|
377
|
-
- lib/rubocop/cop/mixin/autocorrect_alignment.rb
|
378
389
|
- lib/rubocop/cop/mixin/check_assignment.rb
|
379
390
|
- lib/rubocop/cop/mixin/classish_length.rb
|
380
391
|
- lib/rubocop/cop/mixin/code_length.rb
|
@@ -552,7 +563,6 @@ files:
|
|
552
563
|
- lib/rubocop/cop/style/end_block.rb
|
553
564
|
- lib/rubocop/cop/style/eval_with_location.rb
|
554
565
|
- lib/rubocop/cop/style/even_odd.rb
|
555
|
-
- lib/rubocop/cop/style/extend_self.rb
|
556
566
|
- lib/rubocop/cop/style/flip_flop.rb
|
557
567
|
- lib/rubocop/cop/style/for.rb
|
558
568
|
- lib/rubocop/cop/style/format_string.rb
|
@@ -1,149 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
# This module does auto-correction of nodes that should just be moved to
|
6
|
-
# the left or to the right, amount being determined by the instance
|
7
|
-
# variable @column_delta.
|
8
|
-
module AutocorrectAlignment
|
9
|
-
SPACE = ' '.freeze
|
10
|
-
|
11
|
-
def configured_indentation_width
|
12
|
-
cop_config['IndentationWidth'] ||
|
13
|
-
config.for_cop('IndentationWidth')['Width']
|
14
|
-
end
|
15
|
-
|
16
|
-
def indentation(node)
|
17
|
-
offset(node) + (SPACE * configured_indentation_width)
|
18
|
-
end
|
19
|
-
|
20
|
-
def offset(node)
|
21
|
-
SPACE * node.loc.column
|
22
|
-
end
|
23
|
-
|
24
|
-
def display_column(range)
|
25
|
-
line = processed_source.lines[range.line - 1]
|
26
|
-
Unicode::DisplayWidth.of(line[0, range.column])
|
27
|
-
end
|
28
|
-
|
29
|
-
def check_alignment(items, base_column = nil)
|
30
|
-
unless items.empty?
|
31
|
-
base_column ||= display_column(items.first.source_range)
|
32
|
-
end
|
33
|
-
|
34
|
-
each_bad_alignment(items, base_column) do |current|
|
35
|
-
expr = current.source_range
|
36
|
-
if offenses.any? { |o| within?(expr, o.location) }
|
37
|
-
# If this offense is within a line range that is already being
|
38
|
-
# realigned by autocorrect, we report the offense without
|
39
|
-
# autocorrecting it. Two rewrites in the same area by the same
|
40
|
-
# cop can not be handled. The next iteration will find the
|
41
|
-
# offense again and correct it.
|
42
|
-
add_offense(nil, location: expr)
|
43
|
-
else
|
44
|
-
add_offense(current)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def autocorrect(arg)
|
50
|
-
return unless arg
|
51
|
-
|
52
|
-
heredoc_ranges = heredoc_ranges(arg)
|
53
|
-
expr = arg.respond_to?(:loc) ? arg.loc.expression : arg
|
54
|
-
|
55
|
-
# We can't use the instance variable inside the lambda. That would just
|
56
|
-
# give each lambda the same reference and they would all get the last
|
57
|
-
# value of @column_delta. A local variable fixes the problem.
|
58
|
-
column_delta = @column_delta
|
59
|
-
|
60
|
-
return if block_comment_within?(expr)
|
61
|
-
|
62
|
-
lambda do |corrector|
|
63
|
-
each_line(expr) do |line_begin_pos|
|
64
|
-
autocorrect_line(corrector, line_begin_pos, expr, column_delta,
|
65
|
-
heredoc_ranges)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
def each_bad_alignment(items, base_column)
|
73
|
-
prev_line = -1
|
74
|
-
items.each do |current|
|
75
|
-
if current.loc.line > prev_line &&
|
76
|
-
begins_its_line?(current.source_range)
|
77
|
-
@column_delta = base_column - display_column(current.source_range)
|
78
|
-
|
79
|
-
yield current if @column_delta.nonzero?
|
80
|
-
end
|
81
|
-
prev_line = current.loc.line
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def autocorrect_line(corrector, line_begin_pos, expr, column_delta,
|
86
|
-
heredoc_ranges)
|
87
|
-
range = calculate_range(expr, line_begin_pos, column_delta)
|
88
|
-
# We must not change indentation of heredoc strings.
|
89
|
-
return if heredoc_ranges.any? { |h| within?(range, h) }
|
90
|
-
|
91
|
-
if column_delta > 0
|
92
|
-
unless range.source == "\n"
|
93
|
-
corrector.insert_before(range, ' ' * column_delta)
|
94
|
-
end
|
95
|
-
elsif range.source =~ /\A[ \t]+\z/
|
96
|
-
remove(range, corrector)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def heredoc_ranges(arg)
|
101
|
-
return [] unless arg.is_a?(Parser::AST::Node)
|
102
|
-
|
103
|
-
arg.each_node(:dstr)
|
104
|
-
.select { |n| n.loc.respond_to?(:heredoc_body) }
|
105
|
-
.map { |n| n.loc.heredoc_body.join(n.loc.heredoc_end) }
|
106
|
-
end
|
107
|
-
|
108
|
-
def block_comment_within?(expr)
|
109
|
-
processed_source.comments.select(&:document?).any? do |c|
|
110
|
-
within?(c.loc.expression, expr)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def within?(inner, outer)
|
115
|
-
inner.begin_pos >= outer.begin_pos && inner.end_pos <= outer.end_pos
|
116
|
-
end
|
117
|
-
|
118
|
-
def calculate_range(expr, line_begin_pos, column_delta)
|
119
|
-
starts_with_space = expr.source_buffer.source[line_begin_pos] =~ / /
|
120
|
-
pos_to_remove = if column_delta > 0 || starts_with_space
|
121
|
-
line_begin_pos
|
122
|
-
else
|
123
|
-
line_begin_pos - column_delta.abs
|
124
|
-
end
|
125
|
-
|
126
|
-
range_between(pos_to_remove, pos_to_remove + column_delta.abs)
|
127
|
-
end
|
128
|
-
|
129
|
-
def remove(range, corrector)
|
130
|
-
original_stderr = $stderr
|
131
|
-
$stderr = StringIO.new # Avoid error messages on console
|
132
|
-
corrector.remove(range)
|
133
|
-
rescue RuntimeError
|
134
|
-
range = range_between(range.begin_pos + 1, range.end_pos + 1)
|
135
|
-
retry if range.source =~ /^ +$/
|
136
|
-
ensure
|
137
|
-
$stderr = original_stderr
|
138
|
-
end
|
139
|
-
|
140
|
-
def each_line(expr)
|
141
|
-
line_begin_pos = expr.begin_pos
|
142
|
-
expr.source.each_line do |line|
|
143
|
-
yield line_begin_pos
|
144
|
-
line_begin_pos += line.length
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|