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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb18ed08073976272837da47ef0ba86606395c5b
4
- data.tar.gz: f8c1cab8cf88ce7f1cbbb4815349e83cce88b5e3
3
+ metadata.gz: 8c61251297391f7ee7286eb0e4482e975a42122f
4
+ data.tar.gz: 4375f2e36ceaa75510f7e4cdece56d3f1e22700f
5
5
  SHA512:
6
- metadata.gz: 19e21fa9f97a22498da1cf6fe7512e1fe64923ec9b87d10c4abcfbe7583acfc0f6f68bb32d6c4bf020425c20856c4fe9e323c7fda6e435f2dc694556111a819a
7
- data.tar.gz: 4d9e3e6896f2a836d626ef81147288a5eec30bb084f22ad565de20805d52d3a5ee3f9c16cd96d833ae463beca321e6ba45e8dd9e216b5068cce6f549478db9e5
6
+ metadata.gz: 9a6ed60555b7fbc888b9a68190c79bb525ece1e3623e1025fe833016e13b635b947b0cf49fab267ab4bf886bb0c8438bea130011a8c9dd8efa3e6e863b218b8f
7
+ data.tar.gz: 2f4161c9a52973931d695f900045b46b55e56d62debac661ab25308b031e383906cf33b107f8cf8f6701dba4c251cf9bac7ae49f32550aaca278bae7d556d688
@@ -2,4 +2,10 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
- script: bundle exec rspec
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ script: bundle exec rspec
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: jruby-19mode
11
+ - rvm: rbx-19mode
@@ -4,6 +4,25 @@
4
4
 
5
5
  ### New features
6
6
 
7
+ ### Changes
8
+
9
+ ### Bugs fixed
10
+
11
+ ## 0.8.0 (05/28/2012)
12
+
13
+ ### Changes
14
+
15
+ * Folded `ArrayLiteral` and `HashLiteral` into `EmptyLiteral` cop
16
+ * The maximum number of params `ParameterLists` accepts in now configurable
17
+ * Reworked `SymbolSnakeCase` into `SymbolName`, which has an option `AllowCamelCase` enabled by default.
18
+ * Migrated from `Ripper` to the portable [Parser](https://github.com/whitequark/parser).
19
+
20
+ ### New features
21
+
22
+ * New cop `ConstantName` checks for constant which are not using `SCREAMING_SNAKE_CASE`.
23
+ * New cop `AccessControl` checks private/protected indentation and surrounding blank lines.
24
+ * New cop `Loop` checks for `begin/end/while(until)` and suggests the use of `Kernel#loop`.
25
+
7
26
  ## 0.7.2 (05/13/2013)
8
27
 
9
28
  ### Bugs fixed
data/README.md CHANGED
@@ -97,7 +97,7 @@ The file `config/default.yml` under the RuboCop home directory
97
97
  contains the default settings that all configurations inherit
98
98
  from. Project and personal `.rubocop.yml` files need only make
99
99
  settings that are different from the default ones. If there is no
100
- `.rubocop.yml` file in the project or home direcotry,
100
+ `.rubocop.yml` file in the project or home directory,
101
101
  `config/default.yml` will be used.
102
102
 
103
103
  ### Disabling Cops within Source Code
@@ -132,7 +132,7 @@ RuboCop checks all files recursively within the directory it is run
132
132
  on. However, it does not recognize some files as Ruby(only files
133
133
  ending with `.rb` or extensionless files with a `#!.*ruby` declaration
134
134
  are automatically detected) files, and if you'd like it to check these
135
- you'll need to manually pass them in. Files and directories can be
135
+ you'll need to manually pass them in. Files and directories can
136
136
  also be ignored through `.rubocop.yml`.
137
137
 
138
138
  Here is an example that might be used for a Rails project:
@@ -155,12 +155,8 @@ Note: Files and directories are specified relative to the `.rubocop.yml` file.
155
155
 
156
156
  ## Compatibility
157
157
 
158
- Unfortunately every major Ruby implementation has its own code
159
- analysis tooling, which makes the development of a portable code
160
- analyzer a daunting task.
161
-
162
- RuboCop currently supports MRI 1.9 and MRI 2.0. Support for JRuby and
163
- Rubinius is not planned at this point.
158
+ RuboCop supported only MRI 1.9 & MRI 2.0 prior to version 0.8. After
159
+ RuboCop 0.8, JRuby and Rubinius in 1.9 modes are also supported.
164
160
 
165
161
  ## Editor integration
166
162
 
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: utf-8
3
3
 
4
- if RUBY_ENGINE == 'ruby' && RUBY_VERSION >= '1.9.2'
4
+ if RUBY_VERSION >= '1.9.2'
5
5
  $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
6
6
 
7
7
  require 'rubocop'
@@ -17,6 +17,6 @@ if RUBY_ENGINE == 'ruby' && RUBY_VERSION >= '1.9.2'
17
17
  puts "Finished in #{time} seconds" if cli.options[:debug]
18
18
  exit result
19
19
  else
20
- puts 'RuboCop supports only MRI 1.9.2+'
20
+ puts 'RuboCop supports only Ruby 1.9.2+'
21
21
  exit(-1)
22
22
  end
@@ -22,6 +22,10 @@ MethodLength:
22
22
  CountComments: false # count full line comments?
23
23
  Max: 10
24
24
 
25
+ # Avoid parameter lists longer than four parameters.
26
+ ParameterLists:
27
+ Max: 4
28
+
25
29
  # Don't use semicolons to terminate expressions.
26
30
  Semicolon:
27
31
  # For example; def area(height, width); height * width end
@@ -36,3 +40,7 @@ SingleLineMethods:
36
40
  # Use spaces inside hash literal braces - or don't.
37
41
  SpaceInsideHashLiteralBraces:
38
42
  EnforcedStyleIsWithSpaces: true
43
+
44
+ # Symbol literals should use snake_case.
45
+ SymbolName:
46
+ AllowCamelCase: true
@@ -101,11 +101,8 @@ OneLineConditional:
101
101
  Enabled: true
102
102
 
103
103
  # Avoid using {...} for multi-line blocks (multiline chaining is always ugly).
104
- MultilineBlocks:
105
- Enabled: true
106
-
107
104
  # Prefer {...} over do...end for single-line blocks.
108
- SingleLineBlocks:
105
+ Blocks:
109
106
  Enabled: true
110
107
 
111
108
  # Avoid parameter lists longer than three or four parameters.
@@ -128,8 +125,8 @@ NestedTernaryOperator:
128
125
  UnlessElse:
129
126
  Enabled: true
130
127
 
131
- # Use &&/|| for boolean expressions, and/or for control flow.
132
- AmpersandsPipesVsAndOr:
128
+ # Use &&/|| instead of and/or.
129
+ AndOr:
133
130
  Enabled: true
134
131
 
135
132
  # Use when x then ... for one-line cases.
@@ -196,10 +193,6 @@ AvoidPerlBackrefs:
196
193
  AvoidClassVars:
197
194
  Enabled: true
198
195
 
199
- # Symbol literals should use snake_case.
200
- SymbolSnakeCase:
201
- Enabled: true
202
-
203
196
  # Don't interpolate global, instance and class variables directly in strings.
204
197
  VariableInterpolation:
205
198
  Enabled: true
@@ -228,14 +221,6 @@ Not:
228
221
  RescueModifier:
229
222
  Enabled: true
230
223
 
231
- # Avoid the use of %q, %Q, %s and %x.
232
- PercentLiterals:
233
- Enabled: true
234
-
235
- # Prefer () as delimiters for all % literals.
236
- BraceAfterPercent:
237
- Enabled: true
238
-
239
224
  # Never use return in an ensure block.
240
225
  EnsureReturn:
241
226
  Enabled: true
@@ -260,12 +245,8 @@ BlockComments:
260
245
  RescueException:
261
246
  Enabled: true
262
247
 
263
- # Prefer array literal to Array.new.
264
- ArrayLiteral:
265
- Enabled: true
266
-
267
- # Prefer hash {} literail to Hash.new.
268
- HashLiteral:
248
+ # Prefer literals to Array.new/Hash.new/String.new.
249
+ EmptyLiteral:
269
250
  Enabled: true
270
251
 
271
252
  # When defining binary operators, name the argument other.
@@ -323,3 +304,19 @@ AvoidGlobalVars:
323
304
  # The use of eval represents a serious security risk.
324
305
  Eval:
325
306
  Enabled: true
307
+
308
+ # Symbol literals should use snake_case.
309
+ SymbolName:
310
+ Enabled: true
311
+
312
+ # Constants should use SCREAMING_SNAKE_CASE.
313
+ ConstantName:
314
+ Enabled: true
315
+
316
+ # Indent private/protected as deep as defs and keep blank lines around them.
317
+ AccessControl:
318
+ Enabled: true
319
+
320
+ # Use Kernel#loop with break rather than begin/end/until or begin/end/while for post-loop tests.
321
+ Loop:
322
+ Enabled: true
@@ -1,9 +1,11 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'ripper'
4
3
  require 'rainbow'
5
4
  require 'English'
5
+ require 'parser/current'
6
+ require 'ast/sexp'
6
7
 
8
+ require 'rubocop/cop/util'
7
9
  require 'rubocop/cop/offence'
8
10
  require 'rubocop/cop/cop'
9
11
  require 'rubocop/cop/encoding'
@@ -29,7 +31,7 @@ require 'rubocop/cop/parameter_lists'
29
31
  require 'rubocop/cop/string_literals'
30
32
  require 'rubocop/cop/ternary_operator'
31
33
  require 'rubocop/cop/unless_else'
32
- require 'rubocop/cop/ampersands_pipes_vs_and_or'
34
+ require 'rubocop/cop/and_or'
33
35
  require 'rubocop/cop/when_then'
34
36
  require 'rubocop/cop/favor_modifier'
35
37
  require 'rubocop/cop/favor_unless_over_negated_if'
@@ -43,23 +45,21 @@ require 'rubocop/cop/avoid_perlisms'
43
45
  require 'rubocop/cop/avoid_perl_backrefs'
44
46
  require 'rubocop/cop/avoid_global_vars'
45
47
  require 'rubocop/cop/avoid_class_vars'
46
- require 'rubocop/cop/symbol_snake_case'
48
+ require 'rubocop/cop/symbol_name'
49
+ require 'rubocop/cop/constant_name'
47
50
  require 'rubocop/cop/variable_interpolation'
48
51
  require 'rubocop/cop/semicolon'
49
52
  require 'rubocop/cop/favor_sprintf'
50
53
  require 'rubocop/cop/favor_join'
51
54
  require 'rubocop/cop/alias'
52
55
  require 'rubocop/cop/rescue_modifier'
53
- require 'rubocop/cop/percent_literals'
54
- require 'rubocop/cop/brace_after_percent'
55
56
  require 'rubocop/cop/ensure_return'
56
57
  require 'rubocop/cop/handle_exceptions'
57
58
  require 'rubocop/cop/rescue_exception'
58
59
  require 'rubocop/cop/ascii_identifiers'
59
60
  require 'rubocop/cop/ascii_comments'
60
61
  require 'rubocop/cop/block_comments'
61
- require 'rubocop/cop/hash_literal'
62
- require 'rubocop/cop/array_literal'
62
+ require 'rubocop/cop/empty_literal'
63
63
  require 'rubocop/cop/method_length'
64
64
  require 'rubocop/cop/op_method'
65
65
  require 'rubocop/cop/reduce_arguments'
@@ -74,6 +74,8 @@ require 'rubocop/cop/leading_comment_space'
74
74
  require 'rubocop/cop/colon_method_call'
75
75
  require 'rubocop/cop/not'
76
76
  require 'rubocop/cop/eval'
77
+ require 'rubocop/cop/access_control'
78
+ require 'rubocop/cop/loop'
77
79
 
78
80
  require 'rubocop/report/report'
79
81
  require 'rubocop/report/plain_text'
@@ -1,7 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'pathname'
3
3
  require 'optparse'
4
- require_relative 'cop/grammar'
5
4
 
6
5
  module Rubocop
7
6
  # The CLI is a class responsible of handling all the command line interface
@@ -33,7 +32,7 @@ module Rubocop
33
32
  parse_options(args)
34
33
 
35
34
  begin
36
- handle_only_option if @options[:only]
35
+ validate_only_option if @options[:only]
37
36
  rescue ArgumentError => e
38
37
  puts e.message
39
38
  return 1
@@ -44,13 +43,12 @@ module Rubocop
44
43
 
45
44
  config = ConfigStore.for(file)
46
45
  report = Report.create(file, @options[:mode])
47
- source = read_source(file)
48
46
 
49
47
  puts "Scanning #{file}" if @options[:debug]
50
48
 
51
49
  syntax_cop = Rubocop::Cop::Syntax.new
52
50
  syntax_cop.debug = @options[:debug]
53
- syntax_cop.inspect(file, source, nil, nil)
51
+ syntax_cop.inspect_file(file)
54
52
 
55
53
  if syntax_cop.offences.map(&:severity).include?(:error)
56
54
  # In case of a syntax error we just report that error and do
@@ -58,7 +56,7 @@ module Rubocop
58
56
  report << syntax_cop
59
57
  @total_offences += syntax_cop.offences.count
60
58
  else
61
- inspect_file(file, source, config, report)
59
+ inspect_file(file, config, report)
62
60
  end
63
61
 
64
62
  @processed_file_count += 1
@@ -72,41 +70,38 @@ module Rubocop
72
70
  (@total_offences == 0) && !wants_to_quit ? 0 : 1
73
71
  end
74
72
 
75
- def handle_only_option
76
- @cops = @cops.select { |c| c.cop_name == @options[:only] }
77
- if @cops.empty?
73
+ def validate_only_option
74
+ if @cops.none? { |c| c.cop_name == @options[:only] }
78
75
  fail ArgumentError, "Unrecognized cop name: #{@options[:only]}."
79
76
  end
80
77
  end
81
78
 
82
- def read_source(file)
83
- get_rid_of_invalid_byte_sequences(File.read(file)).split($RS)
84
- end
79
+ def inspect_file(file, config, report)
80
+ begin
81
+ ast, comments, tokens, source = CLI.parse(file) do |source_buffer|
82
+ source_buffer.read
83
+ end
84
+ rescue Parser::SyntaxError, Encoding::UndefinedConversionError,
85
+ ArgumentError => e
86
+ handle_error(e, "An error occurred while parsing #{file}.".color(:red))
87
+ return
88
+ end
85
89
 
86
- def inspect_file(file, source, config, report)
87
- tokens, sexp, correlations = CLI.rip_source(source)
88
90
  disabled_lines = disabled_lines_in(source)
89
91
 
90
- @cops.each do |cop_klass|
91
- cop_name = cop_klass.cop_name
92
- cop_config = config.for_cop(cop_name)
92
+ @cops.each do |cop_class|
93
+ cop_name = cop_class.cop_name
93
94
  if config.cop_enabled?(cop_name)
94
- cop_klass.config = cop_config
95
- cop = cop_klass.new
96
- cop.debug = @options[:debug]
97
- cop.correlations = correlations
98
- cop.disabled_lines = disabled_lines[cop_name]
99
- begin
100
- cop.inspect(file, source, tokens, sexp)
101
- rescue => e
102
- message = "An error occurred while #{cop.name} cop".color(:red) +
103
- " was inspecting #{file}.".color(:red)
104
- @errors << message
105
- warn message
106
- if @options[:debug]
107
- puts e.message, e.backtrace
108
- else
109
- warn 'To see the complete backtrace run rubocop -d.'
95
+ cop = setup_cop(cop_class,
96
+ config.for_cop(cop_name),
97
+ disabled_lines)
98
+ if !@options[:only] || @options[:only] == cop_name
99
+ begin
100
+ cop.inspect(source, tokens, ast, comments)
101
+ rescue => e
102
+ handle_error(e,
103
+ "An error occurred while #{cop.name}".color(:red) +
104
+ " cop was inspecting #{file}.".color(:red))
110
105
  end
111
106
  end
112
107
  @total_offences += cop.offences.count
@@ -115,6 +110,24 @@ module Rubocop
115
110
  end
116
111
  end
117
112
 
113
+ def setup_cop(cop_class, cop_config, disabled_lines)
114
+ cop_class.config = cop_config
115
+ cop = cop_class.new
116
+ cop.debug = @options[:debug]
117
+ cop.disabled_lines = disabled_lines[cop_class.cop_name]
118
+ cop
119
+ end
120
+
121
+ def handle_error(e, message)
122
+ @errors << message
123
+ warn message
124
+ if @options[:debug]
125
+ puts e.message, e.backtrace
126
+ else
127
+ warn 'To see the complete backtrace run rubocop -d.'
128
+ end
129
+ end
130
+
118
131
  def parse_options(args)
119
132
  OptionParser.new do |opts|
120
133
  opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
@@ -207,21 +220,28 @@ module Rubocop
207
220
  end
208
221
  end
209
222
 
210
- def get_rid_of_invalid_byte_sequences(source)
211
- source_encoding = source.encoding.name
212
- # UTF-16 works better in this algorithm but is not supported in 1.9.2.
213
- temporary_encoding = (RUBY_VERSION == '1.9.2') ? 'UTF-8' : 'UTF-16'
214
- source.encode!(temporary_encoding, source_encoding,
215
- invalid: :replace, replace: '')
216
- source.encode!(source_encoding, temporary_encoding)
217
- end
223
+ def self.parse(file)
224
+ parser = Parser::CurrentRuby.new
225
+
226
+ parser.diagnostics.all_errors_are_fatal = true
227
+ parser.diagnostics.ignore_warnings = true
228
+
229
+ parser.diagnostics.consumer = lambda do |diagnostic|
230
+ $stderr.puts(diagnostic.render)
231
+ end
232
+
233
+ source_buffer = Parser::Source::Buffer.new(file, 1)
234
+ yield source_buffer
218
235
 
219
- def self.rip_source(source)
220
- tokens = Ripper.lex(source.join("\n")).map { |t| Cop::Token.new(*t) }
221
- sexp = Ripper.sexp(source.join("\n"))
222
- Cop::Position.make_position_objects(sexp)
223
- correlations = Cop::Grammar.new(tokens).correlate(sexp)
224
- [tokens, sexp, correlations]
236
+ ast, comments, tokens = parser.tokenize(source_buffer)
237
+
238
+ tokens = tokens.map do |t|
239
+ type, details = *t
240
+ text, range = *details
241
+ Rubocop::Cop::Token.new(range, type, text)
242
+ end
243
+
244
+ [ast, comments, tokens, source_buffer.source.split($RS)]
225
245
  end
226
246
 
227
247
  # Generate a list of target files by expanding globing patterns
@@ -261,12 +281,13 @@ module Rubocop
261
281
 
262
282
  rb += files.select { |file| File.extname(file) == '.rb' }
263
283
  rb += files.select do |file|
264
- File.extname(file) == '' &&
265
- begin
266
- File.open(file) { |f| f.readline } =~ /#!.*ruby/
267
- rescue EOFError, ArgumentError => e
268
- log_error(e, "Unprocessable file #{file.inspect}: ")
269
- false
284
+ if File.extname(file) == ''
285
+ begin
286
+ File.open(file) { |f| f.readline } =~ /#!.*ruby/
287
+ rescue EOFError, ArgumentError => e
288
+ log_error(e, "Unprocessable file #{file.inspect}: ")
289
+ false
290
+ end
270
291
  end
271
292
  end
272
293