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
@@ -152,6 +152,21 @@ describe Rubocop::Cop::Style::WhileUntilModifier do
152
152
  check_short_multiline(cop, 'while')
153
153
  end
154
154
 
155
+ it 'accepts oneline while when condition has local variable assignment' do
156
+ inspect_source(cop, ['lines = %w{first second third}',
157
+ 'while (line = lines.shift)',
158
+ ' puts line',
159
+ 'end'])
160
+ expect(cop.offences).to be_empty
161
+ end
162
+
163
+ it 'registers an offence for oneline while when assignment is in body' do
164
+ inspect_source(cop, ['while true',
165
+ ' x = 0',
166
+ 'end'])
167
+ expect(cop.offences.size).to eq(1)
168
+ end
169
+
155
170
  it 'registers an offence for multiline until that fits on one line' do
156
171
  check_really_short(cop, 'until')
157
172
  end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::FlipFlop do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'registers an offence for inclusive flip flops' do
9
+ inspect_source(cop,
10
+ ['DATA.each_line do |line|',
11
+ 'print line if (line =~ /begin/)..(line =~ /end/)',
12
+ 'end'])
13
+ expect(cop.offences.size).to eq(1)
14
+ end
15
+
16
+ it 'registers an offence for exclusive flip flops' do
17
+ inspect_source(cop,
18
+ ['DATA.each_line do |line|',
19
+ 'print line if (line =~ /begin/)...(line =~ /end/)',
20
+ 'end'])
21
+ expect(cop.offences.size).to eq(1)
22
+ end
23
+ end
@@ -2,26 +2,104 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::For do
6
- subject(:cop) { described_class.new }
7
-
8
- it 'registers an offence for for' do
9
- inspect_source(cop,
10
- ['def func',
11
- ' for n in [1, 2, 3] do',
12
- ' puts n',
13
- ' end',
14
- 'end'])
15
- expect(cop.offences.size).to eq(1)
16
- end
5
+ describe Rubocop::Cop::Style::For, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+
8
+ context 'when each is the enforced style' do
9
+ let(:cop_config) { { 'EnforcedStyle' => 'each' } }
10
+
11
+ it 'registers an offence for for' do
12
+ inspect_source(cop,
13
+ ['def func',
14
+ ' for n in [1, 2, 3] do',
15
+ ' puts n',
16
+ ' end',
17
+ 'end'])
18
+ expect(cop.messages).to eq(['Prefer *each* over *for*.'])
19
+ expect(cop.highlights).to eq(['for'])
20
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'for')
21
+ end
22
+
23
+ it 'registers an offence for opposite + correct style' do
24
+ inspect_source(cop,
25
+ ['def func',
26
+ ' for n in [1, 2, 3] do',
27
+ ' puts n',
28
+ ' end',
29
+ ' [1, 2, 3].each do |n|',
30
+ ' puts n',
31
+ ' end',
32
+ 'end'])
33
+ expect(cop.messages).to eq(['Prefer *each* over *for*.'])
34
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
35
+ end
36
+
37
+ it 'accepts multiline each' do
38
+ inspect_source(cop,
39
+ ['def func',
40
+ ' [1, 2, 3].each do |n|',
41
+ ' puts n',
42
+ ' end',
43
+ 'end'])
44
+ expect(cop.offences).to be_empty
45
+ end
17
46
 
18
- it 'does not register an offence for :for' do
19
- inspect_source(cop, ['[:for, :ala, :bala]'])
20
- expect(cop.offences).to be_empty
47
+ it 'accepts :for' do
48
+ inspect_source(cop, ['[:for, :ala, :bala]'])
49
+ expect(cop.offences).to be_empty
50
+ end
51
+
52
+ it 'accepts def for' do
53
+ inspect_source(cop, ['def for; end'])
54
+ expect(cop.offences).to be_empty
55
+ end
21
56
  end
22
57
 
23
- it 'does not register an offence for def for' do
24
- inspect_source(cop, ['def for; end'])
25
- expect(cop.offences).to be_empty
58
+ context 'when for is the enforced style' do
59
+ let(:cop_config) { { 'EnforcedStyle' => 'for' } }
60
+
61
+ it 'accepts for' do
62
+ inspect_source(cop,
63
+ ['def func',
64
+ ' for n in [1, 2, 3] do',
65
+ ' puts n',
66
+ ' end',
67
+ 'end'])
68
+ expect(cop.offences).to be_empty
69
+ end
70
+
71
+ it 'registers an offence for multiline each' do
72
+ inspect_source(cop,
73
+ ['def func',
74
+ ' [1, 2, 3].each do |n|',
75
+ ' puts n',
76
+ ' end',
77
+ 'end'])
78
+ expect(cop.messages).to eq(['Prefer *for* over *each*.'])
79
+ expect(cop.highlights).to eq(['each'])
80
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'each')
81
+ end
82
+
83
+ it 'registers an offence for correct + opposite style' do
84
+ inspect_source(cop,
85
+ ['def func',
86
+ ' for n in [1, 2, 3] do',
87
+ ' puts n',
88
+ ' end',
89
+ ' [1, 2, 3].each do |n|',
90
+ ' puts n',
91
+ ' end',
92
+ 'end'])
93
+ expect(cop.messages).to eq(['Prefer *for* over *each*.'])
94
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
95
+ end
96
+
97
+ it 'accepts single line each' do
98
+ inspect_source(cop,
99
+ ['def func',
100
+ ' [1, 2, 3].each { |n| puts n }',
101
+ 'end'])
102
+ expect(cop.offences).to be_empty
103
+ end
26
104
  end
27
105
  end
@@ -7,7 +7,10 @@ describe Rubocop::Cop::Style::HashSyntax, :config do
7
7
 
8
8
  context 'configured to enforce ruby19 style' do
9
9
  let(:config) do
10
- Rubocop::Config.new('HashSyntax' => { 'EnforcedStyle' => 'ruby19' },
10
+ Rubocop::Config.new('HashSyntax' => {
11
+ 'EnforcedStyle' => 'ruby19',
12
+ 'SupportedStyles' => %w(ruby19 hash_rockets)
13
+ },
11
14
  'SpaceAroundOperators' => { 'Enabled' => true })
12
15
  end
13
16
 
@@ -73,7 +76,10 @@ describe Rubocop::Cop::Style::HashSyntax, :config do
73
76
 
74
77
  context 'with SpaceAroundOperators disabled' do
75
78
  let(:config) do
76
- Rubocop::Config.new('HashSyntax' => { 'EnforcedStyle' => 'ruby19' },
79
+ Rubocop::Config.new('HashSyntax' => {
80
+ 'EnforcedStyle' => 'ruby19',
81
+ 'SupportedStyles' => %w(ruby19 hash_rockets)
82
+ },
77
83
  'SpaceAroundOperators' => { 'Enabled' => false })
78
84
  end
79
85
 
@@ -10,9 +10,11 @@ describe Rubocop::Cop::Style::IndentationWidth do
10
10
  inspect_source(cop,
11
11
  ['if cond',
12
12
  ' func',
13
+ ' func',
13
14
  'end'])
14
15
  expect(cop.messages)
15
- .to eq(['Use 2 (not 1) spaces for indentation.'])
16
+ .to eq(['Use 2 (not 1) spaces for indentation.',
17
+ 'Inconsistent indentation detected.'])
16
18
  end
17
19
 
18
20
  it 'registers an offence for bad indentation of an else body' do
@@ -21,9 +23,11 @@ describe Rubocop::Cop::Style::IndentationWidth do
21
23
  ' func1',
22
24
  'else',
23
25
  ' func2',
26
+ ' func2',
24
27
  'end'])
25
28
  expect(cop.messages)
26
- .to eq(['Use 2 (not 1) spaces for indentation.'])
29
+ .to eq(['Use 2 (not 1) spaces for indentation.',
30
+ 'Inconsistent indentation detected.'])
27
31
  end
28
32
 
29
33
  it 'registers an offence for bad indentation of an elsif body' do
@@ -32,11 +36,13 @@ describe Rubocop::Cop::Style::IndentationWidth do
32
36
  ' b1',
33
37
  'elsif a2',
34
38
  ' b2',
39
+ 'b3',
35
40
  'else',
36
41
  ' c',
37
42
  'end'])
38
43
  expect(cop.messages)
39
- .to eq(['Use 2 (not 1) spaces for indentation.'])
44
+ .to eq(['Use 2 (not 1) spaces for indentation.',
45
+ 'Inconsistent indentation detected.'])
40
46
  end
41
47
 
42
48
  it 'registers offence for bad indentation of ternary inside else' do
@@ -106,6 +112,94 @@ describe Rubocop::Cop::Style::IndentationWidth do
106
112
  expect(cop.offences).to be_empty
107
113
  end
108
114
 
115
+ it 'accepts an if in assignment with end aligned with variable' do
116
+ inspect_source(cop,
117
+ ['var = if a',
118
+ ' 0',
119
+ 'end',
120
+ '@var = if a',
121
+ ' 0',
122
+ 'end',
123
+ '$var = if a',
124
+ ' 0',
125
+ 'end',
126
+ 'var ||= if a',
127
+ ' 0',
128
+ 'end',
129
+ 'var &&= if a',
130
+ ' 0',
131
+ 'end',
132
+ 'var -= if a',
133
+ ' 0',
134
+ 'end',
135
+ 'VAR = if a',
136
+ ' 0',
137
+ 'end'])
138
+ expect(cop.offences).to be_empty
139
+ end
140
+
141
+ it 'accepts an if/else in assignment with end aligned with variable' do
142
+ inspect_source(cop,
143
+ ['var = if a',
144
+ ' 0',
145
+ 'else',
146
+ ' 1',
147
+ 'end'])
148
+ expect(cop.offences).to be_empty
149
+ end
150
+
151
+ it 'accepts an if/else in assignment with end aligned with variable ' +
152
+ 'and chaining after the end' do
153
+ inspect_source(cop,
154
+ ['var = if a',
155
+ ' 0',
156
+ 'else',
157
+ ' 1',
158
+ 'end.abc.join("")'])
159
+ expect(cop.offences).to be_empty
160
+ end
161
+
162
+ it 'accepts an if/else in assignment with end aligned with variable ' +
163
+ 'and chaining with a block after the end' do
164
+ inspect_source(cop,
165
+ ['var = if a',
166
+ ' 0',
167
+ 'else',
168
+ ' 1',
169
+ 'end.abc.tap {}'])
170
+ expect(cop.offences).to be_empty
171
+ end
172
+
173
+ it 'accepts an if in assignment with end aligned with if' do
174
+ inspect_source(cop,
175
+ ['var = if a',
176
+ ' 0',
177
+ ' end'])
178
+ expect(cop.offences).to be_empty
179
+ end
180
+
181
+ it 'accepts an if/else in assignment with end aligned with if' do
182
+ inspect_source(cop,
183
+ ['var = if a',
184
+ ' 0',
185
+ ' else',
186
+ ' 1',
187
+ ' end'])
188
+ expect(cop.offences).to be_empty
189
+ end
190
+
191
+ it 'accepts an if/else in assignment on next line with end aligned ' +
192
+ 'with if' do
193
+ inspect_source(cop,
194
+ ['var =',
195
+ ' if a',
196
+ ' 0',
197
+ ' else',
198
+ ' 1',
199
+ ' end'])
200
+ expect(cop.offences).to be_empty
201
+ end
202
+
109
203
  it 'accepts an if/else branches with rescue clauses' do
110
204
  # Because of how the rescue clauses come out of Parser, these are
111
205
  # special and need to be tested.
@@ -124,9 +218,11 @@ describe Rubocop::Cop::Style::IndentationWidth do
124
218
  inspect_source(cop,
125
219
  ['unless cond',
126
220
  ' func',
221
+ ' func',
127
222
  'end'])
128
223
  expect(cop.messages)
129
- .to eq(['Use 2 (not 1) spaces for indentation.'])
224
+ .to eq(['Use 2 (not 1) spaces for indentation.',
225
+ 'Inconsistent indentation detected.'])
130
226
  end
131
227
 
132
228
  it 'accepts an empty unless' do
@@ -144,9 +240,11 @@ describe Rubocop::Cop::Style::IndentationWidth do
144
240
  ['case a',
145
241
  'when b',
146
242
  ' c',
243
+ ' d',
147
244
  'end'])
148
245
  expect(cop.messages)
149
- .to eq(['Use 2 (not 1) spaces for indentation.'])
246
+ .to eq(['Use 2 (not 1) spaces for indentation.',
247
+ 'Inconsistent indentation detected.'])
150
248
  end
151
249
 
152
250
  it 'registers an offence for bad indentation in a case/else body' do
@@ -158,9 +256,11 @@ describe Rubocop::Cop::Style::IndentationWidth do
158
256
  ' e',
159
257
  'else',
160
258
  ' f',
259
+ ' g',
161
260
  'end'])
162
261
  expect(cop.messages)
163
- .to eq(['Use 2 (not 3) spaces for indentation.'])
262
+ .to eq(['Use 2 (not 3) spaces for indentation.',
263
+ 'Inconsistent indentation detected.'])
164
264
  end
165
265
 
166
266
  it 'accepts correctly indented case/when/else' do
@@ -168,6 +268,7 @@ describe Rubocop::Cop::Style::IndentationWidth do
168
268
  ['case a',
169
269
  'when b',
170
270
  ' c',
271
+ ' c',
171
272
  'when d',
172
273
  'else',
173
274
  ' f',
@@ -216,29 +317,33 @@ describe Rubocop::Cop::Style::IndentationWidth do
216
317
  inspect_source(cop,
217
318
  ['while cond',
218
319
  ' func',
320
+ ' func',
219
321
  'end'])
220
322
  expect(cop.messages)
221
- .to eq(['Use 2 (not 1) spaces for indentation.'])
323
+ .to eq(['Use 2 (not 1) spaces for indentation.',
324
+ 'Inconsistent indentation detected.'])
222
325
  end
223
326
 
224
327
  it 'registers an offence for bad indentation of begin/end/while' do
225
328
  inspect_source(cop,
226
- ['begin',
329
+ ['something = begin',
227
330
  ' func1',
228
331
  ' func2',
229
332
  'end while cond'])
230
333
  expect(cop.messages)
231
334
  .to eq(['Use 2 (not 1) spaces for indentation.',
232
- 'Use 2 (not 3) spaces for indentation.'])
335
+ 'Inconsistent indentation detected.'])
233
336
  end
234
337
 
235
338
  it 'registers an offence for bad indentation of an until body' do
236
339
  inspect_source(cop,
237
340
  ['until cond',
238
341
  ' func',
342
+ ' func',
239
343
  'end'])
240
344
  expect(cop.messages)
241
- .to eq(['Use 2 (not 1) spaces for indentation.'])
345
+ .to eq(['Use 2 (not 1) spaces for indentation.',
346
+ 'Inconsistent indentation detected.'])
242
347
  end
243
348
 
244
349
  it 'accepts an empty while' do
@@ -254,9 +359,11 @@ describe Rubocop::Cop::Style::IndentationWidth do
254
359
  inspect_source(cop,
255
360
  ['for var in 1..10',
256
361
  ' func',
362
+ 'func',
257
363
  'end'])
258
364
  expect(cop.messages)
259
- .to eq(['Use 2 (not 1) spaces for indentation.'])
365
+ .to eq(['Use 2 (not 1) spaces for indentation.',
366
+ 'Inconsistent indentation detected.'])
260
367
  end
261
368
 
262
369
  it 'accepts an empty for' do
@@ -272,19 +379,22 @@ describe Rubocop::Cop::Style::IndentationWidth do
272
379
  inspect_source(cop,
273
380
  ['def test',
274
381
  ' func1',
275
- ' func2', # No offence registered for this.
382
+ ' func2',
276
383
  'end'])
277
384
  expect(cop.messages)
278
- .to eq(['Use 2 (not 4) spaces for indentation.'])
385
+ .to eq(['Use 2 (not 4) spaces for indentation.',
386
+ 'Inconsistent indentation detected.'])
279
387
  end
280
388
 
281
389
  it 'registers an offence for bad indentation of a defs body' do
282
390
  inspect_source(cop,
283
391
  ['def self.test',
284
392
  ' func',
393
+ ' func',
285
394
  'end'])
286
395
  expect(cop.messages)
287
- .to eq(['Use 2 (not 3) spaces for indentation.'])
396
+ .to eq(['Use 2 (not 3) spaces for indentation.',
397
+ 'Inconsistent indentation detected.'])
288
398
  end
289
399
 
290
400
  it 'accepts an empty def body' do
@@ -306,11 +416,14 @@ describe Rubocop::Cop::Style::IndentationWidth do
306
416
  it 'registers an offence for bad indentation of a class body' do
307
417
  inspect_source(cop,
308
418
  ['class Test',
309
- ' def func',
419
+ ' def func1',
310
420
  ' end',
421
+ ' def func2',
422
+ ' end',
311
423
  'end'])
312
424
  expect(cop.messages)
313
- .to eq(['Use 2 (not 4) spaces for indentation.'])
425
+ .to eq(['Use 2 (not 4) spaces for indentation.',
426
+ 'Inconsistent indentation detected.'])
314
427
  end
315
428
 
316
429
  it 'accepts an empty class body' do
@@ -319,17 +432,64 @@ describe Rubocop::Cop::Style::IndentationWidth do
319
432
  'end'])
320
433
  expect(cop.offences).to be_empty
321
434
  end
435
+
436
+ it 'accepts indented public, protected, and private' do
437
+ inspect_source(cop,
438
+ ['class Test',
439
+ ' public',
440
+ '',
441
+ ' def e',
442
+ ' end',
443
+ '',
444
+ ' protected',
445
+ '',
446
+ ' def f',
447
+ ' end',
448
+ '',
449
+ ' private',
450
+ '',
451
+ ' def g',
452
+ ' end',
453
+ 'end'])
454
+ expect(cop.offences).to be_empty
455
+ end
456
+
457
+ it 'registers an offence for bad indentation of def but not for ' +
458
+ 'outdented public, protected, and private' do
459
+ inspect_source(cop,
460
+ ['class Test',
461
+ 'public',
462
+ '',
463
+ ' def e',
464
+ ' end',
465
+ '',
466
+ 'protected',
467
+ '',
468
+ ' def f',
469
+ ' end',
470
+ '',
471
+ 'private',
472
+ '',
473
+ ' def g',
474
+ ' end',
475
+ 'end'])
476
+ expect(cop.messages).to eq(['Inconsistent indentation detected.'])
477
+ expect(cop.highlights).to eq([' '])
478
+ end
322
479
  end
323
480
 
324
481
  context 'with module' do
325
482
  it 'registers an offence for bad indentation of a module body' do
326
483
  inspect_source(cop,
327
484
  ['module Test',
328
- ' def func',
485
+ ' def func1',
329
486
  ' end',
487
+ ' def func2',
488
+ ' end',
330
489
  'end'])
331
490
  expect(cop.messages)
332
- .to eq(['Use 2 (not 4) spaces for indentation.'])
491
+ .to eq(['Use 2 (not 4) spaces for indentation.',
492
+ 'Inconsistent indentation detected.'])
333
493
  end
334
494
 
335
495
  it 'accepts an empty module body' do
@@ -345,24 +505,29 @@ describe Rubocop::Cop::Style::IndentationWidth do
345
505
  inspect_source(cop,
346
506
  ['a = func do',
347
507
  ' b',
508
+ ' c',
348
509
  'end'])
349
510
  expect(cop.messages)
350
- .to eq(['Use 2 (not 1) spaces for indentation.'])
511
+ .to eq(['Use 2 (not 1) spaces for indentation.',
512
+ 'Inconsistent indentation detected.'])
351
513
  end
352
514
 
353
515
  it 'registers an offence for bad indentation of a {} body' do
354
516
  inspect_source(cop,
355
517
  ['func {',
356
518
  ' b',
519
+ ' c',
357
520
  '}'])
358
521
  expect(cop.messages)
359
- .to eq(['Use 2 (not 3) spaces for indentation.'])
522
+ .to eq(['Use 2 (not 3) spaces for indentation.',
523
+ 'Inconsistent indentation detected.'])
360
524
  end
361
525
 
362
526
  it 'accepts a correctly indented block body' do
363
527
  inspect_source(cop,
364
528
  ['a = func do',
365
529
  ' b',
530
+ ' c',
366
531
  'end'])
367
532
  expect(cop.offences).to be_empty
368
533
  end