rubocop 0.8.0 → 0.8.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -1
- data/config/enabled.yml +6 -2
- data/lib/rubocop.rb +2 -1
- data/lib/rubocop/cli.rb +2 -3
- data/lib/rubocop/cop/and_or.rb +14 -4
- data/lib/rubocop/cop/cop.rb +3 -0
- data/lib/rubocop/cop/empty_literal.rb +17 -15
- data/lib/rubocop/cop/favor_modifier.rb +8 -6
- data/lib/rubocop/cop/lambda.rb +42 -0
- data/lib/rubocop/cop/line_continuation.rb +1 -1
- data/lib/rubocop/cop/parentheses_around_condition.rb +25 -9
- data/lib/rubocop/cop/proc.rb +26 -0
- data/lib/rubocop/cop/rescue_modifier.rb +4 -6
- data/lib/rubocop/cop/string_literals.rb +11 -8
- data/lib/rubocop/cop/surrounding_space.rb +4 -2
- data/lib/rubocop/cop/trivial_accessors.rb +2 -2
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +1 -1
- data/spec/rubocop/cli_spec.rb +22 -0
- data/spec/rubocop/cops/favor_modifier_spec.rb +39 -15
- data/spec/rubocop/cops/lambda_spec.rb +43 -0
- data/spec/rubocop/cops/proc_spec.rb +26 -0
- data/spec/rubocop/cops/space_inside_hash_literal_braces_spec.rb +7 -0
- data/spec/rubocop/cops/string_literals_spec.rb +17 -6
- data/spec/rubocop/cops/trivial_accessors_spec.rb +11 -3
- data/spec/spec_helper.rb +1 -1
- metadata +17 -8
- data/lib/rubocop/cop/new_lambda_literal.rb +0 -19
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d32a48395898e975b6e30a1e0c135d327b637d5
|
4
|
+
data.tar.gz: 95923132f5031a94cea48306ba714e3510dd1f7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c1d467dc759961b51256357d3e3ba49d1476cb1dc2ed8c69d4b755092e19b20c78746595fc0e532fee545646e36061fbaec8f5a597374be0085fc4dee600f1c
|
7
|
+
data.tar.gz: 51e01dbff570f1ffa8394a64cb7e2193f6717fad24fc5e6ce57468cb99c1385f8a8efc48433fbd25914dd6694bb978fca4edeed418e78738b7970a8536569972
|
data/CHANGELOG.md
CHANGED
@@ -6,9 +6,29 @@
|
|
6
6
|
|
7
7
|
### Changes
|
8
8
|
|
9
|
+
### Bug fixes
|
10
|
+
|
11
|
+
## 0.8.1 (05/30/2013)
|
12
|
+
|
13
|
+
### New features
|
14
|
+
|
15
|
+
* New cop `Proc` tracks uses of `Proc.new`
|
16
|
+
|
17
|
+
### Changes
|
18
|
+
|
19
|
+
* Renamed `NewLambdaLiteral` to `Lambda`.
|
20
|
+
* Aligned the `Lambda` cop more closely to the style guide - it now
|
21
|
+
allows the use of `lambda` for multi-line blocks.
|
22
|
+
|
9
23
|
### Bugs fixed
|
10
24
|
|
11
|
-
|
25
|
+
* [#210](https://github.com/bbatsov/rubocop/issues/210) - fix a false positive for double quotes in regexp literals
|
26
|
+
* [#211](https://github.com/bbatsov/rubocop/issues/211) - fix a false positive for `initialize` method looking like a trivial writer
|
27
|
+
* [#215](https://github.com/bbatsov/rubocop/issues/215) - Fixed a lot of modifier `if/unless/while/until` issues
|
28
|
+
* [#213](https://github.com/bbatsov/rubocop/issues/213) - Make sure even disabled cops get their configuration set
|
29
|
+
* [#214](https://github.com/bbatsov/rubocop/issues/214) - Fix SpaceInsideHashLiteralBraces to handle string interpolation right
|
30
|
+
|
31
|
+
## 0.8.0 (05/28/2013)
|
12
32
|
|
13
33
|
### Changes
|
14
34
|
|
data/config/enabled.yml
CHANGED
@@ -153,8 +153,12 @@ FavorUntilOverNegatedWhile:
|
|
153
153
|
SpaceAroundEqualsInParameterDefault:
|
154
154
|
Enabled: true
|
155
155
|
|
156
|
-
# Use the new lambda literal syntax.
|
157
|
-
|
156
|
+
# Use the new lambda literal syntax for single-line blocks.
|
157
|
+
Lambda:
|
158
|
+
Enabled: true
|
159
|
+
|
160
|
+
# Use proc instead of Proc.new.
|
161
|
+
Proc:
|
158
162
|
Enabled: true
|
159
163
|
|
160
164
|
# Don't use parentheses around the condition of an if/unless/while.
|
data/lib/rubocop.rb
CHANGED
@@ -35,7 +35,8 @@ require 'rubocop/cop/and_or'
|
|
35
35
|
require 'rubocop/cop/when_then'
|
36
36
|
require 'rubocop/cop/favor_modifier'
|
37
37
|
require 'rubocop/cop/favor_unless_over_negated_if'
|
38
|
-
require 'rubocop/cop/
|
38
|
+
require 'rubocop/cop/lambda'
|
39
|
+
require 'rubocop/cop/proc'
|
39
40
|
require 'rubocop/cop/parentheses_around_condition'
|
40
41
|
require 'rubocop/cop/method_and_variable_snake_case'
|
41
42
|
require 'rubocop/cop/class_and_module_camel_case'
|
data/lib/rubocop/cli.rb
CHANGED
@@ -91,9 +91,9 @@ module Rubocop
|
|
91
91
|
|
92
92
|
@cops.each do |cop_class|
|
93
93
|
cop_name = cop_class.cop_name
|
94
|
+
cop_class.config = config.for_cop(cop_name)
|
94
95
|
if config.cop_enabled?(cop_name)
|
95
96
|
cop = setup_cop(cop_class,
|
96
|
-
config.for_cop(cop_name),
|
97
97
|
disabled_lines)
|
98
98
|
if !@options[:only] || @options[:only] == cop_name
|
99
99
|
begin
|
@@ -110,8 +110,7 @@ module Rubocop
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
|
-
def setup_cop(cop_class,
|
114
|
-
cop_class.config = cop_config
|
113
|
+
def setup_cop(cop_class, disabled_lines)
|
115
114
|
cop = cop_class.new
|
116
115
|
cop.debug = @options[:debug]
|
117
116
|
cop.disabled_lines = disabled_lines[cop_class.cop_name]
|
data/lib/rubocop/cop/and_or.rb
CHANGED
@@ -8,6 +8,20 @@ module Rubocop
|
|
8
8
|
OPS = { 'and' => '&&', 'or' => '||' }
|
9
9
|
|
10
10
|
def on_and(node)
|
11
|
+
process_logical_op(node)
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def on_or(node)
|
17
|
+
process_logical_op(node)
|
18
|
+
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def process_logical_op(node)
|
11
25
|
op = node.loc.operator.source
|
12
26
|
op_type = node.type.to_s
|
13
27
|
|
@@ -16,11 +30,7 @@ module Rubocop
|
|
16
30
|
node.loc.operator.line,
|
17
31
|
sprintf(MSG, OPS[op], op))
|
18
32
|
end
|
19
|
-
|
20
|
-
super
|
21
33
|
end
|
22
|
-
|
23
|
-
alias_method :on_or, :on_and
|
24
34
|
end
|
25
35
|
end
|
26
36
|
end
|
data/lib/rubocop/cop/cop.rb
CHANGED
@@ -25,23 +25,25 @@ module Rubocop
|
|
25
25
|
# (const nil :String) :new)
|
26
26
|
STR_NODE = s(:send, s(:const, nil, :String), :new)
|
27
27
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
28
|
+
def on_send(node)
|
29
|
+
case node
|
30
|
+
when ARRAY_NODE
|
31
|
+
add_offence(:convention,
|
32
|
+
node.loc.line,
|
33
|
+
ARR_MSG)
|
34
|
+
when HASH_NODE
|
35
|
+
add_offence(:convention,
|
36
|
+
node.loc.line,
|
37
|
+
HASH_MSG)
|
38
|
+
when STR_NODE
|
39
|
+
add_offence(:convention,
|
40
|
+
node.loc.line,
|
41
|
+
STR_MSG)
|
43
42
|
end
|
44
43
|
end
|
44
|
+
|
45
|
+
# TODO Check block contents as well
|
46
|
+
alias_method :on_block, :ignore_node
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
@@ -7,11 +7,13 @@ module Rubocop
|
|
7
7
|
def check(sexp)
|
8
8
|
# discard if/then/else
|
9
9
|
return false if sexp.loc.respond_to?(:else) && sexp.loc.else
|
10
|
+
# discard modifier while/until
|
11
|
+
return false if [:while, :until].include?(sexp.type) && !sexp.loc.end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
case sexp.loc.keyword.source
|
14
|
+
when 'if' then cond, body, _else = *sexp
|
15
|
+
when 'unless' then cond, _else, body = *sexp
|
16
|
+
else cond, body = *sexp
|
15
17
|
end
|
16
18
|
|
17
19
|
if length(sexp) > 3
|
@@ -20,7 +22,7 @@ module Rubocop
|
|
20
22
|
cond_length = sexp.loc.keyword.size + cond.loc.expression.size + 1
|
21
23
|
body_length = body_length(body)
|
22
24
|
|
23
|
-
(cond_length + body_length) <= LineLength.max
|
25
|
+
body_length > 0 && (cond_length + body_length) <= LineLength.max
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
@@ -29,7 +31,7 @@ module Rubocop
|
|
29
31
|
end
|
30
32
|
|
31
33
|
def body_length(body)
|
32
|
-
if body
|
34
|
+
if body && body.loc.expression
|
33
35
|
body.loc.expression.column + body.loc.expression.size
|
34
36
|
else
|
35
37
|
0
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
class Lambda < Cop
|
6
|
+
SINGLE_MSG = 'Use the new lambda literal syntax ->(params) {...}.'
|
7
|
+
MULTI_MSG = 'Use the lambda method for multi-line lambdas.'
|
8
|
+
|
9
|
+
TARGET = s(:send, nil, :lambda)
|
10
|
+
|
11
|
+
def on_block(node)
|
12
|
+
# We're looking for
|
13
|
+
# (block
|
14
|
+
# (send nil :lambda)
|
15
|
+
# ...)
|
16
|
+
block_method, = *node
|
17
|
+
|
18
|
+
if block_method == TARGET
|
19
|
+
selector = block_method.loc.selector.source
|
20
|
+
lambda_length = lambda_length(node)
|
21
|
+
|
22
|
+
if selector != '->' && lambda_length == 0
|
23
|
+
add_offence(:convention, block_method.loc.line, SINGLE_MSG)
|
24
|
+
elsif selector == '->' && lambda_length > 0
|
25
|
+
add_offence(:convention, block_method.loc.line, MULTI_MSG)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
super
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def lambda_length(block_node)
|
35
|
+
start_line = block_node.loc.begin.line
|
36
|
+
end_line = block_node.loc.end.line
|
37
|
+
|
38
|
+
end_line - start_line
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -6,17 +6,33 @@ module Rubocop
|
|
6
6
|
MSG = "Don't use parentheses around the condition of an " +
|
7
7
|
'if/unless/while/until, unless the condition contains an assignment.'
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
cond, _body = *node
|
9
|
+
def on_if(node)
|
10
|
+
process_control_op(node)
|
12
11
|
|
13
|
-
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def on_while(node)
|
16
|
+
process_control_op(node)
|
17
|
+
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def on_until(node)
|
22
|
+
process_control_op(node)
|
23
|
+
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def process_control_op(node)
|
30
|
+
cond, _body = *node
|
31
|
+
|
32
|
+
cond_source = cond.loc.expression.source
|
14
33
|
|
15
|
-
|
16
|
-
|
17
|
-
cond.loc.line,
|
18
|
-
MSG)
|
19
|
-
end
|
34
|
+
if cond_source.start_with?('(') && cond_source.end_with?(')')
|
35
|
+
add_offence(:convetion, cond.loc.line, MSG)
|
20
36
|
end
|
21
37
|
end
|
22
38
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
class Proc < Cop
|
6
|
+
MSG = 'Use proc instead of Proc.new.'
|
7
|
+
|
8
|
+
TARGET = s(:send, s(:const, nil, :Proc), :new)
|
9
|
+
|
10
|
+
def on_block(node)
|
11
|
+
# We're looking for
|
12
|
+
# (block
|
13
|
+
# (send
|
14
|
+
# (const nil :Proc) :new)
|
15
|
+
# ...)
|
16
|
+
block_method, = *node
|
17
|
+
|
18
|
+
if block_method == TARGET
|
19
|
+
add_offence(:convention, block_method.loc.line, MSG)
|
20
|
+
end
|
21
|
+
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -5,13 +5,11 @@ module Rubocop
|
|
5
5
|
class RescueModifier < Cop
|
6
6
|
MSG = 'Avoid using rescue in its modifier form.'
|
7
7
|
|
8
|
-
def
|
9
|
-
|
10
|
-
add_offence(:convention,
|
11
|
-
s.loc.line,
|
12
|
-
MSG)
|
13
|
-
end
|
8
|
+
def on_rescue(node)
|
9
|
+
add_offence(:convention, node.loc.line, MSG)
|
14
10
|
end
|
11
|
+
|
12
|
+
alias_method :on_begin, :ignore_node
|
15
13
|
end
|
16
14
|
end
|
17
15
|
end
|
@@ -6,17 +6,20 @@ module Rubocop
|
|
6
6
|
MSG = "Prefer single-quoted strings when you don't need " +
|
7
7
|
'string interpolation or special symbols.'
|
8
8
|
|
9
|
-
def
|
10
|
-
|
11
|
-
text = s.to_a[0]
|
9
|
+
def on_str(node)
|
10
|
+
text, = *node
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
# Constants like __FILE__ and __DIR__ are created as strings,
|
13
|
+
# but don't respond to begin.
|
14
|
+
return unless node.loc.respond_to?(:begin)
|
15
|
+
|
16
|
+
if text !~ /['\n\t\r]/ && node.loc.begin.source == '"'
|
17
|
+
add_offence(:convention, node.loc.line, MSG)
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
alias_method :on_dstr, :ignore_node
|
22
|
+
alias_method :on_regexp, :ignore_node
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -246,8 +246,10 @@ module Rubocop
|
|
246
246
|
on_node(:hash, sexp) do |hash|
|
247
247
|
b_ix = index_of_first_token(hash, tokens)
|
248
248
|
e_ix = index_of_last_token(hash, tokens)
|
249
|
-
|
250
|
-
|
249
|
+
if tokens[b_ix].type == :tLBRACE # Hash literal with braces?
|
250
|
+
check(tokens[b_ix], tokens[b_ix + 1])
|
251
|
+
check(tokens[e_ix - 1], tokens[e_ix])
|
252
|
+
end
|
251
253
|
end
|
252
254
|
end
|
253
255
|
|
@@ -6,12 +6,12 @@ module Rubocop
|
|
6
6
|
MSG = 'Use attr_%s to define trivial %s methods.'
|
7
7
|
|
8
8
|
def on_def(node)
|
9
|
-
|
9
|
+
method_name, args, body = *node
|
10
10
|
|
11
11
|
kind = if body.type == :ivar
|
12
12
|
'reader'
|
13
13
|
elsif args.children.size == 1 && body.type == :ivasgn &&
|
14
|
-
body.children[1].type == :lvar
|
14
|
+
body.children[1].type == :lvar && method_name != :initialize
|
15
15
|
'writer'
|
16
16
|
end
|
17
17
|
if kind
|
data/lib/rubocop/version.rb
CHANGED
data/rubocop.gemspec
CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
s.summary = 'Automatic Ruby code style checking tool.'
|
29
29
|
|
30
30
|
s.add_runtime_dependency('rainbow', '>= 1.1.4')
|
31
|
-
s.add_runtime_dependency('parser', '
|
31
|
+
s.add_runtime_dependency('parser', ['>= 2.0.0.beta1', '<= 2.0.0'])
|
32
32
|
s.add_development_dependency('rake', '~> 10.0')
|
33
33
|
s.add_development_dependency('rspec', '~> 2.13')
|
34
34
|
s.add_development_dependency('yard', '~> 0.8')
|
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -257,6 +257,28 @@ module Rubocop
|
|
257
257
|
''].join("\n"))
|
258
258
|
end
|
259
259
|
|
260
|
+
it 'works when a cop that others depend on is disabled' do
|
261
|
+
create_file('example1.rb', ['if a',
|
262
|
+
' b',
|
263
|
+
'end'])
|
264
|
+
create_file('rubocop.yml', [
|
265
|
+
'Encoding:',
|
266
|
+
' Enabled: false',
|
267
|
+
'',
|
268
|
+
'LineLength:',
|
269
|
+
' Enabled: false'
|
270
|
+
])
|
271
|
+
result = cli.run(['-c', 'rubocop.yml', 'example1.rb'])
|
272
|
+
expect($stdout.string).to eq(
|
273
|
+
['== example1.rb ==',
|
274
|
+
'C: 1: Favor modifier if/unless usage when you have a single-line ' +
|
275
|
+
'body. Another good alternative is the usage of control flow &&/||.',
|
276
|
+
'',
|
277
|
+
'1 file inspected, 1 offence detected',
|
278
|
+
''].join("\n"))
|
279
|
+
expect(result).to eq(1)
|
280
|
+
end
|
281
|
+
|
260
282
|
it 'can be configured with project config to disable a certain error' do
|
261
283
|
create_file('example_src/example1.rb', 'puts 0 ')
|
262
284
|
create_file('example_src/.rubocop.yml', [
|
@@ -4,52 +4,53 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
module Rubocop
|
6
6
|
module Cop
|
7
|
-
describe FavorModifier
|
8
|
-
let(:
|
7
|
+
describe FavorModifier do
|
8
|
+
let(:if_unless) { IfUnlessModifier.new }
|
9
9
|
let(:while_until) { WhileUntilModifier.new }
|
10
10
|
before { LineLength.config = { 'Max' => 79 } }
|
11
11
|
|
12
|
-
it 'registers an offence for multiline if that fits on one line'
|
12
|
+
it 'registers an offence for multiline if that fits on one line',
|
13
|
+
broken: true do
|
13
14
|
# This if statement fits exactly on one line if written as a modifier.
|
14
|
-
inspect_source(
|
15
|
+
inspect_source(if_unless,
|
15
16
|
['if a_condition_that_is_just_short_enough',
|
16
17
|
' some_long_metod_name(followed_by_args)',
|
17
18
|
'end'])
|
18
|
-
expect(
|
19
|
+
expect(if_unless.offences.map(&:message)).to eq(
|
19
20
|
['Favor modifier if/unless usage when you have a single-line body.' +
|
20
|
-
' Another good alternative is the usage of control flow
|
21
|
+
' Another good alternative is the usage of control flow &&/||.'])
|
21
22
|
end
|
22
23
|
|
23
24
|
it "accepts multiline if that doesn't fit on one line" do
|
24
|
-
check_too_long(
|
25
|
+
check_too_long(if_unless, 'if')
|
25
26
|
end
|
26
27
|
|
27
28
|
it 'accepts multiline if whose body is more than one line' do
|
28
|
-
check_short_multiline(
|
29
|
+
check_short_multiline(if_unless, 'if')
|
29
30
|
end
|
30
31
|
|
31
32
|
it 'registers an offence for multiline unless that fits on one line' do
|
32
|
-
inspect_source(
|
33
|
+
inspect_source(if_unless, ['unless a',
|
33
34
|
' b',
|
34
35
|
'end'])
|
35
|
-
expect(
|
36
|
+
expect(if_unless.offences.map(&:message)).to eq(
|
36
37
|
['Favor modifier if/unless usage when you have a single-line body.' +
|
37
|
-
' Another good alternative is the usage of control flow
|
38
|
+
' Another good alternative is the usage of control flow &&/||.'])
|
38
39
|
end
|
39
40
|
|
40
41
|
it 'accepts code with EOL comment since user might want to keep it' do
|
41
42
|
pending
|
42
|
-
inspect_source(
|
43
|
+
inspect_source(if_unless, ['unless a',
|
43
44
|
' b # A comment',
|
44
45
|
'end'])
|
45
|
-
expect(
|
46
|
+
expect(if_unless.offences.map(&:message)).to be_empty
|
46
47
|
end
|
47
48
|
|
48
49
|
it 'accepts if-else-end' do
|
49
|
-
inspect_source(
|
50
|
+
inspect_source(if_unless,
|
50
51
|
['if args.last.is_a? Hash then args.pop else ' +
|
51
52
|
'Hash.new end'])
|
52
|
-
expect(
|
53
|
+
expect(if_unless.offences.map(&:message)).to be_empty
|
53
54
|
end
|
54
55
|
|
55
56
|
it "accepts multiline unless that doesn't fit on one line" do
|
@@ -84,6 +85,29 @@ module Rubocop
|
|
84
85
|
check_short_multiline(while_until, 'until')
|
85
86
|
end
|
86
87
|
|
88
|
+
it 'accepts an empty condition' do
|
89
|
+
check_empty(if_unless, 'if')
|
90
|
+
check_empty(if_unless, 'unless')
|
91
|
+
check_empty(while_until, 'while')
|
92
|
+
check_empty(while_until, 'until')
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'accepts modifier while' do
|
96
|
+
inspect_source(while_until, ['ala while bala'])
|
97
|
+
expect(while_until.offences).to be_empty
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'accepts modifier until' do
|
101
|
+
inspect_source(while_until, ['ala until bala'])
|
102
|
+
expect(while_until.offences).to be_empty
|
103
|
+
end
|
104
|
+
|
105
|
+
def check_empty(cop, keyword)
|
106
|
+
inspect_source(cop, ["#{keyword} cond",
|
107
|
+
'end'])
|
108
|
+
expect(cop.offences).to be_empty
|
109
|
+
end
|
110
|
+
|
87
111
|
def check_really_short(cop, keyword)
|
88
112
|
inspect_source(cop, ["#{keyword} a",
|
89
113
|
' b',
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe Lambda do
|
8
|
+
let(:lambda) { Lambda.new }
|
9
|
+
|
10
|
+
it 'registers an offence for an old single-line lambda call' do
|
11
|
+
inspect_source(lambda, ['f = lambda { |x| x }'])
|
12
|
+
expect(lambda.offences.size).to eq(1)
|
13
|
+
expect(lambda.messages).to eq([Lambda::SINGLE_MSG])
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'accepts the new lambda literal with single-line body' do
|
17
|
+
inspect_source(lambda, ['lambda = ->(x) { x }',
|
18
|
+
'lambda.(1)'])
|
19
|
+
expect(lambda.offences).to be_empty
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'registers an offence for a new multi-line lambda call' do
|
23
|
+
inspect_source(lambda, ['f = ->(x) do',
|
24
|
+
' x',
|
25
|
+
'end'])
|
26
|
+
expect(lambda.offences.size).to eq(1)
|
27
|
+
expect(lambda.messages).to eq([Lambda::MULTI_MSG])
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'accepts the old lambda syntax with multi-line body' do
|
31
|
+
inspect_source(lambda, ['l = lambda do |x|',
|
32
|
+
' x',
|
33
|
+
'end'])
|
34
|
+
expect(lambda.offences).to be_empty
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'accepts the lambda call outside of block' do
|
38
|
+
inspect_source(lambda, ['l = lambda.test'])
|
39
|
+
expect(lambda.offences).to be_empty
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
describe Proc do
|
8
|
+
let(:proc) { Proc.new }
|
9
|
+
|
10
|
+
it 'registers an offence for a Proc.new call' do
|
11
|
+
inspect_source(proc, ['f = Proc.new { |x| puts x }'])
|
12
|
+
expect(proc.offences.size).to eq(1)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'accepts the proc method' do
|
16
|
+
inspect_source(proc, ['f = proc { |x| puts x }'])
|
17
|
+
expect(proc.offences).to be_empty
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'accepts the Proc.new call outside of block' do
|
21
|
+
inspect_source(proc, ['p = Proc.new'])
|
22
|
+
expect(proc.offences).to be_empty
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -74,6 +74,13 @@ module Rubocop
|
|
74
74
|
inspect_source(sihlb, ['x(a: b.c)'])
|
75
75
|
expect(sihlb.offences).to be_empty
|
76
76
|
end
|
77
|
+
|
78
|
+
it 'can handle interpolation in a braceless hash literal' do
|
79
|
+
# A tricky special case where the closing brace of the
|
80
|
+
# interpolation risks getting confused for a hash literal brace.
|
81
|
+
inspect_source(sihlb, ['f(get: "#{x}")'])
|
82
|
+
expect(sihlb.offences).to be_empty
|
83
|
+
end
|
77
84
|
end
|
78
85
|
end
|
79
86
|
end
|
@@ -9,9 +9,7 @@ module Rubocop
|
|
9
9
|
|
10
10
|
it 'registers an offence for double quotes when single quotes suffice' do
|
11
11
|
inspect_source(sl, ['s = "abc"'])
|
12
|
-
expect(sl.offences.
|
13
|
-
["Prefer single-quoted strings when you don't need string " +
|
14
|
-
'interpolation or special symbols.'])
|
12
|
+
expect(sl.offences.size).to eq(1)
|
15
13
|
end
|
16
14
|
|
17
15
|
it 'accepts double quotes when they are needed' do
|
@@ -22,7 +20,12 @@ module Rubocop
|
|
22
20
|
'e = "#$test"',
|
23
21
|
'f = "#@@test"']
|
24
22
|
inspect_source(sl, src)
|
25
|
-
expect(sl.offences
|
23
|
+
expect(sl.offences).to be_empty
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'accepts double quotes at the start of regexp literals' do
|
27
|
+
inspect_source(sl, ['s = /"((?:[^\\"]|\\.)*)"/'])
|
28
|
+
expect(sl.offences).to be_empty
|
26
29
|
end
|
27
30
|
|
28
31
|
it 'accepts double quotes with some other special symbols' do
|
@@ -31,7 +34,7 @@ module Rubocop
|
|
31
34
|
# http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
|
32
35
|
src = ['g = "\xf9"']
|
33
36
|
inspect_source(sl, src)
|
34
|
-
expect(sl.offences
|
37
|
+
expect(sl.offences).to be_empty
|
35
38
|
end
|
36
39
|
|
37
40
|
it 'can handle double quotes within embedded expression' do
|
@@ -39,9 +42,17 @@ module Rubocop
|
|
39
42
|
pending do
|
40
43
|
src = ['"#{"A"}"']
|
41
44
|
inspect_source(sl, src)
|
42
|
-
expect(sl.offences
|
45
|
+
expect(sl.offences).to be_empty
|
43
46
|
end
|
44
47
|
end
|
48
|
+
|
49
|
+
it 'can handle a built-in constant parsed as string' do
|
50
|
+
# Parser will produce str nodes for constants such as __FILE__.
|
51
|
+
src = ['if __FILE__ == $PROGRAM_NAME',
|
52
|
+
'end']
|
53
|
+
inspect_source(sl, src)
|
54
|
+
expect(sl.offences).to be_empty
|
55
|
+
end
|
45
56
|
end
|
46
57
|
end
|
47
58
|
end
|
@@ -324,6 +324,14 @@ module Rubocop
|
|
324
324
|
expect(trivial_accessors_finder.offences).to be_empty
|
325
325
|
end
|
326
326
|
|
327
|
-
|
328
|
-
|
329
|
-
|
327
|
+
it 'accepts an initialize method looking like a writer' do
|
328
|
+
inspect_source(trivial_accessors_finder,
|
329
|
+
[' def initialize(value)',
|
330
|
+
' @top = value',
|
331
|
+
' end'])
|
332
|
+
expect(trivial_accessors_finder.offences).to be_empty
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -28,16 +28,22 @@ dependencies:
|
|
28
28
|
name: parser
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.0.beta1
|
34
|
+
- - <=
|
32
35
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.0.0
|
36
|
+
version: 2.0.0
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
|
-
- -
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.0.0.beta1
|
44
|
+
- - <=
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version: 2.0.0
|
46
|
+
version: 2.0.0
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: rake
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -173,13 +179,13 @@ files:
|
|
173
179
|
- lib/rubocop/cop/handle_exceptions.rb
|
174
180
|
- lib/rubocop/cop/hash_syntax.rb
|
175
181
|
- lib/rubocop/cop/if_then_else.rb
|
182
|
+
- lib/rubocop/cop/lambda.rb
|
176
183
|
- lib/rubocop/cop/leading_comment_space.rb
|
177
184
|
- lib/rubocop/cop/line_continuation.rb
|
178
185
|
- lib/rubocop/cop/line_length.rb
|
179
186
|
- lib/rubocop/cop/loop.rb
|
180
187
|
- lib/rubocop/cop/method_and_variable_snake_case.rb
|
181
188
|
- lib/rubocop/cop/method_length.rb
|
182
|
-
- lib/rubocop/cop/new_lambda_literal.rb
|
183
189
|
- lib/rubocop/cop/not.rb
|
184
190
|
- lib/rubocop/cop/numeric_literals.rb
|
185
191
|
- lib/rubocop/cop/offence.rb
|
@@ -187,6 +193,7 @@ files:
|
|
187
193
|
- lib/rubocop/cop/parameter_lists.rb
|
188
194
|
- lib/rubocop/cop/parentheses_around_condition.rb
|
189
195
|
- lib/rubocop/cop/percent_r.rb
|
196
|
+
- lib/rubocop/cop/proc.rb
|
190
197
|
- lib/rubocop/cop/reduce_arguments.rb
|
191
198
|
- lib/rubocop/cop/rescue_exception.rb
|
192
199
|
- lib/rubocop/cop/rescue_modifier.rb
|
@@ -255,6 +262,7 @@ files:
|
|
255
262
|
- spec/rubocop/cops/handle_exceptions_spec.rb
|
256
263
|
- spec/rubocop/cops/hash_syntax_spec.rb
|
257
264
|
- spec/rubocop/cops/if_with_semicolon_spec.rb
|
265
|
+
- spec/rubocop/cops/lambda_spec.rb
|
258
266
|
- spec/rubocop/cops/leading_comment_space_spec.rb
|
259
267
|
- spec/rubocop/cops/line_continuation_spec.rb
|
260
268
|
- spec/rubocop/cops/line_length_spec.rb
|
@@ -262,7 +270,6 @@ files:
|
|
262
270
|
- spec/rubocop/cops/method_and_variable_snake_case_spec.rb
|
263
271
|
- spec/rubocop/cops/method_length_spec.rb
|
264
272
|
- spec/rubocop/cops/multiline_if_then_spec.rb
|
265
|
-
- spec/rubocop/cops/new_lambda_literal_spec.rb
|
266
273
|
- spec/rubocop/cops/not_spec.rb
|
267
274
|
- spec/rubocop/cops/numeric_literals_spec.rb
|
268
275
|
- spec/rubocop/cops/offence_spec.rb
|
@@ -271,6 +278,7 @@ files:
|
|
271
278
|
- spec/rubocop/cops/parameter_lists_spec.rb
|
272
279
|
- spec/rubocop/cops/parentheses_around_condition_spec.rb
|
273
280
|
- spec/rubocop/cops/percent_r_spec.rb
|
281
|
+
- spec/rubocop/cops/proc_spec.rb
|
274
282
|
- spec/rubocop/cops/reduce_arguments_spec.rb
|
275
283
|
- spec/rubocop/cops/rescue_exception_spec.rb
|
276
284
|
- spec/rubocop/cops/rescue_modifier_spec.rb
|
@@ -370,6 +378,7 @@ test_files:
|
|
370
378
|
- spec/rubocop/cops/handle_exceptions_spec.rb
|
371
379
|
- spec/rubocop/cops/hash_syntax_spec.rb
|
372
380
|
- spec/rubocop/cops/if_with_semicolon_spec.rb
|
381
|
+
- spec/rubocop/cops/lambda_spec.rb
|
373
382
|
- spec/rubocop/cops/leading_comment_space_spec.rb
|
374
383
|
- spec/rubocop/cops/line_continuation_spec.rb
|
375
384
|
- spec/rubocop/cops/line_length_spec.rb
|
@@ -377,7 +386,6 @@ test_files:
|
|
377
386
|
- spec/rubocop/cops/method_and_variable_snake_case_spec.rb
|
378
387
|
- spec/rubocop/cops/method_length_spec.rb
|
379
388
|
- spec/rubocop/cops/multiline_if_then_spec.rb
|
380
|
-
- spec/rubocop/cops/new_lambda_literal_spec.rb
|
381
389
|
- spec/rubocop/cops/not_spec.rb
|
382
390
|
- spec/rubocop/cops/numeric_literals_spec.rb
|
383
391
|
- spec/rubocop/cops/offence_spec.rb
|
@@ -386,6 +394,7 @@ test_files:
|
|
386
394
|
- spec/rubocop/cops/parameter_lists_spec.rb
|
387
395
|
- spec/rubocop/cops/parentheses_around_condition_spec.rb
|
388
396
|
- spec/rubocop/cops/percent_r_spec.rb
|
397
|
+
- spec/rubocop/cops/proc_spec.rb
|
389
398
|
- spec/rubocop/cops/reduce_arguments_spec.rb
|
390
399
|
- spec/rubocop/cops/rescue_exception_spec.rb
|
391
400
|
- spec/rubocop/cops/rescue_modifier_spec.rb
|
@@ -1,19 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Rubocop
|
4
|
-
module Cop
|
5
|
-
class NewLambdaLiteral < Cop
|
6
|
-
MSG = 'Use the new lambda literal syntax ->(params) {...}.'
|
7
|
-
|
8
|
-
TARGET = s(:send, nil, :lambda)
|
9
|
-
|
10
|
-
def on_send(node)
|
11
|
-
if node == TARGET && node.loc.selector.source != '->'
|
12
|
-
add_offence(:convention, node.loc.line, MSG)
|
13
|
-
end
|
14
|
-
|
15
|
-
super
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Rubocop
|
6
|
-
module Cop
|
7
|
-
describe NewLambdaLiteral do
|
8
|
-
let(:lambda_literal) { NewLambdaLiteral.new }
|
9
|
-
|
10
|
-
it 'registers an offence for an old lambda call' do
|
11
|
-
inspect_source(lambda_literal, ['f = lambda { |x| x }'])
|
12
|
-
expect(lambda_literal.offences.size).to eq(1)
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'accepts the new lambda literal' do
|
16
|
-
inspect_source(lambda_literal, ['lambda = ->(x) { x }',
|
17
|
-
'lambda.(1)'])
|
18
|
-
expect(lambda_literal.offences).to be_empty
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|