rubocop 0.14.0 → 0.14.1

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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/CHANGELOG.md +15 -0
  4. data/README.md +1 -1
  5. data/config/default.yml +4 -0
  6. data/config/enabled.yml +16 -8
  7. data/lib/rubocop.rb +3 -0
  8. data/lib/rubocop/cli.rb +1 -1
  9. data/lib/rubocop/config.rb +3 -160
  10. data/lib/rubocop/config_loader.rb +156 -0
  11. data/lib/rubocop/config_store.rb +6 -6
  12. data/lib/rubocop/cop/style/align_hash.rb +15 -11
  13. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +46 -0
  14. data/lib/rubocop/cop/style/class_length.rb +27 -4
  15. data/lib/rubocop/cop/style/documentation.rb +23 -25
  16. data/lib/rubocop/cop/style/raise_args.rb +6 -1
  17. data/lib/rubocop/cop/style/space_after_not.rb +37 -0
  18. data/lib/rubocop/cop/util.rb +18 -1
  19. data/lib/rubocop/formatter/clang_style_formatter.rb +11 -2
  20. data/lib/rubocop/options.rb +3 -3
  21. data/lib/rubocop/version.rb +1 -1
  22. data/spec/project_spec.rb +2 -2
  23. data/spec/rubocop/cli_spec.rb +1 -1
  24. data/spec/rubocop/config_loader_spec.rb +314 -0
  25. data/spec/rubocop/config_spec.rb +1 -308
  26. data/spec/rubocop/config_store_spec.rb +9 -9
  27. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +1 -1
  28. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +3 -3
  29. data/spec/rubocop/cop/style/align_hash_spec.rb +17 -20
  30. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +193 -0
  31. data/spec/rubocop/cop/style/class_length_spec.rb +64 -0
  32. data/spec/rubocop/cop/style/documentation_spec.rb +10 -0
  33. data/spec/rubocop/cop/style/indentation_width_spec.rb +0 -7
  34. data/spec/rubocop/cop/style/numeric_literals_spec.rb +1 -1
  35. data/spec/rubocop/cop/style/raise_args_spec.rb +5 -0
  36. data/spec/rubocop/cop/style/space_after_not_spec.rb +22 -0
  37. data/spec/rubocop/cop/team_spec.rb +1 -1
  38. data/spec/rubocop/cop/util_spec.rb +49 -0
  39. data/spec/rubocop/formatter/clang_style_formatter_spec.rb +42 -16
  40. data/spec/rubocop/options_spec.rb +3 -2
  41. data/spec/spec_helper.rb +1 -1
  42. metadata +13 -2
@@ -0,0 +1,193 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::BracesAroundHashParameters, :config do
6
+ subject(:cop) { described_class.new(config) }
7
+
8
+ context 'no_braces' do
9
+ let(:cop_config) do
10
+ { 'EnforcedStyle' => 'no_braces' }
11
+ end
12
+
13
+ describe 'accepts' do
14
+ it 'one non-hash parameter' do
15
+ inspect_source(cop, ['where(2)'])
16
+ expect(cop.messages).to be_empty
17
+ expect(cop.highlights).to be_empty
18
+ end
19
+
20
+ it 'one empty hash parameter' do
21
+ inspect_source(cop, ['where({})'])
22
+ expect(cop.messages).to be_empty
23
+ expect(cop.highlights).to be_empty
24
+ end
25
+
26
+ it 'one hash parameter with separators' do
27
+ inspect_source(cop, ["where( { \n }\t ) "])
28
+ expect(cop.messages).to be_empty
29
+ expect(cop.highlights).to be_empty
30
+ end
31
+
32
+ it 'multiple non-hash parameters' do
33
+ inspect_source(cop, ['where(1, "2")'])
34
+ expect(cop.messages).to be_empty
35
+ expect(cop.highlights).to be_empty
36
+ end
37
+
38
+ it 'one hash parameter without braces' do
39
+ inspect_source(cop, ['where(x: "y")'])
40
+ expect(cop.messages).to be_empty
41
+ expect(cop.highlights).to be_empty
42
+ end
43
+
44
+ it 'one hash parameter without braces and multiple keys' do
45
+ inspect_source(cop, ['where(x: "y", foo: "bar")'])
46
+ expect(cop.messages).to be_empty
47
+ expect(cop.highlights).to be_empty
48
+ end
49
+
50
+ it 'one hash parameter without braces and one hash value' do
51
+ inspect_source(cop, ['where(x: { "y" => "z" })'])
52
+ expect(cop.messages).to be_empty
53
+ expect(cop.highlights).to be_empty
54
+ end
55
+
56
+ it 'multiple hash parameters with braces' do
57
+ inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
58
+ expect(cop.messages).to be_empty
59
+ expect(cop.highlights).to be_empty
60
+ end
61
+
62
+ it 'property assignment with braces' do
63
+ inspect_source(cop, ['x.z = { y: "z" }'])
64
+ expect(cop.messages).to be_empty
65
+ expect(cop.highlights).to be_empty
66
+ end
67
+
68
+ it 'operator with a hash parameter with braces' do
69
+ inspect_source(cop, ['x.z - { y: "z" }'])
70
+ expect(cop.messages).to be_empty
71
+ expect(cop.highlights).to be_empty
72
+ end
73
+
74
+ it 'one non-hash parameter followed by a hash parameter with braces' do
75
+ inspect_source(cop, ['where(1, { y: 2 })'])
76
+ expect(cop.messages).to be_empty
77
+ expect(cop.highlights).to be_empty
78
+ end
79
+ end
80
+
81
+ describe 'registers an offence for' do
82
+ it 'one object method hash parameter with braces' do
83
+ inspect_source(cop, ['x.func({ y: "z" })'])
84
+ expect(cop.messages).to eq([
85
+ 'Redundant curly braces around a hash parameter.'
86
+ ])
87
+ expect(cop.highlights).to eq(['{ y: "z" }'])
88
+ end
89
+
90
+ it 'one hash parameter with braces' do
91
+ inspect_source(cop, ['where({ x: 1 })'])
92
+ expect(cop.messages).to eq([
93
+ 'Redundant curly braces around a hash parameter.'
94
+ ])
95
+ expect(cop.highlights).to eq(['{ x: 1 }'])
96
+ end
97
+
98
+ it 'one hash parameter with braces and separators' do
99
+ inspect_source(cop, ["where( \n { x: 1 } )"])
100
+ expect(cop.messages).to eq([
101
+ 'Redundant curly braces around a hash parameter.'
102
+ ])
103
+ expect(cop.highlights).to eq(['{ x: 1 }'])
104
+ end
105
+
106
+ it 'one hash parameter with braces and multiple keys' do
107
+ inspect_source(cop, ['where({ x: 1, foo: "bar" })'])
108
+ expect(cop.messages).to eq([
109
+ 'Redundant curly braces around a hash parameter.'
110
+ ])
111
+ expect(cop.highlights).to eq(['{ x: 1, foo: "bar" }'])
112
+ end
113
+ end
114
+ end
115
+
116
+ context 'braces' do
117
+ let(:cop_config) do
118
+ { 'EnforcedStyle' => 'braces' }
119
+ end
120
+
121
+ describe 'accepts' do
122
+ it 'an empty hash parameter' do
123
+ inspect_source(cop, ['where({})'])
124
+ expect(cop.messages).to be_empty
125
+ expect(cop.highlights).to be_empty
126
+ end
127
+
128
+ it 'one non-hash parameter' do
129
+ inspect_source(cop, ['where(2)'])
130
+ expect(cop.messages).to be_empty
131
+ expect(cop.highlights).to be_empty
132
+ end
133
+
134
+ it 'multiple non-hash parameters' do
135
+ inspect_source(cop, ['where(1, "2")'])
136
+ expect(cop.messages).to be_empty
137
+ expect(cop.highlights).to be_empty
138
+ end
139
+
140
+ it 'one hash parameter with braces' do
141
+ inspect_source(cop, ['where({ x: 1 })'])
142
+ expect(cop.messages).to be_empty
143
+ expect(cop.highlights).to be_empty
144
+ end
145
+
146
+ it 'multiple hash parameters with braces' do
147
+ inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
148
+ expect(cop.messages).to be_empty
149
+ expect(cop.highlights).to be_empty
150
+ end
151
+
152
+ it 'one hash parameter with braces and spaces around it' do
153
+ inspect_source(cop, [
154
+ 'where( { x: 1 } )'
155
+ ])
156
+ expect(cop.messages).to be_empty
157
+ expect(cop.highlights).to be_empty
158
+ end
159
+
160
+ it 'one hash parameter with braces and separators around it' do
161
+ inspect_source(cop, ["where( \t { x: 1 \n } )"])
162
+ expect(cop.messages).to be_empty
163
+ expect(cop.highlights).to be_empty
164
+ end
165
+ end
166
+
167
+ describe 'registers an offence for' do
168
+ it 'one hash parameter without braces' do
169
+ inspect_source(cop, ['where(x: "y")'])
170
+ expect(cop.messages).to eq([
171
+ 'Missing curly braces around a hash parameter.'
172
+ ])
173
+ expect(cop.highlights).to eq(['x: "y"'])
174
+ end
175
+
176
+ it 'one hash parameter with multiple keys and without braces' do
177
+ inspect_source(cop, ['where(x: "y", foo: "bar")'])
178
+ expect(cop.messages).to eq([
179
+ 'Missing curly braces around a hash parameter.'
180
+ ])
181
+ expect(cop.highlights).to eq(['x: "y", foo: "bar"'])
182
+ end
183
+
184
+ it 'one hash parameter without braces with one hash value' do
185
+ inspect_source(cop, ['where(x: { "y" => "z" })'])
186
+ expect(cop.messages).to eq([
187
+ 'Missing curly braces around a hash parameter.'
188
+ ])
189
+ expect(cop.highlights).to eq(['x: { "y" => "z" }'])
190
+ end
191
+ end
192
+ end
193
+ end
@@ -18,6 +18,17 @@ describe Rubocop::Cop::Style::ClassLength, :config do
18
18
  expect(cop.offences.size).to eq(1)
19
19
  end
20
20
 
21
+ it 'accepts a class with 5 lines' do
22
+ inspect_source(cop, ['class Test',
23
+ ' a = 1',
24
+ ' a = 2',
25
+ ' a = 3',
26
+ ' a = 4',
27
+ ' a = 5',
28
+ 'end'])
29
+ expect(cop.offences).to be_empty
30
+ end
31
+
21
32
  it 'accepts a class with less than 5 lines' do
22
33
  inspect_source(cop, ['class Test',
23
34
  ' a = 1',
@@ -47,6 +58,59 @@ describe Rubocop::Cop::Style::ClassLength, :config do
47
58
  expect(cop.offences).to be_empty
48
59
  end
49
60
 
61
+ context 'when a class has inner classes' do
62
+ it 'does not count lines of inner classes' do
63
+ inspect_source(cop, ['class NamespaceClass',
64
+ ' class TestOne',
65
+ ' a = 1',
66
+ ' a = 2',
67
+ ' a = 3',
68
+ ' a = 4',
69
+ ' a = 5',
70
+ ' end',
71
+ ' class TestTwo',
72
+ ' a = 1',
73
+ ' a = 2',
74
+ ' a = 3',
75
+ ' a = 4',
76
+ ' a = 5',
77
+ ' end',
78
+ ' a = 1',
79
+ ' a = 2',
80
+ ' a = 3',
81
+ ' a = 4',
82
+ ' a = 5',
83
+ 'end'])
84
+ expect(cop.offences).to be_empty
85
+ end
86
+
87
+ it 'rejects a class with 6 lines that belong to the class directly' do
88
+ inspect_source(cop, ['class NamespaceClass',
89
+ ' class TestOne',
90
+ ' a = 1',
91
+ ' a = 2',
92
+ ' a = 3',
93
+ ' a = 4',
94
+ ' a = 5',
95
+ ' end',
96
+ ' class TestTwo',
97
+ ' a = 1',
98
+ ' a = 2',
99
+ ' a = 3',
100
+ ' a = 4',
101
+ ' a = 5',
102
+ ' end',
103
+ ' a = 1',
104
+ ' a = 2',
105
+ ' a = 3',
106
+ ' a = 4',
107
+ ' a = 5',
108
+ ' a = 6',
109
+ 'end'])
110
+ expect(cop.offences.size).to eq(1)
111
+ end
112
+ end
113
+
50
114
  context 'when CountComments is enabled' do
51
115
  before { cop_config['CountComments'] = true }
52
116
 
@@ -70,4 +70,14 @@ describe Rubocop::Cop::Style::Documentation do
70
70
  ])
71
71
  expect(cop.offences).to be_empty
72
72
  end
73
+
74
+ it 'accepts namespace class without documentation' do
75
+ inspect_source(cop,
76
+ ['class Test',
77
+ ' class A; end',
78
+ ' class B; end',
79
+ 'end'
80
+ ])
81
+ expect(cop.offences).to be_empty
82
+ end
73
83
  end
@@ -209,13 +209,6 @@ describe Rubocop::Cop::Style::IndentationWidth do
209
209
  'end'])
210
210
  expect(cop.offences).to be_empty
211
211
  end
212
-
213
- it 'accepts an empty case' do
214
- inspect_source(cop,
215
- ['case a',
216
- 'end'])
217
- expect(cop.offences).to be_empty
218
- end
219
212
  end
220
213
 
221
214
  context 'with while/until' do
@@ -35,7 +35,7 @@ describe Rubocop::Cop::Style::NumericLiterals, :config do
35
35
 
36
36
  it 'ignores non-decimal literals' do
37
37
  inspect_source(cop, ['a = 0b1010101010101',
38
- 'b = 01919191919191',
38
+ 'b = 01717171717171',
39
39
  'c = 0xab11111111bb'])
40
40
  expect(cop.offences).to be_empty
41
41
  end
@@ -37,6 +37,11 @@ describe Rubocop::Cop::Style::RaiseArgs, :config do
37
37
  expect(cop.offences.size).to eq(1)
38
38
  end
39
39
 
40
+ it 'accepts exception constructor with more than 1 argument' do
41
+ inspect_source(cop, ['raise RuntimeError.new(a1, a2, a3)'])
42
+ expect(cop.offences).to be_empty
43
+ end
44
+
40
45
  it 'accepts a raise with 3 args' do
41
46
  inspect_source(cop, ['raise RuntimeError, msg, caller'])
42
47
  expect(cop.offences).to be_empty
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::SpaceAfterNot do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'reports an offence for space after !' do
9
+ inspect_source(cop, ['! something'])
10
+ expect(cop.offences.size).to eq(1)
11
+ end
12
+
13
+ it 'accepts no space after !' do
14
+ inspect_source(cop, ['!something'])
15
+ expect(cop.offences).to be_empty
16
+ end
17
+
18
+ it 'auto-corrects by removing redundant space' do
19
+ new_source = autocorrect_source(cop, '! something')
20
+ expect(new_source).to eq('!something')
21
+ end
22
+ end
@@ -5,7 +5,7 @@ require 'spec_helper'
5
5
  describe Rubocop::Cop::Team do
6
6
  subject(:team) { described_class.new(cop_classes, config, options) }
7
7
  let(:cop_classes) { Rubocop::Cop::Cop.all }
8
- let(:config) { Rubocop::Config.default_configuration }
8
+ let(:config) { Rubocop::ConfigLoader.default_configuration }
9
9
  let(:options) { nil }
10
10
 
11
11
  describe '#autocorrect?' do
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Util do
6
+ describe '#line_range' do
7
+ include ASTHelper
8
+
9
+ let(:source) do
10
+ <<-END
11
+ foo = 1
12
+ bar = 2
13
+ class Test
14
+ def some_method
15
+ do_something
16
+ end
17
+ end
18
+ baz = 8
19
+ END
20
+ end
21
+
22
+ let(:ast) do
23
+ processed_source = parse_source(source)
24
+ processed_source.ast
25
+ end
26
+
27
+ let(:node) do
28
+ target_node = scan_node(ast) do |node|
29
+ break node if node.type == :class
30
+ end
31
+ fail 'No target node found!' unless target_node
32
+ target_node
33
+ end
34
+
35
+ context 'when Source::Range object is passed' do
36
+ it 'returns line range of that' do
37
+ line_range = Rubocop::Cop::Util.line_range(node.loc.expression)
38
+ expect(line_range).to eq(3..7)
39
+ end
40
+ end
41
+
42
+ context 'when AST::Node object is passed' do
43
+ it 'returns line range of the expression' do
44
+ line_range = Rubocop::Cop::Util.line_range(node)
45
+ expect(line_range).to eq(3..7)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -31,23 +31,49 @@ module Rubocop
31
31
  ''].join("\n")
32
32
  end
33
33
 
34
- it 'does not display offending source line if it is blank' do
35
- cop = Cop::Cop.new
36
- source_buffer = Parser::Source::Buffer.new('test', 1)
37
- source_buffer.source = ([' ', 'yaba']).to_a.join($RS)
38
- cop.add_offence(:convention, nil,
39
- Parser::Source::Range.new(source_buffer, 0, 2),
40
- 'message 1')
41
- cop.add_offence(:fatal, nil,
42
- Parser::Source::Range.new(source_buffer, 6, 10),
43
- 'message 2')
34
+ context 'when the source line is blank' do
35
+ it 'does not display offending source line' do
36
+ cop = Cop::Cop.new
37
+ source_buffer = Parser::Source::Buffer.new('test', 1)
38
+ source_buffer.source = ([' ', 'yaba']).to_a.join($RS)
39
+ cop.add_offence(:convention, nil,
40
+ Parser::Source::Range.new(source_buffer, 0, 2),
41
+ 'message 1')
42
+ cop.add_offence(:fatal, nil,
43
+ Parser::Source::Range.new(source_buffer, 6, 10),
44
+ 'message 2')
44
45
 
45
- formatter.report_file('test', cop.offences)
46
- expect(output.string).to eq ['test:1:1: C: message 1',
47
- 'test:2:1: F: message 2',
48
- 'yaba',
49
- '^^^^',
50
- ''].join("\n")
46
+ formatter.report_file('test', cop.offences)
47
+ expect(output.string).to eq ['test:1:1: C: message 1',
48
+ 'test:2:1: F: message 2',
49
+ 'yaba',
50
+ '^^^^',
51
+ ''].join("\n")
52
+ end
53
+ end
54
+
55
+ context 'when the offending source spans multiple lines' do
56
+ it 'displays the first line' do
57
+ source = ['do_something([this,',
58
+ ' is,',
59
+ ' target])'].join($RS)
60
+
61
+ source_buffer = Parser::Source::Buffer.new('test', 1)
62
+ source_buffer.source = source
63
+
64
+ location = Parser::Source::Range.new(source_buffer,
65
+ source.index('['),
66
+ source.index(']') + 1)
67
+
68
+ cop = Cop::Cop.new
69
+ cop.add_offence(:convention, nil, location, 'message 1')
70
+
71
+ formatter.report_file('test', cop.offences)
72
+ expect(output.string).to eq ['test:1:14: C: message 1',
73
+ 'do_something([this,',
74
+ ' ^^^^^^',
75
+ ''].join("\n")
76
+ end
51
77
  end
52
78
 
53
79
  let(:file) { '/path/to/file' }