rubocop 0.42.0 → 0.43.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/README.md +1 -1
- data/assets/output.html.erb +21 -10
- data/config/default.yml +32 -2
- data/config/disabled.yml +8 -1
- data/config/enabled.yml +40 -12
- data/lib/rubocop.rb +14 -2
- data/lib/rubocop/ast_node.rb +2 -0
- data/lib/rubocop/cached_data.rb +13 -11
- data/lib/rubocop/cli.rb +5 -5
- data/lib/rubocop/config.rb +68 -24
- data/lib/rubocop/config_loader.rb +13 -11
- data/lib/rubocop/config_loader_resolver.rb +4 -2
- data/lib/rubocop/cop/cop.rb +16 -5
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +21 -20
- data/lib/rubocop/cop/lint/block_alignment.rb +3 -4
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -3
- data/lib/rubocop/cop/lint/duplicate_methods.rb +16 -6
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +4 -6
- data/lib/rubocop/cop/lint/eval.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +8 -8
- data/lib/rubocop/cop/lint/inherit_exception.rb +22 -7
- data/lib/rubocop/cop/lint/literal_in_condition.rb +5 -5
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +3 -5
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +9 -8
- data/lib/rubocop/cop/lint/percent_string_array.rb +17 -6
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +4 -4
- data/lib/rubocop/cop/lint/rand_one.rb +3 -3
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +39 -44
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +2 -2
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -2
- data/lib/rubocop/cop/lint/unified_integer.rb +38 -0
- data/lib/rubocop/cop/lint/unneeded_disable.rb +51 -38
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +114 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +25 -12
- data/lib/rubocop/cop/lint/useless_setter_call.rb +27 -28
- data/lib/rubocop/cop/lint/void.rb +2 -4
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +5 -5
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +19 -17
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +3 -5
- data/lib/rubocop/cop/mixin/configurable_naming.rb +4 -5
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +52 -0
- data/lib/rubocop/cop/mixin/def_node.rb +28 -0
- data/lib/rubocop/cop/mixin/documentation_comment.rb +41 -0
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +18 -13
- data/lib/rubocop/cop/mixin/if_node.rb +6 -0
- data/lib/rubocop/cop/mixin/match_range.rb +2 -5
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +40 -28
- data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -5
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +14 -4
- data/lib/rubocop/cop/mixin/safe_mode.rb +23 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -4
- data/lib/rubocop/cop/mixin/space_inside.rb +1 -3
- data/lib/rubocop/cop/mixin/statement_modifier.rb +30 -20
- data/lib/rubocop/cop/mixin/trailing_comma.rb +19 -17
- data/lib/rubocop/cop/performance/case_when_splat.rb +16 -41
- data/lib/rubocop/cop/performance/casecmp.rb +28 -16
- data/lib/rubocop/cop/performance/count.rb +58 -34
- data/lib/rubocop/cop/performance/detect.rb +3 -7
- data/lib/rubocop/cop/performance/double_start_end_with.rb +17 -13
- data/lib/rubocop/cop/performance/fixed_size.rb +19 -14
- data/lib/rubocop/cop/performance/flat_map.rb +16 -9
- data/lib/rubocop/cop/performance/hash_each.rb +2 -3
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +4 -6
- data/lib/rubocop/cop/performance/redundant_match.rb +4 -1
- data/lib/rubocop/cop/performance/redundant_merge.rb +63 -32
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +8 -7
- data/lib/rubocop/cop/performance/reverse_each.rb +1 -4
- data/lib/rubocop/cop/performance/size.rb +21 -8
- data/lib/rubocop/cop/performance/sort_with_block.rb +54 -0
- data/lib/rubocop/cop/performance/string_replacement.rb +3 -7
- data/lib/rubocop/cop/rails/delegate.rb +2 -3
- data/lib/rubocop/cop/rails/find_by.rb +4 -8
- data/lib/rubocop/cop/rails/not_null_column.rb +45 -0
- data/lib/rubocop/cop/rails/request_referer.rb +3 -3
- data/lib/rubocop/cop/rails/safe_navigation.rb +89 -0
- data/lib/rubocop/cop/rails/save_bang.rb +78 -9
- data/lib/rubocop/cop/rails/scope_args.rb +3 -1
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +2 -3
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/security/json_load.rb +36 -0
- data/lib/rubocop/cop/style/alias.rb +1 -1
- data/lib/rubocop/cop/style/align_hash.rb +25 -14
- data/lib/rubocop/cop/style/and_or.rb +13 -3
- data/lib/rubocop/cop/style/array_join.rb +3 -3
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -2
- data/lib/rubocop/cop/style/attr.rb +1 -3
- data/lib/rubocop/cop/style/block_comments.rb +2 -6
- data/lib/rubocop/cop/style/block_delimiters.rb +35 -21
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -4
- data/lib/rubocop/cop/style/case_indentation.rb +1 -3
- data/lib/rubocop/cop/style/class_methods.rb +3 -4
- data/lib/rubocop/cop/style/collection_methods.rb +1 -1
- data/lib/rubocop/cop/style/command_literal.rb +15 -8
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +68 -36
- data/lib/rubocop/cop/style/copyright.rb +1 -5
- data/lib/rubocop/cop/style/def_with_parentheses.rb +3 -5
- data/lib/rubocop/cop/style/documentation.rb +28 -56
- data/lib/rubocop/cop/style/documentation_method.rb +80 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +6 -5
- data/lib/rubocop/cop/style/each_with_object.rb +2 -2
- data/lib/rubocop/cop/style/else_alignment.rb +10 -9
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -4
- data/lib/rubocop/cop/style/empty_else.rb +1 -4
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -3
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -5
- data/lib/rubocop/cop/style/encoding.rb +28 -14
- data/lib/rubocop/cop/style/even_odd.rb +28 -17
- data/lib/rubocop/cop/style/extra_spacing.rb +36 -25
- data/lib/rubocop/cop/style/file_name.rb +19 -10
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +2 -3
- data/lib/rubocop/cop/style/for.rb +12 -8
- data/lib/rubocop/cop/style/format_string.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +22 -56
- data/lib/rubocop/cop/style/hash_syntax.rb +72 -7
- data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -19
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -3
- data/lib/rubocop/cop/style/indentation_width.rb +30 -16
- data/lib/rubocop/cop/style/infinite_loop.rb +16 -13
- data/lib/rubocop/cop/style/initial_indentation.rb +23 -18
- data/lib/rubocop/cop/style/inline_comment.rb +16 -3
- data/lib/rubocop/cop/style/lambda.rb +22 -10
- data/lib/rubocop/cop/style/leading_comment_space.rb +12 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +24 -6
- data/lib/rubocop/cop/style/method_call_parentheses.rb +18 -9
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +3 -4
- data/lib/rubocop/cop/style/method_missing.rb +10 -2
- data/lib/rubocop/cop/style/module_function.rb +14 -6
- data/lib/rubocop/cop/style/multiline_assignment_layout.rb +2 -5
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -5
- data/lib/rubocop/cop/style/multiline_block_layout.rb +22 -15
- data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +9 -0
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +41 -20
- data/lib/rubocop/cop/style/multiline_operation_indentation.rb +6 -6
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +3 -5
- data/lib/rubocop/cop/style/mutable_constant.rb +21 -13
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_while.rb +3 -3
- data/lib/rubocop/cop/style/nested_modifier.rb +2 -4
- data/lib/rubocop/cop/style/next.rb +4 -4
- data/lib/rubocop/cop/style/non_nil_check.rb +18 -10
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +8 -0
- data/lib/rubocop/cop/style/numeric_predicate.rb +9 -9
- data/lib/rubocop/cop/style/one_line_conditional.rb +11 -1
- data/lib/rubocop/cop/style/op_method.rb +1 -1
- data/lib/rubocop/cop/style/option_hash.rb +8 -8
- data/lib/rubocop/cop/style/optional_arguments.rb +21 -8
- data/lib/rubocop/cop/style/parallel_assignment.rb +51 -35
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +2 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -15
- data/lib/rubocop/cop/style/redundant_return.rb +5 -5
- data/lib/rubocop/cop/style/redundant_self.rb +20 -11
- data/lib/rubocop/cop/style/regexp_literal.rb +16 -10
- data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +8 -6
- data/lib/rubocop/cop/style/safe_navigation.rb +125 -0
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +9 -10
- data/lib/rubocop/cop/style/signal_exception.rb +2 -4
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +18 -11
- data/lib/rubocop/cop/style/space_after_method_name.rb +2 -3
- data/lib/rubocop/cop/style/space_after_not.rb +4 -6
- data/lib/rubocop/cop/style/space_around_block_parameters.rb +1 -2
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -3
- data/lib/rubocop/cop/style/space_around_operators.rb +21 -16
- data/lib/rubocop/cop/style/space_before_block_braces.rb +2 -12
- data/lib/rubocop/cop/style/space_before_first_arg.rb +1 -3
- data/lib/rubocop/cop/style/space_inside_array_percent_literal.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +33 -40
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +38 -23
- data/lib/rubocop/cop/style/space_inside_percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +26 -12
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/symbol_array.rb +10 -10
- data/lib/rubocop/cop/style/symbol_proc.rb +28 -13
- data/lib/rubocop/cop/style/ternary_parentheses.rb +35 -5
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +2 -4
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +29 -17
- data/lib/rubocop/cop/style/trivial_accessors.rb +6 -6
- data/lib/rubocop/cop/style/unless_else.rb +2 -6
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +8 -4
- data/lib/rubocop/cop/style/unneeded_interpolation.rb +4 -5
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -7
- data/lib/rubocop/cop/style/variable_number.rb +79 -0
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +25 -15
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -0
- data/lib/rubocop/cop/util.rb +23 -4
- data/lib/rubocop/cop/variable_force.rb +59 -25
- data/lib/rubocop/cop/variable_force/locatable.rb +8 -6
- data/lib/rubocop/cop/variable_force/variable.rb +2 -2
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
- data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -11
- data/lib/rubocop/formatter/formatter_set.rb +12 -10
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +4 -4
- data/lib/rubocop/node_pattern.rb +79 -35
- data/lib/rubocop/options.rb +4 -4
- data/lib/rubocop/processed_source.rb +9 -5
- data/lib/rubocop/remote_config.rb +14 -10
- data/lib/rubocop/result_cache.rb +14 -6
- data/lib/rubocop/runner.rb +55 -34
- data/lib/rubocop/string_util.rb +9 -5
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/token.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +15 -4
- data/lib/rubocop/cop/lint/useless_array_splat.rb +0 -56
- data/lib/rubocop/cop/performance/push_splat.rb +0 -47
@@ -26,21 +26,21 @@ module RuboCop
|
|
26
26
|
expected_column = base_column(left_brace, left_parenthesis) +
|
27
27
|
configured_indentation_width + offset
|
28
28
|
@column_delta = expected_column - actual_column
|
29
|
+
styles =
|
30
|
+
detected_styles(actual_column, offset, left_parenthesis, left_brace)
|
29
31
|
|
30
32
|
if @column_delta.zero?
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
33
|
+
check_expected_style(styles)
|
34
|
+
else
|
35
|
+
incorrect_style_detected(styles, first, left_parenthesis)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def check_expected_style(styles)
|
40
|
+
if styles.size > 1
|
41
|
+
ambiguous_style_detected(*styles)
|
41
42
|
else
|
42
|
-
|
43
|
-
left_parenthesis, left_brace)
|
43
|
+
correct_style_detected
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -54,7 +54,12 @@ module RuboCop
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
def detected_styles(
|
57
|
+
def detected_styles(actual_column, offset, left_parenthesis, left_brace)
|
58
|
+
base_column = actual_column - configured_indentation_width - offset
|
59
|
+
detected_styles_for_column(base_column, left_parenthesis, left_brace)
|
60
|
+
end
|
61
|
+
|
62
|
+
def detected_styles_for_column(column, left_parenthesis, left_brace)
|
58
63
|
styles = []
|
59
64
|
if column == (left_brace.source_line =~ /\S/)
|
60
65
|
styles << :consistent
|
@@ -67,12 +72,9 @@ module RuboCop
|
|
67
72
|
styles
|
68
73
|
end
|
69
74
|
|
70
|
-
def incorrect_style_detected(
|
71
|
-
left_brace)
|
75
|
+
def incorrect_style_detected(styles, first, left_parenthesis)
|
72
76
|
add_offense(first, :expression,
|
73
77
|
message(base_description(left_parenthesis))) do
|
74
|
-
base_column = column - configured_indentation_width - offset
|
75
|
-
styles = detected_styles(base_column, left_parenthesis, left_brace)
|
76
78
|
ambiguous_style_detected(*styles)
|
77
79
|
end
|
78
80
|
end
|
@@ -123,8 +123,8 @@ module RuboCop
|
|
123
123
|
else
|
124
124
|
line_begin_pos - column_delta.abs
|
125
125
|
end
|
126
|
-
|
127
|
-
|
126
|
+
|
127
|
+
range_between(pos_to_remove, pos_to_remove + column_delta.abs)
|
128
128
|
end
|
129
129
|
|
130
130
|
def remove(range, corrector)
|
@@ -132,9 +132,7 @@ module RuboCop
|
|
132
132
|
$stderr = StringIO.new # Avoid error messages on console
|
133
133
|
corrector.remove(range)
|
134
134
|
rescue RuntimeError
|
135
|
-
range =
|
136
|
-
range.begin_pos + 1,
|
137
|
-
range.end_pos + 1)
|
135
|
+
range = range_between(range.begin_pos + 1, range.end_pos + 1)
|
138
136
|
retry if range.source =~ /^ +$/
|
139
137
|
ensure
|
140
138
|
$stderr = original_stderr
|
@@ -31,14 +31,13 @@ module RuboCop
|
|
31
31
|
# A class emitter method is a singleton method in a class/module, where
|
32
32
|
# the method has the same name as a class defined in the class/module.
|
33
33
|
def class_emitter_method?(node, name)
|
34
|
-
return false unless node.defs_type?
|
34
|
+
return false unless node.parent && node.defs_type?
|
35
35
|
# a class emitter method may be defined inside `def self.included`,
|
36
36
|
# `def self.extended`, etc.
|
37
|
-
node = node.parent while node.parent
|
38
|
-
return false unless node.parent
|
37
|
+
node = node.parent while node.parent.defs_type?
|
39
38
|
|
40
|
-
node.parent.
|
41
|
-
c.
|
39
|
+
node.parent.each_child_node(:class).any? do |c|
|
40
|
+
c.loc.name.is?(name.to_s)
|
42
41
|
end
|
43
42
|
end
|
44
43
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module RuboCop
|
5
|
+
module Cop
|
6
|
+
# This module provides functionality for checking if numbering match the
|
7
|
+
# configured EnforcedStyle.
|
8
|
+
module ConfigurableNumbering
|
9
|
+
include ConfigurableEnforcedStyle
|
10
|
+
|
11
|
+
SNAKE_CASE = /^@{0,2}[-_a-z]+[_\D]*(_\d)*[!?=]?$/
|
12
|
+
NORMAL_CASE = /^@{0,2}-{0,1}_{0,1}[a-zA-Z\d]*[_\D]*[!?=]?$/
|
13
|
+
NON_INTEGER = /^@{0,2}[-_a-z]+[_\D]*[!?=]?$/
|
14
|
+
|
15
|
+
def check_name(node, name, name_range)
|
16
|
+
return if operator?(name)
|
17
|
+
|
18
|
+
if valid_name?(node, name)
|
19
|
+
correct_style_detected
|
20
|
+
else
|
21
|
+
add_offense(node, name_range, message(style))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_name?(node, name)
|
26
|
+
pattern =
|
27
|
+
case style
|
28
|
+
when :snake_case
|
29
|
+
SNAKE_CASE
|
30
|
+
when :normalcase
|
31
|
+
NORMAL_CASE
|
32
|
+
when :non_integer
|
33
|
+
NON_INTEGER
|
34
|
+
end
|
35
|
+
name.match(pattern) || class_emitter_method?(node, name)
|
36
|
+
end
|
37
|
+
|
38
|
+
# A class emitter method is a singleton method in a class/module, where
|
39
|
+
# the method has the same name as a class defined in the class/module.
|
40
|
+
def class_emitter_method?(node, name)
|
41
|
+
return false unless node.parent && node.defs_type?
|
42
|
+
# a class emitter method may be defined inside `def self.included`,
|
43
|
+
# `def self.extended`, etc.
|
44
|
+
node = node.parent while node.parent.defs_type?
|
45
|
+
|
46
|
+
node.parent.each_child_node(:class).any? do |c|
|
47
|
+
c.loc.name.is?(name.to_s)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module RuboCop
|
5
|
+
module Cop
|
6
|
+
# Common functionality for checking if nodes.
|
7
|
+
module DefNode
|
8
|
+
extend NodePattern::Macros
|
9
|
+
|
10
|
+
NON_PUBLIC_MODIFIERS = %w(private protected).freeze
|
11
|
+
|
12
|
+
def non_public?(node)
|
13
|
+
non_public_modifier?(node.parent) ||
|
14
|
+
preceding_non_public_modifier?(node)
|
15
|
+
end
|
16
|
+
|
17
|
+
def preceding_non_public_modifier?(node)
|
18
|
+
stripped_source_upto(node.loc.line).any? do |line|
|
19
|
+
NON_PUBLIC_MODIFIERS.include?(line)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def_node_matcher :non_public_modifier?, <<-PATTERN
|
24
|
+
(send nil {:private :protected} ({def defs} ...))
|
25
|
+
PATTERN
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module RuboCop
|
5
|
+
module Cop
|
6
|
+
# Common functionality for checking documentation.
|
7
|
+
module DocumentationComment
|
8
|
+
extend NodePattern::Macros
|
9
|
+
include Style::AnnotationComment
|
10
|
+
|
11
|
+
def_node_matcher :constant_definition?, '{class module casgn}'
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def documentation_comment?(node)
|
16
|
+
preceding_lines = preceding_lines(node)
|
17
|
+
|
18
|
+
return false unless preceding_comment?(node, preceding_lines.last)
|
19
|
+
|
20
|
+
preceding_lines.any? do |comment|
|
21
|
+
!annotation?(comment) && !interpreter_directive_comment?(comment)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def preceding_comment?(n1, n2)
|
26
|
+
n1 && n2 && preceed?(n2, n1) &&
|
27
|
+
comment_line?(n2.loc.expression.source)
|
28
|
+
end
|
29
|
+
|
30
|
+
def preceding_lines(node)
|
31
|
+
processed_source.ast_with_comments[node].select do |line|
|
32
|
+
line.loc.line < node.loc.line
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def interpreter_directive_comment?(comment)
|
37
|
+
comment.text =~ /^#\s*(frozen_string_literal|encoding):/
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -59,23 +59,28 @@ module RuboCop
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def align(node, align_to)
|
62
|
-
|
63
|
-
begin_pos = node.loc.end.begin_pos
|
64
|
-
whitespace = Parser::Source::Range.new(source_buffer,
|
65
|
-
begin_pos - node.loc.end.column,
|
66
|
-
begin_pos)
|
62
|
+
whitespace = whitespace_range(node)
|
67
63
|
return false unless whitespace.source.strip.empty?
|
68
64
|
|
69
|
-
column =
|
70
|
-
0
|
71
|
-
elsif align_to.respond_to?(:loc)
|
72
|
-
align_to.source_range.column
|
73
|
-
else
|
74
|
-
align_to.column
|
75
|
-
end
|
76
|
-
|
65
|
+
column = alignment_column(align_to)
|
77
66
|
->(corrector) { corrector.replace(whitespace, ' ' * column) }
|
78
67
|
end
|
68
|
+
|
69
|
+
def whitespace_range(node)
|
70
|
+
begin_pos = node.loc.end.begin_pos
|
71
|
+
|
72
|
+
range_between(begin_pos - node.loc.end.column, begin_pos)
|
73
|
+
end
|
74
|
+
|
75
|
+
def alignment_column(align_to)
|
76
|
+
if !align_to
|
77
|
+
0
|
78
|
+
elsif align_to.respond_to?(:loc)
|
79
|
+
align_to.source_range.column
|
80
|
+
else
|
81
|
+
align_to.column
|
82
|
+
end
|
83
|
+
end
|
79
84
|
end
|
80
85
|
end
|
81
86
|
end
|
@@ -5,6 +5,8 @@ module RuboCop
|
|
5
5
|
module Cop
|
6
6
|
# Common functionality for checking if nodes.
|
7
7
|
module IfNode
|
8
|
+
extend NodePattern::Macros
|
9
|
+
|
8
10
|
def ternary?(node)
|
9
11
|
node.loc.respond_to?(:question)
|
10
12
|
end
|
@@ -32,6 +34,10 @@ module RuboCop
|
|
32
34
|
|
33
35
|
[condition, body, else_clause]
|
34
36
|
end
|
37
|
+
|
38
|
+
def_node_matcher :guard_clause?, <<-PATTERN
|
39
|
+
[{(send nil {:raise :fail} ...) return break next} single_line?]
|
40
|
+
PATTERN
|
35
41
|
end
|
36
42
|
end
|
37
43
|
end
|
@@ -15,11 +15,8 @@ module RuboCop
|
|
15
15
|
|
16
16
|
# For a `match` inside `range`, return a new `Range` covering the match
|
17
17
|
def match_range(range, match)
|
18
|
-
|
19
|
-
|
20
|
-
range.begin_pos + match.begin(1),
|
21
|
-
range.begin_pos + match.end(1)
|
22
|
-
)
|
18
|
+
range_between(range.begin_pos + match.begin(1),
|
19
|
+
range.begin_pos + match.end(1))
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
@@ -177,12 +177,12 @@ module RuboCop
|
|
177
177
|
end
|
178
178
|
|
179
179
|
def grouped_expression?(node)
|
180
|
-
node.
|
180
|
+
node.begin_type? && node.loc.respond_to?(:begin) && node.loc.begin
|
181
181
|
end
|
182
182
|
|
183
183
|
def inside_arg_list_parentheses?(node, ancestor)
|
184
184
|
a = ancestor.loc
|
185
|
-
return false unless ancestor.
|
185
|
+
return false unless ancestor.send_type? && a.begin &&
|
186
186
|
a.begin.is?('(')
|
187
187
|
n = node.source_range
|
188
188
|
n.begin_pos > a.begin.begin_pos && n.end_pos < a.end.end_pos
|
@@ -9,19 +9,13 @@ module RuboCop
|
|
9
9
|
include ConfigurableEnforcedStyle
|
10
10
|
|
11
11
|
def check_brace_layout(node)
|
12
|
-
return
|
13
|
-
return if children(node).empty? # Ignore empty literals.
|
14
|
-
return if node.single_line? # Ignore single-line literals.
|
12
|
+
return if ignored_literal?(node)
|
15
13
|
|
16
14
|
# If the last node is or contains a conflicting HEREDOC, we don't want
|
17
15
|
# to adjust the brace layout because this will result in invalid code.
|
18
|
-
return if last_line_heredoc?(
|
16
|
+
return if last_line_heredoc?(node.children.last)
|
19
17
|
|
20
|
-
|
21
|
-
when :symmetrical then handle_symmetrical(node)
|
22
|
-
when :new_line then handle_new_line(node)
|
23
|
-
when :same_line then handle_same_line(node)
|
24
|
-
end
|
18
|
+
check(node)
|
25
19
|
end
|
26
20
|
|
27
21
|
def autocorrect(node)
|
@@ -42,34 +36,27 @@ module RuboCop
|
|
42
36
|
|
43
37
|
private
|
44
38
|
|
45
|
-
def
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
children(node).last.source_range
|
39
|
+
def check(node)
|
40
|
+
case style
|
41
|
+
when :symmetrical then check_symmetrical(node)
|
42
|
+
when :new_line then check_new_line(node)
|
43
|
+
when :same_line then check_same_line(node)
|
51
44
|
end
|
52
45
|
end
|
53
46
|
|
54
|
-
def
|
55
|
-
range = range_with_surrounding_space(children(node).last.source_range,
|
56
|
-
:right).end.resize(1)
|
57
|
-
range.source == ',' ? range : nil
|
58
|
-
end
|
59
|
-
|
60
|
-
def handle_new_line(node)
|
47
|
+
def check_new_line(node)
|
61
48
|
return unless closing_brace_on_same_line?(node)
|
62
49
|
|
63
50
|
add_offense(node, :end, self.class::ALWAYS_NEW_LINE_MESSAGE)
|
64
51
|
end
|
65
52
|
|
66
|
-
def
|
53
|
+
def check_same_line(node)
|
67
54
|
return if closing_brace_on_same_line?(node)
|
68
55
|
|
69
56
|
add_offense(node, :end, self.class::ALWAYS_SAME_LINE_MESSAGE)
|
70
57
|
end
|
71
58
|
|
72
|
-
def
|
59
|
+
def check_symmetrical(node)
|
73
60
|
if opening_brace_on_same_line?(node)
|
74
61
|
return if closing_brace_on_same_line?(node)
|
75
62
|
|
@@ -81,6 +68,33 @@ module RuboCop
|
|
81
68
|
end
|
82
69
|
end
|
83
70
|
|
71
|
+
def ignored_literal?(node)
|
72
|
+
implicit_literal?(node) || empty_literal?(node) || node.single_line?
|
73
|
+
end
|
74
|
+
|
75
|
+
def implicit_literal?(node)
|
76
|
+
!node.loc.begin
|
77
|
+
end
|
78
|
+
|
79
|
+
def empty_literal?(node)
|
80
|
+
children(node).empty?
|
81
|
+
end
|
82
|
+
|
83
|
+
def last_element_range_with_trailing_comma(node)
|
84
|
+
trailing_comma_range = last_element_trailing_comma_range(node)
|
85
|
+
if trailing_comma_range
|
86
|
+
children(node).last.source_range.join(trailing_comma_range)
|
87
|
+
else
|
88
|
+
children(node).last.source_range
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def last_element_trailing_comma_range(node)
|
93
|
+
range = range_with_surrounding_space(children(node).last.source_range,
|
94
|
+
:right).end.resize(1)
|
95
|
+
range.source == ',' ? range : nil
|
96
|
+
end
|
97
|
+
|
84
98
|
def children(node)
|
85
99
|
node.children
|
86
100
|
end
|
@@ -124,15 +138,13 @@ module RuboCop
|
|
124
138
|
def last_line_heredoc?(node, parent = nil)
|
125
139
|
parent ||= node
|
126
140
|
|
127
|
-
|
128
|
-
|
129
|
-
if node.loc.respond_to?(:heredoc_end) &&
|
141
|
+
if node.respond_to?(:loc) &&
|
142
|
+
node.loc.respond_to?(:heredoc_end) &&
|
130
143
|
node.loc.heredoc_end.last_line >= parent.loc.last_line
|
131
144
|
return true
|
132
145
|
end
|
133
146
|
|
134
147
|
return false unless node.respond_to?(:children)
|
135
|
-
return false if node.children.empty?
|
136
148
|
|
137
149
|
node.children.any? { |child| last_line_heredoc?(child, parent) }
|
138
150
|
end
|
@@ -6,19 +6,19 @@ module RuboCop
|
|
6
6
|
# Some common code shared between FavorUnlessOverNegatedIf and
|
7
7
|
# FavorUntilOverNegatedWhile.
|
8
8
|
module NegativeConditional
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
extend NodePattern::Macros
|
10
|
+
include IfNode
|
11
|
+
|
12
|
+
def_node_matcher :single_negative?, '(send !(send _ :!) :!)'
|
12
13
|
|
13
14
|
def check_negative_conditional(node)
|
14
15
|
condition, _body, _rest = *node
|
15
16
|
|
16
17
|
# Look at last expression of contents if there are parentheses
|
17
18
|
# around condition.
|
18
|
-
condition = condition.children.last while condition.
|
19
|
+
condition = condition.children.last while condition.begin_type?
|
19
20
|
|
20
|
-
return unless single_negative?(condition) &&
|
21
|
-
!(node.loc.respond_to?(:else) && node.loc.else)
|
21
|
+
return unless single_negative?(condition) && !if_else?(node)
|
22
22
|
|
23
23
|
add_offense(node, :expression)
|
24
24
|
end
|