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
@@ -76,11 +76,7 @@ module RuboCop
|
|
76
76
|
"match Notice /#{notice}/" unless autocorrect_notice =~ regex
|
77
77
|
|
78
78
|
lambda do |corrector|
|
79
|
-
range =
|
80
|
-
Parser::Source::Range.new('', 0, 0)
|
81
|
-
else
|
82
|
-
token.pos
|
83
|
-
end
|
79
|
+
range = token.nil? ? range_between(0, 0) : token.pos
|
84
80
|
corrector.insert_before(range, "#{autocorrect_notice}\n")
|
85
81
|
end
|
86
82
|
end
|
@@ -14,12 +14,10 @@ module RuboCop
|
|
14
14
|
'any arguments.'.freeze
|
15
15
|
|
16
16
|
def on_method_def(node, _method_name, args, _body)
|
17
|
-
|
18
|
-
|
17
|
+
return if node.single_line?
|
18
|
+
return unless args.children.empty? && args.loc.begin
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
add_offense(args, :begin) if args.children == [] && args.loc.begin
|
20
|
+
add_offense(args, :begin)
|
23
21
|
end
|
24
22
|
|
25
23
|
def autocorrect(node)
|
@@ -13,94 +13,66 @@ module RuboCop
|
|
13
13
|
# a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the
|
14
14
|
# same for all its children.
|
15
15
|
class Documentation < Cop
|
16
|
-
include
|
16
|
+
include DocumentationComment
|
17
17
|
|
18
18
|
MSG = 'Missing top-level %s documentation comment.'.freeze
|
19
19
|
|
20
20
|
def_node_matcher :constant_definition?, '{class module casgn}'
|
21
21
|
|
22
22
|
def on_class(node)
|
23
|
-
|
23
|
+
_, _, body = *node
|
24
|
+
|
24
25
|
return unless body
|
25
|
-
return if namespace?(body)
|
26
26
|
|
27
|
-
|
28
|
-
return if associated_comment?(node, ast_with_comments)
|
29
|
-
return if nodoc_comment?(node, ast_with_comments)
|
30
|
-
add_offense(node, :keyword, format(MSG, :class))
|
27
|
+
check(node, body, :class)
|
31
28
|
end
|
32
29
|
|
33
30
|
def on_module(node)
|
34
|
-
|
35
|
-
return if namespace?(body)
|
31
|
+
_, body = *node
|
36
32
|
|
37
|
-
|
38
|
-
return if associated_comment?(node, ast_with_comments)
|
39
|
-
return if nodoc_comment?(node, ast_with_comments)
|
40
|
-
add_offense(node, :keyword, format(MSG, :module))
|
33
|
+
check(node, body, :module)
|
41
34
|
end
|
42
35
|
|
43
36
|
private
|
44
37
|
|
45
|
-
def
|
46
|
-
return
|
38
|
+
def check(node, body, type)
|
39
|
+
return if namespace?(body)
|
40
|
+
return if documentation_comment?(node) || nodoc_comment?(node)
|
47
41
|
|
48
|
-
|
49
|
-
when :begin
|
50
|
-
body_node.children.all? { |node| constant_definition?(node) }
|
51
|
-
else
|
52
|
-
constant_definition?(body_node)
|
53
|
-
end
|
42
|
+
add_offense(node, :keyword, format(MSG, type))
|
54
43
|
end
|
55
44
|
|
56
|
-
|
57
|
-
|
58
|
-
def associated_comment?(node, ast_with_comments)
|
59
|
-
preceding_comments = preceding_comments(node, ast_with_comments)
|
60
|
-
return false if preceding_comments.empty?
|
61
|
-
|
62
|
-
distance = node.loc.keyword.line - preceding_comments.last.loc.line
|
63
|
-
return false if distance > 1
|
64
|
-
return false unless comment_line_only?(preceding_comments.last)
|
45
|
+
def namespace?(node)
|
46
|
+
return false unless node
|
65
47
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
48
|
+
if node.begin_type?
|
49
|
+
node.children.all? { |child| constant_definition?(child) }
|
50
|
+
else
|
51
|
+
constant_definition?(node)
|
70
52
|
end
|
71
53
|
end
|
72
54
|
|
73
|
-
def preceding_comments(node, ast_with_comments)
|
74
|
-
ast_with_comments[node].select { |c| c.loc.line < node.loc.line }
|
75
|
-
end
|
76
|
-
|
77
|
-
def comment_line_only?(comment)
|
78
|
-
source_buffer = comment.loc.expression.source_buffer
|
79
|
-
comment_line = source_buffer.source_line(comment.loc.line)
|
80
|
-
comment_line =~ /^\s*#/
|
81
|
-
end
|
82
|
-
|
83
55
|
# First checks if the :nodoc: comment is associated with the
|
84
56
|
# class/module. Unless the element is tagged with :nodoc:, the search
|
85
57
|
# proceeds to check its ancestors for :nodoc: all.
|
86
58
|
# Note: How end-of-line comments are associated with code changed in
|
87
59
|
# parser-2.2.0.4.
|
88
|
-
def nodoc_comment?(node,
|
89
|
-
return false unless node
|
90
|
-
nodoc_node = node.children.first
|
91
|
-
return false unless nodoc_node
|
92
|
-
comment = ast_with_comments[nodoc_node].first
|
60
|
+
def nodoc_comment?(node, require_all = false)
|
61
|
+
return false unless node && node.children.first
|
93
62
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
63
|
+
nodoc = nodoc(node)
|
64
|
+
|
65
|
+
return true if same_line?(nodoc, node) && nodoc?(nodoc, require_all)
|
66
|
+
|
67
|
+
nodoc_comment?(node.parent, true)
|
68
|
+
end
|
98
69
|
|
99
|
-
|
70
|
+
def nodoc?(comment, require_all = false)
|
71
|
+
comment.text =~ /^#\s*:nodoc:#{"\s+all\s*$" if require_all}/
|
100
72
|
end
|
101
73
|
|
102
|
-
def
|
103
|
-
|
74
|
+
def nodoc(node)
|
75
|
+
processed_source.ast_with_comments[node.children.first].first
|
104
76
|
end
|
105
77
|
end
|
106
78
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for missing documentation comment for public methods.
|
7
|
+
# It can optionally be configured to also require documentation for
|
8
|
+
# non-public methods.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
#
|
12
|
+
# # bad
|
13
|
+
#
|
14
|
+
# class Foo
|
15
|
+
# def bar
|
16
|
+
# puts baz
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# module Foo
|
21
|
+
# def bar
|
22
|
+
# puts baz
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# def foo.bar
|
27
|
+
# puts baz
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
#
|
32
|
+
# class Foo
|
33
|
+
# # Documentation
|
34
|
+
# def bar
|
35
|
+
# puts baz
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# module Foo
|
40
|
+
# # Documentation
|
41
|
+
# def bar
|
42
|
+
# puts baz
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# # Documenation
|
47
|
+
# def foo.bar
|
48
|
+
# puts baz
|
49
|
+
# end
|
50
|
+
class DocumentationMethod < Cop
|
51
|
+
include DocumentationComment
|
52
|
+
include OnMethodDef
|
53
|
+
include DefNode
|
54
|
+
|
55
|
+
MSG = 'Missing method documentation comment.'.freeze
|
56
|
+
|
57
|
+
def on_def(node)
|
58
|
+
check(node)
|
59
|
+
end
|
60
|
+
|
61
|
+
def on_method_def(node, *)
|
62
|
+
check(node)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def check(node)
|
68
|
+
return if non_public?(node) && !require_for_non_public_methods?
|
69
|
+
return if documentation_comment?(node)
|
70
|
+
|
71
|
+
add_offense(node, :expression, MSG)
|
72
|
+
end
|
73
|
+
|
74
|
+
def require_for_non_public_methods?
|
75
|
+
cop_config['RequireForNonPublicMethods']
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -28,11 +28,12 @@ module RuboCop
|
|
28
28
|
'number of times.'.freeze
|
29
29
|
|
30
30
|
def on_block(node)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
return unless offending_each_range(node)
|
32
|
+
|
33
|
+
send_node, = *node
|
34
|
+
range = send_node.receiver.source_range.join(send_node.loc.selector)
|
35
|
+
|
36
|
+
add_offense(node, range)
|
36
37
|
end
|
37
38
|
|
38
39
|
private
|
@@ -76,8 +76,8 @@ module RuboCop
|
|
76
76
|
def return_value(body)
|
77
77
|
return unless body
|
78
78
|
|
79
|
-
return_value = body.
|
80
|
-
return_value if return_value && return_value.
|
79
|
+
return_value = body.begin_type? ? body.children.last : body
|
80
|
+
return_value if return_value && return_value.lvar_type?
|
81
81
|
end
|
82
82
|
|
83
83
|
def first_argument_returned?(args, return_value)
|
@@ -12,25 +12,26 @@ module RuboCop
|
|
12
12
|
include EndKeywordAlignment
|
13
13
|
include AutocorrectAlignment
|
14
14
|
include CheckAssignment
|
15
|
+
include IfNode
|
15
16
|
|
16
17
|
MSG = 'Align `%s` with `%s`.'.freeze
|
17
18
|
|
18
19
|
def on_if(node, base = nil)
|
19
20
|
return if ignored_node?(node)
|
20
|
-
return unless
|
21
|
-
return if node.loc.else.nil?
|
21
|
+
return unless if_else?(node)
|
22
22
|
|
23
23
|
else_range = node.loc.else
|
24
24
|
return unless begins_its_line?(else_range)
|
25
25
|
|
26
26
|
check_alignment(base_range(node, base), else_range)
|
27
27
|
|
28
|
-
|
28
|
+
_, _, else_body = *node
|
29
|
+
|
30
|
+
return unless else_body && elsif?(else_body)
|
29
31
|
|
30
32
|
# If the `else` part is actually an `elsif`, we check the `elsif`
|
31
33
|
# node in case it contains an `else` within, because that `else`
|
32
34
|
# should have the same alignment (base).
|
33
|
-
_condition, _if_body, else_body = *node
|
34
35
|
on_if(else_body, base)
|
35
36
|
# The `elsif` node will get an `on_if` call from the framework later,
|
36
37
|
# but we're done here, so we set it to ignored.
|
@@ -38,10 +39,10 @@ module RuboCop
|
|
38
39
|
end
|
39
40
|
|
40
41
|
def on_rescue(node)
|
41
|
-
return unless node
|
42
|
+
return unless if_else?(node)
|
42
43
|
|
43
44
|
parent = node.parent
|
44
|
-
parent = parent.parent if parent.
|
45
|
+
parent = parent.parent if parent.ensure_type?
|
45
46
|
base = case parent.type
|
46
47
|
when :def, :defs then base_for_method_definition(parent)
|
47
48
|
when :kwbegin then parent.loc.begin
|
@@ -52,7 +53,7 @@ module RuboCop
|
|
52
53
|
|
53
54
|
def on_case(node)
|
54
55
|
_cond, *whens, _else = *node
|
55
|
-
return unless node
|
56
|
+
return unless if_else?(node)
|
56
57
|
check_alignment(whens.last.loc.keyword, node.loc.else)
|
57
58
|
end
|
58
59
|
|
@@ -72,7 +73,7 @@ module RuboCop
|
|
72
73
|
|
73
74
|
def base_for_method_definition(node)
|
74
75
|
parent = node.parent
|
75
|
-
if parent && parent.
|
76
|
+
if parent && parent.send_type?
|
76
77
|
parent.loc.selector # For example "private def ..."
|
77
78
|
else
|
78
79
|
node.loc.keyword
|
@@ -90,7 +91,7 @@ module RuboCop
|
|
90
91
|
style = end_config['AlignWith'] || 'keyword'
|
91
92
|
base = variable_alignment?(node.loc, rhs, style.to_sym) ? node : rhs
|
92
93
|
|
93
|
-
return
|
94
|
+
return unless rhs.if_type?
|
94
95
|
|
95
96
|
on_if(rhs, base)
|
96
97
|
ignore_node(rhs)
|
@@ -82,10 +82,8 @@ module RuboCop
|
|
82
82
|
|
83
83
|
first = children.first
|
84
84
|
last = children.last
|
85
|
-
range =
|
86
|
-
|
87
|
-
first.loc.expression.begin_pos,
|
88
|
-
last.loc.expression.end_pos)
|
85
|
+
range = range_between(first.loc.expression.begin_pos,
|
86
|
+
last.loc.expression.end_pos)
|
89
87
|
|
90
88
|
corrector.replace(range, children.map(&:source).join(' || '))
|
91
89
|
end
|
@@ -117,10 +117,7 @@ module RuboCop
|
|
117
117
|
node.parent.loc.end.begin_pos
|
118
118
|
end
|
119
119
|
|
120
|
-
|
121
|
-
node.loc.else.begin_pos,
|
122
|
-
end_pos)
|
123
|
-
corrector.remove(range)
|
120
|
+
corrector.remove(range_between(node.loc.else.begin_pos, end_pos))
|
124
121
|
end
|
125
122
|
end
|
126
123
|
|
@@ -65,9 +65,7 @@ module RuboCop
|
|
65
65
|
end_pos = prev_def.loc.end.end_pos
|
66
66
|
source_buffer = prev_def.loc.end.source_buffer
|
67
67
|
newline_pos = source_buffer.source.index("\n", end_pos)
|
68
|
-
newline =
|
69
|
-
newline_pos,
|
70
|
-
newline_pos + 1)
|
68
|
+
newline = range_between(newline_pos, newline_pos + 1)
|
71
69
|
->(corrector) { corrector.insert_after(newline, "\n") }
|
72
70
|
end
|
73
71
|
end
|
@@ -38,11 +38,8 @@ module RuboCop
|
|
38
38
|
private
|
39
39
|
|
40
40
|
def line_range(node)
|
41
|
-
|
42
|
-
|
43
|
-
node.source_range.begin_pos - node.loc.column,
|
44
|
-
node.source_range.end_pos
|
45
|
-
)
|
41
|
+
range_between(node.source_range.begin_pos - node.loc.column,
|
42
|
+
node.source_range.end_pos)
|
46
43
|
end
|
47
44
|
|
48
45
|
def previous_line_ignoring_comments(processed_source, send_line)
|
@@ -35,13 +35,10 @@ module RuboCop
|
|
35
35
|
|
36
36
|
def autocorrect(range)
|
37
37
|
if @message == MSG_MISSING
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
43
|
-
else
|
44
|
-
raise "#{encoding} does not match #{ENCODING_PATTERN}"
|
38
|
+
raise encoding_mismatch_message unless matching_encoding?
|
39
|
+
|
40
|
+
lambda do |corrector|
|
41
|
+
corrector.insert_before(range, "#{encoding}\n")
|
45
42
|
end
|
46
43
|
else
|
47
44
|
# Need to remove unnecessary encoding comment
|
@@ -53,21 +50,38 @@ module RuboCop
|
|
53
50
|
|
54
51
|
private
|
55
52
|
|
53
|
+
def encoding
|
54
|
+
cop_config[AUTO_CORRECT_ENCODING_COMMENT]
|
55
|
+
end
|
56
|
+
|
57
|
+
def matching_encoding?
|
58
|
+
encoding =~ ENCODING_PATTERN
|
59
|
+
end
|
60
|
+
|
61
|
+
def encoding_mismatch_message
|
62
|
+
"#{encoding} does not match #{ENCODING_PATTERN}"
|
63
|
+
end
|
64
|
+
|
56
65
|
def offense(processed_source, line_number)
|
57
66
|
line = processed_source[line_number]
|
58
|
-
encoding_present = line =~ ENCODING_PATTERN
|
59
|
-
ascii_only = processed_source.buffer.source.ascii_only?
|
60
|
-
always_encode = style == :always
|
61
|
-
never_encode = style == :never
|
62
|
-
encoding_omitable = never_encode || (!always_encode && ascii_only)
|
63
67
|
|
64
|
-
if !encoding_present && !encoding_omitable
|
68
|
+
if !encoding_present?(line) && !encoding_omitable?
|
65
69
|
MSG_MISSING
|
66
|
-
elsif encoding_present && encoding_omitable
|
70
|
+
elsif encoding_present?(line) && encoding_omitable?
|
67
71
|
MSG_UNNECESSARY
|
68
72
|
end
|
69
73
|
end
|
70
74
|
|
75
|
+
def encoding_present?(line)
|
76
|
+
line =~ ENCODING_PATTERN
|
77
|
+
end
|
78
|
+
|
79
|
+
def encoding_omitable?
|
80
|
+
return true if style == :never
|
81
|
+
|
82
|
+
style != :always && processed_source.buffer.source.ascii_only?
|
83
|
+
end
|
84
|
+
|
71
85
|
def encoding_line_number(processed_source)
|
72
86
|
line_number = 0
|
73
87
|
line_number += 1 if processed_source[line_number].start_with?(SHEBANG)
|