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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/README.md +16 -4
- data/config/default.yml +37 -10
- data/config/enabled.yml +25 -7
- data/lib/rubocop.rb +15 -19
- data/lib/rubocop/cli.rb +2 -2
- data/lib/rubocop/config.rb +40 -3
- data/lib/rubocop/config_loader.rb +6 -37
- data/lib/rubocop/config_store.rb +0 -1
- data/lib/rubocop/cop/commissioner.rb +12 -9
- data/lib/rubocop/cop/cop.rb +17 -5
- data/lib/rubocop/cop/force.rb +41 -0
- data/lib/rubocop/cop/ignored_node.rb +10 -10
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -2
- data/lib/rubocop/cop/lint/block_alignment.rb +2 -2
- data/lib/rubocop/cop/lint/condition_position.rb +2 -0
- data/lib/rubocop/cop/lint/debugger.rb +17 -3
- data/lib/rubocop/cop/lint/end_alignment.rb +3 -11
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +11 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +4 -6
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +39 -0
- data/lib/rubocop/cop/lint/unused_block_argument.rb +81 -0
- data/lib/rubocop/cop/lint/unused_method_argument.rb +52 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +6 -8
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +6 -0
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +1 -1
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +2 -2
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +7 -3
- data/lib/rubocop/cop/mixin/check_assignment.rb +11 -0
- data/lib/rubocop/cop/mixin/check_methods.rb +12 -0
- data/lib/rubocop/cop/mixin/percent_literal.rb +26 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
- data/lib/rubocop/cop/mixin/string_help.rb +0 -4
- data/lib/rubocop/cop/rails/delegate.rb +109 -0
- data/lib/rubocop/cop/style/align_hash.rb +3 -3
- data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
- data/lib/rubocop/cop/style/dot_position.rb +1 -1
- data/lib/rubocop/cop/style/encoding.rb +44 -16
- data/lib/rubocop/cop/style/indentation_width.rb +29 -19
- data/lib/rubocop/cop/style/lambda_call.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +3 -21
- data/lib/rubocop/cop/style/predicate_name.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +18 -2
- data/lib/rubocop/cop/style/redundant_self.rb +2 -2
- data/lib/rubocop/cop/style/regexp_literal.rb +29 -37
- data/lib/rubocop/cop/style/space_after_method_name.rb +1 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +37 -23
- data/lib/rubocop/cop/style/trailing_comma.rb +2 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +40 -4
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +29 -0
- data/lib/rubocop/cop/team.rb +10 -2
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/{variable_inspector.rb → variable_force.rb} +45 -37
- data/lib/rubocop/cop/{variable_inspector → variable_force}/assignment.rb +1 -1
- data/lib/rubocop/cop/{variable_inspector → variable_force}/locatable.rb +1 -1
- data/lib/rubocop/cop/{variable_inspector → variable_force}/reference.rb +13 -1
- data/lib/rubocop/cop/{variable_inspector → variable_force}/scope.rb +9 -1
- data/lib/rubocop/cop/{variable_inspector → variable_force}/variable.rb +14 -4
- data/lib/rubocop/cop/{variable_inspector → variable_force}/variable_table.rb +1 -1
- data/lib/rubocop/file_inspector.rb +3 -1
- data/lib/rubocop/formatter/base_formatter.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
- data/lib/rubocop/formatter/disabled_lines_formatter.rb +2 -2
- data/lib/rubocop/formatter/offense_count_formatter.rb +11 -10
- data/lib/rubocop/formatter/progress_formatter.rb +2 -2
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
- data/lib/rubocop/options.rb +74 -58
- data/lib/rubocop/path_util.rb +17 -2
- data/lib/rubocop/rake_task.rb +23 -5
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.21.0.md +45 -0
- data/rubocop-todo.yml +6 -6
- data/rubocop.gemspec +1 -1
- data/spec/rubocop/cli_spec.rb +170 -59
- data/spec/rubocop/config_spec.rb +48 -3
- data/spec/rubocop/config_store_spec.rb +3 -3
- data/spec/rubocop/cop/commissioner_spec.rb +9 -7
- data/spec/rubocop/cop/cop_spec.rb +0 -2
- data/spec/rubocop/cop/force_spec.rb +29 -0
- data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +2 -2
- data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +1 -1
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +24 -24
- data/spec/rubocop/cop/lint/condition_position_spec.rb +7 -5
- data/spec/rubocop/cop/lint/debugger_spec.rb +26 -9
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +6 -3
- data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +7 -6
- data/spec/rubocop/cop/lint/rescue_exception_spec.rb +36 -0
- data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
- data/spec/rubocop/cop/lint/underscore_prefixed_variable_name_spec.rb +179 -0
- data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +147 -0
- data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +140 -0
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +50 -48
- data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +1 -1
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +2 -0
- data/spec/rubocop/cop/rails/delegate_spec.rb +152 -0
- data/spec/rubocop/cop/style/encoding_spec.rb +131 -36
- data/spec/rubocop/cop/style/indentation_width_spec.rb +79 -0
- data/spec/rubocop/cop/style/redundant_begin_spec.rb +32 -0
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +83 -13
- data/spec/rubocop/cop/style/string_literals_spec.rb +9 -3
- data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +65 -25
- data/spec/rubocop/cop/style/trivial_accessors_spec.rb +76 -0
- data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +85 -0
- data/spec/rubocop/cop/team_spec.rb +43 -0
- data/spec/rubocop/cop/{variable_inspector → variable_force}/assignment_spec.rb +3 -3
- data/spec/rubocop/cop/{variable_inspector → variable_force}/locatable_spec.rb +3 -3
- data/spec/rubocop/cop/{variable_inspector → variable_force}/scope_spec.rb +30 -2
- data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_spec.rb +12 -27
- data/spec/rubocop/cop/{variable_inspector → variable_force}/variable_table_spec.rb +1 -1
- data/spec/rubocop/cop/{variable_inspector_spec.rb → variable_force_spec.rb} +4 -8
- data/spec/rubocop/formatter/base_formatter_spec.rb +2 -2
- data/spec/rubocop/formatter/offense_count_formatter_spec.rb +1 -1
- data/spec/rubocop/formatter/progress_formatter_spec.rb +1 -1
- data/spec/rubocop/options_spec.rb +2 -2
- data/spec/rubocop/path_util_spec.rb +47 -14
- data/spec/spec_helper.rb +9 -3
- data/spec/support/file_helper.rb +2 -0
- metadata +43 -26
- data/lib/rubocop/cop/style/final_newline.rb +0 -29
- 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
|
60
|
-
inspect_source(cop,
|
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(
|
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 '
|
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
|
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
|
-
|
12
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
it 'accepts an empty file' do
|
17
|
+
inspect_source(cop, '')
|
18
|
+
expect(cop.offenses).to be_empty
|
19
|
+
end
|
26
20
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
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
|