rubocop 0.93.1 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +36 -16
- data/config/default.yml +276 -82
- data/config/obsoletion.yml +196 -0
- data/exe/rubocop +1 -1
- data/lib/rubocop.rb +31 -2
- data/lib/rubocop/cli.rb +5 -1
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
- data/lib/rubocop/cli/command/execute_runner.rb +26 -11
- data/lib/rubocop/cli/command/suggest_extensions.rb +80 -0
- data/lib/rubocop/cli/command/version.rb +1 -1
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +4 -0
- data/lib/rubocop/config_loader.rb +34 -8
- data/lib/rubocop/config_loader_resolver.rb +12 -6
- data/lib/rubocop/config_obsoletion.rb +65 -247
- data/lib/rubocop/config_obsoletion/changed_enforced_styles.rb +33 -0
- data/lib/rubocop/config_obsoletion/changed_parameter.rb +21 -0
- data/lib/rubocop/config_obsoletion/cop_rule.rb +34 -0
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +44 -0
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +44 -0
- data/lib/rubocop/config_obsoletion/removed_cop.rb +41 -0
- data/lib/rubocop/config_obsoletion/renamed_cop.rb +34 -0
- data/lib/rubocop/config_obsoletion/rule.rb +41 -0
- data/lib/rubocop/config_obsoletion/split_cop.rb +27 -0
- data/lib/rubocop/config_regeneration.rb +1 -1
- data/lib/rubocop/config_validator.rb +25 -10
- data/lib/rubocop/cop/autocorrect_logic.rb +21 -6
- data/lib/rubocop/cop/badge.rb +9 -24
- data/lib/rubocop/cop/base.rb +33 -16
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +26 -6
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
- data/lib/rubocop/cop/commissioner.rb +37 -23
- data/lib/rubocop/cop/cop.rb +2 -2
- data/lib/rubocop/cop/corrector.rb +3 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +6 -8
- data/lib/rubocop/cop/force.rb +1 -1
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -3
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +4 -5
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +3 -10
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +3 -4
- data/lib/rubocop/cop/layout/class_structure.rb +22 -3
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +15 -2
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +80 -10
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +6 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -1
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -3
- data/lib/rubocop/cop/layout/end_of_line.rb +5 -5
- data/lib/rubocop/cop/layout/extra_spacing.rb +1 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +7 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +4 -4
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +12 -0
- data/lib/rubocop/cop/layout/line_length.rb +10 -13
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +7 -3
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +24 -18
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +4 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +35 -13
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +37 -13
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -1
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +26 -2
- data/lib/rubocop/cop/lint/debugger.rb +17 -28
- data/lib/rubocop/cop/lint/duplicate_branch.rb +93 -0
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +2 -12
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +77 -0
- data/lib/rubocop/cop/lint/else_layout.rb +29 -3
- data/lib/rubocop/cop/lint/empty_block.rb +82 -0
- data/lib/rubocop/cop/lint/empty_class.rb +93 -0
- data/lib/rubocop/cop/lint/flip_flop.rb +8 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +39 -7
- data/lib/rubocop/cop/lint/loop.rb +4 -4
- data/lib/rubocop/cop/lint/missing_super.rb +7 -4
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +14 -0
- data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +58 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +46 -13
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +27 -8
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +19 -16
- data/lib/rubocop/cop/lint/shadowed_exception.rb +4 -5
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -0
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +86 -0
- data/lib/rubocop/cop/lint/to_json.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +85 -0
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +199 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
- data/lib/rubocop/cop/lint/useless_method_definition.rb +2 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -1
- data/lib/rubocop/cop/metrics/abc_size.rb +25 -1
- data/lib/rubocop/cop/metrics/block_length.rb +13 -7
- data/lib/rubocop/cop/metrics/method_length.rb +7 -2
- data/lib/rubocop/cop/metrics/parameter_lists.rb +68 -2
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +20 -10
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +146 -0
- data/lib/rubocop/cop/metrics/utils/repeated_csend_discount.rb +6 -1
- data/lib/rubocop/cop/migration/department_name.rb +1 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +4 -3
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +9 -1
- data/lib/rubocop/cop/mixin/ignored_methods.rb +36 -3
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +6 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +9 -4
- data/lib/rubocop/cop/mixin/string_help.rb +4 -1
- data/lib/rubocop/cop/mixin/visibility_help.rb +1 -3
- data/lib/rubocop/cop/naming/accessor_method_name.rb +15 -1
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +12 -2
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +11 -5
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +67 -18
- data/lib/rubocop/cop/naming/predicate_name.rb +2 -1
- data/lib/rubocop/cop/naming/variable_number.rb +100 -8
- data/lib/rubocop/cop/offense.rb +3 -3
- data/lib/rubocop/cop/security/open.rb +12 -10
- data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
- data/lib/rubocop/cop/style/and_or.rb +11 -3
- data/lib/rubocop/cop/style/arguments_forwarding.rb +142 -0
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +0 -4
- data/lib/rubocop/cop/style/case_like_if.rb +0 -4
- data/lib/rubocop/cop/style/character_literal.rb +10 -11
- data/lib/rubocop/cop/style/class_and_module_children.rb +8 -3
- data/lib/rubocop/cop/style/collection_compact.rb +91 -0
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +169 -0
- data/lib/rubocop/cop/style/documentation.rb +12 -1
- data/lib/rubocop/cop/style/double_negation.rb +6 -1
- data/lib/rubocop/cop/style/float_division.rb +44 -1
- data/lib/rubocop/cop/style/format_string.rb +8 -3
- data/lib/rubocop/cop/style/format_string_token.rb +47 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -3
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +7 -2
- data/lib/rubocop/cop/style/if_inside_else.rb +37 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -3
- data/lib/rubocop/cop/style/if_with_semicolon.rb +39 -4
- data/lib/rubocop/cop/style/infinite_loop.rb +4 -0
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +12 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +10 -13
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +8 -13
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +7 -11
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +11 -2
- data/lib/rubocop/cop/style/mixin_grouping.rb +0 -4
- data/lib/rubocop/cop/style/multiple_comparison.rb +55 -7
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +106 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +52 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +14 -11
- data/lib/rubocop/cop/style/perl_backrefs.rb +86 -9
- data/lib/rubocop/cop/style/raise_args.rb +21 -6
- data/lib/rubocop/cop/style/redundant_argument.rb +88 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +7 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +24 -8
- data/lib/rubocop/cop/style/redundant_self.rb +3 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +16 -4
- data/lib/rubocop/cop/style/semicolon.rb +3 -0
- data/lib/rubocop/cop/style/single_line_block_params.rb +30 -7
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +65 -3
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -13
- data/lib/rubocop/cop/style/static_class.rb +97 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +39 -2
- data/lib/rubocop/cop/style/string_literals.rb +14 -8
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +4 -3
- data/lib/rubocop/cop/style/swap_values.rb +108 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +5 -3
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +3 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +9 -0
- data/lib/rubocop/cop/team.rb +6 -1
- data/lib/rubocop/cop/util.rb +6 -2
- data/lib/rubocop/cop/variable_force/branch.rb +1 -1
- data/lib/rubocop/cop/variable_force/scope.rb +1 -1
- data/lib/rubocop/core_ext/hash.rb +20 -0
- data/lib/rubocop/ext/regexp_node.rb +36 -11
- data/lib/rubocop/ext/regexp_parser.rb +95 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +21 -6
- data/lib/rubocop/formatter/emacs_style_formatter.rb +2 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +47 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +2 -0
- data/lib/rubocop/formatter/tap_formatter.rb +2 -0
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/lockfile.rb +40 -0
- data/lib/rubocop/magic_comment.rb +2 -2
- data/lib/rubocop/options.rb +11 -1
- data/lib/rubocop/rake_task.rb +2 -2
- data/lib/rubocop/rspec/shared_contexts.rb +4 -0
- data/lib/rubocop/runner.rb +1 -1
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +65 -1
- data/lib/rubocop/version.rb +56 -6
- metadata +50 -9
- data/bin/console +0 -10
- data/bin/rubocop-profile +0 -32
- data/bin/setup +0 -7
data/lib/rubocop/cop/badge.rb
CHANGED
@@ -10,37 +10,22 @@ module RuboCop
|
|
10
10
|
# allow for badge references in source files that omit the department for
|
11
11
|
# RuboCop to infer.
|
12
12
|
class Badge
|
13
|
-
# Error raised when a badge parse fails.
|
14
|
-
class InvalidBadge < Error
|
15
|
-
MSG = 'Invalid badge %<badge>p. ' \
|
16
|
-
'Expected `Department/CopName` or `CopName`.'
|
17
|
-
|
18
|
-
def initialize(token)
|
19
|
-
super(format(MSG, badge: token))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
13
|
attr_reader :department, :cop_name
|
24
14
|
|
25
15
|
def self.for(class_name)
|
26
|
-
|
16
|
+
parts = class_name.split('::')
|
17
|
+
name_deep_enough = parts.length >= 4
|
18
|
+
new(name_deep_enough ? parts[2..-1] : parts.last(2))
|
27
19
|
end
|
28
20
|
|
29
21
|
def self.parse(identifier)
|
30
|
-
|
31
|
-
|
32
|
-
raise InvalidBadge, identifier if parts.size > 2
|
33
|
-
|
34
|
-
if parts.one?
|
35
|
-
new(nil, *parts)
|
36
|
-
else
|
37
|
-
new(*parts)
|
38
|
-
end
|
22
|
+
new(identifier.split('/'))
|
39
23
|
end
|
40
24
|
|
41
|
-
def initialize(
|
42
|
-
|
43
|
-
@
|
25
|
+
def initialize(class_name_parts)
|
26
|
+
department_parts = class_name_parts[0...-1]
|
27
|
+
@department = (department_parts.join('/').to_sym unless department_parts.empty?)
|
28
|
+
@cop_name = class_name_parts.last
|
44
29
|
end
|
45
30
|
|
46
31
|
def ==(other)
|
@@ -66,7 +51,7 @@ module RuboCop
|
|
66
51
|
end
|
67
52
|
|
68
53
|
def with_department(department)
|
69
|
-
self.class.new(department, cop_name)
|
54
|
+
self.class.new([department.to_s.split('/'), cop_name].flatten)
|
70
55
|
end
|
71
56
|
end
|
72
57
|
end
|
data/lib/rubocop/cop/base.rb
CHANGED
@@ -261,6 +261,21 @@ module RuboCop
|
|
261
261
|
'they are returned as the result of the investigation'
|
262
262
|
end
|
263
263
|
|
264
|
+
### Reserved for Commissioner
|
265
|
+
|
266
|
+
# @api private
|
267
|
+
def callbacks_needed
|
268
|
+
self.class.callbacks_needed
|
269
|
+
end
|
270
|
+
|
271
|
+
# @api private
|
272
|
+
def self.callbacks_needed
|
273
|
+
@callbacks_needed ||= public_instance_methods.select do |m|
|
274
|
+
m.match?(/^on_|^after_/) &&
|
275
|
+
!Base.method_defined?(m) # exclude standard "callbacks" like 'on_begin_investigation'
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
264
279
|
private
|
265
280
|
|
266
281
|
### Reserved for Cop::Cop
|
@@ -273,13 +288,6 @@ module RuboCop
|
|
273
288
|
@current_corrector&.merge!(corrector) if corrector
|
274
289
|
end
|
275
290
|
|
276
|
-
def correction_strategy
|
277
|
-
return :unsupported unless correctable?
|
278
|
-
return :uncorrected unless autocorrect?
|
279
|
-
|
280
|
-
:attempt_correction
|
281
|
-
end
|
282
|
-
|
283
291
|
### Reserved for Commissioner:
|
284
292
|
|
285
293
|
def current_offense_locations
|
@@ -291,7 +299,7 @@ module RuboCop
|
|
291
299
|
end
|
292
300
|
|
293
301
|
private_class_method def self.restrict_on_send
|
294
|
-
@restrict_on_send ||= self::RESTRICT_ON_SEND.
|
302
|
+
@restrict_on_send ||= self::RESTRICT_ON_SEND.to_a.freeze
|
295
303
|
end
|
296
304
|
|
297
305
|
# Called before any investigation
|
@@ -326,33 +334,42 @@ module RuboCop
|
|
326
334
|
|
327
335
|
# @return [Symbol, Corrector] offense status
|
328
336
|
def correct(range)
|
329
|
-
status = correction_strategy
|
330
|
-
|
331
337
|
if block_given?
|
332
338
|
corrector = Corrector.new(self)
|
333
339
|
yield corrector
|
334
|
-
if
|
340
|
+
if corrector.empty?
|
341
|
+
corrector = nil
|
342
|
+
elsif !self.class.support_autocorrect?
|
335
343
|
raise "The Cop #{name} must `extend AutoCorrector` to be able to autocorrect"
|
336
344
|
end
|
337
345
|
end
|
338
346
|
|
339
|
-
|
347
|
+
[use_corrector(range, corrector), corrector]
|
348
|
+
end
|
340
349
|
|
341
|
-
|
350
|
+
# @return [Symbol] offense status
|
351
|
+
def use_corrector(range, corrector)
|
352
|
+
if autocorrect?
|
353
|
+
attempt_correction(range, corrector)
|
354
|
+
elsif corrector
|
355
|
+
:uncorrected
|
356
|
+
else
|
357
|
+
:unsupported
|
358
|
+
end
|
342
359
|
end
|
343
360
|
|
344
361
|
# @return [Symbol] offense status
|
345
362
|
def attempt_correction(range, corrector)
|
346
|
-
if corrector
|
363
|
+
if corrector
|
347
364
|
status = :corrected
|
348
365
|
elsif disable_uncorrectable?
|
349
366
|
corrector = disable_uncorrectable(range)
|
350
367
|
status = :corrected_with_todo
|
351
368
|
else
|
352
|
-
return :
|
369
|
+
return :unsupported
|
353
370
|
end
|
354
371
|
|
355
|
-
apply_correction(corrector)
|
372
|
+
apply_correction(corrector)
|
356
373
|
status
|
357
374
|
end
|
358
375
|
|
@@ -25,13 +25,23 @@ module RuboCop
|
|
25
25
|
#
|
26
26
|
# # good
|
27
27
|
# gem 'rubocop', groups: [:development, :test]
|
28
|
-
|
28
|
+
#
|
29
|
+
# # good - conditional declaration
|
30
|
+
# if Dir.exist?(local)
|
31
|
+
# gem 'rubocop', path: local
|
32
|
+
# elsif ENV['RUBOCOP_VERSION'] == 'master'
|
33
|
+
# gem 'rubocop', git: 'https://github.com/rubocop-hq/rubocop.git'
|
34
|
+
# else
|
35
|
+
# gem 'rubocop', '~> 0.90.0'
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
class DuplicatedGem < Base
|
29
39
|
include RangeHelp
|
30
40
|
|
31
41
|
MSG = 'Gem `%<gem_name>s` requirements already given on line '\
|
32
42
|
'%<line_of_first_occurrence>d of the Gemfile.'
|
33
43
|
|
34
|
-
def
|
44
|
+
def on_new_investigation
|
35
45
|
return if processed_source.blank?
|
36
46
|
|
37
47
|
duplicated_gem_nodes.each do |nodes|
|
@@ -53,11 +63,21 @@ module RuboCop
|
|
53
63
|
gem_declarations(processed_source.ast)
|
54
64
|
.group_by(&:first_argument)
|
55
65
|
.values
|
56
|
-
.select { |nodes| nodes.size > 1 && !
|
66
|
+
.select { |nodes| nodes.size > 1 && !conditional_declaration?(nodes) }
|
57
67
|
end
|
58
68
|
|
59
|
-
def
|
60
|
-
|
69
|
+
def conditional_declaration?(nodes)
|
70
|
+
parent = nodes[0].parent
|
71
|
+
return false unless parent&.if_type? || parent&.when_type?
|
72
|
+
|
73
|
+
root_conditional_node = parent.if_type? ? parent : parent.parent
|
74
|
+
nodes.all? { |node| within_conditional?(node, root_conditional_node) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def within_conditional?(node, conditional_node)
|
78
|
+
conditional_node.branches.any? do |branch|
|
79
|
+
branch == node || branch.child_nodes.include?(node)
|
80
|
+
end
|
61
81
|
end
|
62
82
|
|
63
83
|
def register_offense(node, gem_name, line_of_first_occurrence)
|
@@ -69,7 +89,7 @@ module RuboCop
|
|
69
89
|
gem_name: gem_name,
|
70
90
|
line_of_first_occurrence: line_of_first_occurrence
|
71
91
|
)
|
72
|
-
add_offense(
|
92
|
+
add_offense(offense_location, message: message)
|
73
93
|
end
|
74
94
|
end
|
75
95
|
end
|
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
class Commissioner
|
8
8
|
include RuboCop::AST::Traversal
|
9
9
|
|
10
|
-
RESTRICTED_CALLBACKS = %i[on_send on_csend].freeze
|
10
|
+
RESTRICTED_CALLBACKS = %i[on_send on_csend after_send after_csend].freeze
|
11
11
|
private_constant :RESTRICTED_CALLBACKS
|
12
12
|
|
13
13
|
# How a Commissioner returns the results of the investigation
|
@@ -45,8 +45,7 @@ module RuboCop
|
|
45
45
|
@cops = cops
|
46
46
|
@forces = forces
|
47
47
|
@options = options
|
48
|
-
|
49
|
-
@restricted_map = {}
|
48
|
+
initialize_callbacks
|
50
49
|
|
51
50
|
reset
|
52
51
|
end
|
@@ -61,16 +60,18 @@ module RuboCop
|
|
61
60
|
method_name = :"on_#{node_type}"
|
62
61
|
next unless method_defined?(method_name)
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
# Hacky: Comment-out code as needed
|
64
|
+
r = '#' unless RESTRICTED_CALLBACKS.include?(method_name) # has Restricted?
|
65
|
+
c = '#' if NO_CHILD_NODES.include?(node_type) # has Children?
|
67
66
|
|
68
67
|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
68
|
+
def on_#{node_type}(node) # def on_send(node)
|
69
|
+
trigger_responding_cops(:on_#{node_type}, node) # trigger_responding_cops(:on_send, node)
|
70
|
+
#{r} trigger_restricted_cops(:on_#{node_type}, node) # trigger_restricted_cops(:on_send, node)
|
71
|
+
#{c} super(node) # super(node)
|
72
|
+
#{c} trigger_responding_cops(:after_#{node_type}, node) # trigger_responding_cops(:after_send, node)
|
73
|
+
#{c}#{r} trigger_restricted_cops(:after_#{node_type}, node) # trigger_restricted_cops(:after_send, node)
|
74
|
+
end # end
|
74
75
|
RUBY
|
75
76
|
end
|
76
77
|
|
@@ -94,9 +95,9 @@ module RuboCop
|
|
94
95
|
private
|
95
96
|
|
96
97
|
def trigger_responding_cops(callback, node)
|
97
|
-
@callbacks[callback]
|
98
|
+
@callbacks[callback]&.each do |cop|
|
98
99
|
with_cop_error_handling(cop, node) do
|
99
|
-
cop.
|
100
|
+
cop.public_send(callback, node)
|
100
101
|
end
|
101
102
|
end
|
102
103
|
end
|
@@ -105,29 +106,42 @@ module RuboCop
|
|
105
106
|
@errors = []
|
106
107
|
end
|
107
108
|
|
108
|
-
def
|
109
|
-
callbacks = @cops
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
109
|
+
def initialize_callbacks
|
110
|
+
@callbacks = build_callbacks(@cops)
|
111
|
+
@restricted_map = restrict_callbacks(@callbacks)
|
112
|
+
end
|
113
|
+
|
114
|
+
def build_callbacks(cops)
|
115
|
+
callbacks = {}
|
116
|
+
cops.each do |cop|
|
117
|
+
cop.callbacks_needed.each do |callback|
|
118
|
+
(callbacks[callback] ||= []) << cop
|
119
|
+
end
|
114
120
|
end
|
115
121
|
callbacks
|
116
122
|
end
|
117
123
|
|
124
|
+
def restrict_callbacks(callbacks)
|
125
|
+
restricted = {}
|
126
|
+
RESTRICTED_CALLBACKS.each do |callback|
|
127
|
+
restricted[callback] = restricted_map(callbacks[callback])
|
128
|
+
end
|
129
|
+
restricted
|
130
|
+
end
|
131
|
+
|
118
132
|
def trigger_restricted_cops(event, node)
|
119
133
|
name = node.method_name
|
120
|
-
@restricted_map
|
134
|
+
@restricted_map[event][name]&.each do |cop|
|
121
135
|
with_cop_error_handling(cop, node) do
|
122
|
-
cop.
|
136
|
+
cop.public_send(event, node)
|
123
137
|
end
|
124
138
|
end
|
125
139
|
end
|
126
140
|
|
127
|
-
#
|
141
|
+
# NOTE: mutates `callbacks` in place
|
128
142
|
def restricted_map(callbacks)
|
129
143
|
map = {}
|
130
|
-
callbacks
|
144
|
+
callbacks&.select! do |cop|
|
131
145
|
restrictions = cop.class.send :restrict_on_send
|
132
146
|
restrictions.each do |name|
|
133
147
|
(map[name] ||= []) << cop
|
data/lib/rubocop/cop/cop.rb
CHANGED
@@ -27,7 +27,7 @@ module RuboCop
|
|
27
27
|
def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
|
28
28
|
@v0_argument = node_or_range
|
29
29
|
range = find_location(node_or_range, location)
|
30
|
-
if block.nil? && !
|
30
|
+
if block.nil? && !support_autocorrect?
|
31
31
|
super(range, message: message, severity: severity)
|
32
32
|
else
|
33
33
|
super(range, message: message, severity: severity) do |corrector|
|
@@ -136,7 +136,7 @@ module RuboCop
|
|
136
136
|
end
|
137
137
|
|
138
138
|
def correction_lambda
|
139
|
-
return unless
|
139
|
+
return unless support_autocorrect?
|
140
140
|
|
141
141
|
dedup_on_node(@v0_argument) do
|
142
142
|
autocorrect(@v0_argument)
|
@@ -9,6 +9,8 @@ module RuboCop
|
|
9
9
|
# The nodes modified by the corrections should be part of the
|
10
10
|
# AST of the source_buffer.
|
11
11
|
class Corrector < ::Parser::Source::TreeRewriter
|
12
|
+
NOOP_CONSUMER = ->(diagnostic) {} # noop
|
13
|
+
|
12
14
|
# @param source [Parser::Source::Buffer, or anything
|
13
15
|
# leading to one via `(processed_source.)buffer`]
|
14
16
|
#
|
@@ -23,7 +25,7 @@ module RuboCop
|
|
23
25
|
)
|
24
26
|
|
25
27
|
# Don't print warnings to stderr if corrections conflict with each other
|
26
|
-
diagnostics.consumer =
|
28
|
+
diagnostics.consumer = NOOP_CONSUMER
|
27
29
|
end
|
28
30
|
|
29
31
|
alias rewrite process # Legacy
|
@@ -48,7 +48,7 @@ module RuboCop
|
|
48
48
|
def autocorrect_multiline_words(node, escape, delimiters)
|
49
49
|
contents = process_multiline_words(node, escape, delimiters)
|
50
50
|
contents << end_content(node.source)
|
51
|
-
contents.join
|
51
|
+
contents.join
|
52
52
|
end
|
53
53
|
|
54
54
|
def autocorrect_words(node, escape, delimiters)
|
@@ -89,7 +89,7 @@ module RuboCop
|
|
89
89
|
begin_line_num = previous_line_num - base_line_num + 1
|
90
90
|
end_line_num = node.first_line - base_line_num + 1
|
91
91
|
lines = source_in_lines[begin_line_num...end_line_num]
|
92
|
-
"\n#{
|
92
|
+
"\n#{lines.join("\n").split(node.source).first || ''}"
|
93
93
|
end
|
94
94
|
|
95
95
|
def fix_escaped_content(word_node, escape, delimiters)
|
@@ -7,16 +7,14 @@ module RuboCop
|
|
7
7
|
extend Util
|
8
8
|
|
9
9
|
class << self
|
10
|
-
def correct(node, style)
|
10
|
+
def correct(corrector, node, style)
|
11
11
|
return if node.dstr_type?
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
corrector.replace(node, str.inspect)
|
19
|
-
end
|
13
|
+
str = node.str_content
|
14
|
+
if style == :single_quotes
|
15
|
+
corrector.replace(node, to_string_literal(str))
|
16
|
+
else
|
17
|
+
corrector.replace(node, str.inspect)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
data/lib/rubocop/cop/force.rb
CHANGED
@@ -34,7 +34,7 @@ module RuboCop
|
|
34
34
|
# spec.add_runtime_dependency('parallel', '~> 1.10')
|
35
35
|
# spec.add_runtime_dependency('parser', '>= 2.3.3.1', '< 3.0')
|
36
36
|
# end
|
37
|
-
class DuplicatedAssignment <
|
37
|
+
class DuplicatedAssignment < Base
|
38
38
|
include RangeHelp
|
39
39
|
|
40
40
|
MSG = '`%<assignment>s` method calls already given on line '\
|
@@ -54,7 +54,7 @@ module RuboCop
|
|
54
54
|
(lvar #match_block_variable_name?) #assignment_method? ...)
|
55
55
|
PATTERN
|
56
56
|
|
57
|
-
def
|
57
|
+
def on_new_investigation
|
58
58
|
return if processed_source.blank?
|
59
59
|
|
60
60
|
duplicated_assignment_method_nodes.each do |nodes|
|
@@ -96,7 +96,7 @@ module RuboCop
|
|
96
96
|
assignment: assignment,
|
97
97
|
line_of_first_occurrence: line_of_first_occurrence
|
98
98
|
)
|
99
|
-
add_offense(
|
99
|
+
add_offense(offense_location, message: message)
|
100
100
|
end
|
101
101
|
end
|
102
102
|
end
|
@@ -45,7 +45,7 @@ module RuboCop
|
|
45
45
|
# Gem::Specification.new do |spec|
|
46
46
|
# spec.required_ruby_version = '~> 2.5'
|
47
47
|
# end
|
48
|
-
class RequiredRubyVersion <
|
48
|
+
class RequiredRubyVersion < Base
|
49
49
|
include RangeHelp
|
50
50
|
|
51
51
|
NOT_EQUAL_MSG = '`required_ruby_version` (%<required_ruby_version>s, ' \
|
@@ -64,7 +64,7 @@ module RuboCop
|
|
64
64
|
PATTERN
|
65
65
|
|
66
66
|
# rubocop:disable Metrics/AbcSize
|
67
|
-
def
|
67
|
+
def on_new_investigation
|
68
68
|
version_def = required_ruby_version(processed_source.ast).first
|
69
69
|
|
70
70
|
if version_def
|
@@ -72,13 +72,12 @@ module RuboCop
|
|
72
72
|
return if !ruby_version || ruby_version == target_ruby_version.to_s
|
73
73
|
|
74
74
|
add_offense(
|
75
|
-
|
76
|
-
location: version_def.loc.expression,
|
75
|
+
version_def.loc.expression,
|
77
76
|
message: not_equal_message(ruby_version, target_ruby_version)
|
78
77
|
)
|
79
78
|
else
|
80
79
|
range = source_range(processed_source.buffer, 1, 0)
|
81
|
-
add_offense(
|
80
|
+
add_offense(range, message: MISSING_MSG)
|
82
81
|
end
|
83
82
|
end
|
84
83
|
# rubocop:enable Metrics/AbcSize
|