rubocop 0.15.0 → 0.16.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 (221) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -10
  3. data/.travis.yml +2 -1
  4. data/CHANGELOG.md +55 -0
  5. data/Gemfile +3 -0
  6. data/README.md +37 -0
  7. data/config/default.yml +99 -16
  8. data/config/enabled.yml +28 -16
  9. data/lib/rubocop.rb +16 -2
  10. data/lib/rubocop/cli.rb +10 -91
  11. data/lib/rubocop/config.rb +4 -1
  12. data/lib/rubocop/config_loader.rb +18 -10
  13. data/lib/rubocop/config_store.rb +3 -2
  14. data/lib/rubocop/cop/check_assignment.rb +43 -0
  15. data/lib/rubocop/cop/check_methods.rb +18 -0
  16. data/lib/rubocop/cop/commissioner.rb +15 -3
  17. data/lib/rubocop/cop/cop.rb +51 -43
  18. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  19. data/lib/rubocop/cop/lint/block_alignment.rb +7 -7
  20. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  21. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  22. data/lib/rubocop/cop/lint/end_alignment.rb +56 -19
  23. data/lib/rubocop/cop/lint/end_in_method.rb +4 -10
  24. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  25. data/lib/rubocop/cop/lint/eval.rb +1 -1
  26. data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
  27. data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
  28. data/lib/rubocop/cop/lint/loop.rb +1 -1
  29. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  30. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  31. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  32. data/lib/rubocop/cop/lint/syntax.rb +1 -1
  33. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  34. data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
  35. data/lib/rubocop/cop/lint/useless_comparison.rb +1 -1
  36. data/lib/rubocop/cop/lint/useless_setter_call.rb +6 -16
  37. data/lib/rubocop/cop/lint/void.rb +5 -5
  38. data/lib/rubocop/cop/rails/default_scope.rb +33 -0
  39. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -4
  40. data/lib/rubocop/cop/rails/output.rb +1 -1
  41. data/lib/rubocop/cop/rails/read_attribute.rb +1 -1
  42. data/lib/rubocop/cop/rails/validation.rb +1 -1
  43. data/lib/rubocop/cop/style/access_modifier_indentation.rb +31 -16
  44. data/lib/rubocop/cop/style/accessor_method_name.rb +53 -0
  45. data/lib/rubocop/cop/style/alias.rb +14 -2
  46. data/lib/rubocop/cop/style/align_hash.rb +15 -16
  47. data/lib/rubocop/cop/style/and_or.rb +3 -3
  48. data/lib/rubocop/cop/style/array_syntax.rb +22 -0
  49. data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
  50. data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -3
  51. data/lib/rubocop/cop/style/attr.rb +1 -1
  52. data/lib/rubocop/cop/style/autocorrect_alignment.rb +13 -12
  53. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  54. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  55. data/lib/rubocop/cop/style/block_nesting.rb +5 -1
  56. data/lib/rubocop/cop/style/blocks.rb +4 -4
  57. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +48 -22
  58. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  59. data/lib/rubocop/cop/style/case_indentation.rb +25 -6
  60. data/lib/rubocop/cop/style/class_and_module_camel_case.rb +1 -1
  61. data/lib/rubocop/cop/style/class_length.rb +7 -19
  62. data/lib/rubocop/cop/style/class_methods.rb +1 -1
  63. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  64. data/lib/rubocop/cop/style/code_length.rb +35 -0
  65. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  66. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  67. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  68. data/lib/rubocop/cop/style/configurable_enforced_style.rb +51 -0
  69. data/lib/rubocop/cop/style/configurable_max.rb +17 -0
  70. data/lib/rubocop/cop/style/configurable_naming.rb +4 -10
  71. data/lib/rubocop/cop/style/constant_name.rb +5 -3
  72. data/lib/rubocop/cop/style/cyclomatic_complexity.rb +8 -13
  73. data/lib/rubocop/cop/style/def_parentheses.rb +4 -43
  74. data/lib/rubocop/cop/style/documentation.rb +1 -1
  75. data/lib/rubocop/cop/style/dot_position.rb +6 -2
  76. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  77. data/lib/rubocop/cop/style/empty_lines.rb +5 -1
  78. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
  79. data/lib/rubocop/cop/style/empty_lines_around_body.rb +28 -13
  80. data/lib/rubocop/cop/style/empty_literal.rb +3 -3
  81. data/lib/rubocop/cop/style/encoding.rb +5 -5
  82. data/lib/rubocop/cop/style/end_block.rb +1 -1
  83. data/lib/rubocop/cop/style/end_of_line.rb +8 -6
  84. data/lib/rubocop/cop/style/even_odd.rb +6 -6
  85. data/lib/rubocop/cop/style/favor_join.rb +1 -1
  86. data/lib/rubocop/cop/style/favor_modifier.rb +20 -34
  87. data/lib/rubocop/cop/style/favor_sprintf.rb +1 -1
  88. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -1
  89. data/lib/rubocop/cop/style/final_newline.rb +1 -1
  90. data/lib/rubocop/cop/style/flip_flop.rb +20 -0
  91. data/lib/rubocop/cop/style/for.rb +34 -3
  92. data/lib/rubocop/cop/style/global_vars.rb +1 -1
  93. data/lib/rubocop/cop/style/hash_methods.rb +3 -3
  94. data/lib/rubocop/cop/style/hash_syntax.rb +8 -11
  95. data/lib/rubocop/cop/style/if_node.rb +25 -0
  96. data/lib/rubocop/cop/style/if_then_else.rb +1 -1
  97. data/lib/rubocop/cop/style/indentation_width.rb +77 -43
  98. data/lib/rubocop/cop/style/lambda.rb +2 -2
  99. data/lib/rubocop/cop/style/lambda_call.rb +4 -12
  100. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -1
  101. data/lib/rubocop/cop/style/line_length.rb +9 -5
  102. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -1
  103. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  104. data/lib/rubocop/cop/style/method_def_parentheses.rb +52 -0
  105. data/lib/rubocop/cop/style/method_length.rb +9 -22
  106. data/lib/rubocop/cop/style/module_function.rb +1 -1
  107. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  108. data/lib/rubocop/cop/style/multiline_if_then.rb +11 -1
  109. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  110. data/lib/rubocop/cop/style/not.rb +1 -1
  111. data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
  112. data/lib/rubocop/cop/style/op_method.rb +2 -2
  113. data/lib/rubocop/cop/style/parameter_lists.rb +2 -2
  114. data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -1
  115. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  116. data/lib/rubocop/cop/style/predicate_name.rb +54 -0
  117. data/lib/rubocop/cop/style/proc.rb +1 -1
  118. data/lib/rubocop/cop/style/raise_args.rb +4 -10
  119. data/lib/rubocop/cop/style/redundant_begin.rb +5 -15
  120. data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
  121. data/lib/rubocop/cop/style/redundant_return.rb +9 -19
  122. data/lib/rubocop/cop/style/redundant_self.rb +8 -3
  123. data/lib/rubocop/cop/style/regexp_literal.rb +4 -3
  124. data/lib/rubocop/cop/style/rescue_modifier.rb +9 -17
  125. data/lib/rubocop/cop/style/semicolon.rb +20 -22
  126. data/lib/rubocop/cop/style/signal_exception.rb +10 -17
  127. data/lib/rubocop/cop/style/single_line_block_params.rb +62 -0
  128. data/lib/rubocop/cop/style/single_line_methods.rb +5 -15
  129. data/lib/rubocop/cop/style/space_after_comma_etc.rb +1 -3
  130. data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -1
  131. data/lib/rubocop/cop/style/space_after_method_name.rb +4 -12
  132. data/lib/rubocop/cop/style/space_after_not.rb +1 -1
  133. data/lib/rubocop/cop/style/space_around_block_braces.rb +105 -94
  134. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -1
  135. data/lib/rubocop/cop/style/space_around_operators.rb +38 -123
  136. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +1 -1
  137. data/lib/rubocop/cop/style/space_inside.rb +1 -3
  138. data/lib/rubocop/cop/style/space_inside_brackets.rb +0 -2
  139. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +5 -5
  140. data/lib/rubocop/cop/style/space_inside_parens.rb +0 -2
  141. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  142. data/lib/rubocop/cop/style/string_help.rb +1 -1
  143. data/lib/rubocop/cop/style/string_literals.rb +4 -11
  144. data/lib/rubocop/cop/style/surrounding_space.rb +0 -2
  145. data/lib/rubocop/cop/style/symbol_array.rb +3 -10
  146. data/lib/rubocop/cop/style/tab.rb +5 -5
  147. data/lib/rubocop/cop/style/ternary_operator.rb +2 -2
  148. data/lib/rubocop/cop/style/trailing_blank_lines.rb +1 -1
  149. data/lib/rubocop/cop/style/trailing_whitespace.rb +1 -1
  150. data/lib/rubocop/cop/style/trivial_accessors.rb +4 -14
  151. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  152. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  153. data/lib/rubocop/cop/style/when_then.rb +3 -1
  154. data/lib/rubocop/cop/style/while_until_do.rb +2 -2
  155. data/lib/rubocop/cop/style/word_array.rb +4 -10
  156. data/lib/rubocop/cop/util.rb +85 -12
  157. data/lib/rubocop/cop/variable_inspector.rb +4 -2
  158. data/lib/rubocop/file_inspector.rb +98 -0
  159. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  160. data/lib/rubocop/formatter/disabled_config_formatter.rb +10 -2
  161. data/lib/rubocop/rake_task.rb +5 -1
  162. data/lib/rubocop/target_finder.rb +3 -3
  163. data/lib/rubocop/version.rb +1 -1
  164. data/rubocop-todo.yml +12 -0
  165. data/rubocop.gemspec +1 -1
  166. data/spec/isolated_environment_spec.rb +24 -0
  167. data/spec/rubocop/cli_spec.rb +120 -27
  168. data/spec/rubocop/config_loader_spec.rb +25 -11
  169. data/spec/rubocop/config_spec.rb +25 -9
  170. data/spec/rubocop/config_store_spec.rb +1 -1
  171. data/spec/rubocop/cop/cop_spec.rb +22 -3
  172. data/spec/rubocop/cop/lint/block_alignment_spec.rb +4 -4
  173. data/spec/rubocop/cop/lint/end_alignment_spec.rb +79 -47
  174. data/spec/rubocop/cop/lint/syntax_spec.rb +4 -3
  175. data/spec/rubocop/cop/rails/default_scope_spec.rb +31 -0
  176. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +37 -2
  177. data/spec/rubocop/cop/style/accessor_method_name_spec.rb +81 -0
  178. data/spec/rubocop/cop/style/alias_spec.rb +10 -0
  179. data/spec/rubocop/cop/style/align_parameters_spec.rb +3 -3
  180. data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -1
  181. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +83 -0
  182. data/spec/rubocop/cop/style/case_indentation_spec.rb +227 -71
  183. data/spec/rubocop/cop/style/class_length_spec.rb +2 -0
  184. data/spec/rubocop/cop/style/constant_name_spec.rb +6 -0
  185. data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +1 -0
  186. data/spec/rubocop/cop/style/documentation_spec.rb +12 -0
  187. data/spec/rubocop/cop/style/dot_position_spec.rb +10 -0
  188. data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +22 -0
  189. data/spec/rubocop/cop/style/empty_lines_spec.rb +6 -0
  190. data/spec/rubocop/cop/style/end_of_line_spec.rb +17 -0
  191. data/spec/rubocop/cop/style/favor_modifier_spec.rb +15 -0
  192. data/spec/rubocop/cop/style/flip_flop_spec.rb +23 -0
  193. data/spec/rubocop/cop/style/for_spec.rb +96 -18
  194. data/spec/rubocop/cop/style/hash_syntax_spec.rb +8 -2
  195. data/spec/rubocop/cop/style/indentation_width_spec.rb +185 -20
  196. data/spec/rubocop/cop/style/line_length_spec.rb +1 -0
  197. data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +72 -0
  198. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +16 -0
  199. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +34 -3
  200. data/spec/rubocop/cop/style/predicate_name_spec.rb +25 -0
  201. data/spec/rubocop/cop/style/redundant_self_spec.rb +15 -0
  202. data/spec/rubocop/cop/style/regexp_literal_spec.rb +5 -0
  203. data/spec/rubocop/cop/style/semicolon_spec.rb +16 -16
  204. data/spec/rubocop/cop/style/{reduce_arguments_spec.rb → single_line_block_params_spec.rb} +12 -4
  205. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +58 -8
  206. data/spec/rubocop/cop/style/space_around_operators_spec.rb +71 -0
  207. data/spec/rubocop/cop/style/string_literals_spec.rb +18 -0
  208. data/spec/rubocop/file_inspector_spec.rb +78 -0
  209. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +7 -7
  210. data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +3 -3
  211. data/spec/rubocop/formatter/file_list_formatter_spec.rb +2 -2
  212. data/spec/rubocop/processed_source_spec.rb +7 -3
  213. data/spec/rubocop/token_spec.rb +1 -1
  214. data/spec/spec_helper.rb +3 -2
  215. data/spec/support/isolated_environment.rb +7 -0
  216. data/spec/support/shared_context.rb +5 -1
  217. metadata +35 -12
  218. data/lib/rubocop/cop/style/reduce_arguments.rb +0 -34
  219. data/lib/rubocop/cop/style/symbol_name.rb +0 -45
  220. data/spec/rubocop/cop/style/def_without_parentheses_spec.rb +0 -33
  221. data/spec/rubocop/cop/style/symbol_name_spec.rb +0 -138
@@ -131,12 +131,12 @@ describe Rubocop::Cop::Lint::BlockAlignment do
131
131
  'end']
132
132
  inspect_source(cop, src)
133
133
  expect(cop.messages)
134
- .to eq(['end at 10, 8 is not aligned with bar.get_stuffs at 2, 2 or' +
135
- ' .select do |stuff| at 8, 6',
134
+ .to eq(['end at 5, 8 is not aligned with bar.get_stuffs at 2, 2 or' +
135
+ ' .reject do |stuff| at 3, 6',
136
136
  'end at 7, 4 is not aligned with bar.get_stuffs at 2, 2 or' +
137
137
  ' end.select do |stuff| at 5, 8',
138
- 'end at 5, 8 is not aligned with bar.get_stuffs at 2, 2 or' +
139
- ' .reject do |stuff| at 3, 6'])
138
+ 'end at 10, 8 is not aligned with bar.get_stuffs at 2, 2 or' +
139
+ ' .select do |stuff| at 8, 6'])
140
140
  end
141
141
 
142
142
  # Example from issue 393 of bbatsov/rubocop on github:
@@ -1,64 +1,96 @@
1
1
  # encoding: utf-8
2
- # rubocop:disable LineLength
3
2
 
4
3
  require 'spec_helper'
5
4
 
6
- describe Rubocop::Cop::Lint::EndAlignment do
7
- subject(:cop) { described_class.new }
5
+ describe Rubocop::Cop::Lint::EndAlignment, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+ let(:cop_config) { {} }
8
+ let(:cop_config) { { 'AlignWith' => 'keyword' } }
8
9
 
9
- it 'registers an offence for mismatched class end' do
10
- inspect_source(cop,
11
- ['class Test',
12
- ' end'
13
- ])
14
- expect(cop.offences.size).to eq(1)
10
+ shared_examples 'misaligned' do |alignment_base, arg, end_kw, name|
11
+ name ||= alignment_base
12
+ it "registers an offence for mismatched #{name} ... end" do
13
+ inspect_source(cop, ["#{alignment_base} #{arg}",
14
+ end_kw])
15
+ expect(cop.offences.size).to eq(1)
16
+ expect(cop.messages.first)
17
+ .to match(/end at 2, \d is not aligned with #{alignment_base} at 1,/)
18
+ expect(cop.highlights.first).to eq('end')
19
+ end
15
20
  end
16
21
 
17
- it 'registers an offence for mismatched module end' do
18
- inspect_source(cop,
19
- ['module Test',
20
- ' end'
21
- ])
22
- expect(cop.offences.size).to eq(1)
22
+ shared_examples 'aligned' do |alignment_base, arg, end_kw, name|
23
+ name ||= alignment_base
24
+ it "accepts matching #{name} ... end" do
25
+ inspect_source(cop, ["#{alignment_base} #{arg}",
26
+ end_kw])
27
+ expect(cop.offences).to be_empty
28
+ end
23
29
  end
24
30
 
25
- it 'registers an offence for mismatched def end' do
26
- inspect_source(cop,
27
- ['def test',
28
- ' end'
29
- ])
30
- expect(cop.offences.size).to eq(1)
31
- end
31
+ include_examples 'misaligned', 'class', 'Test', ' end'
32
+ include_examples 'misaligned', 'module', 'Test', ' end'
33
+ include_examples 'misaligned', 'def', 'test', ' end'
34
+ include_examples 'misaligned', 'def', 'Test.test', ' end', 'defs'
35
+ include_examples 'misaligned', 'if', 'test', ' end'
36
+ include_examples 'misaligned', 'unless', 'test', ' end'
37
+ include_examples 'misaligned', 'while', 'test', ' end'
38
+ include_examples 'misaligned', 'until', 'test', ' end'
32
39
 
33
- it 'registers an offence for mismatched defs end' do
34
- inspect_source(cop,
35
- ['def Test.test',
36
- ' end'
37
- ])
38
- expect(cop.offences.size).to eq(1)
39
- end
40
+ include_examples 'aligned', 'class', 'Test', 'end'
41
+ include_examples 'aligned', 'module', 'Test', 'end'
42
+ include_examples 'aligned', 'def', 'test', 'end'
43
+ include_examples 'aligned', 'def', 'Test.test', 'end', 'defs'
44
+ include_examples 'aligned', 'if', 'test', 'end'
45
+ include_examples 'aligned', 'unless', 'test', 'end'
46
+ include_examples 'aligned', 'while', 'test', 'end'
47
+ include_examples 'aligned', 'until', 'test', 'end'
40
48
 
41
- it 'registers an offence for mismatched if end' do
42
- inspect_source(cop,
43
- ['if test',
44
- ' end'
45
- ])
46
- expect(cop.offences.size).to eq(1)
49
+ it 'can handle ternary if' do
50
+ inspect_source(cop, 'a = cond ? x : y')
51
+ expect(cop.offences).to be_empty
47
52
  end
48
53
 
49
- it 'registers an offence for mismatched while end' do
50
- inspect_source(cop,
51
- ['while test',
52
- ' end'
53
- ])
54
- expect(cop.offences.size).to eq(1)
54
+ it 'can handle modifier if' do
55
+ inspect_source(cop, 'a = x if cond')
56
+ expect(cop.offences).to be_empty
55
57
  end
56
58
 
57
- it 'registers an offence for mismatched until end' do
58
- inspect_source(cop,
59
- ['until test',
60
- ' end'
61
- ])
62
- expect(cop.offences.size).to eq(1)
59
+ context 'regarding assignment' do
60
+ context 'when AlignWith is keyword' do
61
+ include_examples 'misaligned', 'var = if', 'test', 'end'
62
+ include_examples 'misaligned', 'var = unless', 'test', 'end'
63
+ include_examples 'misaligned', 'var = while', 'test', 'end'
64
+ include_examples 'misaligned', 'var = until', 'test', 'end'
65
+
66
+ include_examples 'aligned', 'var = if', 'test', ' end'
67
+ include_examples 'aligned', 'var = unless', 'test', ' end'
68
+ include_examples 'aligned', 'var = while', 'test', ' end'
69
+ include_examples 'aligned', 'var = until', 'test', ' end'
70
+ end
71
+
72
+ context 'when AlignWith is variable' do
73
+ let(:cop_config) { { 'AlignWith' => 'variable' } }
74
+
75
+ include_examples 'aligned', 'var = if', 'test', 'end'
76
+ include_examples 'aligned', 'var = unless', 'test', 'end'
77
+ include_examples 'aligned', 'var = while', 'test', 'end'
78
+ include_examples 'aligned', 'var = until', 'test', 'end'
79
+ include_examples 'aligned', 'var = until', 'test', 'end.abc.join("")'
80
+ include_examples 'aligned', 'var = until', 'test', 'end.abc.tap {}'
81
+
82
+ include_examples 'misaligned', 'var = if', 'test', ' end'
83
+ include_examples 'misaligned', 'var = unless', 'test', ' end'
84
+ include_examples 'misaligned', 'var = while', 'test', ' end'
85
+ include_examples 'misaligned', 'var = until', 'test', ' end'
86
+ include_examples 'misaligned', 'var = until', 'test', ' end.join'
87
+
88
+ include_examples 'aligned', '@var = if', 'test', 'end'
89
+ include_examples 'aligned', '$var = if', 'test', 'end'
90
+ include_examples 'aligned', 'CNST = if', 'test', 'end'
91
+ include_examples 'aligned', 'var ||= if', 'test', 'end'
92
+ include_examples 'aligned', 'var &&= if', 'test', 'end'
93
+ include_examples 'aligned', 'var += if', 'test', 'end'
94
+ end
63
95
  end
64
96
  end
@@ -5,9 +5,10 @@ require 'spec_helper'
5
5
  describe Rubocop::Cop::Lint::Syntax do
6
6
  describe '.offences_from_diagnostic' do
7
7
  subject(:offence) { described_class.offence_from_diagnostic(diagnostic) }
8
- let(:diagnostic) { Parser::Diagnostic.new(level, message, location) }
8
+ let(:diagnostic) { Parser::Diagnostic.new(level, reason, args, location) }
9
9
  let(:level) { :warning }
10
- let(:message) { 'This is a message' }
10
+ let(:reason) { :odd_hash }
11
+ let(:args) { [] }
11
12
  let(:location) { double('location').as_null_object }
12
13
 
13
14
  it 'returns an offence' do
@@ -19,7 +20,7 @@ describe Rubocop::Cop::Lint::Syntax do
19
20
  end
20
21
 
21
22
  it "sets diagnostic's message to offence's message" do
22
- expect(offence.message).to eq(message)
23
+ expect(offence.message).to eq('odd number of entries for a hash')
23
24
  end
24
25
 
25
26
  it "sets diagnostic's location to offence's location" do
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Rails::DefaultScope do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'registers an offence for default scope with a lambda arg' do
9
+ inspect_source(cop,
10
+ ['default_scope -> { something }'])
11
+ expect(cop.offences.size).to eq(1)
12
+ end
13
+
14
+ it 'registers an offence for default scope with a proc arg' do
15
+ inspect_source(cop,
16
+ ['default_scope proc { something }'])
17
+ expect(cop.offences.size).to eq(1)
18
+ end
19
+
20
+ it 'registers an offence for default scope with a proc(Proc.new) arg' do
21
+ inspect_source(cop,
22
+ ['default_scope Proc.new { something }'])
23
+ expect(cop.offences.size).to eq(1)
24
+ end
25
+
26
+ it 'accepts a block arg' do
27
+ inspect_source(cop,
28
+ ['default_scope { something }'])
29
+ expect(cop.offences).to be_empty
30
+ end
31
+ end
@@ -19,19 +19,53 @@ describe Rubocop::Cop::Style::AccessModifierIndentation, :config do
19
19
  expect(cop.offences.size).to eq(1)
20
20
  expect(cop.messages)
21
21
  .to eq(['Indent access modifiers like private.'])
22
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'outdent')
22
23
  end
23
24
 
24
25
  it 'registers an offence for misaligned private in module' do
25
26
  inspect_source(cop,
26
27
  ['module Test',
28
+ '',
29
+ ' private',
30
+ '',
31
+ ' def test; end',
32
+ 'end'])
33
+ expect(cop.offences.size).to eq(1)
34
+ expect(cop.messages).to eq(['Indent access modifiers like private.'])
35
+ # Not aligned according to `indent` or `outdent` style:
36
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
37
+ end
38
+
39
+ it 'registers an offence for correct + opposite alignment' do
40
+ inspect_source(cop,
41
+ ['module Test',
42
+ '',
43
+ ' public',
27
44
  '',
28
45
  'private',
29
46
  '',
30
47
  ' def test; end',
31
48
  'end'])
32
49
  expect(cop.offences.size).to eq(1)
33
- expect(cop.messages)
34
- .to eq(['Indent access modifiers like private.'])
50
+ expect(cop.messages).to eq(['Indent access modifiers like private.'])
51
+ # No EnforcedStyle can allow both aligments:
52
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
53
+ end
54
+
55
+ it 'registers an offence for opposite + correct alignment' do
56
+ inspect_source(cop,
57
+ ['module Test',
58
+ '',
59
+ 'public',
60
+ '',
61
+ ' private',
62
+ '',
63
+ ' def test; end',
64
+ 'end'])
65
+ expect(cop.offences.size).to eq(1)
66
+ expect(cop.messages).to eq(['Indent access modifiers like public.'])
67
+ # No EnforcedStyle can allow both aligments:
68
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
35
69
  end
36
70
 
37
71
  it 'registers an offence for misaligned private in singleton class' do
@@ -146,6 +180,7 @@ describe Rubocop::Cop::Style::AccessModifierIndentation, :config do
146
180
  'end'])
147
181
  expect(cop.offences.size).to eq(1)
148
182
  expect(cop.messages).to eq([indent_msg])
183
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'indent')
149
184
  end
150
185
 
151
186
  it 'registers offence for private indented to method depth in a module' do
@@ -0,0 +1,81 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::AccessorMethodName do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'registers an offence for method get_... with no args' do
9
+ inspect_source(cop, ['def get_attr',
10
+ ' # ...',
11
+ 'end'])
12
+ expect(cop.offences.size).to eq(1)
13
+ expect(cop.highlights).to eq(['get_attr'])
14
+ end
15
+
16
+ it 'registers an offence for singleton method get_... with no args' do
17
+ inspect_source(cop, ['def self.get_attr',
18
+ ' # ...',
19
+ 'end'])
20
+ expect(cop.offences.size).to eq(1)
21
+ expect(cop.highlights).to eq(['get_attr'])
22
+ end
23
+
24
+ it 'accepts method get_something with args' do
25
+ inspect_source(cop, ['def get_something(arg)',
26
+ ' # ...',
27
+ 'end'])
28
+ expect(cop.offences).to be_empty
29
+ end
30
+
31
+ it 'accepts singleton method get_something with args' do
32
+ inspect_source(cop, ['def self.get_something(arg)',
33
+ ' # ...',
34
+ 'end'])
35
+ expect(cop.offences).to be_empty
36
+ end
37
+
38
+ it 'registers an offence for method set_something with one arg' do
39
+ inspect_source(cop, ['def set_attr(arg)',
40
+ ' # ...',
41
+ 'end'])
42
+ expect(cop.offences.size).to eq(1)
43
+ expect(cop.highlights).to eq(['set_attr'])
44
+ end
45
+
46
+ it 'registers an offence for singleton method set_... with one args' do
47
+ inspect_source(cop, ['def self.set_attr(arg)',
48
+ ' # ...',
49
+ 'end'])
50
+ expect(cop.offences.size).to eq(1)
51
+ expect(cop.highlights).to eq(['set_attr'])
52
+ end
53
+
54
+ it 'accepts method set_something with no args' do
55
+ inspect_source(cop, ['def set_something',
56
+ ' # ...',
57
+ 'end'])
58
+ expect(cop.offences).to be_empty
59
+ end
60
+
61
+ it 'accepts singleton method set_something with no args' do
62
+ inspect_source(cop, ['def self.set_something',
63
+ ' # ...',
64
+ 'end'])
65
+ expect(cop.offences).to be_empty
66
+ end
67
+
68
+ it 'accepts method set_something with two args' do
69
+ inspect_source(cop, ['def set_something(arg1, arg2)',
70
+ ' # ...',
71
+ 'end'])
72
+ expect(cop.offences).to be_empty
73
+ end
74
+
75
+ it 'accepts singleton method set_something with two args' do
76
+ inspect_source(cop, ['def self.get_something(arg1, arg2)',
77
+ ' # ...',
78
+ 'end'])
79
+ expect(cop.offences).to be_empty
80
+ end
81
+ end
@@ -13,6 +13,11 @@ describe Rubocop::Cop::Style::Alias do
13
13
  .to eq(['Use alias_method instead of alias.'])
14
14
  end
15
15
 
16
+ it 'autocorrects alias with symbol args' do
17
+ corrected = autocorrect_source(cop, ['alias :ala :bala'])
18
+ expect(corrected).to eq 'alias_method :ala, :bala'
19
+ end
20
+
16
21
  it 'registers an offence for alias with bareword args' do
17
22
  inspect_source(cop,
18
23
  ['alias ala bala'])
@@ -21,6 +26,11 @@ describe Rubocop::Cop::Style::Alias do
21
26
  .to eq(['Use alias_method instead of alias.'])
22
27
  end
23
28
 
29
+ it 'autocorrects alias with bareword args' do
30
+ corrected = autocorrect_source(cop, ['alias ala bala'])
31
+ expect(corrected).to eq 'alias_method :ala, :bala'
32
+ end
33
+
24
34
  it 'does not register an offence for alias_method' do
25
35
  inspect_source(cop,
26
36
  ['alias_method :ala, :bala'])
@@ -38,7 +38,7 @@ describe Rubocop::Cop::Style::AlignParameters do
38
38
  end
39
39
 
40
40
  it "doesn't get confused by a symbol argument" do
41
- inspect_source(cop, ['add_offence(:convention, index,',
41
+ inspect_source(cop, ['add_offence(index,',
42
42
  ' MSG % kind)'])
43
43
  expect(cop.offences).to be_empty
44
44
  end
@@ -68,13 +68,13 @@ describe Rubocop::Cop::Style::AlignParameters do
68
68
  end
69
69
 
70
70
  it 'can handle a correctly aligned string literal as first argument' do
71
- inspect_source(cop, ['add_offence(:convention, x,',
71
+ inspect_source(cop, ['add_offence(x,',
72
72
  ' a)'])
73
73
  expect(cop.offences).to be_empty
74
74
  end
75
75
 
76
76
  it 'can handle a string literal as other argument' do
77
- inspect_source(cop, ['add_offence(:convention,',
77
+ inspect_source(cop, ['add_offence(',
78
78
  ' "", a)'])
79
79
  expect(cop.offences).to be_empty
80
80
  end
@@ -144,10 +144,13 @@ describe Rubocop::Cop::Style::BlockNesting, :config do
144
144
  expect_nesting_offences(source, [])
145
145
  end
146
146
 
147
- def expect_nesting_offences(source, lines)
147
+ def expect_nesting_offences(source, lines, used_nesting_level = 3)
148
148
  inspect_source(cop, source)
149
149
  expect(cop.offences.map(&:line)).to eq(lines)
150
150
  expect(cop.messages).to eq(
151
151
  ['Avoid more than 2 levels of block nesting.'] * lines.length)
152
+ if cop.offences.size > 0
153
+ expect(cop.config_to_allow_offences['Max']).to eq(used_nesting_level)
154
+ end
152
155
  end
153
156
  end
@@ -80,6 +80,25 @@ describe Rubocop::Cop::Style::BracesAroundHashParameters, :config do
80
80
  'Redundant curly braces around a hash parameter.'
81
81
  ])
82
82
  expect(cop.highlights).to eq(['{ y: 2 }'])
83
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'braces')
84
+ end
85
+
86
+ it 'correct + opposite style' do
87
+ inspect_source(cop, ['where(1, y: 2)',
88
+ 'where(1, { y: 2 })'])
89
+ expect(cop.messages).to eq([
90
+ 'Redundant curly braces around a hash parameter.'
91
+ ])
92
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
93
+ end
94
+
95
+ it 'opposite + correct style' do
96
+ inspect_source(cop, ['where(1, { y: 2 })',
97
+ 'where(1, y: 2)'])
98
+ expect(cop.messages).to eq([
99
+ 'Redundant curly braces around a hash parameter.'
100
+ ])
101
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
83
102
  end
84
103
 
85
104
  it 'one object method hash parameter with braces' do
@@ -114,6 +133,33 @@ describe Rubocop::Cop::Style::BracesAroundHashParameters, :config do
114
133
  expect(cop.highlights).to eq(['{ x: 1, foo: "bar" }'])
115
134
  end
116
135
  end
136
+
137
+ describe 'auto-corrects' do
138
+ it 'one non-hash parameter followed by a hash parameter with braces' do
139
+ corrected = autocorrect_source(cop, ['where(1, { y: 2 })'])
140
+ expect(corrected).to eq 'where(1, y: 2 )'
141
+ end
142
+
143
+ it 'one object method hash parameter with braces' do
144
+ corrected = autocorrect_source(cop, ['x.func({ y: "z" })'])
145
+ expect(corrected).to eq 'x.func( y: "z" )'
146
+ end
147
+
148
+ it 'one hash parameter with braces' do
149
+ corrected = autocorrect_source(cop, ['where({ x: 1 })'])
150
+ expect(corrected).to eq 'where( x: 1 )'
151
+ end
152
+
153
+ it 'one hash parameter with braces and separators' do
154
+ corrected = autocorrect_source(cop, ["where( \n { x: 1 } )"])
155
+ expect(corrected).to eq "where( \n x: 1 )"
156
+ end
157
+
158
+ it 'one hash parameter with braces and multiple keys' do
159
+ corrected = autocorrect_source(cop, ['where({ x: 1, foo: "bar" })'])
160
+ expect(corrected).to eq 'where( x: 1, foo: "bar" )'
161
+ end
162
+ end
117
163
  end
118
164
 
119
165
  context 'braces' do
@@ -174,6 +220,26 @@ describe Rubocop::Cop::Style::BracesAroundHashParameters, :config do
174
220
  'Missing curly braces around a hash parameter.'
175
221
  ])
176
222
  expect(cop.highlights).to eq(['x: "y"'])
223
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' =>
224
+ 'no_braces')
225
+ end
226
+
227
+ it 'opposite + correct style' do
228
+ inspect_source(cop, ['where(y: 2)',
229
+ 'where({ y: 2 })'])
230
+ expect(cop.messages).to eq([
231
+ 'Missing curly braces around a hash parameter.'
232
+ ])
233
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
234
+ end
235
+
236
+ it 'correct + opposite style' do
237
+ inspect_source(cop, ['where({ y: 2 })',
238
+ 'where(y: 2)'])
239
+ expect(cop.messages).to eq([
240
+ 'Missing curly braces around a hash parameter.'
241
+ ])
242
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
177
243
  end
178
244
 
179
245
  it 'one hash parameter with multiple keys and without braces' do
@@ -192,5 +258,22 @@ describe Rubocop::Cop::Style::BracesAroundHashParameters, :config do
192
258
  expect(cop.highlights).to eq(['x: { "y" => "z" }'])
193
259
  end
194
260
  end
261
+
262
+ describe 'auto-corrects' do
263
+ it 'one hash parameter without braces' do
264
+ corrected = autocorrect_source(cop, ['where(x: "y")'])
265
+ expect(corrected).to eq 'where({x: "y"})'
266
+ end
267
+
268
+ it 'one hash parameter with multiple keys and without braces' do
269
+ corrected = autocorrect_source(cop, ['where(x: "y", foo: "bar")'])
270
+ expect(corrected).to eq 'where({x: "y", foo: "bar"})'
271
+ end
272
+
273
+ it 'one hash parameter without braces with one hash value' do
274
+ corrected = autocorrect_source(cop, ['where(x: { "y" => "z" })'])
275
+ expect(corrected).to eq 'where({x: { "y" => "z" }})'
276
+ end
277
+ end
195
278
  end
196
279
  end