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,23 +3,19 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class EmptyLineBetweenDefs < Cop
|
6
|
-
|
6
|
+
MSG = 'Use empty lines between defs.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# so we iterate starting at index 1.
|
15
|
-
defs[1..-1].each do |child|
|
16
|
-
next_row_ix = child[1][-1].lineno - 1
|
17
|
-
if source[current_row_ix..next_row_ix].grep(/^[ \t]*$/).empty?
|
18
|
-
add_offence(:convention, next_row_ix + 1, ERROR_MESSAGE)
|
19
|
-
end
|
20
|
-
current_row_ix = next_row_ix
|
21
|
-
end
|
8
|
+
def on_def(s)
|
9
|
+
def_start = s.loc.keyword.line
|
10
|
+
def_end = s.loc.end.line
|
11
|
+
|
12
|
+
if @prev_def_end && (def_start - @prev_def_end) < 2
|
13
|
+
add_offence(:convention, def_start, MSG)
|
22
14
|
end
|
15
|
+
|
16
|
+
@prev_def_end = def_end
|
17
|
+
|
18
|
+
super
|
23
19
|
end
|
24
20
|
end
|
25
21
|
end
|
@@ -3,19 +3,30 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class EmptyLines < Cop
|
6
|
-
|
6
|
+
MSG = 'Extra blank line detected.'
|
7
|
+
LINE_OFFSET = 2
|
7
8
|
|
8
|
-
def inspect(
|
9
|
-
|
9
|
+
def inspect(source, tokens, ast, comments)
|
10
|
+
return if tokens.empty?
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
prev_line = 1
|
13
|
+
|
14
|
+
tokens.each do |token|
|
15
|
+
cur_line = token.pos.line
|
16
|
+
line_diff = cur_line - prev_line
|
17
|
+
|
18
|
+
if line_diff > LINE_OFFSET
|
19
|
+
# we need to be wary of comments since they
|
20
|
+
# don't show up in the tokens
|
21
|
+
((prev_line + 1)...cur_line).each do |line|
|
22
|
+
# we check if the prev and current lines are empty
|
23
|
+
if source[line - 2].empty? && source[line - 1].empty?
|
24
|
+
add_offence(:convention, line, MSG)
|
25
|
+
end
|
26
|
+
end
|
16
27
|
end
|
17
28
|
|
18
|
-
|
29
|
+
prev_line = cur_line
|
19
30
|
end
|
20
31
|
end
|
21
32
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
class EmptyLiteral < Cop
|
6
|
+
ARR_MSG = 'Use array literal [] instead of Array.new.'
|
7
|
+
HASH_MSG = 'Use hash literal {} instead of Hash.new.'
|
8
|
+
STR_MSG = "Use string literal '' instead of String.new."
|
9
|
+
|
10
|
+
# Empty array node
|
11
|
+
#
|
12
|
+
# (send
|
13
|
+
# (const nil :Array) :new)
|
14
|
+
ARRAY_NODE = s(:send, s(:const, nil, :Array), :new)
|
15
|
+
|
16
|
+
# Empty hash node
|
17
|
+
#
|
18
|
+
# (send
|
19
|
+
# (const nil :Hash) :new)
|
20
|
+
HASH_NODE = s(:send, s(:const, nil, :Hash), :new)
|
21
|
+
|
22
|
+
# Empty string node
|
23
|
+
#
|
24
|
+
# (send
|
25
|
+
# (const nil :String) :new)
|
26
|
+
STR_NODE = s(:send, s(:const, nil, :String), :new)
|
27
|
+
|
28
|
+
def inspect(source, tokens, ast, comments)
|
29
|
+
on_node(:send, ast, :block) do |node|
|
30
|
+
if node == ARRAY_NODE
|
31
|
+
add_offence(:convention,
|
32
|
+
node.loc.line,
|
33
|
+
ARR_MSG)
|
34
|
+
elsif node == HASH_NODE
|
35
|
+
add_offence(:convention,
|
36
|
+
node.loc.line,
|
37
|
+
HASH_MSG)
|
38
|
+
elsif node == STR_NODE
|
39
|
+
add_offence(:convention,
|
40
|
+
node.loc.line,
|
41
|
+
STR_MSG)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/rubocop/cop/encoding.rb
CHANGED
@@ -3,14 +3,14 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class Encoding < Cop
|
6
|
-
|
6
|
+
MSG = 'Missing utf-8 encoding comment.'
|
7
7
|
|
8
|
-
def inspect(
|
8
|
+
def inspect(source, tokens, ast, comments)
|
9
9
|
unless RUBY_VERSION >= '2.0.0'
|
10
10
|
expected_line = 0
|
11
11
|
expected_line += 1 if source[expected_line] =~ /^#!/
|
12
12
|
unless source[expected_line] =~ /#.*coding: (UTF|utf)-8/
|
13
|
-
add_offence(:convention, 1,
|
13
|
+
add_offence(:convention, 1, MSG)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -3,11 +3,11 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class EndOfLine < Cop
|
6
|
-
|
6
|
+
MSG = 'Carriage return character detected.'
|
7
7
|
|
8
|
-
def inspect(
|
8
|
+
def inspect(source, tokens, ast, comments)
|
9
9
|
source.each_with_index do |line, index|
|
10
|
-
add_offence(:convention, index + 1,
|
10
|
+
add_offence(:convention, index + 1, MSG) if line =~ /\r$/
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -3,33 +3,16 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class EnsureReturn < Cop
|
6
|
-
|
6
|
+
MSG = 'Never return from an ensure block.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
ensure_col = nil
|
8
|
+
def on_ensure(node)
|
9
|
+
_body, ensure_body = *node
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
if ensure_start?(t)
|
15
|
-
in_ensure = true
|
16
|
-
ensure_col = t.pos.column
|
17
|
-
elsif ensure_end?(t, ensure_col)
|
18
|
-
in_ensure = false
|
19
|
-
elsif in_ensure && t.type == :on_kw && t.text == 'return'
|
20
|
-
add_offence(:warning, t.pos.lineno, ERROR_MESSAGE)
|
21
|
-
end
|
11
|
+
on_node(:return, ensure_body) do |e|
|
12
|
+
add_offence(:warning, e.loc.line, MSG)
|
22
13
|
end
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def ensure_start?(t)
|
28
|
-
t.type == :on_kw && t.text == 'ensure'
|
29
|
-
end
|
30
14
|
|
31
|
-
|
32
|
-
t.type == :on_kw && t.text == 'end' && t.pos.column == column
|
15
|
+
super
|
33
16
|
end
|
34
17
|
end
|
35
18
|
end
|
data/lib/rubocop/cop/eval.rb
CHANGED
@@ -3,19 +3,16 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class Eval < Cop
|
6
|
-
|
6
|
+
MSG = 'The use of eval is a serious security risk.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
each(:fcall, sexp) { |s| process_ident(s[1]) }
|
11
|
-
end
|
8
|
+
def on_send(node)
|
9
|
+
receiver, method_name, = *node
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
add_offence(:security,
|
16
|
-
sexp[2].lineno,
|
17
|
-
ERROR_MESSAGE)
|
11
|
+
if receiver.nil? && method_name == :eval
|
12
|
+
add_offence(:security, node.loc.line, MSG)
|
18
13
|
end
|
14
|
+
|
15
|
+
super
|
19
16
|
end
|
20
17
|
end
|
21
18
|
end
|
@@ -3,34 +3,19 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class FavorJoin < Cop
|
6
|
-
|
6
|
+
MSG = 'Favor Array#join over Array#*.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
op1 = s[1]
|
11
|
-
operator = s[2]
|
12
|
-
op2 = s[3]
|
8
|
+
def on_send(node)
|
9
|
+
receiver_node, method_name, *arg_nodes = *node
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
add_offence(
|
21
|
-
:convention,
|
22
|
-
lineno,
|
23
|
-
ERROR_MESSAGE
|
24
|
-
) if lineno
|
11
|
+
if receiver_node && receiver_node.type == :array &&
|
12
|
+
method_name == :* && arg_nodes[0].type == :str
|
13
|
+
add_offence(:convention,
|
14
|
+
node.loc.expression.line,
|
15
|
+
MSG)
|
25
16
|
end
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def matching?(operator, op1, op2)
|
31
|
-
return false unless operator == :*
|
32
17
|
|
33
|
-
|
18
|
+
super
|
34
19
|
end
|
35
20
|
end
|
36
21
|
end
|
@@ -3,53 +3,36 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
module FavorModifier
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
# If it contains an else, it can't be written as a modifier.
|
11
|
-
next if s[3] && s[3][0] == :else
|
6
|
+
# TODO extremely ugly solution that needs lots of polish
|
7
|
+
def check(sexp)
|
8
|
+
# discard if/then/else
|
9
|
+
return false if sexp.loc.respond_to?(:else) && sexp.loc.else
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
if %w(if while).include?(sexp.loc.keyword.source)
|
12
|
+
cond, body = *sexp
|
13
|
+
else
|
14
|
+
cond, _else, body = *sexp
|
15
|
+
end
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
if length(sexp) > 3
|
18
|
+
false
|
19
|
+
else
|
20
|
+
cond_length = sexp.loc.keyword.size + cond.loc.expression.size + 1
|
21
|
+
body_length = body_length(body)
|
24
22
|
|
25
|
-
|
26
|
-
next # not a single-line body
|
27
|
-
end
|
28
|
-
# The start ix is the index of the leftmost token on the
|
29
|
-
# line of the if/unless, i.e. the index of if/unless itself,
|
30
|
-
# or of the indentation space.
|
31
|
-
start_ix = if_ix.downto(0).find do |block_ix|
|
32
|
-
block_ix == 0 || tokens[block_ix - 1].text =~ /\n/
|
33
|
-
end
|
34
|
-
# The stop index is the index of the token just before
|
35
|
-
# 'end', not counting whitespace tokens.
|
36
|
-
stop_ix = (end_ix - 1).downto(0).find do |block_ix|
|
37
|
-
tokens[block_ix].text !~ /\s/
|
38
|
-
end
|
39
|
-
if length(tokens, start_ix, stop_ix) <= LineLength.max
|
40
|
-
add_offence(:convention, token_positions[if_ix].lineno,
|
41
|
-
error_message)
|
42
|
-
end
|
23
|
+
(cond_length + body_length) <= LineLength.max
|
43
24
|
end
|
44
25
|
end
|
45
26
|
|
46
|
-
def length(
|
47
|
-
(
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
27
|
+
def length(sexp)
|
28
|
+
sexp.loc.expression.source.split("\n").size
|
29
|
+
end
|
30
|
+
|
31
|
+
def body_length(body)
|
32
|
+
if body
|
33
|
+
body.loc.expression.column + body.loc.expression.size
|
34
|
+
else
|
35
|
+
0
|
53
36
|
end
|
54
37
|
end
|
55
38
|
end
|
@@ -59,23 +42,30 @@ module Rubocop
|
|
59
42
|
|
60
43
|
def error_message
|
61
44
|
'Favor modifier if/unless usage when you have a single-line body. ' +
|
62
|
-
'Another good alternative is the usage of control flow
|
45
|
+
'Another good alternative is the usage of control flow &&/||.'
|
63
46
|
end
|
64
47
|
|
65
|
-
def
|
66
|
-
|
48
|
+
def on_if(node)
|
49
|
+
# discard ternary ops and modifier if/unless nodes
|
50
|
+
return unless node.loc.respond_to?(:keyword) &&
|
51
|
+
node.loc.respond_to?(:else)
|
52
|
+
|
53
|
+
add_offence(:convention, node.loc.line, error_message) if check(node)
|
54
|
+
|
55
|
+
super
|
67
56
|
end
|
68
57
|
end
|
69
58
|
|
70
59
|
class WhileUntilModifier < Cop
|
71
60
|
include FavorModifier
|
72
61
|
|
73
|
-
|
62
|
+
MSG =
|
74
63
|
'Favor modifier while/until usage when you have a single-line body.'
|
75
|
-
end
|
76
64
|
|
77
|
-
def inspect(
|
78
|
-
[:while, :until]
|
65
|
+
def inspect(source, tokens, ast, comments)
|
66
|
+
on_node([:while, :until], ast) do |node|
|
67
|
+
add_offence(:convention, node.loc.line, MSG) if check(node)
|
68
|
+
end
|
79
69
|
end
|
80
70
|
end
|
81
71
|
end
|
@@ -3,16 +3,16 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class FavorPercentR < Cop
|
6
|
-
|
6
|
+
MSG = 'Use %r for regular expressions matching more ' +
|
7
7
|
"than one '/' character."
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
add_offence(:convention, t1.pos.lineno, ERROR_MESSAGE)
|
14
|
-
end
|
9
|
+
def on_regexp(node)
|
10
|
+
if node.loc.begin.source == '/' &&
|
11
|
+
node.loc.expression.source[1...-1].scan(/\//).size > 1
|
12
|
+
add_offence(:convention, node.loc.line, MSG)
|
15
13
|
end
|
14
|
+
|
15
|
+
super
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -3,34 +3,18 @@
|
|
3
3
|
module Rubocop
|
4
4
|
module Cop
|
5
5
|
class FavorSprintf < Cop
|
6
|
-
|
6
|
+
MSG = 'Favor sprintf over String#%.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
op1 = s[1]
|
11
|
-
operator = s[2]
|
12
|
-
op2 = s[3]
|
8
|
+
def on_send(node)
|
9
|
+
receiver_node, method_name, *arg_nodes = *node
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
lineno = pos.lineno if pos
|
19
|
-
|
20
|
-
add_offence(
|
21
|
-
:convention,
|
22
|
-
lineno,
|
23
|
-
ERROR_MESSAGE
|
24
|
-
) if lineno
|
11
|
+
if method_name == :% &&
|
12
|
+
([:str, :dstr].include?(receiver_node.type) ||
|
13
|
+
arg_nodes[0].type == :array)
|
14
|
+
add_offence(:convention, node.loc.expression.line, MSG)
|
25
15
|
end
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def matching?(operator, op1, op2)
|
31
|
-
return false unless operator == :%
|
32
16
|
|
33
|
-
|
17
|
+
super
|
34
18
|
end
|
35
19
|
end
|
36
20
|
end
|