sabat-rubocop 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (239) hide show
  1. data/.gitignore +50 -0
  2. data/.rspec +1 -0
  3. data/.rubocop.yml +7 -0
  4. data/.travis.yml +7 -0
  5. data/.yardopts +2 -0
  6. data/CHANGELOG.md +268 -0
  7. data/CONTRIBUTING.md +16 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE.txt +20 -0
  10. data/README.md +324 -0
  11. data/Rakefile +29 -0
  12. data/bin/rubocop +22 -0
  13. data/config/default.yml +58 -0
  14. data/config/disabled.yml +5 -0
  15. data/config/enabled.yml +403 -0
  16. data/lib/rubocop.rb +116 -0
  17. data/lib/rubocop/cli.rb +407 -0
  18. data/lib/rubocop/config.rb +250 -0
  19. data/lib/rubocop/config_store.rb +39 -0
  20. data/lib/rubocop/cop/cop.rb +138 -0
  21. data/lib/rubocop/cop/lint/assignment_in_condition.rb +54 -0
  22. data/lib/rubocop/cop/lint/end_alignment.rb +189 -0
  23. data/lib/rubocop/cop/lint/end_in_method.rb +30 -0
  24. data/lib/rubocop/cop/lint/ensure_return.rb +22 -0
  25. data/lib/rubocop/cop/lint/eval.rb +22 -0
  26. data/lib/rubocop/cop/lint/handle_exceptions.rb +20 -0
  27. data/lib/rubocop/cop/lint/literal_in_condition.rb +81 -0
  28. data/lib/rubocop/cop/lint/loop.rb +29 -0
  29. data/lib/rubocop/cop/lint/rescue_exception.rb +29 -0
  30. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +34 -0
  31. data/lib/rubocop/cop/lint/unreachable_code.rb +35 -0
  32. data/lib/rubocop/cop/lint/unused_local_variable.rb +32 -0
  33. data/lib/rubocop/cop/lint/void.rb +58 -0
  34. data/lib/rubocop/cop/offence.rb +136 -0
  35. data/lib/rubocop/cop/rails/validation.rb +30 -0
  36. data/lib/rubocop/cop/style/access_control.rb +58 -0
  37. data/lib/rubocop/cop/style/alias.rb +28 -0
  38. data/lib/rubocop/cop/style/align_parameters.rb +39 -0
  39. data/lib/rubocop/cop/style/and_or.rb +45 -0
  40. data/lib/rubocop/cop/style/ascii_comments.rb +21 -0
  41. data/lib/rubocop/cop/style/ascii_identifiers.rb +22 -0
  42. data/lib/rubocop/cop/style/attr.rb +20 -0
  43. data/lib/rubocop/cop/style/avoid_class_vars.rb +20 -0
  44. data/lib/rubocop/cop/style/avoid_for.rb +18 -0
  45. data/lib/rubocop/cop/style/avoid_global_vars.rb +65 -0
  46. data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +21 -0
  47. data/lib/rubocop/cop/style/avoid_perlisms.rb +50 -0
  48. data/lib/rubocop/cop/style/begin_block.rb +18 -0
  49. data/lib/rubocop/cop/style/block_comments.rb +20 -0
  50. data/lib/rubocop/cop/style/block_nesting.rb +47 -0
  51. data/lib/rubocop/cop/style/blocks.rb +27 -0
  52. data/lib/rubocop/cop/style/case_equality.rb +22 -0
  53. data/lib/rubocop/cop/style/case_indentation.rb +28 -0
  54. data/lib/rubocop/cop/style/character_literal.rb +37 -0
  55. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +33 -0
  56. data/lib/rubocop/cop/style/class_methods.rb +22 -0
  57. data/lib/rubocop/cop/style/collection_methods.rb +56 -0
  58. data/lib/rubocop/cop/style/colon_method_call.rb +29 -0
  59. data/lib/rubocop/cop/style/constant_name.rb +31 -0
  60. data/lib/rubocop/cop/style/def_parentheses.rb +70 -0
  61. data/lib/rubocop/cop/style/documentation.rb +58 -0
  62. data/lib/rubocop/cop/style/dot_position.rb +25 -0
  63. data/lib/rubocop/cop/style/empty_line_between_defs.rb +26 -0
  64. data/lib/rubocop/cop/style/empty_lines.rb +40 -0
  65. data/lib/rubocop/cop/style/empty_literal.rb +53 -0
  66. data/lib/rubocop/cop/style/encoding.rb +29 -0
  67. data/lib/rubocop/cop/style/end_block.rb +18 -0
  68. data/lib/rubocop/cop/style/end_of_line.rb +23 -0
  69. data/lib/rubocop/cop/style/favor_join.rb +29 -0
  70. data/lib/rubocop/cop/style/favor_modifier.rb +118 -0
  71. data/lib/rubocop/cop/style/favor_sprintf.rb +28 -0
  72. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +54 -0
  73. data/lib/rubocop/cop/style/hash_syntax.rb +47 -0
  74. data/lib/rubocop/cop/style/if_then_else.rb +29 -0
  75. data/lib/rubocop/cop/style/if_with_semicolon.rb +20 -0
  76. data/lib/rubocop/cop/style/lambda.rb +47 -0
  77. data/lib/rubocop/cop/style/leading_comment_space.rb +25 -0
  78. data/lib/rubocop/cop/style/line_continuation.rb +26 -0
  79. data/lib/rubocop/cop/style/line_length.rb +30 -0
  80. data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +61 -0
  81. data/lib/rubocop/cop/style/method_call_parentheses.rb +22 -0
  82. data/lib/rubocop/cop/style/method_length.rb +57 -0
  83. data/lib/rubocop/cop/style/multiline_if_then.rb +47 -0
  84. data/lib/rubocop/cop/style/not.rb +24 -0
  85. data/lib/rubocop/cop/style/numeric_literals.rb +25 -0
  86. data/lib/rubocop/cop/style/one_line_conditional.rb +20 -0
  87. data/lib/rubocop/cop/style/op_method.rb +29 -0
  88. data/lib/rubocop/cop/style/parameter_lists.rb +42 -0
  89. data/lib/rubocop/cop/style/parentheses_around_condition.rb +42 -0
  90. data/lib/rubocop/cop/style/proc.rb +30 -0
  91. data/lib/rubocop/cop/style/reduce_arguments.rb +34 -0
  92. data/lib/rubocop/cop/style/regexp_literal.rb +39 -0
  93. data/lib/rubocop/cop/style/rescue_modifier.rb +55 -0
  94. data/lib/rubocop/cop/style/semicolon.rb +51 -0
  95. data/lib/rubocop/cop/style/single_line_methods.rb +48 -0
  96. data/lib/rubocop/cop/style/space_after_comma_etc.rb +69 -0
  97. data/lib/rubocop/cop/style/space_after_control_keyword.rb +32 -0
  98. data/lib/rubocop/cop/style/string_literals.rb +36 -0
  99. data/lib/rubocop/cop/style/surrounding_space.rb +314 -0
  100. data/lib/rubocop/cop/style/symbol_array.rb +31 -0
  101. data/lib/rubocop/cop/style/symbol_name.rb +27 -0
  102. data/lib/rubocop/cop/style/tab.rb +25 -0
  103. data/lib/rubocop/cop/style/ternary_operator.rb +49 -0
  104. data/lib/rubocop/cop/style/trailing_whitespace.rb +24 -0
  105. data/lib/rubocop/cop/style/trivial_accessors.rb +32 -0
  106. data/lib/rubocop/cop/style/unless_else.rb +26 -0
  107. data/lib/rubocop/cop/style/variable_interpolation.rb +32 -0
  108. data/lib/rubocop/cop/style/when_then.rb +25 -0
  109. data/lib/rubocop/cop/style/while_until_do.rb +45 -0
  110. data/lib/rubocop/cop/style/word_array.rb +44 -0
  111. data/lib/rubocop/cop/util.rb +27 -0
  112. data/lib/rubocop/cop/variable_inspector.rb +280 -0
  113. data/lib/rubocop/formatter/base_formatter.rb +119 -0
  114. data/lib/rubocop/formatter/clang_style_formatter.rb +21 -0
  115. data/lib/rubocop/formatter/emacs_style_formatter.rb +17 -0
  116. data/lib/rubocop/formatter/formatter_set.rb +77 -0
  117. data/lib/rubocop/formatter/json_formatter.rb +76 -0
  118. data/lib/rubocop/formatter/progress_formatter.rb +63 -0
  119. data/lib/rubocop/formatter/simple_text_formatter.rb +62 -0
  120. data/lib/rubocop/version.rb +21 -0
  121. data/rubocop.gemspec +36 -0
  122. data/spec/.rubocop.yml +5 -0
  123. data/spec/project_spec.rb +24 -0
  124. data/spec/rubocop/cli_spec.rb +906 -0
  125. data/spec/rubocop/config_spec.rb +470 -0
  126. data/spec/rubocop/config_store_spec.rb +66 -0
  127. data/spec/rubocop/cops/cop_spec.rb +38 -0
  128. data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +111 -0
  129. data/spec/rubocop/cops/lint/end_alignment_spec.rb +333 -0
  130. data/spec/rubocop/cops/lint/end_in_method_spec.rb +35 -0
  131. data/spec/rubocop/cops/lint/ensure_return_spec.rb +37 -0
  132. data/spec/rubocop/cops/lint/eval_spec.rb +41 -0
  133. data/spec/rubocop/cops/lint/handle_exceptions_spec.rb +36 -0
  134. data/spec/rubocop/cops/lint/literal_in_condition_spec.rb +42 -0
  135. data/spec/rubocop/cops/lint/loop_spec.rb +33 -0
  136. data/spec/rubocop/cops/lint/rescue_exception_spec.rb +127 -0
  137. data/spec/rubocop/cops/lint/shadowing_outer_local_variable_spec.rb +243 -0
  138. data/spec/rubocop/cops/lint/unreachable_code_spec.rb +69 -0
  139. data/spec/rubocop/cops/lint/unused_local_variable_spec.rb +497 -0
  140. data/spec/rubocop/cops/lint/void_spec.rb +63 -0
  141. data/spec/rubocop/cops/offence_spec.rb +133 -0
  142. data/spec/rubocop/cops/rails/validation_spec.rb +27 -0
  143. data/spec/rubocop/cops/style/access_control_spec.rb +142 -0
  144. data/spec/rubocop/cops/style/alias_spec.rb +47 -0
  145. data/spec/rubocop/cops/style/align_parameters_spec.rb +199 -0
  146. data/spec/rubocop/cops/style/and_or_spec.rb +39 -0
  147. data/spec/rubocop/cops/style/ascii_comments_spec.rb +28 -0
  148. data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +28 -0
  149. data/spec/rubocop/cops/style/attr_spec.rb +20 -0
  150. data/spec/rubocop/cops/style/avoid_class_vars_spec.rb +27 -0
  151. data/spec/rubocop/cops/style/avoid_for_spec.rb +37 -0
  152. data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +34 -0
  153. data/spec/rubocop/cops/style/avoid_perl_backrefs_spec.rb +20 -0
  154. data/spec/rubocop/cops/style/avoid_perlisms_spec.rb +47 -0
  155. data/spec/rubocop/cops/style/begin_block_spec.rb +19 -0
  156. data/spec/rubocop/cops/style/block_comments_spec.rb +27 -0
  157. data/spec/rubocop/cops/style/block_nesting_spec.rb +159 -0
  158. data/spec/rubocop/cops/style/blocks_spec.rb +35 -0
  159. data/spec/rubocop/cops/style/case_equality_spec.rb +18 -0
  160. data/spec/rubocop/cops/style/case_indentation_spec.rb +88 -0
  161. data/spec/rubocop/cops/style/character_literal_spec.rb +28 -0
  162. data/spec/rubocop/cops/style/class_and_module_camel_case_spec.rb +46 -0
  163. data/spec/rubocop/cops/style/class_methods_spec.rb +51 -0
  164. data/spec/rubocop/cops/style/collection_methods_spec.rb +41 -0
  165. data/spec/rubocop/cops/style/colon_method_call_spec.rb +55 -0
  166. data/spec/rubocop/cops/style/constant_name_spec.rb +56 -0
  167. data/spec/rubocop/cops/style/def_with_parentheses_spec.rb +40 -0
  168. data/spec/rubocop/cops/style/def_without_parentheses_spec.rb +34 -0
  169. data/spec/rubocop/cops/style/documentation_spec.rb +79 -0
  170. data/spec/rubocop/cops/style/dot_position_spec.rb +30 -0
  171. data/spec/rubocop/cops/style/empty_line_between_defs_spec.rb +85 -0
  172. data/spec/rubocop/cops/style/empty_lines_spec.rb +40 -0
  173. data/spec/rubocop/cops/style/empty_literal_spec.rb +91 -0
  174. data/spec/rubocop/cops/style/encoding_spec.rb +49 -0
  175. data/spec/rubocop/cops/style/end_block_spec.rb +19 -0
  176. data/spec/rubocop/cops/style/end_of_line_spec.rb +25 -0
  177. data/spec/rubocop/cops/style/favor_join_spec.rb +37 -0
  178. data/spec/rubocop/cops/style/favor_modifier_spec.rb +160 -0
  179. data/spec/rubocop/cops/style/favor_sprintf_spec.rb +53 -0
  180. data/spec/rubocop/cops/style/favor_unless_over_negated_if_spec.rb +64 -0
  181. data/spec/rubocop/cops/style/favor_until_over_negated_while_spec.rb +47 -0
  182. data/spec/rubocop/cops/style/hash_syntax_spec.rb +51 -0
  183. data/spec/rubocop/cops/style/if_with_semicolon_spec.rb +25 -0
  184. data/spec/rubocop/cops/style/lambda_spec.rb +45 -0
  185. data/spec/rubocop/cops/style/leading_comment_space_spec.rb +65 -0
  186. data/spec/rubocop/cops/style/line_continuation_spec.rb +26 -0
  187. data/spec/rubocop/cops/style/line_length_spec.rb +25 -0
  188. data/spec/rubocop/cops/style/method_and_variable_snake_case_spec.rb +95 -0
  189. data/spec/rubocop/cops/style/method_call_parentheses_spec.rb +25 -0
  190. data/spec/rubocop/cops/style/method_length_spec.rb +151 -0
  191. data/spec/rubocop/cops/style/multiline_if_then_spec.rb +97 -0
  192. data/spec/rubocop/cops/style/not_spec.rb +28 -0
  193. data/spec/rubocop/cops/style/numeric_literals_spec.rb +51 -0
  194. data/spec/rubocop/cops/style/one_line_conditional_spec.rb +18 -0
  195. data/spec/rubocop/cops/style/op_method_spec.rb +80 -0
  196. data/spec/rubocop/cops/style/parameter_lists_spec.rb +49 -0
  197. data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +59 -0
  198. data/spec/rubocop/cops/style/proc_spec.rb +28 -0
  199. data/spec/rubocop/cops/style/reduce_arguments_spec.rb +59 -0
  200. data/spec/rubocop/cops/style/regexp_literal_spec.rb +83 -0
  201. data/spec/rubocop/cops/style/rescue_modifier_spec.rb +122 -0
  202. data/spec/rubocop/cops/style/semicolon_spec.rb +95 -0
  203. data/spec/rubocop/cops/style/single_line_methods_spec.rb +54 -0
  204. data/spec/rubocop/cops/style/space_after_colon_spec.rb +29 -0
  205. data/spec/rubocop/cops/style/space_after_comma_spec.rb +31 -0
  206. data/spec/rubocop/cops/style/space_after_control_keyword_spec.rb +69 -0
  207. data/spec/rubocop/cops/style/space_after_semicolon_spec.rb +24 -0
  208. data/spec/rubocop/cops/style/space_around_braces_spec.rb +49 -0
  209. data/spec/rubocop/cops/style/space_around_equals_in_default_parameter_spec.rb +34 -0
  210. data/spec/rubocop/cops/style/space_around_operators_spec.rb +216 -0
  211. data/spec/rubocop/cops/style/space_inside_brackets_spec.rb +51 -0
  212. data/spec/rubocop/cops/style/space_inside_hash_literal_braces_spec.rb +99 -0
  213. data/spec/rubocop/cops/style/space_inside_parens_spec.rb +33 -0
  214. data/spec/rubocop/cops/style/string_literals_spec.rb +62 -0
  215. data/spec/rubocop/cops/style/symbol_array_spec.rb +45 -0
  216. data/spec/rubocop/cops/style/symbol_name_spec.rb +122 -0
  217. data/spec/rubocop/cops/style/tab_spec.rb +23 -0
  218. data/spec/rubocop/cops/style/ternary_operator_spec.rb +42 -0
  219. data/spec/rubocop/cops/style/trailing_whitespace_spec.rb +29 -0
  220. data/spec/rubocop/cops/style/trivial_accessors_spec.rb +338 -0
  221. data/spec/rubocop/cops/style/unless_else_spec.rb +31 -0
  222. data/spec/rubocop/cops/style/variable_interpolation_spec.rb +53 -0
  223. data/spec/rubocop/cops/style/when_then_spec.rb +40 -0
  224. data/spec/rubocop/cops/style/while_until_do_spec.rb +47 -0
  225. data/spec/rubocop/cops/style/word_array_spec.rb +61 -0
  226. data/spec/rubocop/cops/variable_inspector_spec.rb +374 -0
  227. data/spec/rubocop/formatter/base_formatter_spec.rb +190 -0
  228. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +70 -0
  229. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +32 -0
  230. data/spec/rubocop/formatter/formatter_set_spec.rb +132 -0
  231. data/spec/rubocop/formatter/json_formatter_spec.rb +142 -0
  232. data/spec/rubocop/formatter/progress_formatter_spec.rb +196 -0
  233. data/spec/rubocop/formatter/simple_text_formatter_spec.rb +74 -0
  234. data/spec/spec_helper.rb +92 -0
  235. data/spec/support/file_helper.rb +21 -0
  236. data/spec/support/isolated_environment.rb +27 -0
  237. data/spec/support/mri_syntax_checker.rb +69 -0
  238. data/spec/support/shared_examples.rb +33 -0
  239. metadata +517 -0
@@ -0,0 +1,116 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rainbow'
4
+ require 'English'
5
+ require 'parser/current'
6
+ require 'ast/sexp'
7
+
8
+ require 'rubocop/cop/util'
9
+ require 'rubocop/cop/variable_inspector'
10
+ require 'rubocop/cop/offence'
11
+ require 'rubocop/cop/cop'
12
+
13
+ require 'rubocop/cop/lint/assignment_in_condition'
14
+ require 'rubocop/cop/lint/end_alignment'
15
+ require 'rubocop/cop/lint/end_in_method'
16
+ require 'rubocop/cop/lint/ensure_return'
17
+ require 'rubocop/cop/lint/eval'
18
+ require 'rubocop/cop/lint/handle_exceptions'
19
+ require 'rubocop/cop/lint/literal_in_condition'
20
+ require 'rubocop/cop/lint/loop'
21
+ require 'rubocop/cop/lint/rescue_exception'
22
+ require 'rubocop/cop/lint/shadowing_outer_local_variable'
23
+ require 'rubocop/cop/lint/unreachable_code'
24
+ require 'rubocop/cop/lint/unused_local_variable'
25
+ require 'rubocop/cop/lint/void'
26
+
27
+ require 'rubocop/cop/style/access_control'
28
+ require 'rubocop/cop/style/alias'
29
+ require 'rubocop/cop/style/align_parameters'
30
+ require 'rubocop/cop/style/and_or'
31
+ require 'rubocop/cop/style/ascii_comments'
32
+ require 'rubocop/cop/style/ascii_identifiers'
33
+ require 'rubocop/cop/style/attr'
34
+ require 'rubocop/cop/style/avoid_class_vars'
35
+ require 'rubocop/cop/style/avoid_for'
36
+ require 'rubocop/cop/style/avoid_global_vars'
37
+ require 'rubocop/cop/style/avoid_perl_backrefs'
38
+ require 'rubocop/cop/style/avoid_perlisms'
39
+ require 'rubocop/cop/style/begin_block'
40
+ require 'rubocop/cop/style/block_comments'
41
+ require 'rubocop/cop/style/block_nesting'
42
+ require 'rubocop/cop/style/blocks'
43
+ require 'rubocop/cop/style/character_literal'
44
+ require 'rubocop/cop/style/case_equality'
45
+ require 'rubocop/cop/style/case_indentation'
46
+ require 'rubocop/cop/style/class_and_module_camel_case'
47
+ require 'rubocop/cop/style/class_methods'
48
+ require 'rubocop/cop/style/collection_methods'
49
+ require 'rubocop/cop/style/colon_method_call'
50
+ require 'rubocop/cop/style/constant_name'
51
+ require 'rubocop/cop/style/def_parentheses'
52
+ require 'rubocop/cop/style/documentation'
53
+ require 'rubocop/cop/style/dot_position'
54
+ require 'rubocop/cop/style/empty_line_between_defs'
55
+ require 'rubocop/cop/style/empty_lines'
56
+ require 'rubocop/cop/style/empty_literal'
57
+ require 'rubocop/cop/style/encoding'
58
+ require 'rubocop/cop/style/end_block'
59
+ require 'rubocop/cop/style/end_of_line'
60
+ require 'rubocop/cop/style/favor_join'
61
+ require 'rubocop/cop/style/favor_modifier'
62
+ require 'rubocop/cop/style/favor_sprintf'
63
+ require 'rubocop/cop/style/favor_unless_over_negated_if'
64
+ require 'rubocop/cop/style/hash_syntax'
65
+ require 'rubocop/cop/style/if_then_else'
66
+ require 'rubocop/cop/style/if_with_semicolon'
67
+ require 'rubocop/cop/style/multiline_if_then'
68
+ require 'rubocop/cop/style/one_line_conditional'
69
+ require 'rubocop/cop/style/lambda'
70
+ require 'rubocop/cop/style/leading_comment_space'
71
+ require 'rubocop/cop/style/line_continuation'
72
+ require 'rubocop/cop/style/line_length'
73
+ require 'rubocop/cop/style/method_and_variable_snake_case'
74
+ require 'rubocop/cop/style/method_call_parentheses'
75
+ require 'rubocop/cop/style/method_length'
76
+ require 'rubocop/cop/style/not'
77
+ require 'rubocop/cop/style/numeric_literals'
78
+ require 'rubocop/cop/style/op_method'
79
+ require 'rubocop/cop/style/parameter_lists'
80
+ require 'rubocop/cop/style/parentheses_around_condition'
81
+ require 'rubocop/cop/style/proc'
82
+ require 'rubocop/cop/style/reduce_arguments'
83
+ require 'rubocop/cop/style/regexp_literal'
84
+ require 'rubocop/cop/style/rescue_modifier'
85
+ require 'rubocop/cop/style/semicolon'
86
+ require 'rubocop/cop/style/single_line_methods'
87
+ require 'rubocop/cop/style/space_after_comma_etc'
88
+ require 'rubocop/cop/style/space_after_control_keyword'
89
+ require 'rubocop/cop/style/string_literals'
90
+ require 'rubocop/cop/style/surrounding_space'
91
+ require 'rubocop/cop/style/symbol_array'
92
+ require 'rubocop/cop/style/symbol_name'
93
+ require 'rubocop/cop/style/tab'
94
+ require 'rubocop/cop/style/ternary_operator'
95
+ require 'rubocop/cop/style/trailing_whitespace'
96
+ require 'rubocop/cop/style/trivial_accessors'
97
+ require 'rubocop/cop/style/unless_else'
98
+ require 'rubocop/cop/style/variable_interpolation'
99
+ require 'rubocop/cop/style/when_then'
100
+ require 'rubocop/cop/style/while_until_do'
101
+ require 'rubocop/cop/style/word_array'
102
+
103
+ require 'rubocop/cop/rails/validation'
104
+
105
+ require 'rubocop/formatter/base_formatter'
106
+ require 'rubocop/formatter/simple_text_formatter'
107
+ require 'rubocop/formatter/emacs_style_formatter'
108
+ require 'rubocop/formatter/clang_style_formatter'
109
+ require 'rubocop/formatter/progress_formatter'
110
+ require 'rubocop/formatter/json_formatter'
111
+ require 'rubocop/formatter/formatter_set'
112
+
113
+ require 'rubocop/config'
114
+ require 'rubocop/config_store'
115
+ require 'rubocop/cli'
116
+ require 'rubocop/version'
@@ -0,0 +1,407 @@
1
+ # encoding: utf-8
2
+ require 'pathname'
3
+ require 'optparse'
4
+
5
+ module Rubocop
6
+ # The CLI is a class responsible of handling all the command line interface
7
+ # logic.
8
+ class CLI
9
+ DEFAULT_FORMATTER = 'progress'
10
+
11
+ # If set true while running,
12
+ # RuboCop will abort processing and exit gracefully.
13
+ attr_accessor :wants_to_quit
14
+ attr_accessor :options
15
+
16
+ alias_method :wants_to_quit?, :wants_to_quit
17
+
18
+ def initialize
19
+ @cops = Cop::Cop.all
20
+ @errors = []
21
+ @options = {}
22
+ ConfigStore.prepare
23
+ end
24
+
25
+ # Entry point for the application logic. Here we
26
+ # do the command line arguments processing and inspect
27
+ # the target files
28
+ # @return [Fixnum] UNIX exit code
29
+ def run(args = ARGV)
30
+ trap_interrupt
31
+
32
+ begin
33
+ parse_options(args)
34
+ rescue => e
35
+ $stderr.puts e.message
36
+ return 1
37
+ end
38
+
39
+ # filter out Rails cops unless requested
40
+ @cops.reject!(&:rails?) unless @options[:rails]
41
+
42
+ # filter out style cops when --lint is passed
43
+ @cops.select!(&:lint?) if @options[:lint]
44
+
45
+ target_files = target_files(args)
46
+ target_files.each(&:freeze).freeze
47
+ inspected_files = []
48
+ any_failed = false
49
+
50
+ formatter_set.started(target_files)
51
+
52
+ target_files.each do |file|
53
+ break if wants_to_quit?
54
+
55
+ puts "Scanning #{file}" if @options[:debug]
56
+ formatter_set.file_started(file, {})
57
+
58
+ offences = inspect_file(file)
59
+
60
+ any_failed = true unless offences.empty?
61
+ inspected_files << file
62
+ formatter_set.file_finished(file, offences.freeze)
63
+ end
64
+
65
+ formatter_set.finished(inspected_files.freeze)
66
+ formatter_set.close_output_files
67
+
68
+ display_error_summary(@errors) unless @options[:silent]
69
+
70
+ !any_failed && !wants_to_quit ? 0 : 1
71
+ end
72
+
73
+ def validate_only_option
74
+ if @cops.none? { |c| c.cop_name == @options[:only] }
75
+ fail ArgumentError, "Unrecognized cop name: #{@options[:only]}."
76
+ end
77
+ end
78
+
79
+ def inspect_file(file)
80
+ begin
81
+ ast, comments, tokens, source_buffer, source, syntax_offences =
82
+ CLI.parse(file) { |sb| sb.read }
83
+
84
+ rescue Encoding::UndefinedConversionError, ArgumentError => e
85
+ handle_error(e, "An error occurred while parsing #{file}.".color(:red))
86
+ return []
87
+ end
88
+
89
+ # If we got any syntax errors, return only the syntax offences.
90
+ # Parser may return nil for AST even though there are no syntax errors.
91
+ # e.g. sources which contain only comments
92
+ return syntax_offences unless syntax_offences.empty?
93
+
94
+ config = ConfigStore.for(file)
95
+ disabled_lines = disabled_lines_in(source)
96
+
97
+ set_config_for_all_cops(config)
98
+
99
+ @cops.reduce([]) do |offences, cop_class|
100
+ cop_name = cop_class.cop_name
101
+ if config.cop_enabled?(cop_name)
102
+ cop = setup_cop(cop_class, disabled_lines)
103
+ if !@options[:only] || @options[:only] == cop_name
104
+ begin
105
+ cop.inspect(source_buffer, source, tokens, ast, comments)
106
+ rescue => e
107
+ handle_error(e,
108
+ "An error occurred while #{cop.name}".color(:red) +
109
+ " cop was inspecting #{file}.".color(:red))
110
+ end
111
+ end
112
+ offences.concat(cop.offences)
113
+ end
114
+ offences
115
+ end.sort
116
+ end
117
+
118
+ def set_config_for_all_cops(config)
119
+ @cops.each do |cop_class|
120
+ cop_class.config = config.for_cop(cop_class.cop_name)
121
+ end
122
+ end
123
+
124
+ def setup_cop(cop_class, disabled_lines = nil)
125
+ cop = cop_class.new
126
+ cop.debug = @options[:debug] || @options[:list_cops]
127
+ cop.autocorrect = @options[:autocorrect]
128
+ cop.disabled_lines = disabled_lines[cop_class.cop_name] if disabled_lines
129
+ cop
130
+ end
131
+
132
+ def handle_error(e, message)
133
+ @errors << message
134
+ warn message
135
+ if @options[:debug]
136
+ puts e.message, e.backtrace
137
+ else
138
+ warn 'To see the complete backtrace run rubocop -d.'
139
+ end
140
+ end
141
+
142
+ # rubocop:disable MethodLength
143
+ def parse_options(args)
144
+ convert_deprecated_options!(args)
145
+
146
+ OptionParser.new do |opts|
147
+ opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'
148
+
149
+ opts.on('-d', '--debug', 'Display debug info.') do |d|
150
+ @options[:debug] = d
151
+ end
152
+ opts.on('-L', '--list-cops', 'List class name of cops.') do |c|
153
+ @options[:list_cops] = c
154
+ end
155
+ opts.on('-c', '--config FILE', 'Specify configuration file.') do |f|
156
+ @options[:config] = f
157
+ ConfigStore.set_options_config(@options[:config])
158
+ end
159
+ opts.on('--only COP', 'Run just one cop.') do |s|
160
+ @options[:only] = s
161
+ validate_only_option
162
+ end
163
+ opts.on('-f', '--format FORMATTER',
164
+ 'Choose an output formatter. This option',
165
+ 'can be specified multiple times to enable',
166
+ 'multiple formatters at the same time.',
167
+ ' [p]rogress (default)',
168
+ ' [s]imple',
169
+ ' [c]lang',
170
+ ' [e]macs',
171
+ ' [j]son',
172
+ ' custom formatter class name') do |key|
173
+ @options[:formatters] ||= []
174
+ @options[:formatters] << [key]
175
+ end
176
+ opts.on('-o', '--out FILE',
177
+ 'Write output to a file instead of STDOUT.',
178
+ 'This option applies to the previously',
179
+ 'specified --format, or the default format',
180
+ 'if no format is specified.') do |path|
181
+ @options[:formatters] ||= [[DEFAULT_FORMATTER]]
182
+ @options[:formatters].last << path
183
+ end
184
+ opts.on('-r', '--require FILE', 'Require Ruby file.') do |f|
185
+ require f
186
+ end
187
+ opts.on('-R', '--rails', 'Run extra Rails cops.') do |r|
188
+ @options[:rails] = r
189
+ end
190
+ opts.on('-l', '--lint', 'Run only lint cops.') do |l|
191
+ @options[:lint] = l
192
+ end
193
+ opts.on('-a', '--auto-correct', 'Auto-correct offences.') do |a|
194
+ @options[:autocorrect] = a
195
+ end
196
+ opts.on('-s', '--silent', 'Silence summary.') do |s|
197
+ @options[:silent] = s
198
+ end
199
+ opts.on('-n', '--no-color', 'Disable color output.') do |s|
200
+ Sickill::Rainbow.enabled = false
201
+ end
202
+ opts.on('-v', '--version', 'Display version.') do
203
+ puts Rubocop::Version.version(false)
204
+ exit(0)
205
+ end
206
+ opts.on('-V', '--verbose-version', 'Display verbose version.') do
207
+ puts Rubocop::Version.version(true)
208
+ exit(0)
209
+ end
210
+ end.parse!(args)
211
+ end
212
+ # rubocop:enable MethodLength
213
+
214
+ def convert_deprecated_options!(args)
215
+ args.map! do |arg|
216
+ case arg
217
+ when '-e', '--emacs'
218
+ deprecate("#{arg} option", '--format emacs', '1.0.0')
219
+ %w(--format emacs)
220
+ else
221
+ arg
222
+ end
223
+ end.flatten!
224
+ end
225
+
226
+ def trap_interrupt
227
+ Signal.trap('INT') do
228
+ exit!(1) if wants_to_quit?
229
+ self.wants_to_quit = true
230
+ $stderr.puts
231
+ $stderr.puts 'Exiting... Interrupt again to exit immediately.'
232
+ end
233
+ end
234
+
235
+ def display_error_summary(errors)
236
+ return if errors.empty?
237
+ plural = errors.count > 1 ? 's' : ''
238
+ puts "\n#{errors.count} error#{plural} occurred:".color(:red)
239
+ errors.each { |error| puts error }
240
+ puts 'Errors are usually caused by RuboCop bugs.'
241
+ puts 'Please, report your problems to RuboCop\'s issue tracker.'
242
+ puts 'Mention the following information in the issue report:'
243
+ puts Rubocop::Version.version(true)
244
+ end
245
+
246
+ def disabled_lines_in(source)
247
+ disabled_lines = Hash.new([])
248
+ disabled_section = {}
249
+ regexp = '# rubocop : (%s)\b ((?:\w+,? )+)'.gsub(' ', '\s*')
250
+ section_regexp = '^\s*' + sprintf(regexp, '(?:dis|en)able')
251
+ single_line_regexp = '\S.*' + sprintf(regexp, 'disable')
252
+
253
+ source.each_with_index do |line, ix|
254
+ each_mentioned_cop(/#{section_regexp}/, line) do |cop_name, kind|
255
+ disabled_section[cop_name] = (kind == 'disable')
256
+ end
257
+ disabled_section.keys.each do |cop_name|
258
+ disabled_lines[cop_name] += [ix + 1] if disabled_section[cop_name]
259
+ end
260
+
261
+ each_mentioned_cop(/#{single_line_regexp}/, line) do |cop_name, kind|
262
+ disabled_lines[cop_name] += [ix + 1] if kind == 'disable'
263
+ end
264
+ end
265
+ disabled_lines
266
+ end
267
+
268
+ def each_mentioned_cop(regexp, line)
269
+ match = line.match(regexp)
270
+ if match
271
+ kind, cops = match.captures
272
+ cops = Cop::Cop.all.map(&:cop_name).join(',') if cops.include?('all')
273
+ cops.split(/,\s*/).each { |cop_name| yield cop_name, kind }
274
+ end
275
+ end
276
+
277
+ def self.parse(file)
278
+ parser = Parser::CurrentRuby.new
279
+
280
+ # On JRuby and Rubinius, there's a risk that we hang in
281
+ # tokenize() if we don't set the all errors as fatal flag.
282
+ parser.diagnostics.all_errors_are_fatal = RUBY_ENGINE != 'ruby'
283
+ parser.diagnostics.ignore_warnings = false
284
+
285
+ diagnostics = []
286
+ parser.diagnostics.consumer = lambda do |diagnostic|
287
+ diagnostics << diagnostic
288
+ end
289
+
290
+ source_buffer = Parser::Source::Buffer.new(file, 1)
291
+ yield source_buffer
292
+
293
+ begin
294
+ ast, comments, tokens = parser.tokenize(source_buffer)
295
+ rescue Parser::SyntaxError # rubocop:disable HandleExceptions
296
+ # All errors are in diagnostics. No need to handle exception.
297
+ end
298
+
299
+ if tokens
300
+ tokens = tokens.map do |t|
301
+ type, details = *t
302
+ text, range = *details
303
+ Rubocop::Cop::Token.new(range, type, text)
304
+ end
305
+ end
306
+
307
+ syntax_offences = diagnostics.map do |d|
308
+ Cop::Offence.new(d.level, d.location, "#{d.message}",
309
+ 'Syntax')
310
+ end
311
+
312
+ source = source_buffer.source.split($RS)
313
+
314
+ [ast, comments, tokens, source_buffer, source, syntax_offences]
315
+ end
316
+
317
+ # Generate a list of target files by expanding globing patterns
318
+ # (if any). If args is empty recursively finds all Ruby source
319
+ # files under the current directory
320
+ # @return [Array] array of filenames
321
+ def target_files(args)
322
+ return ruby_files if args.empty?
323
+
324
+ files = []
325
+
326
+ args.each do |target|
327
+ if File.directory?(target)
328
+ files += ruby_files(target.chomp(File::SEPARATOR))
329
+ elsif target =~ /\*/
330
+ files += Dir[target]
331
+ else
332
+ files << target
333
+ end
334
+ end
335
+
336
+ files.map { |f| File.expand_path(f) }.uniq
337
+ end
338
+
339
+ # Finds all Ruby source files under the current or other supplied
340
+ # directory. A Ruby source file is defined as a file with the `.rb`
341
+ # extension or a file with no extension that has a ruby shebang line
342
+ # as its first line.
343
+ # It is possible to specify includes and excludes using the config file,
344
+ # so you can include other Ruby files like Rakefiles and gemspecs.
345
+ # @param root Root directory under which to search for ruby source files
346
+ # @return [Array] Array of filenames
347
+ def ruby_files(root = Dir.pwd)
348
+ files = Dir["#{root}/**/*"].select { |file| FileTest.file?(file) }
349
+
350
+ rb = []
351
+
352
+ rb += files.select { |file| File.extname(file) == '.rb' }
353
+ rb += files.select do |file|
354
+ if File.extname(file) == '' && !excluded_file?(file)
355
+ begin
356
+ File.open(file) { |f| f.readline } =~ /#!.*ruby/
357
+ rescue EOFError, ArgumentError => e
358
+ log_error(e, "Unprocessable file #{file.inspect}: ")
359
+ false
360
+ end
361
+ end
362
+ end
363
+
364
+ rb += files.select do |file|
365
+ config = ConfigStore.for(file)
366
+ config.file_to_include?(file)
367
+ end
368
+
369
+ rb.reject { |file| excluded_file?(file) }.uniq
370
+ end
371
+
372
+ private
373
+
374
+ def log_error(e, msg = '')
375
+ if @options[:debug]
376
+ error_message = "#{e.class}, #{e.message}"
377
+ warn "#{msg}\t#{error_message}"
378
+ end
379
+ end
380
+
381
+ def formatter_set
382
+ @formatter_set ||= begin
383
+ set = Formatter::FormatterSet.new(!@options[:silent])
384
+ pairs = @options[:formatters] || [[DEFAULT_FORMATTER]]
385
+ pairs.each do |formatter_key, output_path|
386
+ set.add_formatter(formatter_key, output_path)
387
+ end
388
+ set
389
+ rescue => error
390
+ warn error.message
391
+ exit(1)
392
+ end
393
+ end
394
+
395
+ def deprecate(subject, alternative = nil, version = nil)
396
+ message = "#{subject} is deprecated"
397
+ message << " and will be removed in RuboCop #{version}" if version
398
+ message << '.'
399
+ message << " Please use #{alternative} instead." if alternative
400
+ warn message
401
+ end
402
+
403
+ def excluded_file?(file)
404
+ ConfigStore.for(file).file_to_exclude?(file)
405
+ end
406
+ end
407
+ end