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.

Files changed (184) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -1
  3. data/CHANGELOG.md +19 -0
  4. data/README.md +4 -8
  5. data/bin/rubocop +2 -2
  6. data/config/default.yml +8 -0
  7. data/config/enabled.yml +21 -24
  8. data/lib/rubocop.rb +9 -7
  9. data/lib/rubocop/cli.rb +73 -52
  10. data/lib/rubocop/config.rb +8 -5
  11. data/lib/rubocop/cop/access_control.rb +41 -0
  12. data/lib/rubocop/cop/alias.rb +7 -5
  13. data/lib/rubocop/cop/align_parameters.rb +20 -96
  14. data/lib/rubocop/cop/and_or.rb +26 -0
  15. data/lib/rubocop/cop/ascii_comments.rb +3 -8
  16. data/lib/rubocop/cop/ascii_identifiers.rb +6 -5
  17. data/lib/rubocop/cop/avoid_class_vars.rb +5 -10
  18. data/lib/rubocop/cop/avoid_for.rb +7 -5
  19. data/lib/rubocop/cop/avoid_global_vars.rb +19 -7
  20. data/lib/rubocop/cop/avoid_perl_backrefs.rb +7 -10
  21. data/lib/rubocop/cop/avoid_perlisms.rb +11 -10
  22. data/lib/rubocop/cop/block_comments.rb +4 -6
  23. data/lib/rubocop/cop/blocks.rb +11 -47
  24. data/lib/rubocop/cop/case_indentation.rb +9 -31
  25. data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -11
  26. data/lib/rubocop/cop/class_methods.rb +5 -10
  27. data/lib/rubocop/cop/collection_methods.rb +16 -16
  28. data/lib/rubocop/cop/colon_method_call.rb +8 -32
  29. data/lib/rubocop/cop/constant_name.rb +24 -0
  30. data/lib/rubocop/cop/cop.rb +20 -78
  31. data/lib/rubocop/cop/def_parentheses.rb +43 -35
  32. data/lib/rubocop/cop/empty_line_between_defs.rb +11 -15
  33. data/lib/rubocop/cop/empty_lines.rb +20 -9
  34. data/lib/rubocop/cop/empty_literal.rb +47 -0
  35. data/lib/rubocop/cop/encoding.rb +3 -3
  36. data/lib/rubocop/cop/end_of_line.rb +3 -3
  37. data/lib/rubocop/cop/ensure_return.rb +6 -23
  38. data/lib/rubocop/cop/eval.rb +7 -10
  39. data/lib/rubocop/cop/favor_join.rb +9 -24
  40. data/lib/rubocop/cop/favor_modifier.rb +38 -48
  41. data/lib/rubocop/cop/favor_percent_r.rb +7 -7
  42. data/lib/rubocop/cop/favor_sprintf.rb +8 -24
  43. data/lib/rubocop/cop/favor_unless_over_negated_if.rb +19 -17
  44. data/lib/rubocop/cop/handle_exceptions.rb +7 -11
  45. data/lib/rubocop/cop/hash_syntax.rb +29 -14
  46. data/lib/rubocop/cop/if_then_else.rb +32 -29
  47. data/lib/rubocop/cop/leading_comment_space.rb +5 -8
  48. data/lib/rubocop/cop/line_continuation.rb +4 -7
  49. data/lib/rubocop/cop/line_length.rb +3 -3
  50. data/lib/rubocop/cop/loop.rb +33 -0
  51. data/lib/rubocop/cop/method_and_variable_snake_case.rb +42 -19
  52. data/lib/rubocop/cop/method_length.rb +34 -37
  53. data/lib/rubocop/cop/new_lambda_literal.rb +8 -6
  54. data/lib/rubocop/cop/not.rb +10 -4
  55. data/lib/rubocop/cop/numeric_literals.rb +9 -7
  56. data/lib/rubocop/cop/offence.rb +1 -1
  57. data/lib/rubocop/cop/op_method.rb +12 -22
  58. data/lib/rubocop/cop/parameter_lists.rb +12 -6
  59. data/lib/rubocop/cop/parentheses_around_condition.rb +11 -11
  60. data/lib/rubocop/cop/percent_r.rb +7 -7
  61. data/lib/rubocop/cop/reduce_arguments.rb +13 -51
  62. data/lib/rubocop/cop/rescue_exception.rb +13 -29
  63. data/lib/rubocop/cop/rescue_modifier.rb +5 -8
  64. data/lib/rubocop/cop/semicolon.rb +15 -74
  65. data/lib/rubocop/cop/single_line_methods.rb +28 -44
  66. data/lib/rubocop/cop/space_after_comma_etc.rb +29 -9
  67. data/lib/rubocop/cop/space_after_control_keyword.rb +16 -15
  68. data/lib/rubocop/cop/string_literals.rb +9 -35
  69. data/lib/rubocop/cop/surrounding_space.rb +213 -112
  70. data/lib/rubocop/cop/symbol_array.rb +9 -7
  71. data/lib/rubocop/cop/symbol_name.rb +23 -0
  72. data/lib/rubocop/cop/syntax.rb +14 -7
  73. data/lib/rubocop/cop/tab.rb +3 -3
  74. data/lib/rubocop/cop/ternary_operator.rb +26 -24
  75. data/lib/rubocop/cop/trailing_whitespace.rb +3 -5
  76. data/lib/rubocop/cop/trivial_accessors.rb +18 -95
  77. data/lib/rubocop/cop/unless_else.rb +11 -7
  78. data/lib/rubocop/cop/util.rb +26 -0
  79. data/lib/rubocop/cop/variable_interpolation.rb +18 -10
  80. data/lib/rubocop/cop/when_then.rb +6 -17
  81. data/lib/rubocop/cop/word_array.rb +18 -19
  82. data/lib/rubocop/version.rb +1 -1
  83. data/rubocop.gemspec +1 -0
  84. data/spec/project_spec.rb +1 -1
  85. data/spec/rubocop/cli_spec.rb +16 -9
  86. data/spec/rubocop/config_spec.rb +13 -3
  87. data/spec/rubocop/cops/access_control_spec.rb +129 -0
  88. data/spec/rubocop/cops/alias_spec.rb +2 -6
  89. data/spec/rubocop/cops/align_parameters_spec.rb +58 -71
  90. data/spec/rubocop/cops/and_or_spec.rb +37 -0
  91. data/spec/rubocop/cops/ascii_comments_spec.rb +3 -4
  92. data/spec/rubocop/cops/ascii_identifiers_spec.rb +3 -4
  93. data/spec/rubocop/cops/avoid_class_vars_spec.rb +7 -2
  94. data/spec/rubocop/cops/avoid_for_spec.rb +1 -4
  95. data/spec/rubocop/cops/{avoid_global_vars.rb → avoid_global_vars_spec.rb} +4 -4
  96. data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +1 -1
  97. data/spec/rubocop/cops/avoid_perlisms_spec.rb +5 -5
  98. data/spec/rubocop/cops/block_comments_spec.rb +0 -4
  99. data/spec/rubocop/cops/blocks_spec.rb +33 -0
  100. data/spec/rubocop/cops/case_indentation_spec.rb +5 -5
  101. data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +15 -5
  102. data/spec/rubocop/cops/class_methods_spec.rb +4 -4
  103. data/spec/rubocop/cops/collection_methods_spec.rb +9 -4
  104. data/spec/rubocop/cops/colon_method_call_spec.rb +11 -5
  105. data/spec/rubocop/cops/constant_name_spec.rb +42 -0
  106. data/spec/rubocop/cops/def_with_parentheses_spec.rb +13 -8
  107. data/spec/rubocop/cops/def_without_parentheses_spec.rb +11 -5
  108. data/spec/rubocop/cops/empty_line_between_defs_spec.rb +38 -38
  109. data/spec/rubocop/cops/empty_lines_spec.rb +15 -3
  110. data/spec/rubocop/cops/empty_literal_spec.rb +90 -0
  111. data/spec/rubocop/cops/encoding_spec.rb +9 -9
  112. data/spec/rubocop/cops/end_of_line_spec.rb +2 -2
  113. data/spec/rubocop/cops/ensure_return_spec.rb +1 -3
  114. data/spec/rubocop/cops/eval_spec.rb +8 -5
  115. data/spec/rubocop/cops/favor_join_spec.rb +1 -5
  116. data/spec/rubocop/cops/favor_modifier_spec.rb +16 -14
  117. data/spec/rubocop/cops/{favor_percent_r.rb → favor_percent_r_spec.rb} +6 -6
  118. data/spec/rubocop/cops/favor_sprintf_spec.rb +3 -9
  119. data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +4 -4
  120. data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +3 -3
  121. data/spec/rubocop/cops/handle_exceptions_spec.rb +1 -3
  122. data/spec/rubocop/cops/hash_syntax_spec.rb +11 -6
  123. data/spec/rubocop/cops/if_with_semicolon_spec.rb +7 -1
  124. data/spec/rubocop/cops/leading_comment_space_spec.rb +0 -7
  125. data/spec/rubocop/cops/line_continuation_spec.rb +2 -2
  126. data/spec/rubocop/cops/line_length_spec.rb +2 -2
  127. data/spec/rubocop/cops/loop_spec.rb +31 -0
  128. data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +38 -12
  129. data/spec/rubocop/cops/method_length_spec.rb +85 -85
  130. data/spec/rubocop/cops/multiline_if_then_spec.rb +15 -15
  131. data/spec/rubocop/cops/new_lambda_literal_spec.rb +3 -3
  132. data/spec/rubocop/cops/not_spec.rb +1 -4
  133. data/spec/rubocop/cops/numeric_literals_spec.rb +13 -13
  134. data/spec/rubocop/cops/one_line_conditional_spec.rb +1 -1
  135. data/spec/rubocop/cops/op_method_spec.rb +2 -9
  136. data/spec/rubocop/cops/parameter_lists_spec.rb +7 -7
  137. data/spec/rubocop/cops/parentheses_around_condition_spec.rb +41 -44
  138. data/spec/rubocop/cops/percent_r_spec.rb +6 -6
  139. data/spec/rubocop/cops/reduce_arguments_spec.rb +4 -4
  140. data/spec/rubocop/cops/rescue_exception_spec.rb +48 -8
  141. data/spec/rubocop/cops/rescue_modifier_spec.rb +2 -5
  142. data/spec/rubocop/cops/semicolon_spec.rb +2 -30
  143. data/spec/rubocop/cops/single_line_methods_spec.rb +13 -13
  144. data/spec/rubocop/cops/space_after_colon_spec.rb +3 -3
  145. data/spec/rubocop/cops/space_after_comma_spec.rb +14 -2
  146. data/spec/rubocop/cops/space_after_control_keyword_spec.rb +42 -3
  147. data/spec/rubocop/cops/space_after_semicolon_spec.rb +2 -2
  148. data/spec/rubocop/cops/space_around_braces_spec.rb +18 -3
  149. data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +4 -4
  150. data/spec/rubocop/cops/space_around_operators_spec.rb +82 -27
  151. data/spec/rubocop/cops/space_inside_brackets_spec.rb +13 -7
  152. data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +14 -9
  153. data/spec/rubocop/cops/space_inside_parens_spec.rb +7 -3
  154. data/spec/rubocop/cops/string_literals_spec.rb +17 -5
  155. data/spec/rubocop/cops/symbol_array_spec.rb +18 -2
  156. data/spec/rubocop/cops/symbol_name_spec.rb +119 -0
  157. data/spec/rubocop/cops/syntax_spec.rb +25 -18
  158. data/spec/rubocop/cops/tab_spec.rb +2 -2
  159. data/spec/rubocop/cops/ternary_operator_spec.rb +13 -17
  160. data/spec/rubocop/cops/trailing_whitespace_spec.rb +3 -3
  161. data/spec/rubocop/cops/trivial_accessors_spec.rb +17 -20
  162. data/spec/rubocop/cops/unless_else_spec.rb +8 -8
  163. data/spec/rubocop/cops/variable_interpolation_spec.rb +0 -5
  164. data/spec/rubocop/cops/when_then_spec.rb +14 -21
  165. data/spec/rubocop/cops/word_array_spec.rb +12 -4
  166. data/spec/spec_helper.rb +12 -4
  167. metadata +40 -31
  168. data/.document +0 -5
  169. data/lib/rubocop/cop/ampersands_pipes_vs_and_or.rb +0 -25
  170. data/lib/rubocop/cop/array_literal.rb +0 -61
  171. data/lib/rubocop/cop/brace_after_percent.rb +0 -32
  172. data/lib/rubocop/cop/grammar.rb +0 -138
  173. data/lib/rubocop/cop/hash_literal.rb +0 -61
  174. data/lib/rubocop/cop/percent_literals.rb +0 -25
  175. data/lib/rubocop/cop/symbol_snake_case.rb +0 -47
  176. data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +0 -57
  177. data/spec/rubocop/cops/array_literal_spec.rb +0 -46
  178. data/spec/rubocop/cops/brace_after_percent_spec.rb +0 -33
  179. data/spec/rubocop/cops/grammar_spec.rb +0 -81
  180. data/spec/rubocop/cops/hash_literal_spec.rb +0 -46
  181. data/spec/rubocop/cops/multiline_blocks_spec.rb +0 -24
  182. data/spec/rubocop/cops/percent_literals_spec.rb +0 -47
  183. data/spec/rubocop/cops/single_line_blocks_spec.rb +0 -22
  184. data/spec/rubocop/cops/symbol_snake_case_spec.rb +0 -93
data/.document DELETED
@@ -1,5 +0,0 @@
1
- lib/**/*.rb
2
- bin/*
3
- -
4
- features/**/*.feature
5
- LICENSE.txt
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Rubocop
4
- module Cop
5
- class AmpersandsPipesVsAndOr < Cop
6
- ERROR_MESSAGE =
7
- 'Use &&/|| for boolean expressions, and/or for control flow.'
8
-
9
- def inspect(file, source, tokens, sexp)
10
- [:if, :unless, :while, :until].each { |keyword| check(keyword, sexp) }
11
- end
12
-
13
- def check(keyword, sexp)
14
- each(keyword, sexp) do |sub_sexp|
15
- condition = sub_sexp[1]
16
- if condition[0] == :binary && [:and, :or].include?(condition[2])
17
- add_offence(:convention,
18
- sub_sexp.flatten.grep(Position).first.lineno,
19
- ERROR_MESSAGE)
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,61 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Rubocop
4
- module Cop
5
- class ArrayLiteral < Cop
6
- ERROR_MESSAGE = 'Use array literal [] instead of Array.new.'
7
-
8
- def inspect(file, source, tokens, sexp)
9
- offences = preliminary_scan(sexp)
10
-
11
- # find Array.new()
12
- each(:method_add_arg, sexp) do |s|
13
- next if s[1][0] != :call
14
-
15
- receiver = s[1][1][1]
16
- method_name = s[1][3][1]
17
-
18
- if receiver && receiver[1] == 'Array' &&
19
- method_name == 'new' && s[2] == [:arg_paren, nil]
20
- offences.delete(Offence.new(:convention,
21
- receiver[2].lineno,
22
- ERROR_MESSAGE))
23
- add_offence(:convention,
24
- receiver[2].lineno,
25
- ERROR_MESSAGE)
26
- end
27
-
28
- # check for false positives
29
- if receiver && receiver[1] == 'Array' &&
30
- method_name == 'new' && s[2] != [:arg_paren, nil]
31
- offences.delete(Offence.new(:convention,
32
- receiver[2].lineno,
33
- ERROR_MESSAGE))
34
- end
35
- end
36
-
37
- offences.each { |o| add_offence(*o.explode) }
38
- end
39
-
40
- def preliminary_scan(sexp)
41
- offences = []
42
-
43
- # find Array.new
44
- # there will be some false positives here, which
45
- # we'll eliminate later on
46
- each(:call, sexp) do |s|
47
- receiver = s[1][1]
48
-
49
- if receiver && receiver[1] == 'Array' &&
50
- s[3][1] == 'new'
51
- offences << Offence.new(:convention,
52
- receiver[2].lineno,
53
- ERROR_MESSAGE)
54
- end
55
- end
56
-
57
- offences
58
- end
59
- end
60
- end
61
- end
@@ -1,32 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Rubocop
4
- module Cop
5
- class BraceAfterPercent < Cop
6
- ERROR_MESSAGE = 'Prefer () as delimiters for all % literals.'
7
- LITERALS = {
8
- on_tstring_beg: ['%q', '%Q'],
9
- on_words_beg: '%W',
10
- on_qwords_beg: '%w',
11
- on_qsymbols_beg: '%i',
12
- on_symbols_beg: '%I',
13
- on_regexp_beg: '%r',
14
- on_symbeg: '%s',
15
- on_backtick: '%x'
16
- }
17
-
18
- def inspect(file, source, tokens, sexp)
19
- tokens.each_index do |ix|
20
- t = tokens[ix]
21
- literals = Array(LITERALS[t.type])
22
- literals.each do |literal|
23
- if literal && t.text.start_with?(literal) && t.text[2] != '('
24
- add_offence(:convention, t.pos.lineno,
25
- ERROR_MESSAGE)
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,138 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Rubocop
4
- module Cop
5
- class Grammar
6
- def initialize(tokens)
7
- @tokens_without_pos = tokens.map { |t| [t.type, t.text] }
8
- process_embedded_expressions if RUBY_VERSION < '2.0'
9
- @token_indexes = {}
10
- @tokens_without_pos.each_with_index do |t, i|
11
- @token_indexes[t] ||= []
12
- @token_indexes[t] << i
13
- end
14
- @ix = 0
15
- @table = {}
16
- token_positions = tokens.map { |t| [t.pos.lineno, t.pos.column] }
17
- @index_by_pos = Hash[*token_positions.each_with_index.to_a.flatten(1)]
18
- @special = {
19
- assign: [[:on_op, '=']],
20
- brace_block: [[:on_lbrace, '{']],
21
- hash: [[:on_lbrace, '{']],
22
- ifop: [[:on_op, '?'], [:on_op, ':']]
23
- }
24
- end
25
-
26
- # In ruby 1.9.3 and below, the string "#{x}" will give the tokens
27
- # [:on_tstring_beg, '"'], [:on_embexpr_beg, '#{'], [:on_ident, 'x'],
28
- # [:on_rbrace, '}'], [:on_tstring_end, '"']
29
- # which is not so good for us. We want to distinguish between a
30
- # right brace that ends an embedded expression inside a string
31
- # and an ordinary right brace. So we replace :on_rbrace with the
32
- # made up :on_embexpr_end.
33
- def process_embedded_expressions
34
- state = :outside
35
- brace_depth = 0
36
- @tokens_without_pos.each_with_index do |(type, _), ix|
37
- case state
38
- when :outside
39
- state = :inside_string if type == :on_tstring_beg
40
- when :inside_string
41
- case type
42
- when :on_tstring_end
43
- state = :outside
44
- when :on_embexpr_beg
45
- brace_depth = 1
46
- state = :inside_expr
47
- end
48
- when :inside_expr
49
- case type
50
- when :on_lbrace, :on_embexpr_end
51
- brace_depth += 1
52
- when :on_rbrace
53
- if brace_depth == 1
54
- @tokens_without_pos[ix][0] = :on_embexpr_end
55
- state = :inside_string
56
- end
57
- brace_depth -= 1
58
- end
59
- end
60
- end
61
- end
62
-
63
- # Returns a hash mapping indexes in the token array to grammar
64
- # paths, e.g.:
65
- # { 0 => [:program, :assign, :var_field, :@ident],
66
- # 1 => [:program, :assign],
67
- # 2 => [:program, :assign, :@int],
68
- # 4 => [:program, :assign, :var_field, :@ident],
69
- # 5 => [:program, :assign],
70
- # 7 => [:program, :assign, :@int],
71
- # 9 => [:program, :assign, :var_field, :@ident],
72
- # 11 => [:program, :assign],
73
- # 12 => [:program, :assign, :@int] }
74
- def correlate(sexp, path = [])
75
- case sexp
76
- when Array
77
- case sexp[0]
78
- when /^@/
79
- # Leaves in the grammar have a corresponding token with a
80
- # position, which we search for and advance @ix.
81
- @ix = @index_by_pos[[sexp[-1].lineno, sexp[-1].column]]
82
- fail "#{sexp}\n#{@index_by_pos}" unless @ix
83
- @table[@ix] = path + [sexp[0]]
84
- @ix += 1
85
- when *@special.keys
86
- # Here we don't advance @ix because there may be other
87
- # tokens inbetween the current one and the one we get from
88
- # @special.
89
- @special[sexp[0]].each do |token_to_find|
90
- find(path, sexp, token_to_find)
91
- end
92
- when :block_var # "{ |...|" or "do |...|"
93
- @ix = find(path, sexp, [:on_op, '|']) + 1
94
- find(path, sexp, [:on_op, '|'])
95
- end
96
- path += [sexp[0]] if Symbol === sexp[0]
97
- # Compensate for reverse order of if/unless/while/until modifier.
98
- modifiers = [:if_mod, :unless_mod, :while_mod, :until_mod]
99
- children = modifiers.include?(sexp[0]) ? sexp.reverse : sexp
100
-
101
- children.each do |elem|
102
- case elem
103
- when Array then correlate(elem, path) # Dive deeper
104
- when Symbol
105
- unless elem.to_s =~ /^@?[a-z_]+$/
106
- # There's a trailing @ in some symbols in sexp,
107
- # e.g. :-@, that don't appear in tokens. That's why we
108
- # chomp it off.
109
- find(path, [elem], [:on_op, elem.to_s.chomp('@')])
110
- end
111
- end
112
- end
113
- end
114
- @table
115
- end
116
-
117
- private
118
-
119
- def find(path, sexp, token_to_find)
120
- indices = @token_indexes[token_to_find] or return
121
- ix = indices.find { |i| i >= @ix } or return
122
- @table[ix] = path + [sexp[0]]
123
- add_matching_rbrace(ix) if token_to_find == [:on_lbrace, '{']
124
- ix
125
- end
126
-
127
- def add_matching_rbrace(ix)
128
- brace_depth = 0
129
- rbrace_offset = @tokens_without_pos[ix..-1].index do |t|
130
- brace_depth += 1 if t == [:on_lbrace, '{']
131
- brace_depth -= 1 if t == [:on_rbrace, '}']
132
- brace_depth == 0 && t == [:on_rbrace, '}']
133
- end
134
- @table[ix + rbrace_offset] = @table[ix] if rbrace_offset
135
- end
136
- end
137
- end
138
- end
@@ -1,61 +0,0 @@
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
- offences = preliminary_scan(sexp)
10
-
11
- # find Hash.new()
12
- each(:method_add_arg, sexp) do |s|
13
- next if s[1][0] != :call
14
-
15
- receiver = s[1][1][1]
16
- method_name = s[1][3][1]
17
-
18
- if receiver && receiver[1] == 'Hash' &&
19
- method_name == 'new' && s[2] == [:arg_paren, nil]
20
- offences.delete(Offence.new(:convention,
21
- receiver[2].lineno,
22
- ERROR_MESSAGE))
23
- add_offence(:convention,
24
- receiver[2].lineno,
25
- ERROR_MESSAGE)
26
- end
27
-
28
- # check for false positives
29
- if receiver && receiver[1] == 'Hash' &&
30
- method_name == 'new' && s[2] != [:arg_paren, nil]
31
- offences.delete(Offence.new(:convention,
32
- receiver[2].lineno,
33
- ERROR_MESSAGE))
34
- end
35
- end
36
-
37
- offences.each { |o| add_offence(*o.explode) }
38
- end
39
-
40
- def preliminary_scan(sexp)
41
- offences = []
42
-
43
- # find Hash.new
44
- # there will be some false positives here, which
45
- # we'll eliminate later on
46
- each(:call, sexp) do |s|
47
- receiver = s[1][1]
48
-
49
- if receiver && receiver[1] == 'Hash' &&
50
- s[3][1] == 'new'
51
- offences << Offence.new(:convention,
52
- receiver[2].lineno,
53
- ERROR_MESSAGE)
54
- end
55
- end
56
-
57
- offences
58
- end
59
- end
60
- end
61
- end
@@ -1,25 +0,0 @@
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
@@ -1,47 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Rubocop
4
- module Cop
5
- class SymbolSnakeCase < Cop
6
- ERROR_MESSAGE = 'Use snake_case for symbols.'
7
- SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
8
- def inspect(file, source, tokens, sexp)
9
- check_for_symbols(sexp)
10
- check_for_hash_labels(sexp)
11
- end
12
-
13
- private
14
-
15
- def check_for_symbols(sexp)
16
- each(:symbol_literal, sexp) do |s|
17
- symbol_type = s[1][0] == :symbol ? s[1][1][0] : s[1][0]
18
-
19
- # don't check operators
20
- next if symbol_type == :@op
21
-
22
- symbol_ident = s[1][1][1]
23
-
24
- unless symbol_ident =~ SNAKE_CASE
25
- line_no = s[1][1][2].lineno
26
- add_offence(:convention,
27
- line_no,
28
- ERROR_MESSAGE)
29
- end
30
- end
31
- end
32
-
33
- def check_for_hash_labels(sexp)
34
- each(:@label, sexp) do |s|
35
- label_ident = s[1].chop
36
-
37
- unless label_ident =~ SNAKE_CASE
38
- line_no = s[2].lineno
39
- add_offence(:convention,
40
- line_no,
41
- ERROR_MESSAGE)
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,57 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- module Rubocop
6
- module Cop
7
- describe AmpersandsPipesVsAndOr do
8
- let(:amp) { AmpersandsPipesVsAndOr.new }
9
-
10
- it 'registers an offence for AND used in condition of if statement' do
11
- check('if', 'and', '&&')
12
- end
13
-
14
- it 'registers an offence for OR used in condition of if statement' do
15
- check('if', 'or', '||')
16
- end
17
-
18
- it 'registers an offence for AND used in condition of unless' do
19
- check('unless', 'and', '&&')
20
- end
21
-
22
- it 'registers an offence for OR used in condition of unless' do
23
- check('unless', 'or', '||')
24
- end
25
-
26
- it 'registers an offence for AND used in condition of while' do
27
- check('while', 'and', '&&')
28
- end
29
-
30
- it 'registers an offence for OR used in condition of while' do
31
- check('while', 'or', '||')
32
- end
33
-
34
- it 'registers an offence for AND used in condition of until' do
35
- check('until', 'and', '&&')
36
- end
37
-
38
- it 'registers an offence for OR used in condition of until' do
39
- check('until', 'or', '||')
40
- end
41
-
42
- def check(keyword, bad_operator, good_operator)
43
- inspect_source(amp, 'file.rb', ["#{keyword} a #{bad_operator} b",
44
- ' c',
45
- 'end',
46
- "#{keyword} a #{good_operator} b",
47
- ' c',
48
- 'end'])
49
- # Just one offence should be registered. The good_operator
50
- # should be accepted.
51
- expect(amp.offences.map(&:message)).to eq(
52
- ['Use &&/|| for boolean expressions, and/or for control flow.'])
53
- expect(amp.offences[0].line_number).to eq(1)
54
- end
55
- end
56
- end
57
- end