rubocop 0.3.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rubocop.yml +176 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +60 -0
- data/CONTRIBUTING.md +13 -6
- data/Gemfile +1 -13
- data/README.md +131 -6
- data/Rakefile +1 -15
- data/bin/rubocop +2 -1
- data/lib/rubocop/cli.rb +133 -21
- data/lib/rubocop/cop/alias.rb +18 -0
- data/lib/rubocop/cop/align_parameters.rb +5 -4
- data/lib/rubocop/cop/array_literal.rb +22 -0
- data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
- data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
- data/lib/rubocop/cop/avoid_for.rb +17 -0
- data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
- data/lib/rubocop/cop/avoid_perlisms.rb +46 -0
- data/lib/rubocop/cop/blocks.rb +1 -1
- data/lib/rubocop/cop/brace_after_percent.rb +28 -0
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
- data/lib/rubocop/cop/collection_methods.rb +29 -0
- data/lib/rubocop/cop/cop.rb +16 -4
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +7 -5
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/ensure_return.rb +36 -0
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_percent_r.rb +19 -0
- data/lib/rubocop/cop/favor_sprintf.rb +37 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +12 -8
- data/lib/rubocop/cop/handle_exceptions.rb +21 -0
- data/lib/rubocop/cop/hash_literal.rb +22 -0
- data/lib/rubocop/cop/indentation.rb +1 -1
- data/lib/rubocop/cop/line_length.rb +7 -3
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
- data/lib/rubocop/cop/method_length.rb +66 -0
- data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/offence.rb +3 -1
- data/lib/rubocop/cop/op_method.rb +23 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/percent_literals.rb +25 -0
- data/lib/rubocop/cop/percent_r.rb +19 -0
- data/lib/rubocop/cop/reduce_arguments.rb +67 -0
- data/lib/rubocop/cop/rescue_exception.rb +39 -0
- data/lib/rubocop/cop/rescue_modifier.rb +20 -0
- data/lib/rubocop/cop/semicolon.rb +18 -0
- data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
- data/lib/rubocop/cop/string_literals.rb +12 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
- data/lib/rubocop/cop/syntax.rb +42 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/cop/variable_interpolation.rb +20 -0
- data/lib/rubocop/cop/when_then.rb +3 -0
- data/lib/rubocop/report/emacs_style.rb +2 -2
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +3 -1
- data/lib/rubocop.rb +37 -0
- data/rubocop.gemspec +36 -134
- data/spec/rubocop/cli_spec.rb +326 -48
- data/spec/rubocop/cops/alias_spec.rb +36 -0
- data/spec/rubocop/cops/align_parameters_spec.rb +43 -34
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- data/spec/rubocop/cops/array_literal_spec.rb +29 -0
- data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
- data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
- data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
- data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
- data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
- data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
- data/spec/rubocop/cops/cop_spec.rb +5 -5
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
- data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
- data/spec/rubocop/cops/encoding_spec.rb +17 -11
- data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
- data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
- data/spec/rubocop/cops/favor_percent_r.rb +29 -0
- data/spec/rubocop/cops/favor_sprintf_spec.rb +57 -0
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
- data/spec/rubocop/cops/grammar_spec.rb +59 -44
- data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
- data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/indentation_spec.rb +7 -7
- data/spec/rubocop/cops/line_length_spec.rb +4 -4
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
- data/spec/rubocop/cops/method_length_spec.rb +150 -0
- data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
- data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
- data/spec/rubocop/cops/offence_spec.rb +10 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- data/spec/rubocop/cops/op_method_spec.rb +58 -0
- data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
- data/spec/rubocop/cops/percent_literals_spec.rb +47 -0
- data/spec/rubocop/cops/percent_r_spec.rb +29 -0
- data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
- data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
- data/spec/rubocop/cops/rescue_modifier.rb +40 -0
- data/spec/rubocop/cops/semicolon_spec.rb +29 -0
- data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
- data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
- data/spec/rubocop/cops/space_around_operators_spec.rb +57 -25
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
- data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
- data/spec/rubocop/cops/string_literals_spec.rb +9 -6
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +74 -0
- data/spec/rubocop/cops/syntax_spec.rb +34 -0
- data/spec/rubocop/cops/tab_spec.rb +4 -4
- data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
- data/spec/rubocop/cops/unless_else_spec.rb +4 -4
- data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
- data/spec/rubocop/cops/when_then_spec.rb +12 -5
- data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
- data/spec/rubocop/reports/report_spec.rb +5 -5
- data/spec/spec_helper.rb +6 -0
- metadata +165 -36
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -39
- data/VERSION +0 -1
- data/features/rubocop.feature +0 -9
- data/features/step_definitions/rubocop_steps.rb +0 -1
- data/features/support/env.rb +0 -15
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
module FavorModifier
|
|
6
|
+
def check(kind, tokens, sexp)
|
|
7
|
+
token_positions = tokens.map(&:pos)
|
|
8
|
+
token_texts = tokens.map(&:text)
|
|
9
|
+
each(kind, sexp) do |s|
|
|
10
|
+
# If it contains an else, it can't be written as a modifier.
|
|
11
|
+
next if s[3] && s[3][0] == :else
|
|
12
|
+
|
|
13
|
+
sexp_positions = all_positions(s)
|
|
14
|
+
ix = token_positions.index(sexp_positions.first)
|
|
15
|
+
if_ix = token_texts[0..ix].rindex(kind.to_s) # index of if/unless/...
|
|
16
|
+
ix = token_positions.index(sexp_positions.last)
|
|
17
|
+
end_ix = ix + token_texts[ix..-1].index('end')
|
|
18
|
+
|
|
19
|
+
# If there's a comment anywhere between
|
|
20
|
+
# if/unless/while/until and end, we don't report. It's
|
|
21
|
+
# possible that the comment will be less clear if put above
|
|
22
|
+
# a one liner rather than inside.
|
|
23
|
+
next if tokens[if_ix...end_ix].map(&:type).include?(:on_comment)
|
|
24
|
+
|
|
25
|
+
if token_positions[end_ix].lineno - token_positions[if_ix].lineno > 2
|
|
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
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def length(tokens, start_ix, stop_ix)
|
|
47
|
+
(start_ix..stop_ix).reduce(0) do |acc, ix|
|
|
48
|
+
acc + if ix > start_ix && tokens[ix - 1].text =~ /\n/
|
|
49
|
+
0
|
|
50
|
+
else
|
|
51
|
+
tokens[ix].text.length
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class IfUnlessModifier < Cop
|
|
58
|
+
include FavorModifier
|
|
59
|
+
|
|
60
|
+
def error_message
|
|
61
|
+
'Favor modifier if/unless usage when you have a single-line body. ' +
|
|
62
|
+
'Another good alternative is the usage of control flow and/or.'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def inspect(file, source, tokens, sexp)
|
|
66
|
+
[:if, :unless].each { |kind| check(kind, tokens, sexp) }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class WhileUntilModifier < Cop
|
|
71
|
+
include FavorModifier
|
|
72
|
+
|
|
73
|
+
def error_message
|
|
74
|
+
'Favor modifier while/until usage when you have a single-line body.'
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def inspect(file, source, tokens, sexp)
|
|
78
|
+
[:while, :until].each { |kind| check(kind, tokens, sexp) }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class FavorPercentR < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use %r for regular expressions matching more ' +
|
|
7
|
+
"than one '/' character."
|
|
8
|
+
|
|
9
|
+
def inspect(file, source, tokens, sexp)
|
|
10
|
+
tokens.each_cons(2) do |t1, t2|
|
|
11
|
+
if t1.type == :on_regexp_beg && t1.text == '/' &&
|
|
12
|
+
t2.text.scan(/\//).size > 1
|
|
13
|
+
add_offence(:convention, t1.pos.lineno, ERROR_MESSAGE)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class FavorSprintf < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Favor sprintf over String#%.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:binary, sexp) do |s|
|
|
10
|
+
op1 = s[1]
|
|
11
|
+
operator = s[2]
|
|
12
|
+
op2 = s[3]
|
|
13
|
+
|
|
14
|
+
# we care only about the % operator
|
|
15
|
+
next unless matching?(operator, op1, op2)
|
|
16
|
+
|
|
17
|
+
pos = all_positions(s[1]).first
|
|
18
|
+
lineno = pos.lineno if pos
|
|
19
|
+
|
|
20
|
+
add_offence(
|
|
21
|
+
:convention,
|
|
22
|
+
lineno,
|
|
23
|
+
ERROR_MESSAGE
|
|
24
|
+
) if lineno
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def matching?(operator, op1, op2)
|
|
31
|
+
return false unless operator == :%
|
|
32
|
+
|
|
33
|
+
op1[0] == :string_literal or op2[0] == :array
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
module FavorOtherKeywordOverNegation
|
|
6
|
+
private
|
|
7
|
+
def check(grammar_part, sexp)
|
|
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])
|
|
11
|
+
|
|
12
|
+
condition = s[1]
|
|
13
|
+
condition = condition[1][0] while condition[0] == :paren
|
|
14
|
+
|
|
15
|
+
if condition[0] == :unary && [:!, :not].include?(condition[1])
|
|
16
|
+
add_offence(:convention, all_positions(s).first.lineno,
|
|
17
|
+
error_message)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class FavorUnlessOverNegatedIf < Cop
|
|
24
|
+
include FavorOtherKeywordOverNegation
|
|
25
|
+
|
|
26
|
+
def error_message
|
|
27
|
+
'Favor unless (or control flow or) over if for negative conditions.'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def inspect(file, source, tokens, sexp)
|
|
31
|
+
[:if, :if_mod].each { |grammar_part| check(grammar_part, sexp) }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
class FavorUntilOverNegatedWhile < Cop
|
|
36
|
+
include FavorOtherKeywordOverNegation
|
|
37
|
+
|
|
38
|
+
def error_message
|
|
39
|
+
'Favor until over while for negative conditions.'
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def inspect(file, source, tokens, sexp)
|
|
43
|
+
[:while, :while_mod].each { |grammar_part| check(grammar_part, sexp) }
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/rubocop/cop/grammar.rb
CHANGED
|
@@ -5,7 +5,7 @@ module Rubocop
|
|
|
5
5
|
class Grammar
|
|
6
6
|
def initialize(tokens)
|
|
7
7
|
@tokens_without_pos = tokens.map { |t| [t.type, t.text] }
|
|
8
|
-
process_embedded_expressions
|
|
8
|
+
process_embedded_expressions if RUBY_VERSION < '2.0'
|
|
9
9
|
@token_indexes = {}
|
|
10
10
|
@tokens_without_pos.each_with_index do |t, i|
|
|
11
11
|
@token_indexes[t] ||= []
|
|
@@ -16,12 +16,13 @@ module Rubocop
|
|
|
16
16
|
token_positions = tokens.map { |t| [t.pos.lineno, t.pos.column] }
|
|
17
17
|
@index_by_pos = Hash[*token_positions.each_with_index.to_a.flatten(1)]
|
|
18
18
|
@special = {
|
|
19
|
-
assign: [:on_op, '='],
|
|
20
|
-
brace_block: [:on_lbrace, '{']
|
|
19
|
+
assign: [[:on_op, '=']],
|
|
20
|
+
brace_block: [[:on_lbrace, '{']],
|
|
21
|
+
ifop: [[:on_op, '?'], [:on_op, ':']]
|
|
21
22
|
}
|
|
22
23
|
end
|
|
23
24
|
|
|
24
|
-
#
|
|
25
|
+
# In ruby 1.9.3 and below, the string "#{x}" will give the tokens
|
|
25
26
|
# [:on_tstring_beg, '"'], [:on_embexpr_beg, '#{'], [:on_ident, 'x'],
|
|
26
27
|
# [:on_rbrace, '}'], [:on_tstring_end, '"']
|
|
27
28
|
# which is not so good for us. We want to distinguish between a
|
|
@@ -45,7 +46,7 @@ module Rubocop
|
|
|
45
46
|
end
|
|
46
47
|
when :inside_expr
|
|
47
48
|
case type
|
|
48
|
-
when :on_lbrace
|
|
49
|
+
when :on_lbrace, :on_embexpr_end
|
|
49
50
|
brace_depth += 1
|
|
50
51
|
when :on_rbrace
|
|
51
52
|
if brace_depth == 1
|
|
@@ -84,14 +85,17 @@ module Rubocop
|
|
|
84
85
|
# Here we don't advance @ix because there may be other
|
|
85
86
|
# tokens inbetween the current one and the one we get from
|
|
86
87
|
# @special.
|
|
87
|
-
|
|
88
|
+
@special[sexp[0]].each do |token_to_find|
|
|
89
|
+
find(path, sexp, token_to_find)
|
|
90
|
+
end
|
|
88
91
|
when :block_var # "{ |...|" or "do |...|"
|
|
89
92
|
@ix = find(path, sexp, [:on_op, '|']) + 1
|
|
90
93
|
find(path, sexp, [:on_op, '|'])
|
|
91
94
|
end
|
|
92
95
|
path += [sexp[0]] if Symbol === sexp[0]
|
|
93
|
-
# Compensate for reverse order of if modifier
|
|
94
|
-
|
|
96
|
+
# Compensate for reverse order of if/unless/while/until modifier.
|
|
97
|
+
modifiers = [:if_mod, :unless_mod, :while_mod, :until_mod]
|
|
98
|
+
children = modifiers.include?(sexp[0]) ? sexp.reverse : sexp
|
|
95
99
|
|
|
96
100
|
children.each do |elem|
|
|
97
101
|
case elem
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class HandleExceptions < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Do not suppress exceptions.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:begin, sexp) do |s|
|
|
10
|
+
each(:rescue, s) do |rs|
|
|
11
|
+
if rs[3] == [[:void_stmt]]
|
|
12
|
+
add_offence(:warning,
|
|
13
|
+
all_positions(s)[-1].lineno + 1,
|
|
14
|
+
ERROR_MESSAGE)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class HashLiteral < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use hash literal {} instead of Hash.new.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:method_add_arg, sexp) do |s|
|
|
10
|
+
potential_class = s[1][1][1]
|
|
11
|
+
|
|
12
|
+
if potential_class[1] == 'Hash' && s[1][3][1] == 'new' &&
|
|
13
|
+
s[2] == [:arg_paren, nil]
|
|
14
|
+
add_offence(:convention,
|
|
15
|
+
potential_class[2].lineno,
|
|
16
|
+
ERROR_MESSAGE)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -4,16 +4,20 @@ module Rubocop
|
|
|
4
4
|
module Cop
|
|
5
5
|
class LineLength < Cop
|
|
6
6
|
ERROR_MESSAGE = 'Line is too long. [%d/%d]'
|
|
7
|
-
MAX_LINE_LENGTH = 79
|
|
8
7
|
|
|
9
8
|
def inspect(file, source, tokens, sexp)
|
|
10
9
|
source.each_with_index do |line, index|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
max = LineLength.max
|
|
11
|
+
if line.length > max
|
|
12
|
+
message = sprintf(ERROR_MESSAGE, line.length, max)
|
|
13
13
|
add_offence(:convention, index + 1, message)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
end
|
|
17
|
+
|
|
18
|
+
def self.max
|
|
19
|
+
LineLength.config ? LineLength.config['Max'] || 79 : 79
|
|
20
|
+
end
|
|
17
21
|
end
|
|
18
22
|
end
|
|
19
23
|
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class MethodAndVariableSnakeCase < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use snake_case for methods and variables.'
|
|
7
|
+
SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
|
|
8
|
+
|
|
9
|
+
def inspect(file, source, tokens, sexp)
|
|
10
|
+
each(:def, sexp) { |s| check(s[1]) }
|
|
11
|
+
|
|
12
|
+
each(:assign, sexp) do |s|
|
|
13
|
+
case s[1][0]
|
|
14
|
+
when :var_field
|
|
15
|
+
check(s[1][1])
|
|
16
|
+
when :field
|
|
17
|
+
if s[1][1][0] == :var_ref && s[1][1][1][0..1] == [:@kw, 'self']
|
|
18
|
+
check(s[1][3])
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def check(sexp)
|
|
25
|
+
if [:@ivar, :@ident].include?(sexp[0]) && sexp[1] !~ SNAKE_CASE
|
|
26
|
+
add_offence(:convention, sexp[2].lineno, ERROR_MESSAGE)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class MethodLength < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Method has too many lines. [%d/%d]'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
def_token_indices(tokens, source).each do |t_ix|
|
|
10
|
+
def_lineno, end_lineno = def_and_end_lines(tokens, t_ix)
|
|
11
|
+
length = calculate_length(def_lineno, end_lineno, source)
|
|
12
|
+
|
|
13
|
+
if length > MethodLength.max
|
|
14
|
+
message = sprintf(ERROR_MESSAGE, length, MethodLength.max)
|
|
15
|
+
add_offence(:convention, def_lineno, message)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.max
|
|
21
|
+
MethodLength.config ? MethodLength.config['Max'] || 10 : 10
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.count_comments?
|
|
25
|
+
if MethodLength.config
|
|
26
|
+
MethodLength.config['CountComments'] || false
|
|
27
|
+
else
|
|
28
|
+
false
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def calculate_length(def_lineno, end_lineno, source)
|
|
35
|
+
lines = source[def_lineno..(end_lineno - 2)].reject(&:empty?)
|
|
36
|
+
unless MethodLength.count_comments?
|
|
37
|
+
lines = lines.reject { |line| line =~ /^\s*#/ }
|
|
38
|
+
end
|
|
39
|
+
lines.size
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def def_token_indices(tokens, source)
|
|
43
|
+
tokens.each_index.select do |ix|
|
|
44
|
+
t = tokens[ix]
|
|
45
|
+
|
|
46
|
+
# Need to check:
|
|
47
|
+
# 1. if the previous character is a ':' to prevent matching ':def'
|
|
48
|
+
# 2. if the method is a one line, which we will ignore
|
|
49
|
+
[t.type, t.text] == [:on_kw, 'def'] &&
|
|
50
|
+
source[t.pos.lineno - 1][t.pos.column - 1] != ':' &&
|
|
51
|
+
source[t.pos.lineno - 1] !~ /^\s*def.*(?:\(.*\)|;).*end\s*$/
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Find the matching 'end' based on the indentation of 'def'
|
|
56
|
+
# Fall back to last token if indentation cannot be matched
|
|
57
|
+
def def_and_end_lines(tokens, t_ix)
|
|
58
|
+
t1 = tokens[t_ix]
|
|
59
|
+
t2 = tokens[(t_ix + 1)..-1].find(-> { tokens[-1] }) do |t|
|
|
60
|
+
[t1.pos.column, t.type, t.text] == [t.pos.column, :on_kw, 'end']
|
|
61
|
+
end
|
|
62
|
+
[t1.pos.lineno, t2.pos.lineno]
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class NewLambdaLiteral < Cop
|
|
6
|
+
ERROR_MESSAGE = 'The new lambda literal syntax is preferred in Ruby 1.9.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:fcall, sexp) do |s|
|
|
10
|
+
if s[1][0..1] == [:@ident, 'lambda']
|
|
11
|
+
add_offence(:convention, s[1][-1].lineno, ERROR_MESSAGE)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/rubocop/cop/offence.rb
CHANGED
|
@@ -14,7 +14,9 @@ module Rubocop
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def to_s
|
|
17
|
-
|
|
17
|
+
# we must be wary of messages containing % in them
|
|
18
|
+
sprintf("#{encode_severity}:%3d: #{message.gsub(/%/, '%%')}",
|
|
19
|
+
line_number)
|
|
18
20
|
end
|
|
19
21
|
|
|
20
22
|
def encode_severity
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class OpMethod < Cop
|
|
6
|
+
ERROR_MESSAGE = 'When defining the %s operator, name its argument other.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:def, sexp) do |s|
|
|
10
|
+
if s[1][0] == :@op && !%w([] []= <<).include?(s[1][1])
|
|
11
|
+
param = s[2][1][1][0]
|
|
12
|
+
|
|
13
|
+
unless param[1] == 'other'
|
|
14
|
+
add_offence(:convention,
|
|
15
|
+
param[2].lineno,
|
|
16
|
+
sprintf(ERROR_MESSAGE, s[1][1]))
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class ParenthesesAroundCondition < Cop
|
|
6
|
+
ERROR_MESSAGE = "Don't use parentheses around the condition of an " +
|
|
7
|
+
'if/unless/while/until, unless the condition contains an assignment.'
|
|
8
|
+
|
|
9
|
+
def inspect(file, source, tokens, sexp)
|
|
10
|
+
[:if, :elsif, :unless, :while, :until,
|
|
11
|
+
:if_mod, :unless_mod, :while_mod, :until_mod].each do |keyword|
|
|
12
|
+
each(keyword, sexp) do |s|
|
|
13
|
+
if s[1][0] == :paren && s[1][1][0][0] != :assign
|
|
14
|
+
positions = all_positions(s[1])
|
|
15
|
+
if positions.first.lineno == positions.last.lineno
|
|
16
|
+
add_offence(:convention, positions.first.lineno, ERROR_MESSAGE)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class PercentLiterals < Cop
|
|
6
|
+
ERROR_MESSAGE = 'The use of %s is discouraged.'
|
|
7
|
+
BAD_LITERALS = {
|
|
8
|
+
on_tstring_beg: '%q',
|
|
9
|
+
on_symbeg: '%s',
|
|
10
|
+
on_backtick: '%x'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
def inspect(file, source, tokens, sexp)
|
|
14
|
+
tokens.each_index do |ix|
|
|
15
|
+
t = tokens[ix]
|
|
16
|
+
bad_token = BAD_LITERALS[t.type]
|
|
17
|
+
if bad_token && t.text.downcase.start_with?(bad_token)
|
|
18
|
+
add_offence(:convention, t.pos.lineno,
|
|
19
|
+
sprintf(ERROR_MESSAGE, t.text[0, 2]))
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class PercentR < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Use %r only for regular expressions matching more ' +
|
|
7
|
+
"than one '/' character."
|
|
8
|
+
|
|
9
|
+
def inspect(file, source, tokens, sexp)
|
|
10
|
+
tokens.each_cons(2) do |t1, t2|
|
|
11
|
+
if t1.type == :on_regexp_beg && t1.text =~ /^%r/ &&
|
|
12
|
+
t2.text !~ %r(/.*/)
|
|
13
|
+
add_offence(:convention, t1.pos.lineno, ERROR_MESSAGE)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class ReduceArguments < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Name reduce arguments |a, e| (accumulator, element)'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
find_reduce_blocks(tokens).each do |reduce_block|
|
|
10
|
+
l_ix, r_ix = reduce_block
|
|
11
|
+
unless arguments_named_properly?(l_ix, r_ix, tokens)
|
|
12
|
+
add_offence(:convention, tokens[l_ix].pos.lineno, ERROR_MESSAGE)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def arguments_named_properly?(l_ix, r_ix, tokens)
|
|
20
|
+
a, e = false, false
|
|
21
|
+
tokens[l_ix..r_ix].each do |t|
|
|
22
|
+
if a == true && [t.type, t.text] == [:on_ident, 'e']
|
|
23
|
+
e = true
|
|
24
|
+
elsif [t.type, t.text] == [:on_ident, 'a']
|
|
25
|
+
a = true
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
a && e
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def find_reduce_blocks(tokens)
|
|
32
|
+
blocks = []
|
|
33
|
+
tokens.each_with_index do |t, ix|
|
|
34
|
+
if [t.type, t.text] == [:on_ident, 'reduce']
|
|
35
|
+
# Make sure we didn't select a :reduce symbol
|
|
36
|
+
next if tokens[ix - 1].text == ':'
|
|
37
|
+
|
|
38
|
+
block = find_brace_block(tokens, ix)
|
|
39
|
+
blocks << block if block
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
blocks
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def find_brace_block(tokens, reduce_ix)
|
|
46
|
+
stack = []
|
|
47
|
+
block = false
|
|
48
|
+
|
|
49
|
+
# When we find the braces we need to add reduce_ix in order to
|
|
50
|
+
# find the real tokens index, since we're looping through a subset
|
|
51
|
+
tokens[reduce_ix..-1].each_with_index do |t, ix|
|
|
52
|
+
break if t.pos.lineno != tokens[reduce_ix].pos.lineno
|
|
53
|
+
if [:on_lbrace, :on_tlambeg].include? t.type
|
|
54
|
+
stack.push ix + reduce_ix
|
|
55
|
+
elsif t.type == :on_rbrace
|
|
56
|
+
left_ix = stack.pop
|
|
57
|
+
ix += reduce_ix
|
|
58
|
+
block = [left_ix, ix] if (stack.empty? &&
|
|
59
|
+
tokens[left_ix].type != :on_tlambeg)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
block
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class RescueException < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Avoid rescuing the Exception class.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:rescue, sexp) do |s|
|
|
10
|
+
# TODO Improve handling of rescue One, Two => e
|
|
11
|
+
if valid_case?(s)
|
|
12
|
+
target_class = s[1][0][1][1]
|
|
13
|
+
|
|
14
|
+
lineno = s[1][0][1][2].lineno
|
|
15
|
+
|
|
16
|
+
add_offence(:warning,
|
|
17
|
+
lineno,
|
|
18
|
+
ERROR_MESSAGE) if target_class == 'Exception'
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def valid_case?(s)
|
|
24
|
+
if s[1].nil?
|
|
25
|
+
# rescue with no args
|
|
26
|
+
false
|
|
27
|
+
elsif s[1][0] == :mrhs_new_from_args
|
|
28
|
+
# rescue One, Two => e
|
|
29
|
+
false
|
|
30
|
+
elsif s[1][0][0] == :const_path_ref
|
|
31
|
+
# rescue Module::Class
|
|
32
|
+
false
|
|
33
|
+
else
|
|
34
|
+
true
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module Rubocop
|
|
4
|
+
module Cop
|
|
5
|
+
class RescueModifier < Cop
|
|
6
|
+
ERROR_MESSAGE = 'Avoid using rescue in its modifier form.'
|
|
7
|
+
|
|
8
|
+
def inspect(file, source, tokens, sexp)
|
|
9
|
+
each(:rescue_mod, sexp) do |s|
|
|
10
|
+
ident = find_first(:@ident, s)
|
|
11
|
+
lineno = ident ? ident[2].lineno : nil
|
|
12
|
+
|
|
13
|
+
add_offence(:convention,
|
|
14
|
+
lineno,
|
|
15
|
+
ERROR_MESSAGE) if lineno
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|