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
@@ -5,6 +5,12 @@ require 'spec_helper'
5
5
  describe Rubocop::Cop::Style::RedundantBegin do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
+ it 'reports an offense for single line def with redundant begin block' do
9
+ src = [' def func; begin; x; y; rescue; z end end']
10
+ inspect_source(cop, src)
11
+ expect(cop.offenses.size).to eq(1)
12
+ end
13
+
8
14
  it 'reports an offense for def with redundant begin block' do
9
15
  src = ['def func',
10
16
  ' begin',
@@ -54,4 +60,30 @@ describe Rubocop::Cop::Style::RedundantBegin do
54
60
  inspect_source(cop, src)
55
61
  expect(cop.offenses).to be_empty
56
62
  end
63
+
64
+ it 'auto-corrects by removing redundant begin blocks' do
65
+ src = [' def func',
66
+ ' begin',
67
+ ' foo',
68
+ ' bar',
69
+ ' rescue',
70
+ ' baz',
71
+ ' end',
72
+ ' end'].join("\n")
73
+ result_src = [' def func',
74
+ ' foo',
75
+ ' bar',
76
+ ' rescue',
77
+ ' baz',
78
+ ' end'].join("\n")
79
+ new_source = autocorrect_source(cop, src)
80
+ expect(new_source).to eq(result_src)
81
+ end
82
+
83
+ it 'auto-corrects by removing redundant begin blocks' do
84
+ src = ' def func; begin; x; y; rescue; z end end'
85
+ result_src = ' def func; x; y; rescue; z end'
86
+ new_source = autocorrect_source(cop, src)
87
+ expect(new_source).to eq(result_src)
88
+ end
57
89
  end
@@ -22,7 +22,6 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
22
22
  expect(cop.messages)
23
23
  .to eq(['Use %r for regular expressions matching more ' \
24
24
  "than 0 '/' characters."])
25
- expect(cop.config_to_allow_offenses).to eq('MaxSlashes' => 1)
26
25
  end
27
26
 
28
27
  it 'accepts zero slashes in // regexp' do
@@ -35,13 +34,31 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
35
34
  expect(cop.messages)
36
35
  .to eq(['Use %r only for regular expressions matching more ' \
37
36
  "than 0 '/' characters."])
38
- expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
39
37
  end
40
38
 
41
39
  it 'accepts %r regexp with one slash' do
42
40
  inspect_source(cop, ['x =~ %r(/home)'])
43
41
  expect(cop.offenses).to be_empty
44
42
  end
43
+
44
+ describe '--auto-gen-config' do
45
+ subject(:cop) { described_class.new(config, auto_gen_config: true) }
46
+
47
+ it 'sets MaxSlashes: 1 for one slash in // regexp' do
48
+ inspect_source(cop, ['x =~ /home\//'])
49
+ expect(cop.config_to_allow_offenses).to eq('MaxSlashes' => 1)
50
+ end
51
+
52
+ it 'disables the cop for zero slashes in %r regexp' do
53
+ inspect_source(cop, ['y =~ %r(etc)'])
54
+ expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
55
+ end
56
+
57
+ it 'generates nothing if there are no offenses' do
58
+ inspect_source(cop, ['x =~ %r(/home)'])
59
+ expect(cop.config_to_allow_offenses).to eq(nil)
60
+ end
61
+ end
45
62
  end
46
63
 
47
64
  context 'when MaxSlashes is 1' do
@@ -53,24 +70,23 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
53
70
  expect(cop.messages)
54
71
  .to eq(['Use %r for regular expressions matching more ' \
55
72
  "than 1 '/' character."] * 2)
56
- expect(cop.config_to_allow_offenses).to eq('MaxSlashes' => 2)
57
73
  end
58
74
 
59
- it 'registers offenses for slashes with too many and %r with too few' do
60
- inspect_source(cop, ['x =~ /home\/\//',
75
+ it 'registers offenses for slashes with too many' do
76
+ inspect_source(cop, 'x =~ /home\/\/\//')
77
+ expect(cop.messages)
78
+ .to eq(['Use %r for regular expressions matching more ' \
79
+ "than 1 '/' character."])
80
+ end
81
+
82
+ it 'registers offenses for %r with too few' do
83
+ inspect_source(cop, ['x =~ /home\/\/\//',
61
84
  'y =~ %r{home}'])
62
85
  expect(cop.messages)
63
86
  .to eq(['Use %r for regular expressions matching more ' \
64
87
  "than 1 '/' character.",
65
88
  'Use %r only for regular expressions matching more ' \
66
89
  "than 1 '/' character."])
67
- expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
68
- end
69
-
70
- it 'registers offenses for %r with too few and slashes with too many' do
71
- inspect_source(cop, ['y =~ %r{home}',
72
- 'x =~ /home\/\//'])
73
- expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
74
90
  end
75
91
 
76
92
  it 'accepts zero or one slash in // regexp' do
@@ -92,7 +108,6 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
92
108
  expect(cop.messages)
93
109
  .to eq(['Use %r only for regular expressions matching more ' \
94
110
  "than 1 '/' character."] * 2)
95
- expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
96
111
  end
97
112
 
98
113
  it 'accepts %r regexp with two or more slashes' do
@@ -100,5 +115,60 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
100
115
  'y =~ %r(/////)'])
101
116
  expect(cop.offenses).to be_empty
102
117
  end
118
+
119
+ describe '--auto-gen-config' do
120
+ subject(:cop) { described_class.new(config, auto_gen_config: true) }
121
+
122
+ it 'sets MaxSlashes: 2 for two slashes in // and 3 in %r' do
123
+ inspect_source(cop, ['x =~ /home\/\//',
124
+ 'y =~ %r{/usr/lib/ext}'])
125
+ expect(cop.config_to_allow_offenses).to eq('MaxSlashes' => 2)
126
+ end
127
+
128
+ it 'sets MaxSlashes: 0 for one slash in %r regexp' do
129
+ inspect_source(cop, 'x =~ %r{/home}')
130
+ expect(cop.config_to_allow_offenses).to eq('MaxSlashes' => 0)
131
+ end
132
+
133
+ it 'disables the cop for zero or one slash in %r regexp' do
134
+ inspect_source(cop, ['x =~ %r(/home)',
135
+ 'y =~ %r(etc)'])
136
+ expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
137
+ end
138
+
139
+ it 'disables the cop for // with too many and %r with too few' do
140
+ inspect_source(cop, ['x =~ /home\/\/\//',
141
+ 'y =~ %r{home}'])
142
+ expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
143
+ end
144
+
145
+ it 'disables the cop for %r with too few and // with too many' do
146
+ inspect_source(cop, ['y =~ %r{home}',
147
+ 'x =~ /home\/\//'])
148
+ expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
149
+ end
150
+
151
+ context 'when Enabled has been set to false' do
152
+ before do
153
+ inspect_source(cop, ['x =~ /home\/\/\//',
154
+ 'y =~ %r{/home/}'])
155
+ end
156
+
157
+ it 'does not change it' do
158
+ inspect_source(cop, 'x =~ %r{/home}')
159
+ expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
160
+ end
161
+ end
162
+ end
163
+
164
+ context 'when --auto-gen-config is not given' do
165
+ subject(:cop) { described_class.new(config) }
166
+
167
+ it 'does not set MaxSlashes' do
168
+ inspect_source(cop, ['x =~ /home\/\//',
169
+ 'y =~ %r{/usr/lib/ext}'])
170
+ expect(cop.config_to_allow_offenses).to eq(nil)
171
+ end
172
+ end
103
173
  end
104
174
  end
@@ -61,7 +61,7 @@ describe Rubocop::Cop::Style::StringLiterals, :config do
61
61
  it 'accepts double quotes when they are needed' do
62
62
  src = ['a = "\n"',
63
63
  'b = "#{encode_severity}:' \
64
- '#{sprintf("%3d", line_number)}: #{m}"',
64
+ '#{sprintf(\'%3d\', line_number)}: #{m}"',
65
65
  'c = "\'"',
66
66
  'd = "#@test"',
67
67
  'e = "#$test"',
@@ -95,10 +95,16 @@ describe Rubocop::Cop::Style::StringLiterals, :config do
95
95
  expect(cop.offenses).to be_empty
96
96
  end
97
97
 
98
- it 'can handle double quotes within embedded expression' do
98
+ it 'detects unneeded double quotes within embedded expression' do
99
99
  src = ['"#{"A"}"']
100
100
  inspect_source(cop, src)
101
- expect(cop.offenses).to be_empty
101
+ expect(cop.offenses.size).to eq(1)
102
+ end
103
+
104
+ it 'detects unneeded double quotes within concatenated string' do
105
+ src = ['"#{x}" \\', '"y"']
106
+ inspect_source(cop, src)
107
+ expect(cop.offenses.size).to eq(1)
102
108
  end
103
109
 
104
110
  it 'can handle a built-in constant parsed as string' do
@@ -2,37 +2,36 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::TrailingBlankLines do
5
+ describe Rubocop::Cop::Style::TrailingBlankLines, :config do
6
6
  subject(:cop) { described_class.new(config) }
7
- let(:config) do
8
- Rubocop::Config.new('TrailingWhitespace' => { 'Enabled' => true })
9
- end
10
7
 
11
- it 'accepts final newline' do
12
- inspect_source(cop, ['x = 0', ''])
13
- expect(cop.offenses).to be_empty
14
- end
8
+ context 'when EnforcedStyle is final_newline' do
9
+ let(:cop_config) { { 'EnforcedStyle' => 'final_newline' } }
15
10
 
16
- it 'registers an offense for multiple trailing blank lines' do
17
- inspect_source(cop, ['x = 0', '', '', '', ''])
18
- expect(cop.offenses.size).to eq(1)
19
- expect(cop.messages).to eq(['3 trailing blank lines detected.'])
20
- end
11
+ it 'accepts final newline' do
12
+ inspect_source(cop, ['x = 0', ''])
13
+ expect(cop.offenses).to be_empty
14
+ end
21
15
 
22
- it 'auto-corrects unwanted blank lines' do
23
- new_source = autocorrect_source(cop, ['x = 0', '', '', '', ''])
24
- expect(new_source).to eq(['x = 0', ''].join("\n"))
25
- end
16
+ it 'accepts an empty file' do
17
+ inspect_source(cop, '')
18
+ expect(cop.offenses).to be_empty
19
+ end
26
20
 
27
- it 'does not auto-correct if it interferes with TrailingWhitespace' do
28
- original = ['x = 0', '', ' ', '', '']
29
- new_source = autocorrect_source(cop, original)
30
- expect(new_source).to eq(original.join("\n"))
31
- end
21
+ it 'registers an offense for multiple trailing blank lines' do
22
+ inspect_source(cop, ['x = 0', '', '', '', ''])
23
+ expect(cop.offenses.size).to eq(1)
24
+ expect(cop.messages).to eq(['3 trailing blank lines detected.'])
25
+ end
26
+
27
+ it 'registers an offense for no final newline' do
28
+ inspect_source(cop, ['x = 0'])
29
+ expect(cop.messages).to eq(['Final newline missing.'])
30
+ end
32
31
 
33
- context 'with TrailingWhitespace disabled' do
34
- let(:config) do
35
- Rubocop::Config.new('TrailingWhitespace' => { 'Enabled' => false })
32
+ it 'auto-corrects unwanted blank lines' do
33
+ new_source = autocorrect_source(cop, ['x = 0', '', '', '', ''])
34
+ expect(new_source).to eq(['x = 0', ''].join("\n"))
36
35
  end
37
36
 
38
37
  it 'auto-corrects even if some lines have space' do
@@ -40,4 +39,45 @@ describe Rubocop::Cop::Style::TrailingBlankLines do
40
39
  expect(new_source).to eq(['x = 0', ''].join("\n"))
41
40
  end
42
41
  end
42
+
43
+ context 'when EnforcedStyle is final_blank_line' do
44
+ let(:cop_config) { { 'EnforcedStyle' => 'final_blank_line' } }
45
+
46
+ it 'registers an offense for final newline' do
47
+ inspect_source(cop, ['x = 0', ''])
48
+ expect(cop.messages).to eq(['Trailing blank line missing.'])
49
+ end
50
+
51
+ it 'registers an offense for multiple trailing blank lines' do
52
+ inspect_source(cop, ['x = 0', '', '', '', ''])
53
+ expect(cop.offenses.size).to eq(1)
54
+ expect(cop.messages)
55
+ .to eq(['3 trailing blank lines instead of 1 detected.'])
56
+ end
57
+
58
+ it 'registers an offense for no final newline' do
59
+ inspect_source(cop, ['x = 0'])
60
+ expect(cop.messages).to eq(['Final newline missing.'])
61
+ end
62
+
63
+ it 'accepts final blank line' do
64
+ inspect_source(cop, ['x = 0', '', ''])
65
+ expect(cop.offenses).to be_empty
66
+ end
67
+
68
+ it 'auto-corrects unwanted blank lines' do
69
+ new_source = autocorrect_source(cop, ['x = 0', '', '', '', ''])
70
+ expect(new_source).to eq(['x = 0', '', ''].join("\n"))
71
+ end
72
+
73
+ it 'auto-corrects missing blank line' do
74
+ new_source = autocorrect_source(cop, ['x = 0', ''])
75
+ expect(new_source).to eq(['x = 0', '', ''].join("\n"))
76
+ end
77
+
78
+ it 'auto-corrects missing newline' do
79
+ new_source = autocorrect_source(cop, ['x = 0'])
80
+ expect(new_source).to eq(['x = 0', '', ''].join("\n"))
81
+ end
82
+ end
43
83
  end
@@ -241,6 +241,16 @@ describe Rubocop::Cop::Style::TrivialAccessors, :config do
241
241
  .map(&:line).sort).to eq([1])
242
242
  end
243
243
 
244
+ it 'finds DSL-style trivial writer' do
245
+ inspect_source(cop,
246
+ ['def foo(val)',
247
+ ' @foo = val',
248
+ 'end'])
249
+ expect(cop.offenses.size).to eq(1)
250
+ expect(cop.offenses
251
+ .map(&:line).sort).to eq([1])
252
+ end
253
+
244
254
  it 'finds trivial writer in a class' do
245
255
  inspect_source(cop,
246
256
  ['class TrivialFoo',
@@ -415,4 +425,70 @@ describe Rubocop::Cop::Style::TrivialAccessors, :config do
415
425
  expect(cop.offenses).to be_empty
416
426
  end
417
427
  end
428
+
429
+ context 'with DSL writers allowed' do
430
+ let(:cop_config) { { 'AllowDSLWriters' => true } }
431
+
432
+ it 'does not find DSL-style writer' do
433
+ inspect_source(cop,
434
+ ['def foo(val)',
435
+ ' @foo = val',
436
+ 'end'])
437
+ expect(cop.offenses).to be_empty
438
+ end
439
+ end
440
+
441
+ describe '#autocorrect' do
442
+ context 'matching reader' do
443
+ let(:source) do
444
+ ['def foo',
445
+ ' @foo',
446
+ 'end']
447
+ end
448
+
449
+ let(:corrected_source) { 'attr_reader :foo' }
450
+
451
+ it 'autocorrects' do
452
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
453
+ end
454
+ end
455
+
456
+ context 'matching non-DSL writer' do
457
+ let(:source) do
458
+ ['def foo=(f)',
459
+ ' @foo=f',
460
+ 'end']
461
+ end
462
+
463
+ let(:corrected_source) { 'attr_writer :foo' }
464
+
465
+ it 'autocorrects' do
466
+ expect(autocorrect_source(cop, source)).to eq(corrected_source)
467
+ end
468
+ end
469
+
470
+ context 'matching DSL-style writer' do
471
+ let(:source) do
472
+ ['def foo(f)',
473
+ ' @foo=f',
474
+ 'end']
475
+ end
476
+
477
+ it 'does not autocorrect' do
478
+ expect(autocorrect_source(cop, source)).to eq(source.join("\n"))
479
+ end
480
+ end
481
+
482
+ context 'explicit receiver writer' do
483
+ let(:source) do
484
+ ['def derp.foo=(f)',
485
+ ' @foo=f',
486
+ 'end']
487
+ end
488
+
489
+ it 'does not autocorrect' do
490
+ expect(autocorrect_source(cop, source)).to eq(source.join("\n"))
491
+ end
492
+ end
493
+ end
418
494
  end
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::UnneededCapitalW do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'registers no offense for normal arrays of strings' do
9
+ inspect_source(cop,
10
+ ['["one", "two", "three"]'])
11
+ expect(cop.offenses).to be_empty
12
+ end
13
+
14
+ it 'registers no offense for normal arrays of strings with interpolation' do
15
+ inspect_source(cop,
16
+ ['["one", "two", "th#{ ?r }ee"]'])
17
+ expect(cop.offenses).to be_empty
18
+ end
19
+
20
+ it 'registers an offense for misused %W' do
21
+ inspect_source(cop,
22
+ ['%W(cat dog)'])
23
+ expect(cop.offenses.size).to eq(1)
24
+ end
25
+
26
+ it 'registers no offense for %W with interpolation' do
27
+ inspect_source(cop,
28
+ ['%W(c#{ ?a }t dog)'])
29
+ expect(cop.offenses).to be_empty
30
+ end
31
+
32
+ it 'registers no offense for %w without interpolation' do
33
+ inspect_source(cop,
34
+ ['%w(cat dog)'])
35
+ expect(cop.offenses).to be_empty
36
+ end
37
+
38
+ it 'registers no offense for %w with interpolation-like syntax' do
39
+ inspect_source(cop,
40
+ ['%w(c#{ ?a }t dog)'])
41
+ expect(cop.offenses).to be_empty
42
+ end
43
+
44
+ it 'registers no offense for arrays with character constants' do
45
+ inspect_source(cop,
46
+ ['["one", ?\n]'])
47
+ expect(cop.offenses).to be_empty
48
+ end
49
+
50
+ it 'does not register an offense for array of non-words' do
51
+ inspect_source(cop,
52
+ ['["one space", "two", "three"]'])
53
+ expect(cop.offenses).to be_empty
54
+ end
55
+
56
+ it 'does not register an offense for array containing non-string' do
57
+ inspect_source(cop,
58
+ ['["one", "two", 3]'])
59
+ expect(cop.offenses).to be_empty
60
+ end
61
+
62
+ it 'does not register an offense for array with one element' do
63
+ inspect_source(cop,
64
+ ['["three"]'])
65
+ expect(cop.offenses).to be_empty
66
+ end
67
+
68
+ it 'does not register an offense for array with empty strings' do
69
+ inspect_source(cop,
70
+ ['["", "two", "three"]'])
71
+ expect(cop.offenses).to be_empty
72
+ end
73
+
74
+ # TESTS FOR AUTOCORRECTION
75
+
76
+ # it 'auto-corrects an array of words' do
77
+ # new_source = autocorrect_source(cop, "['one', %q(two), 'three']")
78
+ # expect(new_source).to eq('%w(one two three)')
79
+ # end
80
+
81
+ # it 'auto-corrects an array of words and character constants' do
82
+ # new_source = autocorrect_source(cop, '[%{one}, %Q(two), ?\n, ?\t]')
83
+ # expect(new_source).to eq('%W(one two \n \t)')
84
+ # end
85
+ end