rubocop 0.19.1 → 0.20.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/.travis.yml +4 -0
- data/CHANGELOG.md +60 -1
- data/CONTRIBUTING.md +2 -1
- data/README.md +9 -7
- data/config/default.yml +3 -3
- data/config/disabled.yml +4 -0
- data/config/enabled.yml +45 -21
- data/lib/rubocop.rb +30 -9
- data/lib/rubocop/cli.rb +1 -1
- data/lib/rubocop/comment_config.rb +4 -2
- data/lib/rubocop/config.rb +16 -22
- data/lib/rubocop/config_loader.rb +29 -26
- data/lib/rubocop/cop/commissioner.rb +1 -1
- data/lib/rubocop/cop/cop.rb +6 -6
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/condition_position.rb +1 -1
- data/lib/rubocop/cop/lint/debugger.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +9 -7
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +22 -0
- data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/eval.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +9 -0
- data/lib/rubocop/cop/lint/loop.rb +2 -2
- data/lib/rubocop/cop/lint/require_parentheses.rb +0 -4
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/space_before_first_arg.rb +36 -0
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +14 -2
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -0
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +3 -3
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/offense.rb +3 -21
- data/lib/rubocop/cop/rails/action_filter.rb +1 -1
- data/lib/rubocop/cop/rails/default_scope.rb +1 -1
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
- data/lib/rubocop/cop/rails/read_write_attribute.rb +43 -0
- data/lib/rubocop/cop/rails/scope_args.rb +1 -1
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/severity.rb +76 -0
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +1 -1
- data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
- data/lib/rubocop/cop/style/alias.rb +1 -1
- data/lib/rubocop/cop/style/align_hash.rb +1 -1
- data/lib/rubocop/cop/style/and_or.rb +1 -1
- data/lib/rubocop/cop/style/{favor_join.rb → array_join.rb} +2 -2
- data/lib/rubocop/cop/style/begin_block.rb +1 -1
- data/lib/rubocop/cop/style/block_nesting.rb +12 -8
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +1 -1
- data/lib/rubocop/cop/style/case_equality.rb +1 -1
- data/lib/rubocop/cop/style/case_indentation.rb +5 -5
- data/lib/rubocop/cop/style/class_methods.rb +19 -3
- data/lib/rubocop/cop/style/class_vars.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +17 -7
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
- data/lib/rubocop/cop/style/{def_parentheses.rb → def_with_parentheses.rb} +0 -0
- data/lib/rubocop/cop/style/{hash_methods.rb → deprecated_hash_methods.rb} +5 -4
- data/lib/rubocop/cop/style/double_negation.rb +1 -1
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
- data/lib/rubocop/cop/style/end_block.rb +1 -1
- data/lib/rubocop/cop/style/even_odd.rb +2 -2
- data/lib/rubocop/cop/style/file_name.rb +1 -1
- data/lib/rubocop/cop/style/for.rb +2 -2
- data/lib/rubocop/cop/style/format_string.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +69 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +6 -10
- data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
- data/lib/rubocop/cop/style/lambda.rb +2 -2
- data/lib/rubocop/cop/style/line_end_concatenation.rb +16 -9
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +2 -2
- data/lib/rubocop/cop/style/negated_if.rb +37 -0
- data/lib/rubocop/cop/style/negated_while.rb +33 -0
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/nil_comparison.rb +14 -10
- data/lib/rubocop/cop/style/non_nil_check.rb +70 -0
- data/lib/rubocop/cop/style/not.rb +25 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/op_method.rb +4 -4
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
- data/lib/rubocop/cop/style/predicate_name.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +3 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/signal_exception.rb +6 -3
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +6 -5
- data/lib/rubocop/cop/style/single_space_before_first_arg.rb +41 -0
- data/lib/rubocop/cop/style/space_around_operators.rb +1 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +8 -8
- data/lib/rubocop/cop/style/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/variable_interpolation.rb +10 -5
- data/lib/rubocop/cop/style/when_then.rb +1 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/team.rb +12 -13
- data/lib/rubocop/cop/util.rb +4 -0
- data/lib/rubocop/cop/variable_inspector/locatable.rb +1 -1
- data/lib/rubocop/cop/variable_inspector/variable_table.rb +1 -1
- data/lib/rubocop/file_inspector.rb +46 -10
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/disabled_lines_formatter.rb +56 -0
- data/lib/rubocop/formatter/emacs_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +2 -4
- data/lib/rubocop/formatter/json_formatter.rb +3 -7
- data/lib/rubocop/formatter/progress_formatter.rb +1 -3
- data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
- data/lib/rubocop/options.rb +29 -10
- data/lib/rubocop/path_util.rb +2 -1
- data/lib/rubocop/processed_source.rb +8 -0
- data/lib/rubocop/target_finder.rb +33 -12
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.20.0.md +69 -0
- data/rubocop-todo.yml +2 -2
- data/spec/rubocop/cli_spec.rb +269 -94
- data/spec/rubocop/config_loader_spec.rb +14 -14
- data/spec/rubocop/config_spec.rb +8 -8
- data/spec/rubocop/cop/lint/deprecated_class_methods_spec.rb +11 -6
- data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +18 -0
- data/spec/rubocop/cop/lint/eval_spec.rb +2 -4
- data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +10 -0
- data/spec/rubocop/cop/lint/rescue_exception_spec.rb +0 -8
- data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
- data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +48 -0
- data/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb +10 -0
- data/spec/rubocop/cop/lint/useless_access_modifier_spec.rb +154 -0
- data/spec/rubocop/cop/offense_spec.rb +1 -1
- data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +19 -0
- data/spec/rubocop/cop/severity_spec.rb +113 -0
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +10 -10
- data/spec/rubocop/cop/style/alias_spec.rb +2 -2
- data/spec/rubocop/cop/style/and_or_spec.rb +2 -2
- data/spec/rubocop/cop/style/{favor_join_spec.rb → array_join_spec.rb} +1 -3
- data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -4
- data/spec/rubocop/cop/style/case_equality_spec.rb +1 -0
- data/spec/rubocop/cop/style/case_indentation_spec.rb +12 -9
- data/spec/rubocop/cop/style/class_methods_spec.rb +23 -0
- data/spec/rubocop/cop/style/collection_methods_spec.rb +2 -2
- data/spec/rubocop/cop/style/{hash_methods_spec.rb → deprecated_hash_methods_spec.rb} +3 -3
- data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/style/even_odd_spec.rb +8 -8
- data/spec/rubocop/cop/style/file_name_spec.rb +55 -42
- data/spec/rubocop/cop/style/for_spec.rb +4 -4
- data/spec/rubocop/cop/style/format_string_spec.rb +10 -10
- data/spec/rubocop/cop/style/guard_clause_spec.rb +77 -0
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +4 -2
- data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +4 -4
- data/spec/rubocop/cop/style/lambda_spec.rb +2 -2
- data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +21 -0
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +1 -1
- data/spec/rubocop/cop/style/{favor_unless_over_negated_if_spec.rb → negated_if_spec.rb} +8 -3
- data/spec/rubocop/cop/style/{favor_until_over_negated_while_spec.rb → negated_while_spec.rb} +8 -3
- data/spec/rubocop/cop/style/nil_comparison_spec.rb +7 -13
- data/spec/rubocop/cop/style/non_nil_check_spec.rb +35 -0
- data/spec/rubocop/cop/style/not_spec.rb +11 -0
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +0 -2
- data/spec/rubocop/cop/style/op_method_spec.rb +10 -2
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +2 -2
- data/spec/rubocop/cop/style/predicate_name_spec.rb +2 -1
- data/spec/rubocop/cop/style/raise_args_spec.rb +5 -0
- data/spec/rubocop/cop/style/rescue_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/style/self_assignment_spec.rb +4 -4
- data/spec/rubocop/cop/style/signal_exception_spec.rb +24 -0
- data/spec/rubocop/cop/style/single_line_block_params_spec.rb +2 -0
- data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +63 -0
- data/spec/rubocop/cop/style/special_global_vars_spec.rb +6 -5
- data/spec/rubocop/cop/style/trivial_accessors_spec.rb +6 -3
- data/spec/rubocop/cop/style/unless_else_spec.rb +2 -4
- data/spec/rubocop/cop/style/variable_interpolation_spec.rb +15 -6
- data/spec/rubocop/cop/style/when_then_spec.rb +3 -4
- data/spec/rubocop/cop/team_spec.rb +4 -18
- data/spec/rubocop/file_inspector_spec.rb +4 -0
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +1 -1
- data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +69 -0
- data/spec/rubocop/options_spec.rb +5 -0
- data/spec/rubocop/target_finder_spec.rb +42 -11
- data/spec/support/shared_context.rb +1 -1
- data/spec/support/statement_modifier_helper.rb +1 -1
- metadata +75 -50
- data/lib/rubocop/cop/rails/read_attribute.rb +0 -28
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -24
- data/lib/rubocop/cop/style/favor_until_over_negated_while.rb +0 -20
- data/spec/rubocop/cop/rails/read_attribute_spec.rb +0 -13
@@ -12,6 +12,9 @@ module Rubocop
|
|
12
12
|
# some_str = 'ala' +
|
13
13
|
# 'bala'
|
14
14
|
#
|
15
|
+
# some_str = 'ala' <<
|
16
|
+
# 'bala'
|
17
|
+
#
|
15
18
|
# # good
|
16
19
|
# some_str = 'ala' \
|
17
20
|
# 'bala'
|
@@ -36,22 +39,26 @@ module Rubocop
|
|
36
39
|
receiver, method, arg = *node
|
37
40
|
|
38
41
|
# TODO: Report Emacs bug.
|
39
|
-
return false unless
|
42
|
+
return false unless [:+, :<<].include?(method)
|
40
43
|
|
41
|
-
return false unless string_type?(receiver
|
44
|
+
return false unless string_type?(receiver)
|
42
45
|
|
43
|
-
return false unless string_type?(arg
|
46
|
+
return false unless string_type?(arg)
|
44
47
|
|
45
|
-
|
48
|
+
expression = node.loc.expression.source
|
49
|
+
concatenator_at_line_end?(expression)
|
46
50
|
end
|
47
51
|
|
48
|
-
def
|
49
|
-
# check if the first line of the expression ends with a +
|
50
|
-
expression =~
|
52
|
+
def concatenator_at_line_end?(expression)
|
53
|
+
# check if the first line of the expression ends with a + or a <<
|
54
|
+
expression =~ /.+(\+|<<)\s*$/
|
51
55
|
end
|
52
56
|
|
53
|
-
def string_type?(
|
54
|
-
[:str, :dstr].include?(
|
57
|
+
def string_type?(node)
|
58
|
+
return false unless [:str, :dstr].include?(node.type)
|
59
|
+
|
60
|
+
# we care only about quotes-delimited literals
|
61
|
+
node.loc.begin && ["'", '"'].include?(node.loc.begin.source)
|
55
62
|
end
|
56
63
|
end
|
57
64
|
end
|
@@ -5,8 +5,8 @@ module Rubocop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for multi-line ternary op expressions.
|
7
7
|
class MultilineTernaryOperator < Cop
|
8
|
-
MSG =
|
9
|
-
|
8
|
+
MSG = 'Avoid multi-line ?: (the ternary operator);' \
|
9
|
+
' use `if`/`unless` instead.'
|
10
10
|
|
11
11
|
def on_if(node)
|
12
12
|
loc = node.loc
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for uses of if with a negated condition. Only ifs
|
7
|
+
# without else are considered.
|
8
|
+
class NegatedIf < Cop
|
9
|
+
include NegativeConditional
|
10
|
+
|
11
|
+
def on_if(node)
|
12
|
+
return unless node.loc.respond_to?(:keyword)
|
13
|
+
return if node.loc.keyword.is?('elsif')
|
14
|
+
|
15
|
+
check(node)
|
16
|
+
end
|
17
|
+
|
18
|
+
def error_message
|
19
|
+
'Favor `unless` over `if` for negative conditions.'
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def autocorrect(node)
|
25
|
+
@corrections << lambda do |corrector|
|
26
|
+
condition, _body, _rest = *node
|
27
|
+
# unwrap the negated portion of the condition (a send node)
|
28
|
+
pos_condition, _method, = *condition
|
29
|
+
corrector.replace(node.loc.keyword, 'unless')
|
30
|
+
corrector.replace(condition.loc.expression,
|
31
|
+
pos_condition.loc.expression.source)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for uses of while with a negated condition.
|
7
|
+
class NegatedWhile < Cop
|
8
|
+
include NegativeConditional
|
9
|
+
|
10
|
+
def on_while(node)
|
11
|
+
check(node)
|
12
|
+
end
|
13
|
+
|
14
|
+
def error_message
|
15
|
+
'Favor `until` over `while` for negative conditions.'
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def autocorrect(node)
|
21
|
+
@corrections << lambda do |corrector|
|
22
|
+
condition, _body, _rest = *node
|
23
|
+
# unwrap the negated portion of the condition (a send node)
|
24
|
+
pos_condition, _method, = *condition
|
25
|
+
corrector.replace(node.loc.keyword, 'until')
|
26
|
+
corrector.replace(condition.loc.expression,
|
27
|
+
pos_condition.loc.expression.source)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -5,7 +5,7 @@ module Rubocop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for nested ternary op expressions.
|
7
7
|
class NestedTernaryOperator < Cop
|
8
|
-
MSG = 'Ternary operators must not be nested. Prefer if
|
8
|
+
MSG = 'Ternary operators must not be nested. Prefer `if`/`else` ' \
|
9
9
|
'constructs instead.'
|
10
10
|
|
11
11
|
def on_if(node)
|
@@ -9,29 +9,33 @@ module Rubocop
|
|
9
9
|
#
|
10
10
|
# # bad
|
11
11
|
# if x == nil
|
12
|
-
# if x != nil
|
13
12
|
#
|
14
13
|
# # good
|
15
14
|
# if x.nil?
|
16
|
-
# if !x.nil?
|
17
15
|
class NilComparison < Cop
|
18
|
-
MSG = 'Prefer the use of the nil
|
16
|
+
MSG = 'Prefer the use of the `nil?` predicate.'
|
19
17
|
|
20
|
-
OPS =
|
18
|
+
OPS = [:==, :===]
|
21
19
|
|
22
20
|
NIL_NODE = s(:nil)
|
23
21
|
|
24
22
|
def on_send(node)
|
25
|
-
|
26
|
-
return unless node.loc.selector
|
27
|
-
op = node.loc.selector.source
|
28
|
-
|
29
|
-
if OPS.include?(op)
|
30
|
-
_receiver, _method, args = *node
|
23
|
+
_receiver, method, args = *node
|
31
24
|
|
25
|
+
if OPS.include?(method)
|
32
26
|
add_offense(node, :selector) if args == NIL_NODE
|
33
27
|
end
|
34
28
|
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def autocorrect(node)
|
33
|
+
@corrections << lambda do |corrector|
|
34
|
+
expr = node.loc.expression
|
35
|
+
new_code = expr.source.sub(/\s*==\s*nil/, '.nil?')
|
36
|
+
corrector.replace(expr, new_code)
|
37
|
+
end
|
38
|
+
end
|
35
39
|
end
|
36
40
|
end
|
37
41
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for non-nil checks, which are usually redundant.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# if x != nil
|
12
|
+
# if !x.nil?
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# if x
|
16
|
+
class NonNilCheck < Cop
|
17
|
+
MSG = 'Explicit non-nil checks are usually redundant.'
|
18
|
+
|
19
|
+
NIL_NODE = s(:nil)
|
20
|
+
|
21
|
+
def on_send(node)
|
22
|
+
receiver, method, args = *node
|
23
|
+
|
24
|
+
return unless [:!=, :!].include?(method)
|
25
|
+
|
26
|
+
if method == :!=
|
27
|
+
add_offense(node, :selector) if args == NIL_NODE
|
28
|
+
elsif method == :!
|
29
|
+
add_offense(node, :expression) if nil_check?(receiver)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def nil_check?(node)
|
36
|
+
return false unless node && node.type == :send
|
37
|
+
|
38
|
+
_receiver, method, *_args = *node
|
39
|
+
method == :nil?
|
40
|
+
end
|
41
|
+
|
42
|
+
def autocorrect(node)
|
43
|
+
receiver, method, _args = *node
|
44
|
+
|
45
|
+
if method == :!=
|
46
|
+
autocorrect_comparison(node)
|
47
|
+
elsif method == :!
|
48
|
+
autocorrect_non_nil(node, receiver)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def autocorrect_comparison(node)
|
53
|
+
@corrections << lambda do |corrector|
|
54
|
+
expr = node.loc.expression
|
55
|
+
new_code = expr.source.sub(/\s*!=\s*nil/, '')
|
56
|
+
corrector.replace(expr, new_code)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def autocorrect_non_nil(node, inner_node)
|
61
|
+
@corrections << lambda do |corrector|
|
62
|
+
receiver, _method, _args = *inner_node
|
63
|
+
corrector.replace(node.loc.expression,
|
64
|
+
receiver.loc.expression.source)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -5,7 +5,7 @@ module Rubocop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for uses if the keyword *not* instead of !.
|
7
7
|
class Not < Cop
|
8
|
-
MSG = 'Use
|
8
|
+
MSG = 'Use `!` instead of `not`.'
|
9
9
|
|
10
10
|
def on_send(node)
|
11
11
|
_receiver, method_name, *args = *node
|
@@ -16,6 +16,30 @@ module Rubocop
|
|
16
16
|
add_offense(node, :selector)
|
17
17
|
end
|
18
18
|
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def autocorrect(node)
|
23
|
+
c = correction(node)
|
24
|
+
new_source = rewrite_node(node, c)
|
25
|
+
|
26
|
+
# Make the correction only if it doesn't change the AST.
|
27
|
+
@corrections << c if node == SourceParser.parse(new_source).ast
|
28
|
+
end
|
29
|
+
|
30
|
+
def rewrite_node(node, correction)
|
31
|
+
processed_source = SourceParser.parse(node.loc.expression.source)
|
32
|
+
c = correction(processed_source.ast)
|
33
|
+
Corrector.new(processed_source.buffer, [c]).rewrite
|
34
|
+
end
|
35
|
+
|
36
|
+
def correction(node)
|
37
|
+
lambda do |corrector|
|
38
|
+
old_source = node.loc.expression.source
|
39
|
+
new_source = old_source.sub(/not\s+/, '!')
|
40
|
+
corrector.replace(node.loc.expression, new_source)
|
41
|
+
end
|
42
|
+
end
|
19
43
|
end
|
20
44
|
end
|
21
45
|
end
|
@@ -4,19 +4,19 @@ module Rubocop
|
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
6
|
# This cop makes sure that certain operator methods have their sole
|
7
|
-
# parameter named
|
7
|
+
# parameter named `other`.
|
8
8
|
class OpMethod < Cop
|
9
|
-
MSG = 'When defining the
|
9
|
+
MSG = 'When defining the `%s` operator, name its argument `other`.'
|
10
10
|
|
11
11
|
BLACKLISTED = [:+@, :-@, :[], :[]=, :<<]
|
12
12
|
|
13
|
-
TARGET_ARGS = s(:args, s(:arg, :other))
|
13
|
+
TARGET_ARGS = [s(:args, s(:arg, :other)), s(:args, s(:arg, :_other))]
|
14
14
|
|
15
15
|
def on_def(node)
|
16
16
|
name, args, _body = *node
|
17
17
|
|
18
18
|
if name !~ /\A\w/ && !BLACKLISTED.include?(name) &&
|
19
|
-
args.children.size == 1 && args
|
19
|
+
args.children.size == 1 && !TARGET_ARGS.include?(args)
|
20
20
|
add_offense(args.children[0], :expression,
|
21
21
|
format(MSG, name))
|
22
22
|
end
|
@@ -47,7 +47,7 @@ module Rubocop
|
|
47
47
|
def message(node)
|
48
48
|
kw = node.loc.keyword.source
|
49
49
|
article = kw == 'while' ? 'a' : 'an'
|
50
|
-
"Don't use parentheses around the condition of #{article}
|
50
|
+
"Don't use parentheses around the condition of #{article} `#{kw}`."
|
51
51
|
end
|
52
52
|
|
53
53
|
def autocorrect(node)
|
@@ -34,7 +34,7 @@ module Rubocop
|
|
34
34
|
def message(method_name, prefix)
|
35
35
|
new_name = method_name.sub(prefix, '')
|
36
36
|
new_name << '?' unless method_name.end_with?('?')
|
37
|
-
"Rename
|
37
|
+
"Rename `#{method_name}` to `#{new_name}`."
|
38
38
|
end
|
39
39
|
|
40
40
|
def prefix_blacklist
|
@@ -56,9 +56,10 @@ module Rubocop
|
|
56
56
|
def message(method)
|
57
57
|
case style
|
58
58
|
when :compact
|
59
|
-
"Provide an exception object as an argument to
|
59
|
+
"Provide an exception object as an argument to `#{method}`."
|
60
60
|
when :exploded
|
61
|
-
"Provide an exception class and message as
|
61
|
+
"Provide an exception class and message as " \
|
62
|
+
"arguments to `#{method}`."
|
62
63
|
end
|
63
64
|
end
|
64
65
|
end
|
@@ -51,7 +51,7 @@ module Rubocop
|
|
51
51
|
if receiver == target_node
|
52
52
|
add_offense(node,
|
53
53
|
:expression,
|
54
|
-
"Use self-assignment shorthand
|
54
|
+
"Use self-assignment shorthand `#{method_name}=`.")
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -64,7 +64,7 @@ module Rubocop
|
|
64
64
|
operator = rhs.loc.operator.source
|
65
65
|
add_offense(node,
|
66
66
|
:expression,
|
67
|
-
"Use self-assignment shorthand
|
67
|
+
"Use self-assignment shorthand `#{operator}=`.")
|
68
68
|
end
|
69
69
|
end
|
70
70
|
end
|
@@ -12,11 +12,14 @@ module Rubocop
|
|
12
12
|
|
13
13
|
def on_rescue(node)
|
14
14
|
if style == :semantic
|
15
|
-
begin_node,
|
15
|
+
begin_node, *rescue_nodes, _else_node = *node
|
16
16
|
|
17
17
|
check_for(:raise, begin_node)
|
18
|
-
|
19
|
-
|
18
|
+
|
19
|
+
rescue_nodes.each do |rescue_node|
|
20
|
+
check_for(:fail, rescue_node)
|
21
|
+
allow(:raise, rescue_node)
|
22
|
+
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
@@ -49,11 +49,12 @@ module Rubocop
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def break_line_before(range, node, corrector, indent_steps)
|
52
|
-
corrector.insert_before(
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
corrector.insert_before(
|
53
|
+
range,
|
54
|
+
"\n" + ' ' * (node.loc.keyword.column +
|
55
|
+
indent_steps *
|
56
|
+
IndentationWidth::CORRECT_INDENTATION)
|
57
|
+
)
|
57
58
|
end
|
58
59
|
|
59
60
|
def move_comment(eol_comment, node, corrector)
|