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
@@ -87,9 +87,12 @@ module Rubocop
87
87
  merge_with_default(config, config_file)
88
88
  end
89
89
 
90
+ def default_configuration
91
+ @default_configuration ||= load_file(DEFAULT_FILE)
92
+ end
93
+
90
94
  def merge_with_default(config, config_file)
91
- default_config = load_file(DEFAULT_FILE)
92
- new(merge(default_config, config), config_file)
95
+ new(merge(default_configuration, config), config_file)
93
96
  end
94
97
 
95
98
  private
@@ -120,17 +123,17 @@ module Rubocop
120
123
  end
121
124
 
122
125
  def warn_unless_valid
123
- validate!
126
+ validate
124
127
  rescue Config::ValidationError => e
125
128
  puts "Warning: #{e.message}".color(:red)
126
129
  end
127
130
 
128
131
  # TODO: This should be a private method
129
- def validate!
132
+ def validate
130
133
  # Don't validate RuboCop's own files. Avoids inifinite recursion.
131
134
  return if @loaded_path.start_with?(RUBOCOP_HOME)
132
135
 
133
- default_config = Config.load_file(DEFAULT_FILE)
136
+ default_config = self.class.default_configuration
134
137
 
135
138
  valid_cop_names, invalid_cop_names = @hash.keys.partition do |key|
136
139
  default_config.has_key?(key)
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AccessControl < Cop
6
+ INDENT_MSG = 'Indent private and protected as deep as method defs.'
7
+ BLANK_MSG = 'Keep a blank line before and after private/protected.'
8
+
9
+ PRIVATE_NODE = s(:send, nil, :private)
10
+ PROTECTED_NODE = s(:send, nil, :protected)
11
+
12
+ def inspect(source, tokens, ast, comments)
13
+ on_node([:class, :module, :sclass], ast) do |class_node|
14
+ class_start_col = class_node.loc.expression.column
15
+
16
+ # we'll have to walk all class children nodes
17
+ # except other class/module nodes
18
+ class_node.children.compact.each do |node|
19
+ on_node(:send, node, [:class, :module, :sclass]) do |send_node|
20
+ if [PRIVATE_NODE, PROTECTED_NODE].include?(send_node)
21
+ send_start_col = send_node.loc.expression.column
22
+
23
+ if send_start_col - 2 != class_start_col
24
+ add_offence(:convention,
25
+ send_node.loc.line,
26
+ INDENT_MSG)
27
+ end
28
+
29
+ send_line = send_node.loc.line
30
+
31
+ unless source[send_line].empty? && source[send_line - 2].empty?
32
+ add_offence(:convention, send_line, BLANK_MSG)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -3,12 +3,14 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class Alias < Cop
6
- ERROR_MESSAGE = 'Use alias_method instead of alias.'
6
+ MSG = 'Use alias_method instead of alias.'
7
7
 
8
- def inspect(file, source, tokens, sexp)
9
- each_keyword('alias', tokens) do |t|
10
- add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
11
- end
8
+ def on_alias(node)
9
+ add_offence(:convention,
10
+ node.loc.keyword.line,
11
+ MSG)
12
+
13
+ super
12
14
  end
13
15
  end
14
16
  end
@@ -3,110 +3,34 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class AlignParameters < Cop
6
- ERROR_MESSAGE = 'Align the parameters of a method call if they span ' +
6
+ MSG = 'Align the parameters of a method call if they span ' +
7
7
  'more than one line.'
8
8
 
9
- def inspect(file, source, tokens, sexp)
10
- @file = file
11
- @tokens = tokens
12
- @token_indexes = {}
13
- @tokens.each_with_index { |t, ix| @token_indexes[t.pos] = ix }
9
+ def on_send(node)
10
+ _receiver, method, *args = *node
14
11
 
15
- each(:method_add_arg, sexp) do |method_add_arg|
16
- args = get_args(method_add_arg) or next
17
- first_arg, rest_of_args = divide_args(args)
18
- @first_lparen_ix = get_lparen_ix(method_add_arg)
19
- pos_of_1st_arg = position_of(first_arg) or next # Give up.
20
- rest_of_args.each do |arg|
21
- pos = position_of(arg) or next # Give up if no position found.
22
- if pos.lineno != pos_of_1st_arg.lineno
23
- if pos.column != pos_of_1st_arg.column
24
- add_offence(:convention, pos.lineno, ERROR_MESSAGE)
25
- end
26
- end
27
- end
28
- end
29
- end
30
-
31
- private
32
-
33
- def get_args(method_add_arg)
34
- fcall = method_add_arg[1]
35
- return nil if fcall[0] != :fcall
36
- return nil if fcall[1][0..1] == [:@ident, 'lambda']
37
- arg_paren = method_add_arg[2..-1][0]
38
- return nil if arg_paren[0] != :arg_paren || arg_paren[1].nil?
39
-
40
- # A command (call wihtout parentheses) as first parameter
41
- # means there's only one parameter.
42
- return nil if [:command, :command_call].include?(arg_paren[1][0][0])
43
-
44
- if arg_paren[1][0] == :args_add_block
45
- args_add_block = arg_paren[1]
46
- args_add_block[1].empty? ? [args_add_block[2]] : args_add_block[1]
47
- else
48
- arg_paren[1]
49
- end
50
- end
51
-
52
- def divide_args(args)
53
- if args[0] == :args_add_star
54
- first_arg = args[1]
55
- rest_of_args = args[2..-1]
56
- else
57
- first_arg = args[0]
58
- rest_of_args = args[1..-1]
59
- end
60
- [first_arg, rest_of_args]
61
- end
62
-
63
- def get_lparen_ix(method_add_arg)
64
- method_name_pos = method_add_arg[1][1][-1]
65
- method_name_ix = @token_indexes[method_name_pos]
66
- method_name_ix +
67
- @tokens[method_name_ix..-1].map(&:type).index(:on_lparen)
68
- end
69
-
70
- def position_of(sexp)
71
- # Indentation inside a string literal is irrelevant.
72
- return nil if sexp[0] == :string_literal
12
+ if method != :[]= && args.size > 1
13
+ first_arg_col = args.first.loc.expression.column
14
+ prev_arg_line = args.first.loc.expression.line
15
+ prev_arg_col = first_arg_col
73
16
 
74
- pos = find_pos_in_sexp(sexp) or return nil # Nil means not found.
75
- ix = find_first_non_whitespace_token(pos) or return nil
76
- @tokens[ix].pos
77
- end
17
+ args.each do |arg|
18
+ cur_arg_line = arg.loc.expression.line
19
+ cur_arg_col = arg.loc.expression.column
78
20
 
79
- def find_pos_in_sexp(sexp)
80
- return sexp[2] if Position === sexp[2]
81
- sexp.grep(Array).each do |s|
82
- pos = find_pos_in_sexp(s) and return pos
83
- end
84
- nil
85
- end
86
-
87
- def find_first_non_whitespace_token(pos)
88
- ix = @token_indexes[pos]
89
- newline_found = false
90
- start_ix = ix.downto(0) do |i|
91
- case @tokens[i].text
92
- when '('
93
- break i + 1 if i == @first_lparen_ix
94
- when "\n"
95
- newline_found = true
96
- when /\t/
97
- # Bail out if tabs are used. Too difficult to calculate column.
98
- return nil
99
- when ','
100
- if newline_found
101
- break i + 1
102
- else
103
- # Bail out if there's a preceding comma on the same line.
104
- return nil
21
+ if cur_arg_line != prev_arg_line &&
22
+ cur_arg_col != first_arg_col
23
+ add_offence(:convetion,
24
+ cur_arg_line,
25
+ MSG)
105
26
  end
27
+
28
+ prev_arg_col = cur_arg_col
29
+ prev_arg_line = cur_arg_line
106
30
  end
107
31
  end
108
- offset = @tokens[start_ix..-1].index { |t| !whitespace?(t) }
109
- start_ix + offset
32
+
33
+ super
110
34
  end
111
35
  end
112
36
  end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ class AndOr < Cop
6
+ MSG = 'Use %s instead of %s.'
7
+
8
+ OPS = { 'and' => '&&', 'or' => '||' }
9
+
10
+ def on_and(node)
11
+ op = node.loc.operator.source
12
+ op_type = node.type.to_s
13
+
14
+ if op == op_type
15
+ add_offence(:convention,
16
+ node.loc.operator.line,
17
+ sprintf(MSG, OPS[op], op))
18
+ end
19
+
20
+ super
21
+ end
22
+
23
+ alias_method :on_or, :on_and
24
+ end
25
+ end
26
+ end
@@ -3,15 +3,10 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class AsciiComments < Cop
6
- ERROR_MESSAGE = 'Use only ascii symbols in comments.'
6
+ MSG = 'Use only ascii symbols in comments.'
7
7
 
8
- def inspect(file, source, tokens, sexp)
9
- tokens.each do |t|
10
- if t.type == :on_comment &&
11
- t.text =~ /[^\x00-\x7f]/
12
- add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
13
- end
14
- end
8
+ def on_comment(c)
9
+ add_offence(:convention, c.loc.line, MSG) if c.text =~ /[^\x00-\x7f]/
15
10
  end
16
11
  end
17
12
  end
@@ -1,15 +1,16 @@
1
1
  # encoding: utf-8
2
2
 
3
+ # rubocop:disable SymbolName
4
+
3
5
  module Rubocop
4
6
  module Cop
5
7
  class AsciiIdentifiers < Cop
6
- ERROR_MESSAGE = 'Use only ascii symbols in identifiers.'
8
+ MSG = 'Use only ascii symbols in identifiers.'
7
9
 
8
- def inspect(file, source, tokens, sexp)
10
+ def inspect(source, tokens, ast, comments)
9
11
  tokens.each do |t|
10
- if t.type == :on_ident &&
11
- t.text =~ /[^\x00-\x7f]/
12
- add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
12
+ if t.type == :tIDENTIFIER && t.text =~ /[^\x00-\x7f]/
13
+ add_offence(:convention, t.pos.line, MSG)
13
14
  end
14
15
  end
15
16
  end
@@ -3,17 +3,12 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class AvoidClassVars < Cop
6
- def inspect(file, source, tokens, sexp)
7
- each(:@cvar, sexp) do |s|
8
- class_var = s[1]
9
- lineno = s[2].lineno
6
+ MSG = 'Replace class var %s with a class instance var.'
10
7
 
11
- add_offence(
12
- :convention,
13
- lineno,
14
- "Replace class var #{class_var} with a class instance var."
15
- )
16
- end
8
+ def on_cvasgn(node)
9
+ class_var, = *node
10
+ add_offence(:convention, node.loc.name.line, sprintf(MSG, class_var))
11
+ super
17
12
  end
18
13
  end
19
14
  end
@@ -3,12 +3,14 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class AvoidFor < Cop
6
- ERROR_MESSAGE = 'Prefer *each* over *for*.'
6
+ MSG = 'Prefer *each* over *for*.'
7
7
 
8
- def inspect(file, source, tokens, sexp)
9
- each_keyword('for', tokens) do |t|
10
- add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
11
- end
8
+ def on_for(node)
9
+ add_offence(:convention,
10
+ node.loc.keyword.line,
11
+ MSG)
12
+
13
+ super
12
14
  end
13
15
  end
14
16
  end
@@ -3,7 +3,7 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class AvoidGlobalVars < Cop
6
- ERROR_MESSAGE = 'Do not introduce global variables.'
6
+ MSG = 'Do not introduce global variables.'
7
7
 
8
8
  # predefined global variables their English aliases
9
9
  # http://www.zenspider.com/Languages/Ruby/QuickRef.html
@@ -35,13 +35,25 @@ module Rubocop
35
35
  $-0 $-a $-d $-F $-i $-I $-l $-p $-v $-w
36
36
  )
37
37
 
38
- def inspect(file, source, tokens, sexp)
39
- each(:@gvar, sexp) do |s|
40
- global_var = s[1]
38
+ def on_gvar(node)
39
+ check(node)
41
40
 
42
- unless BUILT_IN_VARS.include?(global_var)
43
- add_offence(:convention, s[2].lineno, ERROR_MESSAGE)
44
- end
41
+ super
42
+ end
43
+
44
+ def on_gvasgn(node)
45
+ check(node)
46
+
47
+ super
48
+ end
49
+
50
+ def check(node)
51
+ global_var, = *node
52
+
53
+ unless BUILT_IN_VARS.include?(global_var.to_s)
54
+ add_offence(:convention,
55
+ node.loc.name.line,
56
+ MSG)
45
57
  end
46
58
  end
47
59
  end
@@ -3,17 +3,14 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class AvoidPerlBackrefs < Cop
6
- def inspect(file, source, tokens, sexp)
7
- each(:@backref, sexp) do |s|
8
- backref = s[1]
9
- lineno = s[2].lineno
6
+ def on_nth_ref(node)
7
+ backref, = *node
10
8
 
11
- add_offence(
12
- :convention,
13
- lineno,
14
- "Prefer the use of MatchData over #{backref}."
15
- )
16
- end
9
+ add_offence(:convention,
10
+ node.loc.line,
11
+ "Prefer the use of MatchData over $#{backref}.")
12
+
13
+ super
17
14
  end
18
15
  end
19
16
  end
@@ -28,18 +28,19 @@ module Rubocop
28
28
  '$+' => '$LAST_PAREN_MATCH from English library'
29
29
  }
30
30
 
31
- def inspect(file, source, tokens, sexp)
32
- each(:@gvar, sexp) do |s|
33
- global_var = s[1]
31
+ def on_gvar(node)
32
+ global_var, = *node
33
+ global_var = global_var.to_s
34
34
 
35
- if PREFERRED_VARS[global_var]
36
- add_offence(
37
- :convention,
38
- s[2].lineno,
39
- "Prefer #{PREFERRED_VARS[global_var]} over #{global_var}."
40
- )
41
- end
35
+ if PREFERRED_VARS[global_var]
36
+ add_offence(
37
+ :convention,
38
+ node.loc.line,
39
+ "Prefer #{PREFERRED_VARS[global_var]} over #{global_var}."
40
+ )
42
41
  end
42
+
43
+ super
43
44
  end
44
45
  end
45
46
  end
@@ -3,13 +3,11 @@
3
3
  module Rubocop
4
4
  module Cop
5
5
  class BlockComments < Cop
6
- ERROR_MESSAGE = 'Do not use block comments.'
6
+ MSG = 'Do not use block comments.'
7
7
 
8
- def inspect(file, source, tokens, sexp)
9
- tokens.each do |t|
10
- if t.type == :on_embdoc_beg
11
- add_offence(:convention, t.pos.lineno, ERROR_MESSAGE)
12
- end
8
+ def on_comment(comment)
9
+ if comment.text.start_with?('=begin')
10
+ add_offence(:convention, comment.loc.line, MSG)
13
11
  end
14
12
  end
15
13
  end