rubocop 0.9.1 → 0.10.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.
- data/.travis.yml +3 -1
- data/CHANGELOG.md +38 -0
- data/README.md +34 -0
- data/Rakefile +3 -0
- data/config/default.yml +14 -1
- data/config/enabled.yml +30 -7
- data/lib/rubocop.rb +15 -0
- data/lib/rubocop/cli.rb +48 -154
- data/lib/rubocop/config.rb +19 -22
- data/lib/rubocop/config_store.rb +2 -4
- data/lib/rubocop/cop/commissioner.rb +90 -0
- data/lib/rubocop/cop/cop.rb +38 -31
- data/lib/rubocop/cop/corrector.rb +84 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
- data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
- data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
- data/lib/rubocop/cop/lint/eval.rb +0 -2
- data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
- data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
- data/lib/rubocop/cop/lint/loop.rb +0 -2
- data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
- data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +0 -2
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/rails/validation.rb +2 -1
- data/lib/rubocop/cop/style/access_control.rb +4 -3
- data/lib/rubocop/cop/style/alias.rb +2 -4
- data/lib/rubocop/cop/style/align_parameters.rb +0 -2
- data/lib/rubocop/cop/style/and_or.rb +4 -6
- data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
- data/lib/rubocop/cop/style/attr.rb +0 -2
- data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
- data/lib/rubocop/cop/style/avoid_for.rb +0 -2
- data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
- data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
- data/lib/rubocop/cop/style/begin_block.rb +0 -2
- data/lib/rubocop/cop/style/block_comments.rb +2 -2
- data/lib/rubocop/cop/style/block_nesting.rb +3 -3
- data/lib/rubocop/cop/style/blocks.rb +0 -2
- data/lib/rubocop/cop/style/case_equality.rb +0 -2
- data/lib/rubocop/cop/style/case_indentation.rb +0 -2
- data/lib/rubocop/cop/style/character_literal.rb +10 -6
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +3 -5
- data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
- data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
- data/lib/rubocop/cop/style/constant_name.rb +0 -2
- data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
- data/lib/rubocop/cop/style/documentation.rb +6 -2
- data/lib/rubocop/cop/style/dot_position.rb +0 -2
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
- data/lib/rubocop/cop/style/empty_lines.rb +10 -8
- data/lib/rubocop/cop/style/empty_literal.rb +3 -1
- data/lib/rubocop/cop/style/encoding.rb +7 -6
- data/lib/rubocop/cop/style/end_block.rb +0 -2
- data/lib/rubocop/cop/style/end_of_line.rb +4 -3
- data/lib/rubocop/cop/style/favor_join.rb +0 -2
- data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
- data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
- data/lib/rubocop/cop/style/if_then_else.rb +0 -2
- data/lib/rubocop/cop/style/lambda.rb +0 -2
- data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/style/line_continuation.rb +4 -3
- data/lib/rubocop/cop/style/line_length.rb +4 -3
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
- data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
- data/lib/rubocop/cop/style/method_length.rb +0 -4
- data/lib/rubocop/cop/style/not.rb +0 -2
- data/lib/rubocop/cop/style/op_method.rb +0 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
- data/lib/rubocop/cop/style/proc.rb +0 -2
- data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
- data/lib/rubocop/cop/style/redundant_return.rb +59 -0
- data/lib/rubocop/cop/style/redundant_self.rb +83 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
- data/lib/rubocop/cop/style/semicolon.rb +15 -9
- data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
- data/lib/rubocop/cop/style/string_literals.rb +5 -2
- data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
- data/lib/rubocop/cop/style/tab.rb +4 -3
- data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
- data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
- data/lib/rubocop/cop/style/unless_else.rb +0 -2
- data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
- data/lib/rubocop/cop/style/when_then.rb +3 -3
- data/lib/rubocop/cop/style/while_until_do.rb +3 -5
- data/lib/rubocop/cop/style/word_array.rb +0 -2
- data/lib/rubocop/cop/util.rb +0 -4
- data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/processed_source.rb +27 -0
- data/lib/rubocop/rake_task.rb +50 -0
- data/lib/rubocop/source_parser.rb +105 -0
- data/lib/rubocop/target_finder.rb +67 -0
- data/lib/rubocop/token.rb +22 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +5 -3
- data/spec/project_spec.rb +0 -11
- data/spec/rubocop/cli_spec.rb +112 -6
- data/spec/rubocop/config_spec.rb +13 -17
- data/spec/rubocop/config_store_spec.rb +8 -23
- data/spec/rubocop/cops/commissioner_spec.rb +72 -0
- data/spec/rubocop/cops/corrector_spec.rb +63 -0
- data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
- data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
- data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
- data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
- data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
- data/spec/rubocop/cops/offence_spec.rb +28 -0
- data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
- data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
- data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
- data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
- data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
- data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
- data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
- data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
- data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
- data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
- data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
- data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
- data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
- data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
- data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
- data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
- data/spec/rubocop/processed_source_spec.rb +67 -0
- data/spec/rubocop/source_parser_spec.rb +141 -0
- data/spec/rubocop/target_finder_spec.rb +180 -0
- data/spec/rubocop/token_spec.rb +27 -0
- data/spec/spec_helper.rb +24 -4
- metadata +108 -18
- checksums.yaml +0 -7
@@ -8,8 +8,8 @@ module Rubocop
|
|
8
8
|
class AsciiComments < Cop
|
9
9
|
MSG = 'Use only ascii symbols in comments.'
|
10
10
|
|
11
|
-
def
|
12
|
-
comments.each do |comment|
|
11
|
+
def investigate(processed_source)
|
12
|
+
processed_source.comments.each do |comment|
|
13
13
|
if comment.text =~ /[^\x00-\x7f]/
|
14
14
|
add_offence(:convention, comment.loc, MSG)
|
15
15
|
end
|
@@ -9,8 +9,8 @@ module Rubocop
|
|
9
9
|
class AsciiIdentifiers < Cop
|
10
10
|
MSG = 'Use only ascii symbols in identifiers.'
|
11
11
|
|
12
|
-
def
|
13
|
-
tokens.each do |t|
|
12
|
+
def investigate(processed_source)
|
13
|
+
processed_source.tokens.each do |t|
|
14
14
|
if t.type == :tIDENTIFIER && t.text =~ /[^\x00-\x7f]/
|
15
15
|
add_offence(:convention, t.pos, MSG)
|
16
16
|
end
|
@@ -34,26 +34,22 @@ module Rubocop
|
|
34
34
|
$' $POSTMATCH
|
35
35
|
$+ $LAST_PAREN_MATCH
|
36
36
|
$stdin $stdout $stderr
|
37
|
-
$DEBUG $FILENAME $VERBOSE
|
37
|
+
$DEBUG $FILENAME $VERBOSE $SAFE
|
38
38
|
$-0 $-a $-d $-F $-i $-I $-l $-p $-v $-w
|
39
|
-
)
|
39
|
+
).map(&:to_sym)
|
40
40
|
|
41
41
|
def on_gvar(node)
|
42
42
|
check(node)
|
43
|
-
|
44
|
-
super
|
45
43
|
end
|
46
44
|
|
47
45
|
def on_gvasgn(node)
|
48
46
|
check(node)
|
49
|
-
|
50
|
-
super
|
51
47
|
end
|
52
48
|
|
53
49
|
def check(node)
|
54
50
|
global_var, = *node
|
55
51
|
|
56
|
-
unless BUILT_IN_VARS.include?(global_var
|
52
|
+
unless BUILT_IN_VARS.include?(global_var)
|
57
53
|
add_offence(:convention,
|
58
54
|
node.loc.name,
|
59
55
|
MSG)
|
@@ -28,11 +28,11 @@ module Rubocop
|
|
28
28
|
'$`' => '$PREMATCH from English library',
|
29
29
|
'$\'' => '$POSTMATCH from English library',
|
30
30
|
'$+' => '$LAST_PAREN_MATCH from English library'
|
31
|
-
}
|
31
|
+
}.symbolize_keys
|
32
32
|
|
33
33
|
def on_gvar(node)
|
34
34
|
global_var, = *node
|
35
|
-
global_var = global_var
|
35
|
+
global_var = global_var
|
36
36
|
|
37
37
|
if PREFERRED_VARS[global_var]
|
38
38
|
add_offence(
|
@@ -41,8 +41,6 @@ module Rubocop
|
|
41
41
|
"Prefer #{PREFERRED_VARS[global_var]} over #{global_var}."
|
42
42
|
)
|
43
43
|
end
|
44
|
-
|
45
|
-
super
|
46
44
|
end
|
47
45
|
end
|
48
46
|
end
|
@@ -7,8 +7,8 @@ module Rubocop
|
|
7
7
|
class BlockComments < Cop
|
8
8
|
MSG = 'Do not use block comments.'
|
9
9
|
|
10
|
-
def
|
11
|
-
comments.each do |comment|
|
10
|
+
def investigate(processed_source)
|
11
|
+
processed_source.comments.each do |comment|
|
12
12
|
if comment.text.start_with?('=begin')
|
13
13
|
add_offence(:convention, comment.loc, MSG)
|
14
14
|
end
|
@@ -12,10 +12,10 @@ module Rubocop
|
|
12
12
|
NESTING_BLOCKS = [:case, :if, :while, :while_post, :until, :until_post,
|
13
13
|
:for, :resbody]
|
14
14
|
|
15
|
-
def
|
16
|
-
return unless ast
|
15
|
+
def investigate(processed_source)
|
16
|
+
return unless processed_source.ast
|
17
17
|
max = BlockNesting.config['Max']
|
18
|
-
check_nesting_level(ast, max, 0)
|
18
|
+
check_nesting_level(processed_source.ast, max, 0)
|
19
19
|
end
|
20
20
|
|
21
21
|
private
|
@@ -11,9 +11,11 @@ module Rubocop
|
|
11
11
|
# Constants like __FILE__ are handled as strings,
|
12
12
|
# but don't respond to begin.
|
13
13
|
return unless node.loc.respond_to?(:begin)
|
14
|
+
return if part_of_ignored_node?(node)
|
14
15
|
|
15
16
|
# we don't register an offence for things like ?\C-\M-d
|
16
|
-
if node.loc.begin.is?('?') &&
|
17
|
+
if node.loc.begin.is?('?') &&
|
18
|
+
node.loc.expression.source.size.between?(2, 3)
|
17
19
|
add_offence(:convention, node.loc.expression, MSG)
|
18
20
|
do_autocorrect(node)
|
19
21
|
end
|
@@ -23,12 +25,14 @@ module Rubocop
|
|
23
25
|
alias_method :on_regexp, :ignore_node
|
24
26
|
|
25
27
|
def autocorrect_action(node)
|
26
|
-
|
28
|
+
@corrections << lambda do |corrector|
|
29
|
+
string = node.loc.expression.source[1..-1]
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
31
|
+
if string.length == 1 # normal character
|
32
|
+
corrector.replace(node.loc.expression, "'#{string}'")
|
33
|
+
elsif string.length == 2 # special character like \n
|
34
|
+
corrector.replace(node.loc.expression, %Q("#{string}"))
|
35
|
+
end
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
@@ -13,15 +13,15 @@ module Rubocop
|
|
13
13
|
MSG = 'Prefer %s over %s.'
|
14
14
|
|
15
15
|
def self.preferred_methods
|
16
|
-
|
16
|
+
if config['PreferredMethods']
|
17
|
+
config['PreferredMethods'].symbolize_keys
|
18
|
+
end
|
17
19
|
end
|
18
20
|
|
19
21
|
def on_block(node)
|
20
22
|
method, _args, _body = *node
|
21
23
|
|
22
24
|
check_method_node(method)
|
23
|
-
|
24
|
-
super
|
25
25
|
end
|
26
26
|
|
27
27
|
def on_send(node)
|
@@ -30,8 +30,6 @@ module Rubocop
|
|
30
30
|
if args.size == 1 && args.first.type == :block_pass
|
31
31
|
check_method_node(node)
|
32
32
|
end
|
33
|
-
|
34
|
-
super
|
35
33
|
end
|
36
34
|
|
37
35
|
private
|
@@ -16,12 +16,12 @@ module Rubocop
|
|
16
16
|
add_offence(:convention, node.loc.dot, MSG)
|
17
17
|
do_autocorrect(node)
|
18
18
|
end
|
19
|
-
|
20
|
-
super
|
21
19
|
end
|
22
20
|
|
23
21
|
def autocorrect_action(node)
|
24
|
-
|
22
|
+
@corrections << lambda do |corrector|
|
23
|
+
corrector.replace(node.loc.dot, '.')
|
24
|
+
end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks that comment annotation keywords are written according
|
7
|
+
# to guidelines.
|
8
|
+
class CommentAnnotation < Cop
|
9
|
+
MSG = 'Annotation keywords shall be all upper case, followed by a ' +
|
10
|
+
'colon and a space, then a note describing the problem.'
|
11
|
+
KEYWORDS = %w(TODO FIXME OPTIMIZE HACK REVIEW)
|
12
|
+
|
13
|
+
def investigate(processed_source)
|
14
|
+
processed_source.comments.each do |comment|
|
15
|
+
match = comment.text.match(/^(# ?)([A-Za-z]+)(\s*:)?(\s+)?(\S+)?/)
|
16
|
+
if match
|
17
|
+
margin, first_word, colon, space, note = *match.captures
|
18
|
+
if annotation?(first_word, colon, space, note) &&
|
19
|
+
!correct_annotation?(first_word, colon, space, note)
|
20
|
+
start = comment.loc.begin_pos + margin.length
|
21
|
+
length = first_word.length + (colon || '').length
|
22
|
+
range = Parser::Source::Range.new(processed_source.buffer,
|
23
|
+
start,
|
24
|
+
start + length)
|
25
|
+
add_offence(:convention, range, MSG)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def annotation?(first_word, colon, space, note)
|
34
|
+
KEYWORDS.include?(first_word.upcase) && (colon || space || !note)
|
35
|
+
end
|
36
|
+
|
37
|
+
def correct_annotation?(first_word, colon, space, note)
|
38
|
+
KEYWORDS.include?(first_word) &&
|
39
|
+
(colon && space && note || !colon && !note)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -20,8 +20,6 @@ module Rubocop
|
|
20
20
|
if args.children == [] && args.loc.begin
|
21
21
|
add_offence(:convention, args.loc.begin, MSG)
|
22
22
|
end
|
23
|
-
|
24
|
-
super
|
25
23
|
end
|
26
24
|
|
27
25
|
def on_defs(node)
|
@@ -34,8 +32,6 @@ module Rubocop
|
|
34
32
|
if args.children == [] && args.loc.begin
|
35
33
|
add_offence(:convention, args.loc.begin, MSG)
|
36
34
|
end
|
37
|
-
|
38
|
-
super
|
39
35
|
end
|
40
36
|
end
|
41
37
|
|
@@ -51,8 +47,6 @@ module Rubocop
|
|
51
47
|
if args.children.size > 0 && args.loc.begin.nil?
|
52
48
|
add_offence(:convention, args.loc.expression, MSG)
|
53
49
|
end
|
54
|
-
|
55
|
-
super
|
56
50
|
end
|
57
51
|
|
58
52
|
def on_defs(node)
|
@@ -61,8 +55,6 @@ module Rubocop
|
|
61
55
|
if args.children.size > 0 && args.loc.begin.nil?
|
62
56
|
add_offence(:convention, args.loc.expression, MSG)
|
63
57
|
end
|
64
|
-
|
65
|
-
super
|
66
58
|
end
|
67
59
|
end
|
68
60
|
end
|
@@ -10,10 +10,14 @@ module Rubocop
|
|
10
10
|
class Documentation < Cop
|
11
11
|
MSG = 'Missing top-level %s documentation comment.'
|
12
12
|
|
13
|
-
def
|
13
|
+
def investigate(processed_source)
|
14
|
+
ast = processed_source.ast
|
14
15
|
return unless ast
|
15
16
|
|
16
|
-
ast_with_comments = Parser::Source::Comment.associate(
|
17
|
+
ast_with_comments = Parser::Source::Comment.associate(
|
18
|
+
ast,
|
19
|
+
processed_source.comments
|
20
|
+
)
|
17
21
|
|
18
22
|
check_classes(ast, ast_with_comments)
|
19
23
|
check_modules(ast, ast_with_comments)
|
@@ -8,12 +8,12 @@ module Rubocop
|
|
8
8
|
MSG = 'Extra blank line detected.'
|
9
9
|
LINE_OFFSET = 2
|
10
10
|
|
11
|
-
def
|
12
|
-
return if tokens.empty?
|
11
|
+
def investigate(processed_source)
|
12
|
+
return if processed_source.tokens.empty?
|
13
13
|
|
14
14
|
prev_line = 1
|
15
15
|
|
16
|
-
tokens.each do |token|
|
16
|
+
processed_source.tokens.each do |token|
|
17
17
|
cur_line = token.pos.line
|
18
18
|
line_diff = cur_line - prev_line
|
19
19
|
|
@@ -22,11 +22,13 @@ module Rubocop
|
|
22
22
|
# don't show up in the tokens
|
23
23
|
((prev_line + 1)...cur_line).each do |line|
|
24
24
|
# we check if the prev and current lines are empty
|
25
|
-
if
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
if processed_source[line - 2].empty? &&
|
26
|
+
processed_source[line - 1].empty?
|
27
|
+
range = source_range(processed_source.buffer,
|
28
|
+
processed_source[0...(line - 1)],
|
29
|
+
0,
|
30
|
+
1)
|
31
|
+
add_offence(:convention, range, MSG)
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|