rubocop 0.15.0 → 0.16.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/.rubocop.yml +1 -10
- data/.travis.yml +2 -1
- data/CHANGELOG.md +55 -0
- data/Gemfile +3 -0
- data/README.md +37 -0
- data/config/default.yml +99 -16
- data/config/enabled.yml +28 -16
- data/lib/rubocop.rb +16 -2
- data/lib/rubocop/cli.rb +10 -91
- data/lib/rubocop/config.rb +4 -1
- data/lib/rubocop/config_loader.rb +18 -10
- data/lib/rubocop/config_store.rb +3 -2
- data/lib/rubocop/cop/check_assignment.rb +43 -0
- data/lib/rubocop/cop/check_methods.rb +18 -0
- data/lib/rubocop/cop/commissioner.rb +15 -3
- data/lib/rubocop/cop/cop.rb +51 -43
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/block_alignment.rb +7 -7
- data/lib/rubocop/cop/lint/debugger.rb +1 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +56 -19
- data/lib/rubocop/cop/lint/end_in_method.rb +4 -10
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/eval.rb +1 -1
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
- data/lib/rubocop/cop/lint/loop.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
- 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/syntax.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -16
- data/lib/rubocop/cop/lint/void.rb +5 -5
- data/lib/rubocop/cop/rails/default_scope.rb +33 -0
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -4
- data/lib/rubocop/cop/rails/output.rb +1 -1
- data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +31 -16
- data/lib/rubocop/cop/style/accessor_method_name.rb +53 -0
- data/lib/rubocop/cop/style/alias.rb +14 -2
- data/lib/rubocop/cop/style/align_hash.rb +15 -16
- data/lib/rubocop/cop/style/and_or.rb +3 -3
- data/lib/rubocop/cop/style/array_syntax.rb +22 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -3
- data/lib/rubocop/cop/style/attr.rb +1 -1
- data/lib/rubocop/cop/style/autocorrect_alignment.rb +13 -12
- data/lib/rubocop/cop/style/begin_block.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_nesting.rb +5 -1
- data/lib/rubocop/cop/style/blocks.rb +4 -4
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +48 -22
- data/lib/rubocop/cop/style/case_equality.rb +1 -1
- data/lib/rubocop/cop/style/case_indentation.rb +25 -6
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/style/class_length.rb +7 -19
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/class_vars.rb +1 -1
- data/lib/rubocop/cop/style/code_length.rb +35 -0
- data/lib/rubocop/cop/style/collection_methods.rb +1 -1
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
- data/lib/rubocop/cop/style/configurable_enforced_style.rb +51 -0
- data/lib/rubocop/cop/style/configurable_max.rb +17 -0
- data/lib/rubocop/cop/style/configurable_naming.rb +4 -10
- data/lib/rubocop/cop/style/constant_name.rb +5 -3
- data/lib/rubocop/cop/style/cyclomatic_complexity.rb +8 -13
- data/lib/rubocop/cop/style/def_parentheses.rb +4 -43
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/dot_position.rb +6 -2
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
- data/lib/rubocop/cop/style/empty_lines.rb +5 -1
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +28 -13
- data/lib/rubocop/cop/style/empty_literal.rb +3 -3
- data/lib/rubocop/cop/style/encoding.rb +5 -5
- data/lib/rubocop/cop/style/end_block.rb +1 -1
- data/lib/rubocop/cop/style/end_of_line.rb +8 -6
- data/lib/rubocop/cop/style/even_odd.rb +6 -6
- data/lib/rubocop/cop/style/favor_join.rb +1 -1
- data/lib/rubocop/cop/style/favor_modifier.rb +20 -34
- data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
- data/lib/rubocop/cop/style/final_newline.rb +1 -1
- data/lib/rubocop/cop/style/flip_flop.rb +20 -0
- data/lib/rubocop/cop/style/for.rb +34 -3
- data/lib/rubocop/cop/style/global_vars.rb +1 -1
- data/lib/rubocop/cop/style/hash_methods.rb +3 -3
- data/lib/rubocop/cop/style/hash_syntax.rb +8 -11
- data/lib/rubocop/cop/style/if_node.rb +25 -0
- data/lib/rubocop/cop/style/if_then_else.rb +1 -1
- data/lib/rubocop/cop/style/indentation_width.rb +77 -43
- data/lib/rubocop/cop/style/lambda.rb +2 -2
- data/lib/rubocop/cop/style/lambda_call.rb +4 -12
- data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/style/line_length.rb +9 -5
- data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +52 -0
- data/lib/rubocop/cop/style/method_length.rb +9 -22
- data/lib/rubocop/cop/style/module_function.rb +1 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +11 -1
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +2 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/predicate_name.rb +54 -0
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +4 -10
- data/lib/rubocop/cop/style/redundant_begin.rb +5 -15
- data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +9 -19
- data/lib/rubocop/cop/style/redundant_self.rb +8 -3
- data/lib/rubocop/cop/style/regexp_literal.rb +4 -3
- data/lib/rubocop/cop/style/rescue_modifier.rb +9 -17
- data/lib/rubocop/cop/style/semicolon.rb +20 -22
- data/lib/rubocop/cop/style/signal_exception.rb +10 -17
- data/lib/rubocop/cop/style/single_line_block_params.rb +62 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +5 -15
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -3
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
- data/lib/rubocop/cop/style/space_after_method_name.rb +4 -12
- data/lib/rubocop/cop/style/space_after_not.rb +1 -1
- data/lib/rubocop/cop/style/space_around_block_braces.rb +105 -94
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -1
- data/lib/rubocop/cop/style/space_around_operators.rb +38 -123
- data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +1 -1
- data/lib/rubocop/cop/style/space_inside.rb +1 -3
- data/lib/rubocop/cop/style/space_inside_brackets.rb +0 -2
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +5 -5
- data/lib/rubocop/cop/style/space_inside_parens.rb +0 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
- data/lib/rubocop/cop/style/string_help.rb +1 -1
- data/lib/rubocop/cop/style/string_literals.rb +4 -11
- data/lib/rubocop/cop/style/surrounding_space.rb +0 -2
- data/lib/rubocop/cop/style/symbol_array.rb +3 -10
- data/lib/rubocop/cop/style/tab.rb +5 -5
- data/lib/rubocop/cop/style/ternary_operator.rb +2 -2
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +1 -1
- data/lib/rubocop/cop/style/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +4 -14
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/when_then.rb +3 -1
- data/lib/rubocop/cop/style/while_until_do.rb +2 -2
- data/lib/rubocop/cop/style/word_array.rb +4 -10
- data/lib/rubocop/cop/util.rb +85 -12
- data/lib/rubocop/cop/variable_inspector.rb +4 -2
- data/lib/rubocop/file_inspector.rb +98 -0
- data/lib/rubocop/formatter/base_formatter.rb +2 -2
- data/lib/rubocop/formatter/disabled_config_formatter.rb +10 -2
- data/lib/rubocop/rake_task.rb +5 -1
- data/lib/rubocop/target_finder.rb +3 -3
- data/lib/rubocop/version.rb +1 -1
- data/rubocop-todo.yml +12 -0
- data/rubocop.gemspec +1 -1
- data/spec/isolated_environment_spec.rb +24 -0
- data/spec/rubocop/cli_spec.rb +120 -27
- data/spec/rubocop/config_loader_spec.rb +25 -11
- data/spec/rubocop/config_spec.rb +25 -9
- data/spec/rubocop/config_store_spec.rb +1 -1
- data/spec/rubocop/cop/cop_spec.rb +22 -3
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +79 -47
- data/spec/rubocop/cop/lint/syntax_spec.rb +4 -3
- data/spec/rubocop/cop/rails/default_scope_spec.rb +31 -0
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +37 -2
- data/spec/rubocop/cop/style/accessor_method_name_spec.rb +81 -0
- data/spec/rubocop/cop/style/alias_spec.rb +10 -0
- data/spec/rubocop/cop/style/align_parameters_spec.rb +3 -3
- data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -1
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +83 -0
- data/spec/rubocop/cop/style/case_indentation_spec.rb +227 -71
- data/spec/rubocop/cop/style/class_length_spec.rb +2 -0
- data/spec/rubocop/cop/style/constant_name_spec.rb +6 -0
- data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +1 -0
- data/spec/rubocop/cop/style/documentation_spec.rb +12 -0
- data/spec/rubocop/cop/style/dot_position_spec.rb +10 -0
- data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +22 -0
- data/spec/rubocop/cop/style/empty_lines_spec.rb +6 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +17 -0
- data/spec/rubocop/cop/style/favor_modifier_spec.rb +15 -0
- data/spec/rubocop/cop/style/flip_flop_spec.rb +23 -0
- data/spec/rubocop/cop/style/for_spec.rb +96 -18
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +8 -2
- data/spec/rubocop/cop/style/indentation_width_spec.rb +185 -20
- data/spec/rubocop/cop/style/line_length_spec.rb +1 -0
- data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +72 -0
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +16 -0
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +34 -3
- data/spec/rubocop/cop/style/predicate_name_spec.rb +25 -0
- data/spec/rubocop/cop/style/redundant_self_spec.rb +15 -0
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +5 -0
- data/spec/rubocop/cop/style/semicolon_spec.rb +16 -16
- data/spec/rubocop/cop/style/{reduce_arguments_spec.rb → single_line_block_params_spec.rb} +12 -4
- data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +58 -8
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +71 -0
- data/spec/rubocop/cop/style/string_literals_spec.rb +18 -0
- data/spec/rubocop/file_inspector_spec.rb +78 -0
- data/spec/rubocop/formatter/clang_style_formatter_spec.rb +7 -7
- data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +2 -2
- data/spec/rubocop/processed_source_spec.rb +7 -3
- data/spec/rubocop/token_spec.rb +1 -1
- data/spec/spec_helper.rb +3 -2
- data/spec/support/isolated_environment.rb +7 -0
- data/spec/support/shared_context.rb +5 -1
- metadata +35 -12
- data/lib/rubocop/cop/style/reduce_arguments.rb +0 -34
- data/lib/rubocop/cop/style/symbol_name.rb +0 -45
- data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +0 -33
- data/spec/rubocop/cop/style/symbol_name_spec.rb +0 -138
@@ -6,7 +6,7 @@ module Rubocop
|
|
6
6
|
# This cop checks for big numeric literals without _ between groups
|
7
7
|
# of digits in them.
|
8
8
|
class NumericLiterals < Cop
|
9
|
-
MSG = 'Separate every 3 digits in the integer portion of a number' \
|
9
|
+
MSG = 'Separate every 3 digits in the integer portion of a number ' \
|
10
10
|
'with underscores(_).'
|
11
11
|
|
12
12
|
def on_int(node)
|
@@ -27,7 +27,7 @@ module Rubocop
|
|
27
27
|
|
28
28
|
if int.size >= min_digits
|
29
29
|
if int =~ /\d{4}/ || int =~ /_\d{1,2}_/
|
30
|
-
|
30
|
+
add_offence(node, :expression)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
@@ -17,8 +17,8 @@ module Rubocop
|
|
17
17
|
|
18
18
|
if name !~ /\A\w/ && !BLACKLISTED.include?(name) &&
|
19
19
|
args.children.size == 1 && args != TARGET_ARGS
|
20
|
-
|
21
|
-
|
20
|
+
add_offence(args.children[0], :expression,
|
21
|
+
sprintf(MSG, name))
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -31,7 +31,7 @@ module Rubocop
|
|
31
31
|
# allow safe assignment
|
32
32
|
return if safe_assignment?(cond) && safe_assignment_allowed?
|
33
33
|
|
34
|
-
|
34
|
+
add_offence(cond, :expression)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -42,6 +42,13 @@ module Rubocop
|
|
42
42
|
def safe_assignment_allowed?
|
43
43
|
cop_config['AllowSafeAssignment']
|
44
44
|
end
|
45
|
+
|
46
|
+
def autocorrect(node)
|
47
|
+
@corrections << lambda do |corrector|
|
48
|
+
corrector.remove(node.loc.begin)
|
49
|
+
corrector.remove(node.loc.end)
|
50
|
+
end
|
51
|
+
end
|
45
52
|
end
|
46
53
|
end
|
47
54
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop makes sure that predicates are named properly.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# def is_even?(value) ...
|
11
|
+
#
|
12
|
+
# # good
|
13
|
+
# def even?(value)
|
14
|
+
#
|
15
|
+
# # bad
|
16
|
+
# def has_value? ...
|
17
|
+
#
|
18
|
+
# # good
|
19
|
+
# def value? ...
|
20
|
+
class PredicateName < Cop
|
21
|
+
def on_def(node)
|
22
|
+
method_name, args, _body = *node
|
23
|
+
check(node, method_name.to_s, args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def on_defs(node)
|
27
|
+
_scope, method_name, args, _body = *node
|
28
|
+
check(node, method_name.to_s, args)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def check(node, method_name, args)
|
34
|
+
prefix_blacklist.each do |prefix|
|
35
|
+
if method_name.start_with?(prefix)
|
36
|
+
add_offence(node, :name,
|
37
|
+
message(method_name, prefix))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def message(method_name, prefix)
|
43
|
+
new_name = method_name.sub(prefix, '')
|
44
|
+
new_name << '?' unless method_name.end_with?('?')
|
45
|
+
"Rename #{method_name} to #{new_name}."
|
46
|
+
end
|
47
|
+
|
48
|
+
def prefix_blacklist
|
49
|
+
cop_config['NamePrefixBlacklist']
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -5,6 +5,8 @@ module Rubocop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks the args passed to `fail` and `raise`.
|
7
7
|
class RaiseArgs < Cop
|
8
|
+
include ConfigurableEnforcedStyle
|
9
|
+
|
8
10
|
def on_send(node)
|
9
11
|
return unless command?(:raise, node) || command?(:fail, node)
|
10
12
|
|
@@ -23,7 +25,7 @@ module Rubocop
|
|
23
25
|
|
24
26
|
return unless args.size > 1
|
25
27
|
|
26
|
-
|
28
|
+
add_offence(node, :expression, message(selector))
|
27
29
|
end
|
28
30
|
|
29
31
|
def check_exploded(node)
|
@@ -38,19 +40,11 @@ module Rubocop
|
|
38
40
|
|
39
41
|
# Allow code like `raise Ex.new(arg1, arg2)`.
|
40
42
|
unless constructor_args.size > 1
|
41
|
-
|
43
|
+
add_offence(node, :expression, message(selector))
|
42
44
|
end
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
46
|
-
def style
|
47
|
-
case cop_config['EnforcedStyle']
|
48
|
-
when 'compact' then :compact
|
49
|
-
when 'exploded' then :exploded
|
50
|
-
else fail 'Unknown style selected!'
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
48
|
def message(method)
|
55
49
|
case style
|
56
50
|
when :compact
|
@@ -18,26 +18,16 @@ module Rubocop
|
|
18
18
|
# end
|
19
19
|
# end
|
20
20
|
class RedundantBegin < Cop
|
21
|
-
|
22
|
-
|
23
|
-
def on_def(node)
|
24
|
-
_method_name, _args, body = *node
|
25
|
-
|
26
|
-
check(body)
|
27
|
-
end
|
21
|
+
include CheckMethods
|
28
22
|
|
29
|
-
|
30
|
-
_scope, _method_name, _args, body = *node
|
31
|
-
|
32
|
-
check(body)
|
33
|
-
end
|
23
|
+
MSG = 'Redundant `begin` block detected.'
|
34
24
|
|
35
25
|
private
|
36
26
|
|
37
|
-
def check(
|
38
|
-
return unless
|
27
|
+
def check(_node, _method_name, _args, body)
|
28
|
+
return unless body && body.type == :kwbegin
|
39
29
|
|
40
|
-
|
30
|
+
add_offence(body, :begin)
|
41
31
|
end
|
42
32
|
end
|
43
33
|
end
|
@@ -21,19 +21,9 @@ module Rubocop
|
|
21
21
|
# It should be extended to handle methods whose body is if/else
|
22
22
|
# or a case expression with a default branch.
|
23
23
|
class RedundantReturn < Cop
|
24
|
-
|
25
|
-
|
26
|
-
def on_def(node)
|
27
|
-
_method_name, _args, body = *node
|
28
|
-
|
29
|
-
check(body)
|
30
|
-
end
|
24
|
+
include CheckMethods
|
31
25
|
|
32
|
-
|
33
|
-
_scope, _method_name, _args, body = *node
|
34
|
-
|
35
|
-
check(body)
|
36
|
-
end
|
26
|
+
MSG = 'Redundant `return` detected.'
|
37
27
|
|
38
28
|
private
|
39
29
|
|
@@ -49,13 +39,13 @@ module Rubocop
|
|
49
39
|
end
|
50
40
|
end
|
51
41
|
|
52
|
-
def check(
|
53
|
-
return unless
|
42
|
+
def check(_node, _method_name, _args, body)
|
43
|
+
return unless body
|
54
44
|
|
55
|
-
if
|
56
|
-
check_return_node(
|
57
|
-
elsif
|
58
|
-
expressions = *
|
45
|
+
if body.type == :return
|
46
|
+
check_return_node(body)
|
47
|
+
elsif body.type == :begin
|
48
|
+
expressions = *body
|
59
49
|
last_expr = expressions.last
|
60
50
|
|
61
51
|
if last_expr && last_expr.type == :return
|
@@ -68,7 +58,7 @@ module Rubocop
|
|
68
58
|
return if cop_config['AllowMultipleReturnValues'] &&
|
69
59
|
node.children.size > 1
|
70
60
|
|
71
|
-
|
61
|
+
add_offence(node, :keyword)
|
72
62
|
end
|
73
63
|
end
|
74
64
|
end
|
@@ -75,8 +75,8 @@ module Rubocop
|
|
75
75
|
@local_variables = []
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
79
|
-
on_argument(
|
78
|
+
def on_args(node)
|
79
|
+
node.children.each { |arg| on_argument(arg) }
|
80
80
|
end
|
81
81
|
|
82
82
|
def on_blockarg(node)
|
@@ -94,9 +94,10 @@ module Rubocop
|
|
94
94
|
receiver, method_name, *_args = *node
|
95
95
|
if receiver && receiver.type == :self
|
96
96
|
unless operator?(method_name) || keyword?(method_name) ||
|
97
|
+
constant_name?(method_name) ||
|
97
98
|
@allowed_send_nodes.include?(node) ||
|
98
99
|
@local_variables.include?(method_name)
|
99
|
-
|
100
|
+
add_offence(node, :expression)
|
100
101
|
end
|
101
102
|
end
|
102
103
|
end
|
@@ -128,6 +129,10 @@ module Rubocop
|
|
128
129
|
:yield].include?(method_name)
|
129
130
|
end
|
130
131
|
|
132
|
+
def constant_name?(method_name)
|
133
|
+
method_name.match(/^[A-Z]/)
|
134
|
+
end
|
135
|
+
|
131
136
|
def allow_self(node)
|
132
137
|
if node.type == :send
|
133
138
|
receiver, _method_name, *_args = *node
|
@@ -8,14 +8,15 @@ module Rubocop
|
|
8
8
|
# value of the configuration parameter MaxSlashes.
|
9
9
|
class RegexpLiteral < Cop
|
10
10
|
def on_regexp(node)
|
11
|
-
|
11
|
+
string_parts = node.children.select { |child| child.type == :str }
|
12
|
+
total_string = string_parts.map { |s| s.loc.expression.source }.join
|
13
|
+
slashes = total_string.count('/')
|
12
14
|
msg = if node.loc.begin.is?('/')
|
13
|
-
slashes -= 2 # subtract delimiters
|
14
15
|
error_message('') if slashes > max_slashes
|
15
16
|
else
|
16
17
|
error_message('only ') if slashes <= max_slashes
|
17
18
|
end
|
18
|
-
|
19
|
+
add_offence(node, :expression, msg) if msg
|
19
20
|
end
|
20
21
|
|
21
22
|
def max_slashes
|
@@ -5,37 +5,29 @@ module Rubocop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for uses of rescue in its modifier form.
|
7
7
|
class RescueModifier < Cop
|
8
|
+
include CheckMethods
|
9
|
+
|
8
10
|
MSG = 'Avoid using rescue in its modifier form.'
|
9
11
|
|
10
12
|
def on_rescue(node)
|
11
13
|
return if ignored_node?(node)
|
12
14
|
|
13
|
-
|
15
|
+
add_offence(node, :expression)
|
14
16
|
end
|
15
17
|
|
16
18
|
def on_kwbegin(node)
|
17
19
|
body, *_ = *node
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
def on_def(node)
|
22
|
-
_method_name, _args, body = *node
|
23
|
-
check_rescue(body)
|
24
|
-
end
|
25
|
-
|
26
|
-
def on_defs(node)
|
27
|
-
_receiver, _method_name, _args, body = *node
|
28
|
-
check_rescue(body)
|
20
|
+
check(nil, nil, nil, body)
|
29
21
|
end
|
30
22
|
|
31
|
-
def
|
32
|
-
return unless
|
23
|
+
def check(_node, _method_name, _args, body)
|
24
|
+
return unless body
|
33
25
|
|
34
|
-
case
|
26
|
+
case body.type
|
35
27
|
when :rescue
|
36
|
-
ignore_node(
|
28
|
+
ignore_node(body)
|
37
29
|
when :ensure
|
38
|
-
first_child =
|
30
|
+
first_child = body.children.first
|
39
31
|
if first_child && first_child.type == :rescue
|
40
32
|
ignore_node(first_child)
|
41
33
|
end
|
@@ -10,20 +10,16 @@ module Rubocop
|
|
10
10
|
|
11
11
|
def investigate(processed_source)
|
12
12
|
return unless processed_source.ast
|
13
|
+
@processed_source = processed_source
|
13
14
|
|
14
|
-
|
15
|
-
check_for_expr_separator(processed_source)
|
16
|
-
end
|
17
|
-
check_for_line_terminator(processed_source)
|
15
|
+
check_for_line_terminator
|
18
16
|
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
def check_for_expr_separator(processed_source)
|
23
|
-
on_node(:begin, processed_source.ast) do |node|
|
18
|
+
def on_begin(node)
|
19
|
+
unless cop_config['AllowAsExpressionSeparator']
|
24
20
|
exprs = node.children
|
25
21
|
|
26
|
-
|
22
|
+
return if exprs.size < 2
|
27
23
|
|
28
24
|
# create a map matching lines to the number of expressions on them
|
29
25
|
exprs_lines = exprs.map { |e| e.loc.expression.line }
|
@@ -35,31 +31,33 @@ module Rubocop
|
|
35
31
|
# TODO: Find the correct position of the semicolon. We don't
|
36
32
|
# know if the first semicolon on the line is a separator of
|
37
33
|
# expressions. It's just a guess.
|
38
|
-
column = processed_source[line - 1].index(';')
|
39
|
-
|
40
|
-
source_range(processed_source.buffer,
|
41
|
-
processed_source[0...(line - 1)],
|
42
|
-
column, 1))
|
34
|
+
column = @processed_source[line - 1].index(';')
|
35
|
+
convention_on(line, column)
|
43
36
|
end
|
44
37
|
end
|
45
38
|
end
|
46
39
|
end
|
47
40
|
|
48
|
-
|
49
|
-
|
41
|
+
private
|
42
|
+
|
43
|
+
def check_for_line_terminator
|
44
|
+
tokens_for_lines = @processed_source.tokens.group_by do |token|
|
50
45
|
token.pos.line
|
51
46
|
end
|
52
47
|
|
53
48
|
tokens_for_lines.each do |line, tokens|
|
54
|
-
if tokens.last.type == :tSEMI
|
55
|
-
|
56
|
-
convention(nil,
|
57
|
-
source_range(processed_source.buffer,
|
58
|
-
processed_source[0...(line - 1)],
|
59
|
-
column, 1))
|
49
|
+
if tokens.last.type == :tSEMI
|
50
|
+
convention_on(line, tokens.last.pos.column)
|
60
51
|
end
|
61
52
|
end
|
62
53
|
end
|
54
|
+
|
55
|
+
def convention_on(line, column)
|
56
|
+
add_offence(nil,
|
57
|
+
source_range(@processed_source.buffer,
|
58
|
+
@processed_source[0...(line - 1)], column,
|
59
|
+
1))
|
60
|
+
end
|
63
61
|
end
|
64
62
|
end
|
65
63
|
end
|