rubocop 0.20.1 → 0.21.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 (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -0
  3. data/README.md +16 -4
  4. data/config/default.yml +37 -10
  5. data/config/enabled.yml +25 -7
  6. data/lib/rubocop.rb +15 -19
  7. data/lib/rubocop/cli.rb +2 -2
  8. data/lib/rubocop/config.rb +40 -3
  9. data/lib/rubocop/config_loader.rb +6 -37
  10. data/lib/rubocop/config_store.rb +0 -1
  11. data/lib/rubocop/cop/commissioner.rb +12 -9
  12. data/lib/rubocop/cop/cop.rb +17 -5
  13. data/lib/rubocop/cop/force.rb +41 -0
  14. data/lib/rubocop/cop/ignored_node.rb +10 -10
  15. data/lib/rubocop/cop/lint/ambiguous_operator.rb +1 -1
  16. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
  17. data/lib/rubocop/cop/lint/block_alignment.rb +2 -2
  18. data/lib/rubocop/cop/lint/condition_position.rb +2 -0
  19. data/lib/rubocop/cop/lint/debugger.rb +17 -3
  20. data/lib/rubocop/cop/lint/end_alignment.rb +3 -11
  21. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  22. data/lib/rubocop/cop/lint/rescue_exception.rb +11 -0
  23. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +4 -6
  24. data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +39 -0
  25. data/lib/rubocop/cop/lint/unused_block_argument.rb +81 -0
  26. data/lib/rubocop/cop/lint/unused_method_argument.rb +52 -0
  27. data/lib/rubocop/cop/lint/useless_assignment.rb +6 -8
  28. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +6 -0
  29. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  30. data/lib/rubocop/cop/lint/void.rb +1 -1
  31. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +2 -2
  32. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +7 -3
  33. data/lib/rubocop/cop/mixin/check_assignment.rb +11 -0
  34. data/lib/rubocop/cop/mixin/check_methods.rb +12 -0
  35. data/lib/rubocop/cop/mixin/percent_literal.rb +26 -0
  36. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
  37. data/lib/rubocop/cop/mixin/string_help.rb +0 -4
  38. data/lib/rubocop/cop/rails/delegate.rb +109 -0
  39. data/lib/rubocop/cop/style/align_hash.rb +3 -3
  40. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
  41. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  42. data/lib/rubocop/cop/style/dot_position.rb +1 -1
  43. data/lib/rubocop/cop/style/encoding.rb +44 -16
  44. data/lib/rubocop/cop/style/indentation_width.rb +29 -19
  45. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  46. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  47. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +3 -21
  48. data/lib/rubocop/cop/style/predicate_name.rb +1 -1
  49. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  50. data/lib/rubocop/cop/style/redundant_begin.rb +18 -2
  51. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  52. data/lib/rubocop/cop/style/regexp_literal.rb +29 -37
  53. data/lib/rubocop/cop/style/space_after_method_name.rb +1 -1
  54. data/lib/rubocop/cop/style/string_literals.rb +1 -1
  55. data/lib/rubocop/cop/style/trailing_blank_lines.rb +37 -23
  56. data/lib/rubocop/cop/style/trailing_comma.rb +2 -3
  57. data/lib/rubocop/cop/style/trivial_accessors.rb +40 -4
  58. data/lib/rubocop/cop/style/unneeded_capital_w.rb +29 -0
  59. data/lib/rubocop/cop/team.rb +10 -2
  60. data/lib/rubocop/cop/util.rb +2 -2
  61. data/lib/rubocop/cop/{variable_inspector.rb → variable_force.rb} +45 -37
  62. data/lib/rubocop/cop/{variable_inspector → variable_force}/assignment.rb +1 -1
  63. data/lib/rubocop/cop/{variable_inspector → variable_force}/locatable.rb +1 -1
  64. data/lib/rubocop/cop/{variable_inspector → variable_force}/reference.rb +13 -1
  65. data/lib/rubocop/cop/{variable_inspector → variable_force}/scope.rb +9 -1
  66. data/lib/rubocop/cop/{variable_inspector → variable_force}/variable.rb +14 -4
  67. data/lib/rubocop/cop/{variable_inspector → variable_force}/variable_table.rb +1 -1
  68. data/lib/rubocop/file_inspector.rb +3 -1
  69. data/lib/rubocop/formatter/base_formatter.rb +1 -1
  70. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
  71. data/lib/rubocop/formatter/disabled_lines_formatter.rb +2 -2
  72. data/lib/rubocop/formatter/offense_count_formatter.rb +11 -10
  73. data/lib/rubocop/formatter/progress_formatter.rb +2 -2
  74. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  75. data/lib/rubocop/options.rb +74 -58
  76. data/lib/rubocop/path_util.rb +17 -2
  77. data/lib/rubocop/rake_task.rb +23 -5
  78. data/lib/rubocop/version.rb +1 -1
  79. data/relnotes/v0.21.0.md +45 -0
  80. data/rubocop-todo.yml +6 -6
  81. data/rubocop.gemspec +1 -1
  82. data/spec/rubocop/cli_spec.rb +170 -59
  83. data/spec/rubocop/config_spec.rb +48 -3
  84. data/spec/rubocop/config_store_spec.rb +3 -3
  85. data/spec/rubocop/cop/commissioner_spec.rb +9 -7
  86. data/spec/rubocop/cop/cop_spec.rb +0 -2
  87. data/spec/rubocop/cop/force_spec.rb +29 -0
  88. data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +2 -2
  89. data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +1 -1
  90. data/spec/rubocop/cop/lint/block_alignment_spec.rb +24 -24
  91. data/spec/rubocop/cop/lint/condition_position_spec.rb +7 -5
  92. data/spec/rubocop/cop/lint/debugger_spec.rb +26 -9
  93. data/spec/rubocop/cop/lint/end_alignment_spec.rb +6 -3
  94. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +7 -6
  95. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +36 -0
  96. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
  97. data/spec/rubocop/cop/lint/underscore_prefixed_variable_name_spec.rb +179 -0
  98. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +147 -0
  99. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +140 -0
  100. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +50 -48
  101. data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +1 -1
  102. data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +2 -0
  103. data/spec/rubocop/cop/rails/delegate_spec.rb +152 -0
  104. data/spec/rubocop/cop/style/encoding_spec.rb +131 -36
  105. data/spec/rubocop/cop/style/indentation_width_spec.rb +79 -0
  106. data/spec/rubocop/cop/style/redundant_begin_spec.rb +32 -0
  107. data/spec/rubocop/cop/style/regexp_literal_spec.rb +83 -13
  108. data/spec/rubocop/cop/style/string_literals_spec.rb +9 -3
  109. data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +65 -25
  110. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +76 -0
  111. data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +85 -0
  112. data/spec/rubocop/cop/team_spec.rb +43 -0
  113. data/spec/rubocop/cop/{variable_inspector → variable_force}/assignment_spec.rb +3 -3
  114. data/spec/rubocop/cop/{variable_inspector → variable_force}/locatable_spec.rb +3 -3
  115. data/spec/rubocop/cop/{variable_inspector → variable_force}/scope_spec.rb +30 -2
  116. data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_spec.rb +12 -27
  117. data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_table_spec.rb +1 -1
  118. data/spec/rubocop/cop/{variable_inspector_spec.rb → variable_force_spec.rb} +4 -8
  119. data/spec/rubocop/formatter/base_formatter_spec.rb +2 -2
  120. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +1 -1
  121. data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
  122. data/spec/rubocop/options_spec.rb +2 -2
  123. data/spec/rubocop/path_util_spec.rb +47 -14
  124. data/spec/spec_helper.rb +9 -3
  125. data/spec/support/file_helper.rb +2 -0
  126. metadata +43 -26
  127. data/lib/rubocop/cop/style/final_newline.rb +0 -29
  128. data/spec/rubocop/cop/style/final_newline_spec.rb +0 -30
@@ -23,7 +23,7 @@ describe Rubocop::Cop::Lint::UselessElseWithoutRescue do
23
23
  it 'registers an offense' do
24
24
  expect(cop.offenses.size).to eq(1)
25
25
  expect(cop.offenses.first.message)
26
- .to eq('Else without rescue is useless')
26
+ .to eq('`else` without `rescue` is useless.')
27
27
  expect(cop.highlights).to eq(['else'])
28
28
  end
29
29
  end
@@ -13,6 +13,8 @@ describe Rubocop::Cop::Lint::UselessSetterCall do
13
13
  'end'
14
14
  ])
15
15
  expect(cop.offenses.size).to eq(1)
16
+ expect(cop.messages)
17
+ .to eq(['Useless setter call to local variable `top`.'])
16
18
  end
17
19
 
18
20
  it 'registers an offense for defs ending with lvar attr assignment' do
@@ -0,0 +1,152 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Rails::Delegate do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'finds trivial delegate' do
9
+ inspect_source(cop,
10
+ ['def foo',
11
+ ' bar.foo',
12
+ 'end'])
13
+ expect(cop.offenses.size).to eq(1)
14
+ expect(cop.offenses
15
+ .map(&:line).sort).to eq([1])
16
+ expect(cop.messages)
17
+ .to eq(['Use `delegate` to define delegations.'])
18
+ expect(cop.highlights).to eq(['def'])
19
+ end
20
+
21
+ it 'finds trivial delegate with arguments' do
22
+ inspect_source(cop,
23
+ ['def foo(baz)',
24
+ ' bar.foo(baz)',
25
+ 'end'])
26
+ expect(cop.offenses.size).to eq(1)
27
+ expect(cop.offenses
28
+ .map(&:line).sort).to eq([1])
29
+ expect(cop.messages)
30
+ .to eq(['Use `delegate` to define delegations.'])
31
+ expect(cop.highlights).to eq(['def'])
32
+ end
33
+
34
+ it 'finds trivial delegate with prefix' do
35
+ inspect_source(cop,
36
+ ['def bar_foo',
37
+ ' bar.foo',
38
+ 'end'])
39
+ expect(cop.offenses.size).to eq(1)
40
+ expect(cop.offenses
41
+ .map(&:line).sort).to eq([1])
42
+ expect(cop.messages)
43
+ .to eq(['Use `delegate` to define delegations.'])
44
+ expect(cop.highlights).to eq(['def'])
45
+ end
46
+
47
+ it 'ignores non trivial delegate' do
48
+ inspect_source(cop,
49
+ ['def fox',
50
+ ' bar.foo.fox',
51
+ 'end'])
52
+ expect(cop.offenses).to be_empty
53
+ end
54
+
55
+ it 'ignores trivial delegate with mismatched arguments' do
56
+ inspect_source(cop,
57
+ ['def fox(baz)',
58
+ ' bar.fox(foo)',
59
+ 'end'])
60
+ expect(cop.offenses).to be_empty
61
+ end
62
+
63
+ it 'ignores trivial delegate with mismatched arguments' do
64
+ inspect_source(cop,
65
+ ['def fox(foo = nil)',
66
+ ' bar.fox(foo || 5)',
67
+ 'end'])
68
+ expect(cop.offenses).to be_empty
69
+ end
70
+
71
+ it 'ignores trivial delegate with mismatched arguments' do
72
+ inspect_source(cop,
73
+ ['def fox(a, baz)',
74
+ ' bar.fox(a)',
75
+ 'end'])
76
+ expect(cop.offenses).to be_empty
77
+ end
78
+
79
+ it 'ignores trivial delegate with other prefix' do
80
+ inspect_source(cop,
81
+ ['def fox_foo',
82
+ ' bar.foo',
83
+ 'end'])
84
+ expect(cop.offenses).to be_empty
85
+ end
86
+
87
+ it 'ignores methods with arguments' do
88
+ inspect_source(cop,
89
+ ['def fox(bar)',
90
+ ' bar.fox',
91
+ 'end'])
92
+ expect(cop.offenses).to be_empty
93
+ end
94
+
95
+ it 'ignores private delegations' do
96
+ inspect_source(cop,
97
+ [' private def fox', # leading spaces are on purpose
98
+ ' bar.fox',
99
+ ' end',
100
+ ' ',
101
+ ' private',
102
+ ' ',
103
+ ' def fox',
104
+ ' bar.fox',
105
+ ' end'])
106
+ expect(cop.offenses).to be_empty
107
+ end
108
+
109
+ it 'ignores protected delegations' do
110
+ inspect_source(cop,
111
+ [' protected def fox', # leading spaces are on purpose
112
+ ' bar.fox',
113
+ ' end',
114
+ ' ',
115
+ ' protected',
116
+ ' ' ,
117
+ ' def fox',
118
+ ' bar.fox',
119
+ ' end'])
120
+ expect(cop.offenses).to be_empty
121
+ end
122
+
123
+ describe '#autocorrect' do
124
+ context 'trivial delegation' do
125
+ let(:source) do
126
+ ['def bar',
127
+ ' foo.bar',
128
+ 'end']
129
+ end
130
+
131
+ let(:corrected_source) { 'delegate :bar, to: :foo' }
132
+
133
+ it 'autocorrects' do
134
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
135
+ end
136
+ end
137
+
138
+ context 'trivial delegation with prefix' do
139
+ let(:source) do
140
+ ['def foo_bar',
141
+ ' foo.bar',
142
+ 'end']
143
+ end
144
+
145
+ let(:corrected_source) { 'delegate :bar, to: :foo, prefix: true' }
146
+
147
+ it 'autocorrects' do
148
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
149
+ end
150
+ end
151
+ end
152
+ end
@@ -2,55 +2,150 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::Encoding do
6
- subject(:cop) { described_class.new }
5
+ describe Rubocop::Cop::Style::Encoding, :config do
6
+ subject(:cop) { described_class.new(config) }
7
7
 
8
- it 'registers an offense when no encoding present', ruby: 1.9 do
9
- inspect_source(cop, ['def foo() end'])
8
+ context 'when_needed' do
9
+ let(:cop_config) do
10
+ { 'EnforcedStyle' => 'when_needed' }
11
+ end
10
12
 
11
- expect(cop.messages).to eq(
12
- ['Missing utf-8 encoding comment.'])
13
- end
13
+ it 'registers no offense when no encoding present but only ASCII ' \
14
+ 'characters', ruby: 1.9 do
15
+ inspect_source(cop, ['def foo() end'])
14
16
 
15
- it 'accepts encoding on first line', ruby: 1.9 do
16
- inspect_source(cop, ['# encoding: utf-8',
17
- 'def foo() end'])
17
+ expect(cop.offenses).to be_empty
18
+ end
18
19
 
19
- expect(cop.offenses).to be_empty
20
- end
20
+ it 'registers an offense when there is no encoding present but non ' \
21
+ 'ASCII characters', ruby: 1.9 do
22
+ inspect_source(cop, ['def foo() \'ä\' end'])
21
23
 
22
- it 'accepts encoding on second line when shebang present', ruby: 1.9 do
23
- inspect_source(cop, ['#!/usr/bin/env ruby',
24
- '# encoding: utf-8',
25
- 'def foo() end'])
24
+ expect(cop.offenses.size).to eq(1)
25
+ expect(cop.messages).to eq(
26
+ ['Missing utf-8 encoding comment.'])
27
+ end
26
28
 
27
- expect(cop.messages).to be_empty
28
- end
29
+ it 'registers an offense when encoding present but only ASCII ' \
30
+ 'characters', ruby: 1.9 do
31
+ inspect_source(cop, ['# encoding: utf-8',
32
+ 'def foo() end'])
29
33
 
30
- it 'books an offense when encoding is in the wrong place', ruby: 1.9 do
31
- inspect_source(cop, ['def foo() end',
32
- '# encoding: utf-8'])
34
+ expect(cop.offenses.size).to eq(1)
35
+ expect(cop.messages).to eq(
36
+ ['Unnecessary utf-8 encoding comment.'])
37
+ end
33
38
 
34
- expect(cop.messages).to eq(
35
- ['Missing utf-8 encoding comment.'])
36
- end
39
+ it 'accepts an empty file', ruby: 1.9 do
40
+ inspect_source(cop, '')
37
41
 
38
- it 'does not register an offense on Ruby 2.0', ruby: 2.0 do
39
- inspect_source(cop, ['def foo() end'])
42
+ expect(cop.offenses).to be_empty
43
+ end
40
44
 
41
- expect(cop.offenses).to be_empty
42
- end
45
+ it 'accepts encoding on first line', ruby: 1.9 do
46
+ inspect_source(cop, ['# encoding: utf-8',
47
+ 'def foo() \'ä\' end'])
48
+
49
+ expect(cop.offenses).to be_empty
50
+ end
51
+
52
+ it 'accepts encoding on second line when shebang present', ruby: 1.9 do
53
+ inspect_source(cop, ['#!/usr/bin/env ruby',
54
+ '# encoding: utf-8',
55
+ 'def foo() \'ä\' end'])
43
56
 
44
- it 'accepts encoding inserted by magic_encoding gem', ruby: 1.9 do
45
- inspect_source(cop, ['# -*- encoding : utf-8 -*-',
46
- 'def foo() end'])
57
+ expect(cop.messages).to be_empty
58
+ end
47
59
 
48
- expect(cop.messages).to be_empty
60
+ it 'books an offense when encoding is in the wrong place', ruby: 1.9 do
61
+ inspect_source(cop, ['def foo() \'ä\' end',
62
+ '# encoding: utf-8'])
63
+
64
+ expect(cop.offenses.size).to eq(1)
65
+ expect(cop.messages).to eq(
66
+ ['Missing utf-8 encoding comment.'])
67
+ end
68
+
69
+ it 'does not register an offense on Ruby 2.0', ruby: 2.0 do
70
+ inspect_source(cop, ['def foo() \'ä\' end'])
71
+
72
+ expect(cop.offenses).to be_empty
73
+ end
74
+
75
+ it 'accepts encoding inserted by magic_encoding gem', ruby: 1.9 do
76
+ inspect_source(cop, ['# -*- encoding : utf-8 -*-',
77
+ 'def foo() \'ä\' end'])
78
+
79
+ expect(cop.messages).to be_empty
80
+ end
81
+
82
+ it 'accepts vim-style encoding comments', ruby: 1.9 do
83
+ inspect_source(cop, ['# vim:fileencoding=utf-8',
84
+ 'def foo() \'ä\' end'])
85
+ expect(cop.messages).to be_empty
86
+ end
49
87
  end
50
88
 
51
- it 'accepts vim-style encoding comments', ruby: 1.9 do
52
- inspect_source(cop, ['# vim:fileencoding=utf-8',
53
- 'def foo() end'])
54
- expect(cop.messages).to be_empty
89
+ context 'always' do
90
+ let(:cop_config) do
91
+ { 'EnforcedStyle' => 'always' }
92
+ end
93
+
94
+ it 'registers an offense when no encoding present', ruby: 1.9 do
95
+ inspect_source(cop, ['def foo() end'])
96
+
97
+ expect(cop.offenses.size).to eq(1)
98
+ expect(cop.messages).to eq(
99
+ ['Missing utf-8 encoding comment.'])
100
+ end
101
+
102
+ it 'accepts an empty file', ruby: 1.9 do
103
+ inspect_source(cop, '')
104
+
105
+ expect(cop.offenses).to be_empty
106
+ end
107
+
108
+ it 'accepts encoding on first line', ruby: 1.9 do
109
+ inspect_source(cop, ['# encoding: utf-8',
110
+ 'def foo() end'])
111
+
112
+ expect(cop.offenses).to be_empty
113
+ end
114
+
115
+ it 'accepts encoding on second line when shebang present', ruby: 1.9 do
116
+ inspect_source(cop, ['#!/usr/bin/env ruby',
117
+ '# encoding: utf-8',
118
+ 'def foo() end'])
119
+
120
+ expect(cop.messages).to be_empty
121
+ end
122
+
123
+ it 'books an offense when encoding is in the wrong place', ruby: 1.9 do
124
+ inspect_source(cop, ['def foo() end',
125
+ '# encoding: utf-8'])
126
+
127
+ expect(cop.offenses.size).to eq(1)
128
+ expect(cop.messages).to eq(
129
+ ['Missing utf-8 encoding comment.'])
130
+ end
131
+
132
+ it 'does not register an offense on Ruby 2.0', ruby: 2.0 do
133
+ inspect_source(cop, ['def foo() end'])
134
+
135
+ expect(cop.offenses).to be_empty
136
+ end
137
+
138
+ it 'accepts encoding inserted by magic_encoding gem', ruby: 1.9 do
139
+ inspect_source(cop, ['# -*- encoding : utf-8 -*-',
140
+ 'def foo() end'])
141
+
142
+ expect(cop.messages).to be_empty
143
+ end
144
+
145
+ it 'accepts vim-style encoding comments', ruby: 1.9 do
146
+ inspect_source(cop, ['# vim:fileencoding=utf-8',
147
+ 'def foo() end'])
148
+ expect(cop.messages).to be_empty
149
+ end
55
150
  end
56
151
  end
@@ -106,6 +106,20 @@ describe Rubocop::Cop::Style::IndentationWidth do
106
106
  expect(cop.offenses).to be_empty
107
107
  end
108
108
 
109
+ it 'accepts a correctly aligned if/elsif/else/end as a method argument' do
110
+ inspect_source(cop,
111
+ ['foo(',
112
+ ' if a1',
113
+ ' b1',
114
+ ' elsif a2',
115
+ ' b2',
116
+ ' else',
117
+ ' c',
118
+ ' end',
119
+ ')'])
120
+ expect(cop.offenses).to be_empty
121
+ end
122
+
109
123
  it 'accepts if/elsif/else/end laid out as a table' do
110
124
  inspect_source(cop,
111
125
  ['if @io == $stdout then str << "$stdout"',
@@ -136,6 +150,22 @@ describe Rubocop::Cop::Style::IndentationWidth do
136
150
  context 'with assignment' do
137
151
  context 'when alignment style is variable' do
138
152
  context 'and end is aligned with variable' do
153
+ it 'accepts an if with end aligned with setter' do
154
+ inspect_source(cop,
155
+ ['foo.bar = if baz',
156
+ ' derp',
157
+ 'end'])
158
+ expect(cop.offenses).to be_empty
159
+ end
160
+
161
+ it 'accepts an if with end aligned with element assignment' do
162
+ inspect_source(cop,
163
+ ['foo[bar] = if baz',
164
+ ' derp',
165
+ 'end'])
166
+ expect(cop.offenses).to be_empty
167
+ end
168
+
139
169
  it 'accepts an if with end aligned with variable' do
140
170
  inspect_source(cop,
141
171
  ['var = if a',
@@ -194,6 +224,24 @@ describe Rubocop::Cop::Style::IndentationWidth do
194
224
  end
195
225
 
196
226
  context 'and end is aligned with keyword' do
227
+ it 'registers an offense for an if with setter' do
228
+ inspect_source(cop,
229
+ ['foo.bar = if baz',
230
+ ' derp',
231
+ ' end'])
232
+ expect(cop.messages)
233
+ .to eq(['Use 2 (not 12) spaces for indentation.'])
234
+ end
235
+
236
+ it 'registers an offense for an if with element assignment' do
237
+ inspect_source(cop,
238
+ ['foo[bar] = if baz',
239
+ ' derp',
240
+ ' end'])
241
+ expect(cop.messages)
242
+ .to eq(['Use 2 (not 13) spaces for indentation.'])
243
+ end
244
+
197
245
  it 'registers an offense for an if' do
198
246
  inspect_source(cop,
199
247
  ['var = if a',
@@ -512,6 +560,27 @@ describe Rubocop::Cop::Style::IndentationWidth do
512
560
  'end'])
513
561
  expect(cop.offenses).to be_empty
514
562
  end
563
+
564
+ if RUBY_VERSION >= '2.1'
565
+ context 'when modifier and def are on the same line' do
566
+ it 'accepts a correctly aligned body' do
567
+ inspect_source(cop,
568
+ ['private def test',
569
+ ' something',
570
+ 'end'])
571
+ expect(cop.offenses).to be_empty
572
+ end
573
+
574
+ it 'registers an offense for bad indentation of a def body' do
575
+ inspect_source(cop,
576
+ ['private def test',
577
+ ' something',
578
+ ' end'])
579
+ expect(cop.messages)
580
+ .to eq(['Use 2 (not 10) spaces for indentation.'])
581
+ end
582
+ end
583
+ end
515
584
  end
516
585
 
517
586
  context 'with class' do
@@ -602,5 +671,15 @@ describe Rubocop::Cop::Style::IndentationWidth do
602
671
  'end'])
603
672
  expect(cop.offenses).to be_empty
604
673
  end
674
+
675
+ # The cop uses the block end/} as the base for indentation, so if it's not
676
+ # on its own line, all bets are off.
677
+ it 'accepts badly indented code if block end is not on separate line' do
678
+ inspect_source(cop,
679
+ ['foo {',
680
+ 'def baz',
681
+ 'end }'])
682
+ expect(cop.offenses).to be_empty
683
+ end
605
684
  end
606
685
  end