rbhint 0.85.1.rc2 → 0.87.1.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -1
- data/bin/rubocop-profile +16 -0
- data/config/default.yml +118 -10
- data/lib/rbhint/version.rb +1 -1
- data/lib/rubocop.rb +15 -1
- data/lib/rubocop/cli.rb +0 -2
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +40 -5
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_cops.rb +1 -1
- data/lib/rubocop/config_loader.rb +24 -66
- data/lib/rubocop/config_obsoletion.rb +0 -1
- data/lib/rubocop/cop/autocorrect_logic.rb +14 -24
- data/lib/rubocop/cop/badge.rb +1 -1
- data/lib/rubocop/cop/base.rb +407 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
- data/lib/rubocop/cop/commissioner.rb +48 -50
- data/lib/rubocop/cop/cop.rb +85 -236
- data/lib/rubocop/cop/corrector.rb +38 -115
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +26 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +6 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
- data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
- data/lib/rubocop/cop/layout/class_structure.rb +2 -37
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -8
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -0
- data/lib/rubocop/cop/layout/hash_alignment.rb +1 -2
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +17 -7
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +22 -27
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +27 -68
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +3 -2
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
- data/lib/rubocop/cop/legacy/corrector.rb +29 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
- data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +3 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +11 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +14 -20
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
- data/lib/rubocop/cop/lint/raise_exception.rb +8 -0
- data/lib/rubocop/cop/lint/rand_one.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +27 -23
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +8 -0
- data/lib/rubocop/cop/lint/syntax.rb +11 -26
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +24 -2
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +26 -3
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/method_length.rb +24 -1
- data/lib/rubocop/cop/metrics/module_length.rb +26 -3
- data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +3 -3
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
- data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
- data/lib/rubocop/cop/mixin/code_length.rb +4 -0
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
- data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
- data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -2
- data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -2
- data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +2 -2
- data/lib/rubocop/cop/naming/method_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
- data/lib/rubocop/cop/naming/variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/variable_number.rb +1 -1
- data/lib/rubocop/cop/offense.rb +16 -2
- data/lib/rubocop/cop/style/accessor_grouping.rb +147 -0
- data/lib/rubocop/cop/style/array_coercion.rb +63 -0
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -2
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +146 -0
- data/lib/rubocop/cop/style/case_like_if.rb +217 -0
- data/lib/rubocop/cop/style/class_vars.rb +21 -0
- data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
- data/lib/rubocop/cop/style/date_time.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +5 -5
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
- data/lib/rubocop/cop/style/exponential_notation.rb +6 -8
- data/lib/rubocop/cop/style/float_division.rb +7 -10
- data/lib/rubocop/cop/style/format_string_token.rb +5 -5
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +62 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +76 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -11
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
- data/lib/rubocop/cop/style/missing_else.rb +1 -11
- data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
- data/lib/rubocop/cop/style/numeric_predicate.rb +3 -4
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -3
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
- data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +50 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +7 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -2
- data/lib/rubocop/cop/style/redundant_sort.rb +3 -2
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
- data/lib/rubocop/cop/style/signal_exception.rb +1 -1
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +3 -2
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +2 -2
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +9 -32
- data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
- data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
- data/lib/rubocop/cop/team.rb +97 -81
- data/lib/rubocop/cop/utils/format_string.rb +1 -2
- data/lib/rubocop/cop/variable_force/variable.rb +5 -3
- data/lib/rubocop/file_finder.rb +4 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/name_similarity.rb +1 -3
- data/lib/rubocop/options.rb +15 -8
- data/lib/rubocop/path_util.rb +2 -17
- data/lib/rubocop/rake_task.rb +6 -9
- data/lib/rubocop/result_cache.rb +9 -5
- data/lib/rubocop/rspec/cop_helper.rb +4 -4
- data/lib/rubocop/rspec/expect_offense.rb +52 -22
- data/lib/rubocop/rspec/shared_contexts.rb +8 -8
- data/lib/rubocop/runner.rb +33 -32
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +25 -8
- data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks that there are no repeated conditions used in if 'elsif'.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# if x == 1
|
11
|
+
# do_something
|
12
|
+
# elsif x == 1
|
13
|
+
# do_something_else
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# if x == 1
|
18
|
+
# do_something
|
19
|
+
# elsif x == 2
|
20
|
+
# do_something_else
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
class DuplicateElsifCondition < Base
|
24
|
+
MSG = 'Duplicate `elsif` condition detected.'
|
25
|
+
|
26
|
+
def on_if(node)
|
27
|
+
previous = []
|
28
|
+
while node.if? || node.elsif?
|
29
|
+
condition = node.condition
|
30
|
+
add_offense(condition) if previous.include?(condition)
|
31
|
+
previous << condition
|
32
|
+
node = node.else_branch
|
33
|
+
break unless node&.if_type?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -126,7 +126,7 @@ module RuboCop
|
|
126
126
|
end
|
127
127
|
|
128
128
|
def message_for_dup(node, method_name)
|
129
|
-
format(MSG, method: method_name, defined: @definitions[method_name],
|
129
|
+
format(MSG, method: method_name, defined: source_location(@definitions[method_name]),
|
130
130
|
current: source_location(node))
|
131
131
|
end
|
132
132
|
|
@@ -152,7 +152,7 @@ module RuboCop
|
|
152
152
|
|
153
153
|
add_offense(node, location: loc, message: message)
|
154
154
|
else
|
155
|
-
@definitions[method_name] =
|
155
|
+
@definitions[method_name] = node
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
@@ -30,6 +30,19 @@ module RuboCop
|
|
30
30
|
add_offense(node)
|
31
31
|
end
|
32
32
|
|
33
|
+
def autocorrect(node)
|
34
|
+
lambda do |corrector|
|
35
|
+
starting_token, ending_token = if node.source.include?('"')
|
36
|
+
['%{', '}']
|
37
|
+
else
|
38
|
+
['"', '"']
|
39
|
+
end
|
40
|
+
|
41
|
+
corrector.replace(node.loc.begin, starting_token)
|
42
|
+
corrector.replace(node.loc.end, ending_token)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
33
46
|
def heredoc?(node)
|
34
47
|
node.loc.is_a?(Parser::Source::Map::Heredoc) ||
|
35
48
|
(node.parent && heredoc?(node.parent))
|
@@ -30,6 +30,8 @@ module RuboCop
|
|
30
30
|
# break if condition
|
31
31
|
# end
|
32
32
|
class LiteralAsCondition < Cop
|
33
|
+
include RangeHelp
|
34
|
+
|
33
35
|
MSG = 'Literal `%<literal>s` appeared as a condition.'
|
34
36
|
|
35
37
|
def on_if(node)
|
@@ -57,7 +59,8 @@ module RuboCop
|
|
57
59
|
case_node.each_when do |when_node|
|
58
60
|
next unless when_node.conditions.all?(&:literal?)
|
59
61
|
|
60
|
-
|
62
|
+
range = when_conditions_range(when_node)
|
63
|
+
add_offense(when_node, location: range, message: message(range))
|
61
64
|
end
|
62
65
|
end
|
63
66
|
end
|
@@ -129,6 +132,13 @@ module RuboCop
|
|
129
132
|
node.condition
|
130
133
|
end
|
131
134
|
end
|
135
|
+
|
136
|
+
def when_conditions_range(when_node)
|
137
|
+
range_between(
|
138
|
+
when_node.conditions.first.source_range.begin_pos,
|
139
|
+
when_node.conditions.last.source_range.end_pos
|
140
|
+
)
|
141
|
+
end
|
132
142
|
end
|
133
143
|
end
|
134
144
|
end
|
@@ -59,31 +59,25 @@ module RuboCop
|
|
59
59
|
'Use `lambda` instead.'
|
60
60
|
|
61
61
|
def on_def(node)
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
end
|
66
|
-
alias on_defs on_def
|
67
|
-
|
68
|
-
private
|
62
|
+
subject, = *node
|
63
|
+
return if node.defs_type? && subject.lvar_type?
|
69
64
|
|
70
|
-
|
71
|
-
|
72
|
-
if child.def_type?
|
73
|
-
yield child
|
74
|
-
elsif child.defs_type?
|
75
|
-
subject, = *child
|
76
|
-
next if subject.lvar_type?
|
65
|
+
def_ancestor = node.each_ancestor(:def, :defs).first
|
66
|
+
return unless def_ancestor
|
77
67
|
|
78
|
-
|
79
|
-
|
80
|
-
|
68
|
+
within_scoping_def =
|
69
|
+
node.each_ancestor(:block, :sclass).any? do |ancestor|
|
70
|
+
scoping_method_call?(ancestor)
|
81
71
|
end
|
82
|
-
|
72
|
+
|
73
|
+
add_offense(node) if def_ancestor && !within_scoping_def
|
83
74
|
end
|
75
|
+
alias on_defs on_def
|
76
|
+
|
77
|
+
private
|
84
78
|
|
85
79
|
def scoping_method_call?(child)
|
86
|
-
eval_call?(child) || exec_call?(child) ||
|
80
|
+
child.sclass_type? || eval_call?(child) || exec_call?(child) ||
|
87
81
|
class_or_module_or_struct_new_call?(child)
|
88
82
|
end
|
89
83
|
|
@@ -96,7 +90,7 @@ module RuboCop
|
|
96
90
|
PATTERN
|
97
91
|
|
98
92
|
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
|
99
|
-
(block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
|
93
|
+
(block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
|
100
94
|
PATTERN
|
101
95
|
end
|
102
96
|
end
|
@@ -69,11 +69,11 @@ module RuboCop
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def_node_matcher :unsorted_dir_block?, <<~PATTERN
|
72
|
-
(send (const nil? :Dir) :glob ...)
|
72
|
+
(send (const {nil? cbase} :Dir) :glob ...)
|
73
73
|
PATTERN
|
74
74
|
|
75
75
|
def_node_matcher :unsorted_dir_each?, <<~PATTERN
|
76
|
-
(send (send (const nil? :Dir) {:[] :glob} ...) :each)
|
76
|
+
(send (send (const {nil? cbase} :Dir) {:[] :glob} ...) :each)
|
77
77
|
PATTERN
|
78
78
|
|
79
79
|
def_node_matcher :loop_variable, <<~PATTERN
|
@@ -48,17 +48,22 @@ module RuboCop
|
|
48
48
|
return true
|
49
49
|
end
|
50
50
|
|
51
|
-
node.operator_method? || node.setter_method? ||
|
51
|
+
node.operator_method? || node.setter_method? || chained_calls?(node) ||
|
52
|
+
operator_keyword?(node)
|
52
53
|
end
|
53
54
|
|
54
55
|
def first_argument_starts_with_left_parenthesis?(node)
|
55
56
|
node.first_argument.source.start_with?('(')
|
56
57
|
end
|
57
58
|
|
58
|
-
def
|
59
|
+
def chained_calls?(node)
|
59
60
|
first_argument = node.first_argument
|
61
|
+
first_argument.send_type? && (node.children.last&.children&.count || 0) > 1
|
62
|
+
end
|
60
63
|
|
61
|
-
|
64
|
+
def operator_keyword?(node)
|
65
|
+
first_argument = node.first_argument
|
66
|
+
first_argument.operator_keyword?
|
62
67
|
end
|
63
68
|
|
64
69
|
def spaces_before_left_parenthesis(node)
|
@@ -44,6 +44,14 @@ module RuboCop
|
|
44
44
|
exception_new_with_message?(node, &check(node))
|
45
45
|
end
|
46
46
|
|
47
|
+
def autocorrect(node)
|
48
|
+
lambda do |corrector|
|
49
|
+
exception_class = node.children.first&.cbase_type? ? '::StandardError' : 'StandardError'
|
50
|
+
|
51
|
+
corrector.replace(node, exception_class)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
47
55
|
private
|
48
56
|
|
49
57
|
def check(node)
|
@@ -25,7 +25,7 @@ module RuboCop
|
|
25
25
|
'Perhaps you meant `rand(2)` or `rand`?'
|
26
26
|
|
27
27
|
def_node_matcher :rand_one?, <<~PATTERN
|
28
|
-
(send {(const nil? :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
|
28
|
+
(send {(const {nil? cbase} :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
|
29
29
|
PATTERN
|
30
30
|
|
31
31
|
def on_send(node)
|
@@ -25,34 +25,34 @@ module RuboCop
|
|
25
25
|
#
|
26
26
|
# # good
|
27
27
|
# x += 1
|
28
|
-
class RedundantCopDisableDirective <
|
28
|
+
class RedundantCopDisableDirective < Base
|
29
29
|
include RangeHelp
|
30
|
+
extend AutoCorrector
|
30
31
|
|
31
32
|
COP_NAME = 'Lint/RedundantCopDisableDirective'
|
32
33
|
|
33
|
-
|
34
|
+
attr_accessor :offenses_to_check
|
35
|
+
|
36
|
+
def initialize(config = nil, options = nil, offenses = nil)
|
37
|
+
@offenses_to_check = offenses
|
38
|
+
super(config, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def on_new_investigation
|
42
|
+
return unless offenses_to_check
|
43
|
+
|
44
|
+
comments = processed_source.comments
|
45
|
+
cop_disabled_line_ranges = processed_source.disabled_line_ranges
|
46
|
+
|
34
47
|
redundant_cops = Hash.new { |h, k| h[k] = Set.new }
|
35
48
|
|
36
49
|
each_redundant_disable(cop_disabled_line_ranges,
|
37
|
-
|
50
|
+
offenses_to_check, comments) do |comment, redundant_cop|
|
38
51
|
redundant_cops[comment].add(redundant_cop)
|
39
52
|
end
|
40
53
|
|
41
54
|
add_offenses(redundant_cops)
|
42
|
-
|
43
|
-
|
44
|
-
def autocorrect(args)
|
45
|
-
lambda do |corrector|
|
46
|
-
ranges, range = *args # Ranges are sorted by position.
|
47
|
-
|
48
|
-
range = if range.source.start_with?('#')
|
49
|
-
comment_range_with_surrounding_space(range)
|
50
|
-
else
|
51
|
-
directive_range_in_list(range, ranges)
|
52
|
-
end
|
53
|
-
|
54
|
-
corrector.remove(range)
|
55
|
-
end
|
55
|
+
super
|
56
56
|
end
|
57
57
|
|
58
58
|
private
|
@@ -187,10 +187,12 @@ module RuboCop
|
|
187
187
|
cop_list = cops.sort.map { |c| describe(c) }
|
188
188
|
|
189
189
|
add_offense(
|
190
|
-
|
191
|
-
location: location,
|
190
|
+
location,
|
192
191
|
message: "Unnecessary disabling of #{cop_list.join(', ')}."
|
193
|
-
)
|
192
|
+
) do |corrector|
|
193
|
+
range = comment_range_with_surrounding_space(location)
|
194
|
+
corrector.remove(range)
|
195
|
+
end
|
194
196
|
end
|
195
197
|
|
196
198
|
def add_offense_for_some_cops(comment, cops)
|
@@ -200,10 +202,12 @@ module RuboCop
|
|
200
202
|
|
201
203
|
cop_ranges.each do |cop, range|
|
202
204
|
add_offense(
|
203
|
-
|
204
|
-
location: range,
|
205
|
+
range,
|
205
206
|
message: "Unnecessary disabling of #{describe(cop)}."
|
206
|
-
)
|
207
|
+
) do |corrector|
|
208
|
+
range = directive_range_in_list(range, ranges)
|
209
|
+
corrector.remove(range)
|
210
|
+
end
|
207
211
|
end
|
208
212
|
end
|
209
213
|
|
@@ -60,8 +60,8 @@ module RuboCop
|
|
60
60
|
|
61
61
|
def_node_matcher :array_new?, <<~PATTERN
|
62
62
|
{
|
63
|
-
$(send (const nil? :Array) :new ...)
|
64
|
-
$(block (send (const nil? :Array) :new ...) ...)
|
63
|
+
$(send (const {nil? cbase} :Array) :new ...)
|
64
|
+
$(block (send (const {nil? cbase} :Array) :new ...) ...)
|
65
65
|
}
|
66
66
|
PATTERN
|
67
67
|
|
@@ -32,6 +32,14 @@ module RuboCop
|
|
32
32
|
|
33
33
|
add_offense(node.condition)
|
34
34
|
end
|
35
|
+
|
36
|
+
def autocorrect(node)
|
37
|
+
lambda do |corrector|
|
38
|
+
receiver = node.receiver.source
|
39
|
+
|
40
|
+
corrector.replace(node, "#{receiver} && #{receiver}.#{node.method_name}")
|
41
|
+
end
|
42
|
+
end
|
35
43
|
end
|
36
44
|
end
|
37
45
|
end
|
@@ -3,49 +3,34 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
# This
|
7
|
-
# provides methods to repack Parser's diagnostics/errors
|
6
|
+
# This cop repacks Parser's diagnostics/errors
|
8
7
|
# into RuboCop's offenses.
|
9
|
-
class Syntax <
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
ERROR_SOURCE_RANGE = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
|
14
|
-
|
15
|
-
def self.offenses_from_processed_source(processed_source,
|
16
|
-
config, options)
|
17
|
-
cop = new(config, options)
|
18
|
-
|
19
|
-
cop.add_offense_from_error(processed_source.parser_error) if processed_source.parser_error
|
20
|
-
|
8
|
+
class Syntax < Base
|
9
|
+
def on_other_file
|
10
|
+
add_offense_from_error(processed_source.parser_error) if processed_source.parser_error
|
21
11
|
processed_source.diagnostics.each do |diagnostic|
|
22
|
-
|
23
|
-
|
12
|
+
add_offense_from_diagnostic(diagnostic,
|
13
|
+
processed_source.ruby_version)
|
24
14
|
end
|
25
|
-
|
26
|
-
cop.offenses
|
15
|
+
super
|
27
16
|
end
|
28
17
|
|
18
|
+
private
|
19
|
+
|
29
20
|
def add_offense_from_diagnostic(diagnostic, ruby_version)
|
30
21
|
message =
|
31
22
|
"#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
|
32
23
|
'configure using `TargetRubyVersion` parameter, under `AllCops`)'
|
33
|
-
add_offense(
|
34
|
-
location: diagnostic.location,
|
24
|
+
add_offense(diagnostic.location,
|
35
25
|
message: message,
|
36
26
|
severity: diagnostic.level)
|
37
27
|
end
|
38
28
|
|
39
29
|
def add_offense_from_error(error)
|
40
30
|
message = beautify_message(error.message)
|
41
|
-
|
42
|
-
location: ERROR_SOURCE_RANGE,
|
43
|
-
message: message,
|
44
|
-
severity: :fatal)
|
31
|
+
add_global_offense(message, severity: :fatal)
|
45
32
|
end
|
46
33
|
|
47
|
-
private
|
48
|
-
|
49
34
|
def beautify_message(message)
|
50
35
|
message = message.capitalize
|
51
36
|
message << '.' unless message.end_with?('.')
|
@@ -172,7 +172,7 @@ module RuboCop
|
|
172
172
|
PATTERN
|
173
173
|
|
174
174
|
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
|
175
|
-
(block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
|
175
|
+
(block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
|
176
176
|
PATTERN
|
177
177
|
|
178
178
|
def check_node(node)
|