rubocop 0.7.2 → 0.8.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 +7 -1
- data/CHANGELOG.md +19 -0
- data/README.md +4 -8
- data/bin/rubocop +2 -2
- data/config/default.yml +8 -0
- data/config/enabled.yml +21 -24
- data/lib/rubocop.rb +9 -7
- data/lib/rubocop/cli.rb +73 -52
- data/lib/rubocop/config.rb +8 -5
- data/lib/rubocop/cop/access_control.rb +41 -0
- data/lib/rubocop/cop/alias.rb +7 -5
- data/lib/rubocop/cop/align_parameters.rb +20 -96
- data/lib/rubocop/cop/and_or.rb +26 -0
- data/lib/rubocop/cop/ascii_comments.rb +3 -8
- data/lib/rubocop/cop/ascii_identifiers.rb +6 -5
- data/lib/rubocop/cop/avoid_class_vars.rb +5 -10
- data/lib/rubocop/cop/avoid_for.rb +7 -5
- data/lib/rubocop/cop/avoid_global_vars.rb +19 -7
- data/lib/rubocop/cop/avoid_perl_backrefs.rb +7 -10
- data/lib/rubocop/cop/avoid_perlisms.rb +11 -10
- data/lib/rubocop/cop/block_comments.rb +4 -6
- data/lib/rubocop/cop/blocks.rb +11 -47
- data/lib/rubocop/cop/case_indentation.rb +9 -31
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -11
- data/lib/rubocop/cop/class_methods.rb +5 -10
- data/lib/rubocop/cop/collection_methods.rb +16 -16
- data/lib/rubocop/cop/colon_method_call.rb +8 -32
- data/lib/rubocop/cop/constant_name.rb +24 -0
- data/lib/rubocop/cop/cop.rb +20 -78
- data/lib/rubocop/cop/def_parentheses.rb +43 -35
- data/lib/rubocop/cop/empty_line_between_defs.rb +11 -15
- data/lib/rubocop/cop/empty_lines.rb +20 -9
- data/lib/rubocop/cop/empty_literal.rb +47 -0
- data/lib/rubocop/cop/encoding.rb +3 -3
- data/lib/rubocop/cop/end_of_line.rb +3 -3
- data/lib/rubocop/cop/ensure_return.rb +6 -23
- data/lib/rubocop/cop/eval.rb +7 -10
- data/lib/rubocop/cop/favor_join.rb +9 -24
- data/lib/rubocop/cop/favor_modifier.rb +38 -48
- data/lib/rubocop/cop/favor_percent_r.rb +7 -7
- data/lib/rubocop/cop/favor_sprintf.rb +8 -24
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +19 -17
- data/lib/rubocop/cop/handle_exceptions.rb +7 -11
- data/lib/rubocop/cop/hash_syntax.rb +29 -14
- data/lib/rubocop/cop/if_then_else.rb +32 -29
- data/lib/rubocop/cop/leading_comment_space.rb +5 -8
- data/lib/rubocop/cop/line_continuation.rb +4 -7
- data/lib/rubocop/cop/line_length.rb +3 -3
- data/lib/rubocop/cop/loop.rb +33 -0
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +42 -19
- data/lib/rubocop/cop/method_length.rb +34 -37
- data/lib/rubocop/cop/new_lambda_literal.rb +8 -6
- data/lib/rubocop/cop/not.rb +10 -4
- data/lib/rubocop/cop/numeric_literals.rb +9 -7
- data/lib/rubocop/cop/offence.rb +1 -1
- data/lib/rubocop/cop/op_method.rb +12 -22
- data/lib/rubocop/cop/parameter_lists.rb +12 -6
- data/lib/rubocop/cop/parentheses_around_condition.rb +11 -11
- data/lib/rubocop/cop/percent_r.rb +7 -7
- data/lib/rubocop/cop/reduce_arguments.rb +13 -51
- data/lib/rubocop/cop/rescue_exception.rb +13 -29
- data/lib/rubocop/cop/rescue_modifier.rb +5 -8
- data/lib/rubocop/cop/semicolon.rb +15 -74
- data/lib/rubocop/cop/single_line_methods.rb +28 -44
- data/lib/rubocop/cop/space_after_comma_etc.rb +29 -9
- data/lib/rubocop/cop/space_after_control_keyword.rb +16 -15
- data/lib/rubocop/cop/string_literals.rb +9 -35
- data/lib/rubocop/cop/surrounding_space.rb +213 -112
- data/lib/rubocop/cop/symbol_array.rb +9 -7
- data/lib/rubocop/cop/symbol_name.rb +23 -0
- data/lib/rubocop/cop/syntax.rb +14 -7
- data/lib/rubocop/cop/tab.rb +3 -3
- data/lib/rubocop/cop/ternary_operator.rb +26 -24
- data/lib/rubocop/cop/trailing_whitespace.rb +3 -5
- data/lib/rubocop/cop/trivial_accessors.rb +18 -95
- data/lib/rubocop/cop/unless_else.rb +11 -7
- data/lib/rubocop/cop/util.rb +26 -0
- data/lib/rubocop/cop/variable_interpolation.rb +18 -10
- data/lib/rubocop/cop/when_then.rb +6 -17
- data/lib/rubocop/cop/word_array.rb +18 -19
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +1 -0
- data/spec/project_spec.rb +1 -1
- data/spec/rubocop/cli_spec.rb +16 -9
- data/spec/rubocop/config_spec.rb +13 -3
- data/spec/rubocop/cops/access_control_spec.rb +129 -0
- data/spec/rubocop/cops/alias_spec.rb +2 -6
- data/spec/rubocop/cops/align_parameters_spec.rb +58 -71
- data/spec/rubocop/cops/and_or_spec.rb +37 -0
- data/spec/rubocop/cops/ascii_comments_spec.rb +3 -4
- data/spec/rubocop/cops/ascii_identifiers_spec.rb +3 -4
- data/spec/rubocop/cops/avoid_class_vars_spec.rb +7 -2
- data/spec/rubocop/cops/avoid_for_spec.rb +1 -4
- data/spec/rubocop/cops/{avoid_global_vars.rb → avoid_global_vars_spec.rb} +4 -4
- data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +1 -1
- data/spec/rubocop/cops/avoid_perlisms_spec.rb +5 -5
- data/spec/rubocop/cops/block_comments_spec.rb +0 -4
- data/spec/rubocop/cops/blocks_spec.rb +33 -0
- data/spec/rubocop/cops/case_indentation_spec.rb +5 -5
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +15 -5
- data/spec/rubocop/cops/class_methods_spec.rb +4 -4
- data/spec/rubocop/cops/collection_methods_spec.rb +9 -4
- data/spec/rubocop/cops/colon_method_call_spec.rb +11 -5
- data/spec/rubocop/cops/constant_name_spec.rb +42 -0
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +13 -8
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +11 -5
- data/spec/rubocop/cops/empty_line_between_defs_spec.rb +38 -38
- data/spec/rubocop/cops/empty_lines_spec.rb +15 -3
- data/spec/rubocop/cops/empty_literal_spec.rb +90 -0
- data/spec/rubocop/cops/encoding_spec.rb +9 -9
- data/spec/rubocop/cops/end_of_line_spec.rb +2 -2
- data/spec/rubocop/cops/ensure_return_spec.rb +1 -3
- data/spec/rubocop/cops/eval_spec.rb +8 -5
- data/spec/rubocop/cops/favor_join_spec.rb +1 -5
- data/spec/rubocop/cops/favor_modifier_spec.rb +16 -14
- data/spec/rubocop/cops/{favor_percent_r.rb → favor_percent_r_spec.rb} +6 -6
- data/spec/rubocop/cops/favor_sprintf_spec.rb +3 -9
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +4 -4
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +3 -3
- data/spec/rubocop/cops/handle_exceptions_spec.rb +1 -3
- data/spec/rubocop/cops/hash_syntax_spec.rb +11 -6
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +7 -1
- data/spec/rubocop/cops/leading_comment_space_spec.rb +0 -7
- data/spec/rubocop/cops/line_continuation_spec.rb +2 -2
- data/spec/rubocop/cops/line_length_spec.rb +2 -2
- data/spec/rubocop/cops/loop_spec.rb +31 -0
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +38 -12
- data/spec/rubocop/cops/method_length_spec.rb +85 -85
- data/spec/rubocop/cops/multiline_if_then_spec.rb +15 -15
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +3 -3
- data/spec/rubocop/cops/not_spec.rb +1 -4
- data/spec/rubocop/cops/numeric_literals_spec.rb +13 -13
- data/spec/rubocop/cops/one_line_conditional_spec.rb +1 -1
- data/spec/rubocop/cops/op_method_spec.rb +2 -9
- data/spec/rubocop/cops/parameter_lists_spec.rb +7 -7
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +41 -44
- data/spec/rubocop/cops/percent_r_spec.rb +6 -6
- data/spec/rubocop/cops/reduce_arguments_spec.rb +4 -4
- data/spec/rubocop/cops/rescue_exception_spec.rb +48 -8
- data/spec/rubocop/cops/rescue_modifier_spec.rb +2 -5
- data/spec/rubocop/cops/semicolon_spec.rb +2 -30
- data/spec/rubocop/cops/single_line_methods_spec.rb +13 -13
- data/spec/rubocop/cops/space_after_colon_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_comma_spec.rb +14 -2
- data/spec/rubocop/cops/space_after_control_keyword_spec.rb +42 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +2 -2
- data/spec/rubocop/cops/space_around_braces_spec.rb +18 -3
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +4 -4
- data/spec/rubocop/cops/space_around_operators_spec.rb +82 -27
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +13 -7
- data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +14 -9
- data/spec/rubocop/cops/space_inside_parens_spec.rb +7 -3
- data/spec/rubocop/cops/string_literals_spec.rb +17 -5
- data/spec/rubocop/cops/symbol_array_spec.rb +18 -2
- data/spec/rubocop/cops/symbol_name_spec.rb +119 -0
- data/spec/rubocop/cops/syntax_spec.rb +25 -18
- data/spec/rubocop/cops/tab_spec.rb +2 -2
- data/spec/rubocop/cops/ternary_operator_spec.rb +13 -17
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +3 -3
- data/spec/rubocop/cops/trivial_accessors_spec.rb +17 -20
- data/spec/rubocop/cops/unless_else_spec.rb +8 -8
- data/spec/rubocop/cops/variable_interpolation_spec.rb +0 -5
- data/spec/rubocop/cops/when_then_spec.rb +14 -21
- data/spec/rubocop/cops/word_array_spec.rb +12 -4
- data/spec/spec_helper.rb +12 -4
- metadata +40 -31
- data/.document +0 -5
- data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +0 -25
- data/lib/rubocop/cop/array_literal.rb +0 -61
- data/lib/rubocop/cop/brace_after_percent.rb +0 -32
- data/lib/rubocop/cop/grammar.rb +0 -138
- data/lib/rubocop/cop/hash_literal.rb +0 -61
- data/lib/rubocop/cop/percent_literals.rb +0 -25
- data/lib/rubocop/cop/symbol_snake_case.rb +0 -47
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +0 -57
- data/spec/rubocop/cops/array_literal_spec.rb +0 -46
- data/spec/rubocop/cops/brace_after_percent_spec.rb +0 -33
- data/spec/rubocop/cops/grammar_spec.rb +0 -81
- data/spec/rubocop/cops/hash_literal_spec.rb +0 -46
- data/spec/rubocop/cops/multiline_blocks_spec.rb +0 -24
- data/spec/rubocop/cops/percent_literals_spec.rb +0 -47
- data/spec/rubocop/cops/single_line_blocks_spec.rb +0 -22
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +0 -93
@@ -3,17 +3,17 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
module FavorOtherKeywordOverNegation
|
6
|
-
|
7
|
-
|
8
|
-
each(grammar_part, sexp) do |s|
|
9
|
-
# Don't complain about negative if/else. We don't want unless/else.
|
10
|
-
next if s[3] && [:else, :elsif].include?(s[3][0])
|
6
|
+
def check(node)
|
7
|
+
condition, _body, _rest = *node
|
11
8
|
|
12
|
-
|
13
|
-
|
9
|
+
# Look at last expression of contents if there's a parenthesis
|
10
|
+
# around condition.
|
11
|
+
*_, condition = *condition while condition.type == :begin
|
14
12
|
|
15
|
-
|
16
|
-
|
13
|
+
if condition.type == :send
|
14
|
+
_object, method = *condition
|
15
|
+
if method == :! && !(node.loc.respond_to?(:else) && node.loc.else)
|
16
|
+
add_offence(:convention, node.loc.expression.line,
|
17
17
|
error_message)
|
18
18
|
end
|
19
19
|
end
|
@@ -23,24 +23,26 @@ module Rubocop
|
|
23
23
|
class FavorUnlessOverNegatedIf < Cop
|
24
24
|
include FavorOtherKeywordOverNegation
|
25
25
|
|
26
|
-
def
|
27
|
-
|
26
|
+
def on_if(node)
|
27
|
+
check(node)
|
28
|
+
super
|
28
29
|
end
|
29
30
|
|
30
|
-
def
|
31
|
-
|
31
|
+
def error_message
|
32
|
+
'Favor unless (or control flow or) over if for negative conditions.'
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
36
|
class FavorUntilOverNegatedWhile < Cop
|
36
37
|
include FavorOtherKeywordOverNegation
|
37
38
|
|
38
|
-
def
|
39
|
-
|
39
|
+
def on_while(node)
|
40
|
+
check(node)
|
41
|
+
super
|
40
42
|
end
|
41
43
|
|
42
|
-
def
|
43
|
-
|
44
|
+
def error_message
|
45
|
+
'Favor until over while for negative conditions.'
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
@@ -3,18 +3,14 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class HandleExceptions < Cop
|
6
|
-
|
6
|
+
MSG = 'Do not suppress exceptions.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
ERROR_MESSAGE)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
8
|
+
def on_resbody(node)
|
9
|
+
_exc_list_node, _exc_var_node, body_node = *node
|
10
|
+
|
11
|
+
add_offence(:warning, node.loc.line, MSG) if body_node.type == :nil
|
12
|
+
|
13
|
+
super
|
18
14
|
end
|
19
15
|
end
|
20
16
|
end
|
@@ -3,22 +3,37 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class HashSyntax < Cop
|
6
|
-
|
7
|
-
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
MSG = 'Ruby 1.8 hash syntax detected'
|
7
|
+
|
8
|
+
def on_hash(node)
|
9
|
+
pairs = *node
|
10
|
+
|
11
|
+
sym_indices = pairs.all? { |p| word_symbol_pair?(p) }
|
12
|
+
|
13
|
+
if sym_indices
|
14
|
+
pairs.each do |pair|
|
15
|
+
if pair.loc.operator && pair.loc.operator.source == '=>'
|
16
|
+
add_offence(:convention,
|
17
|
+
pair.loc.line,
|
18
|
+
MSG)
|
19
|
+
end
|
15
20
|
end
|
16
21
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def word_symbol_pair?(pair)
|
29
|
+
key, _value = *pair
|
30
|
+
|
31
|
+
if key.type == :sym
|
32
|
+
sym_name = key.to_a[0]
|
33
|
+
|
34
|
+
sym_name =~ /\A\w+\z/
|
35
|
+
else
|
36
|
+
false
|
22
37
|
end
|
23
38
|
end
|
24
39
|
end
|
@@ -3,43 +3,34 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
module IfThenElse
|
6
|
-
def
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
def on_if(node)
|
7
|
+
check(node)
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def on_unless(node)
|
12
|
+
check(node)
|
13
|
+
super
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
case t.text
|
22
|
-
when 'then' then then_found = true
|
23
|
-
when 'end' then return OneLineConditional
|
24
|
-
end
|
25
|
-
when :on_ignored_nl, :on_nl
|
26
|
-
break
|
27
|
-
when :on_semicolon
|
28
|
-
return IfWithSemicolon
|
29
|
-
when :on_comment
|
30
|
-
break if t.text =~ /\n/
|
31
|
-
when :on_sp
|
32
|
-
nil
|
33
|
-
else
|
34
|
-
then_found = false
|
35
|
-
end
|
16
|
+
def check(node)
|
17
|
+
# We won't check modifier or ternary conditionals.
|
18
|
+
if node.loc.expression.source =~ /\A(if|unless)\b/
|
19
|
+
line = offending_line(node)
|
20
|
+
add_offence(:convention, line, error_message) if line
|
36
21
|
end
|
37
|
-
then_found ? MultilineIfThen : nil
|
38
22
|
end
|
39
23
|
end
|
40
24
|
|
41
25
|
class IfWithSemicolon < Cop
|
42
26
|
include IfThenElse
|
27
|
+
|
28
|
+
def offending_line(node)
|
29
|
+
if node.loc.begin && node.loc.begin.source == ';'
|
30
|
+
node.loc.begin.line
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
43
34
|
def error_message
|
44
35
|
'Never use if x; Use the ternary operator instead.'
|
45
36
|
end
|
@@ -47,6 +38,13 @@ module Rubocop
|
|
47
38
|
|
48
39
|
class MultilineIfThen < Cop
|
49
40
|
include IfThenElse
|
41
|
+
|
42
|
+
def offending_line(node)
|
43
|
+
if node.loc.expression.source =~ /\bthen\s*(#.*)?\s*$/
|
44
|
+
node.loc.begin.line
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
50
48
|
def error_message
|
51
49
|
'Never use then for multi-line if/unless.'
|
52
50
|
end
|
@@ -54,6 +52,11 @@ module Rubocop
|
|
54
52
|
|
55
53
|
class OneLineConditional < Cop
|
56
54
|
include IfThenElse
|
55
|
+
|
56
|
+
def offending_line(node)
|
57
|
+
node.loc.expression.line unless node.loc.expression.source =~ /\n/
|
58
|
+
end
|
59
|
+
|
57
60
|
def error_message
|
58
61
|
'Favor the ternary operator (?:) over if/then/else/end constructs.'
|
59
62
|
end
|
@@ -3,15 +3,12 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class LeadingCommentSpace < Cop
|
6
|
-
|
6
|
+
MSG = 'Missing space after #.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
unless t.text.start_with?('#!') && t.pos.lineno == 1
|
13
|
-
add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
|
14
|
-
end
|
8
|
+
def on_comment(c)
|
9
|
+
if c.text =~ /^#+[^#\s]/
|
10
|
+
unless c.text.start_with?('#!') && c.loc.line == 1
|
11
|
+
add_offence(:convention, c.loc.line, MSG)
|
15
12
|
end
|
16
13
|
end
|
17
14
|
end
|
@@ -3,14 +3,11 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class LineContinuation < Cop
|
6
|
-
|
6
|
+
MSG = 'Avoid the use of the line continuation character(\).'
|
7
7
|
|
8
|
-
def inspect(
|
9
|
-
|
10
|
-
|
11
|
-
if t.type == :on_sp && t.text == "\\\n"
|
12
|
-
add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
|
13
|
-
end
|
8
|
+
def inspect(source, tokens, ast, comments)
|
9
|
+
source.each_with_index do |line, index|
|
10
|
+
add_offence(:convention, index, MSG) if line =~ /.*\\\z/
|
14
11
|
end
|
15
12
|
end
|
16
13
|
end
|
@@ -3,13 +3,13 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class LineLength < Cop
|
6
|
-
|
6
|
+
MSG = 'Line is too long. [%d/%d]'
|
7
7
|
|
8
|
-
def inspect(
|
8
|
+
def inspect(source, tokens, ast, comments)
|
9
9
|
source.each_with_index do |line, index|
|
10
10
|
max = LineLength.max
|
11
11
|
if line.length > max
|
12
|
-
message = sprintf(
|
12
|
+
message = sprintf(MSG, line.length, max)
|
13
13
|
add_offence(:convention, index + 1, message)
|
14
14
|
end
|
15
15
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
class Loop < Cop
|
6
|
+
MSG = 'Use Kernel#loop with break rather than begin/end/until(or while).'
|
7
|
+
|
8
|
+
def on_while(node)
|
9
|
+
check(node)
|
10
|
+
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_until(node)
|
15
|
+
check(node)
|
16
|
+
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def check(node)
|
23
|
+
_cond, body = *node
|
24
|
+
type = node.type.to_s
|
25
|
+
|
26
|
+
if body.type == :begin &&
|
27
|
+
!node.loc.expression.source.start_with?(type)
|
28
|
+
add_offence(:warning, node.loc.keyword.line, MSG)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -3,29 +3,52 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class MethodAndVariableSnakeCase < Cop
|
6
|
-
|
6
|
+
MSG = 'Use snake_case for methods and variables.'
|
7
7
|
SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
8
|
+
|
9
|
+
# http://phrogz.net/programmingruby/language.html#table_18.4
|
10
|
+
OPERATOR_METHODS = %w(
|
11
|
+
| ^ & <=> == === =~ > >= < <= << >>
|
12
|
+
+ - * / % ** ~ +@ -@ [] []= ` ! != !~
|
13
|
+
).map(&:to_sym)
|
14
|
+
|
15
|
+
def inspect(source, tokens, node, comments)
|
16
|
+
on_node([:def, :defs, :lvasgn, :ivasgn, :send], node) do |n|
|
17
|
+
name = case n.type
|
18
|
+
when :def
|
19
|
+
name_of_instance_method(n)
|
20
|
+
when :defs
|
21
|
+
name_of_singleton_method(n)
|
22
|
+
when :lvasgn, :ivasgn
|
23
|
+
name_of_variable(n)
|
24
|
+
when :send
|
25
|
+
name_of_setter(n)
|
26
|
+
end
|
27
|
+
|
28
|
+
next unless name
|
29
|
+
next if name =~ SNAKE_CASE || OPERATOR_METHODS.include?(name)
|
30
|
+
|
31
|
+
add_offence(:convention, n.location.line, MSG)
|
22
32
|
end
|
23
33
|
end
|
24
34
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
35
|
+
def name_of_instance_method(def_node)
|
36
|
+
def_node.children.first
|
37
|
+
end
|
38
|
+
|
39
|
+
def name_of_singleton_method(defs_node)
|
40
|
+
defs_node.children[1]
|
41
|
+
end
|
42
|
+
|
43
|
+
def name_of_variable(vasgn_node)
|
44
|
+
vasgn_node.children.first
|
45
|
+
end
|
46
|
+
|
47
|
+
def name_of_setter(send_node)
|
48
|
+
receiver, method_name = *send_node
|
49
|
+
return nil unless receiver && receiver.type == :self
|
50
|
+
return nil unless method_name.to_s.end_with?('=')
|
51
|
+
method_name
|
29
52
|
end
|
30
53
|
end
|
31
54
|
end
|
@@ -3,52 +3,49 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class MethodLength < Cop
|
6
|
-
|
7
|
-
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
max = MethodLength.config['Max']
|
14
|
-
if length > max
|
15
|
-
message = sprintf(ERROR_MESSAGE, length, max)
|
16
|
-
add_offence(:convention, def_lineno, message)
|
17
|
-
end
|
18
|
-
end
|
6
|
+
MSG = 'Method has too many lines. [%d/%d]'
|
7
|
+
|
8
|
+
def on_def(node)
|
9
|
+
check(node)
|
10
|
+
|
11
|
+
super
|
19
12
|
end
|
20
13
|
|
21
|
-
|
14
|
+
def on_defs(node)
|
15
|
+
check(node)
|
22
16
|
|
23
|
-
|
24
|
-
lines = source[def_lineno..(end_lineno - 2)].reject(&:empty?)
|
25
|
-
unless MethodLength.config['CountComments']
|
26
|
-
lines = lines.reject { |line| line =~ /^\s*#/ }
|
27
|
-
end
|
28
|
-
lines.size
|
17
|
+
super
|
29
18
|
end
|
30
19
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
20
|
+
def max_length
|
21
|
+
MethodLength.config['Max']
|
22
|
+
end
|
34
23
|
|
35
|
-
|
36
|
-
|
37
|
-
# 2. if the method is a one line, which we will ignore
|
38
|
-
[t.type, t.text] == [:on_kw, 'def'] &&
|
39
|
-
source[t.pos.lineno - 1][t.pos.column - 1] != ':' &&
|
40
|
-
source[t.pos.lineno - 1] !~ /^\s*def.*(?:\(.*\)|;).*end\s*$/
|
41
|
-
end
|
24
|
+
def count_comments?
|
25
|
+
MethodLength.config['CountComments']
|
42
26
|
end
|
43
27
|
|
44
|
-
|
45
|
-
|
46
|
-
def
|
47
|
-
|
48
|
-
|
49
|
-
|
28
|
+
private
|
29
|
+
|
30
|
+
def check(node)
|
31
|
+
method_length = calculate_length(node.loc.expression.source)
|
32
|
+
|
33
|
+
if method_length > max_length
|
34
|
+
message = sprintf(MSG, method_length, max_length)
|
35
|
+
add_offence(:convention, node.loc.keyword.line, message)
|
50
36
|
end
|
51
|
-
|
37
|
+
end
|
38
|
+
|
39
|
+
def calculate_length(source)
|
40
|
+
lines = source.lines.to_a[1...-1]
|
41
|
+
|
42
|
+
return 0 unless lines
|
43
|
+
|
44
|
+
lines.map!(&:strip).reject!(&:empty?)
|
45
|
+
|
46
|
+
lines.reject! { |line| line =~ /^\s*#/ } unless count_comments?
|
47
|
+
|
48
|
+
lines.size
|
52
49
|
end
|
53
50
|
end
|
54
51
|
end
|