rubocop 0.19.0 → 0.19.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 +18 -1
- data/README.md +9 -3
- data/config/default.yml +18 -12
- data/lib/rubocop/cli.rb +7 -5
- data/lib/rubocop/cop/lint/literal_in_condition.rb +23 -1
- data/lib/rubocop/cop/style/indent_hash.rb +31 -6
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +26 -16
- data/lib/rubocop/cop/style/regexp_literal.rb +5 -1
- data/lib/rubocop/options.rb +5 -0
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/{0.19.0.md → v0.19.0.md} +24 -0
- data/relnotes/v0.19.1.md +16 -0
- data/rubocop-todo.yml +1 -1
- data/spec/rubocop/cli_spec.rb +34 -2
- data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +36 -0
- data/spec/rubocop/cop/rails/action_filter_spec.rb +14 -14
- data/spec/rubocop/cop/style/indent_hash_spec.rb +73 -1
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +10 -2
- data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +12 -0
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +2 -2
- data/spec/rubocop/cop/team_spec.rb +1 -1
- data/spec/rubocop/options_spec.rb +27 -0
- data/spec/spec_helper.rb +15 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7123eae3f7c324c84d026c2aeb11ebddb376064
|
4
|
+
data.tar.gz: 18fe22cdfb3bebefc347a49510eddfc2ef5bc39b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3435aa3b04824630ecccfbc802c7d4ef8c74f4d8b0c7d697cd2e2b6d535ca0c6e7953ddb2a2f741303f3fc307776725053c6d9e3dc7b7a61d4e73811cadf6bb
|
7
|
+
data.tar.gz: dbc6fd5c8ab02d9f759c416d6a2e111f2715c4a9cb93620980e6bd8dac59b03d3b5c1b349fd59b86f0693b0dff9b66f1924870868e58c2a1245a5d90158c00d1
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,20 @@
|
|
2
2
|
|
3
3
|
## master (unreleased)
|
4
4
|
|
5
|
+
## 0.19.1 (17/03/2014)
|
6
|
+
|
7
|
+
### Bugs fixed
|
8
|
+
|
9
|
+
* [#884](https://github.com/bbatsov/rubocop/issues/884): Fix --auto-gen-config for `NumericLiterals` so MinDigits is correct. ([@tmorris-fiksu][])
|
10
|
+
* [#879](https://github.com/bbatsov/rubocop/issues/879): Fix --auto-gen-config for `RegexpLiteral` so we don't generate illegal values for `MaxSlashes`. ([@jonas054][])
|
11
|
+
* Fix the name of the `Include` param in the default config of the Rails cops. ([@bbatsov][])
|
12
|
+
* [#878](https://github.com/bbatsov/rubocop/pull/878): Blacklist `Rakefile`, `Gemfile` and `Capfile` by default in the `FileName` cop. ([@bbatsov][])
|
13
|
+
* [#875](https://github.com/bbatsov/rubocop/issues/875): Handle `separator` style hashes in `IndentHash`. ([@jonas054][])
|
14
|
+
* Fix a bug where multiple cli options that result in exit can be specified at once (e.g. `-vV`, `-v --show-cops`). ([@jkogara][])
|
15
|
+
* [#889](https://github.com/bbatsov/rubocop/issues/889): Fix a false positive for `LiteralInCondition` when the condition is non-primitive array. ([@bbatsov][])
|
16
|
+
|
17
|
+
## 0.19.0 (13/03/2014)
|
18
|
+
|
5
19
|
### New features
|
6
20
|
|
7
21
|
* New cop `FileName` makes sure that source files have snake_case names. ([@bbatsov][])
|
@@ -61,6 +75,7 @@
|
|
61
75
|
* [#862](https://github.com/bbatsov/rubocop/issues/862): Fix a bug where single line `rubocop:disable` comments with indentations were treated as multiline cop disabling comments. ([@yujinakayama][])
|
62
76
|
* Fix a bug where `rubocop:disable` comments with a cop name including `all` (e.g. `MethodCallParentheses`) were disabling all cops. ([@yujinakayama][])
|
63
77
|
* Fix a bug where string and regexp literals including `# rubocop:disable` were confused with real comments. ([@yujinakayama][])
|
78
|
+
* [#877](https://github.com/bbatsov/rubocop/issues/877): Fix bug in `PercentLiteralDelimiters` concerning auto-correct of regular expressions with interpolation. ([@hannestyden][])
|
64
79
|
|
65
80
|
## 0.18.1 (02/02/2014)
|
66
81
|
|
@@ -349,7 +364,7 @@
|
|
349
364
|
|
350
365
|
* [#449](https://github.com/bbatsov/rubocop/issues/449): Remove whitespaces between condition and `do` with `WhileUntilDo` auto-correction.
|
351
366
|
* Continue with file inspection after parser warnings. Give up only on syntax errors.
|
352
|
-
* Don
|
367
|
+
* Don't trigger the HashSyntax cop on digit-starting keys.
|
353
368
|
* Fix crashes while inspecting class definition subclassing another class stored in a local variable in `UselessAssignment` (formerly of `UnusedLocalVariable`) and `ShadowingOuterLocalVariable` (like `clazz = Array; class SomeClass < clazz; end`).
|
354
369
|
* [#463](https://github.com/bbatsov/rubocop/issues/463): Do not warn if using destructuring in second `reduce` argument (`ReduceArguments`).
|
355
370
|
|
@@ -755,3 +770,5 @@
|
|
755
770
|
[@jeremyolliver]: https://github.com/jeremyolliver
|
756
771
|
[@hannestyden]: https://github.com/hannestyden
|
757
772
|
[@geniou]: https://github.com/geniou
|
773
|
+
[@jkogara]: https://github.com/jkogara
|
774
|
+
[@tmorris-fiksu]: https://github.com/tmorris-fiksu
|
data/README.md
CHANGED
@@ -70,6 +70,12 @@ automatically fix some of the problems for you.
|
|
70
70
|
$ gem install rubocop
|
71
71
|
```
|
72
72
|
|
73
|
+
If you'd rather install RuboCop using `bundler`, don't require it in your `Gemfile`:
|
74
|
+
|
75
|
+
```
|
76
|
+
gem 'rubocop', require: false
|
77
|
+
```
|
78
|
+
|
73
79
|
## Basic Usage
|
74
80
|
|
75
81
|
Running `rubocop` with no arguments will check all Ruby source files
|
@@ -259,14 +265,14 @@ follow RuboCop's general principle that configuration for an inspected
|
|
259
265
|
file is taken from the nearest `.rubocop.yml`, searching upwards.
|
260
266
|
|
261
267
|
Cops can be run only on specific sets of files when that's needed (for
|
262
|
-
instance you might want to run some Rails model checks only on files
|
263
|
-
|
268
|
+
instance you might want to run some Rails model checks only on files whose
|
269
|
+
paths match `app/models/*.rb`). All cops support the
|
264
270
|
`Include` param.
|
265
271
|
|
266
272
|
```yaml
|
267
273
|
DefaultScope:
|
268
274
|
Include:
|
269
|
-
- app/models
|
275
|
+
- app/models/*.rb
|
270
276
|
```
|
271
277
|
|
272
278
|
Cops can also exclude only specific sets of files when that's needed (for
|
data/config/default.yml
CHANGED
@@ -200,6 +200,12 @@ EndAlignment:
|
|
200
200
|
- keyword
|
201
201
|
- variable
|
202
202
|
|
203
|
+
FileName:
|
204
|
+
Exclude:
|
205
|
+
- Rakefile
|
206
|
+
- Gemfile
|
207
|
+
- Capfile
|
208
|
+
|
203
209
|
# Checks use of for or each in multiline loops.
|
204
210
|
For:
|
205
211
|
EnforcedStyle: each
|
@@ -420,26 +426,26 @@ ActionFilter:
|
|
420
426
|
SupportedStyles:
|
421
427
|
- action
|
422
428
|
- filter
|
423
|
-
|
424
|
-
-app/controllers
|
429
|
+
Include:
|
430
|
+
- app/controllers/*.rb
|
425
431
|
|
426
432
|
DefaultScope:
|
427
|
-
|
428
|
-
- app/models
|
433
|
+
Include:
|
434
|
+
- app/models/*.rb
|
429
435
|
|
430
436
|
HasAndBelongsToMany:
|
431
|
-
|
432
|
-
- app/models
|
437
|
+
Include:
|
438
|
+
- app/models/*.rb
|
433
439
|
|
434
440
|
ReadAttribute:
|
435
|
-
|
436
|
-
- app/models
|
441
|
+
Include:
|
442
|
+
- app/models/*.rb
|
437
443
|
|
438
444
|
ScopeArgs:
|
439
|
-
|
440
|
-
- app/models
|
445
|
+
Include:
|
446
|
+
- app/models/*.rb
|
441
447
|
|
442
448
|
Validation:
|
443
|
-
|
444
|
-
- app/models
|
449
|
+
Include:
|
450
|
+
- app/models/*.rb
|
445
451
|
|
data/lib/rubocop/cli.rb
CHANGED
@@ -52,10 +52,7 @@ module Rubocop
|
|
52
52
|
private
|
53
53
|
|
54
54
|
def act_on_options(args)
|
55
|
-
|
56
|
-
print_available_cops
|
57
|
-
exit(0)
|
58
|
-
end
|
55
|
+
handle_exiting_options
|
59
56
|
|
60
57
|
ConfigLoader.debug = @options[:debug]
|
61
58
|
ConfigLoader.auto_gen_config = @options[:auto_gen_config]
|
@@ -63,10 +60,15 @@ module Rubocop
|
|
63
60
|
@config_store.options_config = @options[:config] if @options[:config]
|
64
61
|
|
65
62
|
Rainbow.enabled = false unless @options[:color]
|
63
|
+
end
|
64
|
+
|
65
|
+
def handle_exiting_options
|
66
|
+
return unless Options::EXITING_OPTIONS.any? { |o| @options.key? o }
|
66
67
|
|
67
68
|
puts Rubocop::Version.version(false) if @options[:version]
|
68
69
|
puts Rubocop::Version.version(true) if @options[:verbose_version]
|
69
|
-
|
70
|
+
print_available_cops if @options[:show_cops]
|
71
|
+
exit(0)
|
70
72
|
end
|
71
73
|
|
72
74
|
def print_available_cops
|
@@ -23,6 +23,8 @@ module Rubocop
|
|
23
23
|
LITERALS = [:str, :dstr, :int, :float, :array,
|
24
24
|
:hash, :regexp, :nil, :true, :false]
|
25
25
|
|
26
|
+
BASIC_LITERALS = LITERALS - [:dstr, :array, :hash]
|
27
|
+
|
26
28
|
def on_if(node)
|
27
29
|
check_for_literal(node)
|
28
30
|
end
|
@@ -45,8 +47,9 @@ module Rubocop
|
|
45
47
|
|
46
48
|
def on_case(node)
|
47
49
|
cond, *whens, _else = *node
|
50
|
+
|
48
51
|
if cond
|
49
|
-
|
52
|
+
check_case_cond(cond)
|
50
53
|
else
|
51
54
|
whens.each do |when_node|
|
52
55
|
check_for_literal(when_node)
|
@@ -85,6 +88,18 @@ module Rubocop
|
|
85
88
|
LITERALS.include?(node.type)
|
86
89
|
end
|
87
90
|
|
91
|
+
def basic_literal?(node)
|
92
|
+
if node && node.type == :array
|
93
|
+
primitive_array?(node)
|
94
|
+
else
|
95
|
+
BASIC_LITERALS.include?(node.type)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def primitive_array?(node)
|
100
|
+
node.children.all? { |n| basic_literal?(n) }
|
101
|
+
end
|
102
|
+
|
88
103
|
def check_node(node)
|
89
104
|
return unless node
|
90
105
|
|
@@ -110,6 +125,13 @@ module Rubocop
|
|
110
125
|
check_node(node)
|
111
126
|
end
|
112
127
|
end
|
128
|
+
|
129
|
+
def check_case_cond(node)
|
130
|
+
return if node.type == :array && !primitive_array?(node)
|
131
|
+
return if node.type == :dstr
|
132
|
+
|
133
|
+
handle_node(node)
|
134
|
+
end
|
113
135
|
end
|
114
136
|
end
|
115
137
|
end
|
@@ -51,16 +51,40 @@ module Rubocop
|
|
51
51
|
left_brace = hash_node.loc.begin
|
52
52
|
return if first_pair.loc.expression.line == left_brace.line
|
53
53
|
|
54
|
+
if separator_style?(first_pair)
|
55
|
+
check_based_on_longest_key(hash_node.children, left_brace,
|
56
|
+
left_parenthesis)
|
57
|
+
else
|
58
|
+
check_first_pair(first_pair, left_brace, left_parenthesis, 0)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def separator_style?(first_pair)
|
63
|
+
separator = first_pair.loc.operator
|
64
|
+
key = "Enforced#{separator.is?(':') ? 'Colon' : 'HashRocket'}Style"
|
65
|
+
config.for_cop('AlignHash')[key] == 'separator'
|
66
|
+
end
|
67
|
+
|
68
|
+
def check_based_on_longest_key(pairs, left_brace, left_parenthesis)
|
69
|
+
key_lengths = pairs.map do |pair|
|
70
|
+
pair.children.first.loc.expression.length
|
71
|
+
end
|
72
|
+
check_first_pair(pairs.first, left_brace, left_parenthesis,
|
73
|
+
key_lengths.max - key_lengths.first)
|
74
|
+
end
|
75
|
+
|
76
|
+
def check_first_pair(first_pair, left_brace, left_parenthesis, offset)
|
54
77
|
column = first_pair.loc.expression.column
|
55
78
|
@column_delta =
|
56
|
-
expected_column(left_brace, left_parenthesis) - column
|
79
|
+
expected_column(left_brace, left_parenthesis, offset) - column
|
57
80
|
|
58
81
|
if @column_delta == 0
|
59
82
|
correct_style_detected
|
60
83
|
else
|
61
84
|
add_offense(first_pair, :expression,
|
62
85
|
message(base_description(left_parenthesis))) do
|
63
|
-
if column == unexpected_column(left_brace, left_parenthesis
|
86
|
+
if column == unexpected_column(left_brace, left_parenthesis,
|
87
|
+
offset)
|
64
88
|
opposite_style_detected
|
65
89
|
else
|
66
90
|
unrecognized_style_detected
|
@@ -70,7 +94,7 @@ module Rubocop
|
|
70
94
|
end
|
71
95
|
|
72
96
|
# Returns the expected, or correct indentation.
|
73
|
-
def expected_column(left_brace, left_parenthesis)
|
97
|
+
def expected_column(left_brace, left_parenthesis, offset)
|
74
98
|
base_column =
|
75
99
|
if left_parenthesis && style == :special_inside_parentheses
|
76
100
|
left_parenthesis.column + 1
|
@@ -78,7 +102,7 @@ module Rubocop
|
|
78
102
|
left_brace.source_line =~ /\S/
|
79
103
|
end
|
80
104
|
|
81
|
-
base_column + IndentationWidth::CORRECT_INDENTATION
|
105
|
+
base_column + IndentationWidth::CORRECT_INDENTATION + offset
|
82
106
|
end
|
83
107
|
|
84
108
|
# Returns the description of what the correct indentation is based on.
|
@@ -92,7 +116,7 @@ module Rubocop
|
|
92
116
|
|
93
117
|
# Returns the "unexpected column", which is the column that would be
|
94
118
|
# correct if the configuration was changed.
|
95
|
-
def unexpected_column(left_brace, left_parenthesis)
|
119
|
+
def unexpected_column(left_brace, left_parenthesis, offset)
|
96
120
|
# Set a crazy value by default, indicating that there's no other
|
97
121
|
# configuration that can be chosen to make the used indentation
|
98
122
|
# accepted.
|
@@ -106,7 +130,8 @@ module Rubocop
|
|
106
130
|
end
|
107
131
|
end
|
108
132
|
|
109
|
-
unexpected_base_column + IndentationWidth::CORRECT_INDENTATION
|
133
|
+
unexpected_base_column + IndentationWidth::CORRECT_INDENTATION +
|
134
|
+
offset
|
110
135
|
end
|
111
136
|
|
112
137
|
def message(base_description)
|
@@ -37,7 +37,7 @@ module Rubocop
|
|
37
37
|
if int.size >= min_digits
|
38
38
|
case int
|
39
39
|
when /^\d+$/
|
40
|
-
add_offense(node, :expression) { self.max = int.size }
|
40
|
+
add_offense(node, :expression) { self.max = int.size + 1 }
|
41
41
|
when /\d{4}/, /_\d{1,2}_/
|
42
42
|
add_offense(node, :expression) do
|
43
43
|
self.config_to_allow_offenses = { 'Enabled' => false }
|
@@ -43,13 +43,7 @@ module Rubocop
|
|
43
43
|
opening_newline = new_line(node.loc.begin, node.children.first)
|
44
44
|
closing_newline = new_line(node.loc.end, node.children.last)
|
45
45
|
|
46
|
-
expression, reg_opt =
|
47
|
-
if node.type == :regexp
|
48
|
-
[contents(node.children.first), contents(node.children.last)]
|
49
|
-
else
|
50
|
-
[contents(node), '']
|
51
|
-
end
|
52
|
-
|
46
|
+
expression, reg_opt = *contents(node)
|
53
47
|
corrected_source =
|
54
48
|
type + opening_delimiter + opening_newline +
|
55
49
|
expression +
|
@@ -91,19 +85,35 @@ module Rubocop
|
|
91
85
|
end
|
92
86
|
|
93
87
|
def contents(node)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
node
|
101
|
-
|
88
|
+
first_child, *middle, last_child = *node
|
89
|
+
last_child ||= first_child
|
90
|
+
if node.type == :regexp
|
91
|
+
*_, next_to_last_child = *middle
|
92
|
+
next_to_last_child ||= first_child
|
93
|
+
[
|
94
|
+
source(node, first_child, next_to_last_child),
|
95
|
+
last_child.loc.expression.source
|
96
|
+
]
|
102
97
|
else
|
103
|
-
|
98
|
+
[
|
99
|
+
if first_child.is_a?(Parser::AST::Node)
|
100
|
+
source(node, first_child, last_child)
|
101
|
+
else
|
102
|
+
first_child.to_s
|
103
|
+
end,
|
104
|
+
''
|
105
|
+
]
|
104
106
|
end
|
105
107
|
end
|
106
108
|
|
109
|
+
def source(node, begin_node, end_node)
|
110
|
+
Parser::Source::Range.new(
|
111
|
+
node.loc.expression.source_buffer,
|
112
|
+
begin_node.loc.expression.begin_pos,
|
113
|
+
end_node.loc.expression.end_pos
|
114
|
+
).source
|
115
|
+
end
|
116
|
+
|
107
117
|
def uses_preferred_delimiter?(node, type)
|
108
118
|
preferred_delimiters(type)[0] == begin_source(node)[-1]
|
109
119
|
end
|
data/lib/rubocop/options.rb
CHANGED
@@ -6,6 +6,7 @@ module Rubocop
|
|
6
6
|
# This class handles command line options.
|
7
7
|
class Options
|
8
8
|
DEFAULT_FORMATTER = 'progress'
|
9
|
+
EXITING_OPTIONS = [:version, :verbose_version, :show_cops]
|
9
10
|
|
10
11
|
def initialize
|
11
12
|
@options = {}
|
@@ -34,6 +35,10 @@ module Rubocop
|
|
34
35
|
add_boolean_flags(opts)
|
35
36
|
end.parse!(args)
|
36
37
|
|
38
|
+
if (incompat = @options.keys & EXITING_OPTIONS).size > 1
|
39
|
+
fail ArgumentError, "Incompatible cli options: #{incompat.inspect}"
|
40
|
+
end
|
41
|
+
|
37
42
|
[@options, args]
|
38
43
|
end
|
39
44
|
|
data/lib/rubocop/version.rb
CHANGED
@@ -68,3 +68,27 @@ Below is the list of all the gory details. Enjoy!
|
|
68
68
|
* [#862](https://github.com/bbatsov/rubocop/issues/862): Fix a bug where single line `rubocop:disable` comments with indentations were treated as multiline cop disabling comments. ([@yujinakayama][])
|
69
69
|
* Fix a bug where `rubocop:disable` comments with a cop name including `all` (e.g. `MethodCallParentheses`) were disabling all cops. ([@yujinakayama][])
|
70
70
|
* Fix a bug where string and regexp literals including `# rubocop:disable` were confused with real comments. ([@yujinakayama][])
|
71
|
+
|
72
|
+
[@bbatsov]: https://github.com/bbatsov
|
73
|
+
[@jonas054]: https://github.com/jonas054
|
74
|
+
[@yujinakayama]: https://github.com/yujinakayama
|
75
|
+
[@dblock]: https://github.com/dblock
|
76
|
+
[@nevir]: https://github.com/nevir
|
77
|
+
[@daviddavis]: https://github.com/daviddavis
|
78
|
+
[@sds]: https://github.com/sds
|
79
|
+
[@fancyremarker]: https://github.com/fancyremarker
|
80
|
+
[@sinisterchipmunk]: https://github.com/sinisterchipmunk
|
81
|
+
[@vonTronje]: https://github.com/vonTronje
|
82
|
+
[@agrimm]: https://github.com/agrimm
|
83
|
+
[@pmenglund]: https://github.com/pmenglund
|
84
|
+
[@chulkilee]: https://github.com/chulkilee
|
85
|
+
[@codez]: https://github.com/codez
|
86
|
+
[@emou]: https://github.com/emou
|
87
|
+
[@skanev]: http://github.com/skanev
|
88
|
+
[@claco]: http://github.com/claco
|
89
|
+
[@rifraf]: http://github.com/rifraf
|
90
|
+
[@scottmatthewman]: https://github.com/scottmatthewman
|
91
|
+
[@ma2gedev]: http://github.com/ma2gedev
|
92
|
+
[@jeremyolliver]: https://github.com/jeremyolliver
|
93
|
+
[@hannestyden]: https://github.com/hannestyden
|
94
|
+
[@geniou]: https://github.com/geniou
|
data/relnotes/v0.19.1.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
RuboCop 0.19.1 is a bugfix-only release. Below is a list of the bugs we've fixed since 0.19.0:
|
2
|
+
|
3
|
+
### Bugs fixed
|
4
|
+
|
5
|
+
* [#884](https://github.com/bbatsov/rubocop/issues/884): Fix --auto-gen-config for `NumericLiterals` so MinDigits is correct. ([@tmorris-fiksu][])
|
6
|
+
* [#879](https://github.com/bbatsov/rubocop/issues/879): Fix --auto-gen-config for `RegexpLiteral` so we don't generate illegal values for `MaxSlashes`. ([@jonas054][])
|
7
|
+
* Fix the name of the `Include` param in the default config of the Rails cops. ([@bbatsov][])
|
8
|
+
* [#878](https://github.com/bbatsov/rubocop/pull/878): Blacklist `Rakefile`, `Gemfile` and `Capfile` by default in the `FileName` cop. ([@bbatsov][])
|
9
|
+
* [#875](https://github.com/bbatsov/rubocop/issues/875): Handle `separator` style hashes in `IndentHash`. ([@jonas054][])
|
10
|
+
* Fix a bug where multiple cli options that result in exit can be specified at once (e.g. `-vV`, `-v --show-cops`). ([@jkogara][])
|
11
|
+
* [#889](https://github.com/bbatsov/rubocop/issues/889): Fix a false positive for `LiteralInCondition` when the condition is non-primitive array. ([@bbatsov][])
|
12
|
+
|
13
|
+
[@bbatsov]: https://github.com/bbatsov
|
14
|
+
[@jonas054]: https://github.com/jonas054
|
15
|
+
[@jkogara]: https://github.com/jkogara
|
16
|
+
[@tmorris-fiksu]: https://github.com/tmorris-fiksu
|
data/rubocop-todo.yml
CHANGED
data/spec/rubocop/cli_spec.rb
CHANGED
@@ -321,6 +321,30 @@ describe Rubocop::CLI, :isolated_environment do
|
|
321
321
|
''].join("\n"))
|
322
322
|
end
|
323
323
|
|
324
|
+
it 'can correct IndentHash offenses with separator style' do
|
325
|
+
create_file('example.rb',
|
326
|
+
['# encoding: utf-8',
|
327
|
+
'CONVERSION_CORRESPONDENCE = {',
|
328
|
+
' match_for_should: :match,',
|
329
|
+
' match_for_should_not: :match_when_negated,',
|
330
|
+
' failure_message_for_should: :failure_message,',
|
331
|
+
'failure_message_for_should_not: :failure_message_when',
|
332
|
+
'}'])
|
333
|
+
create_file('.rubocop.yml',
|
334
|
+
['AlignHash:',
|
335
|
+
' EnforcedColonStyle: separator'])
|
336
|
+
expect(cli.run(%w(--auto-correct))).to eq(1)
|
337
|
+
expect(IO.read('example.rb'))
|
338
|
+
.to eq(['# encoding: utf-8',
|
339
|
+
'CONVERSION_CORRESPONDENCE = {',
|
340
|
+
' match_for_should: :match,',
|
341
|
+
' match_for_should_not: :match_when_negated,',
|
342
|
+
' failure_message_for_should: :failure_message,',
|
343
|
+
' failure_message_for_should_not: :failure_message_when',
|
344
|
+
'}',
|
345
|
+
''].join("\n"))
|
346
|
+
end
|
347
|
+
|
324
348
|
it 'should not hang SpaceAfterPunctuation and SpaceInsideParens' do
|
325
349
|
create_file('example.rb',
|
326
350
|
['# encoding: utf-8',
|
@@ -1086,11 +1110,19 @@ describe Rubocop::CLI, :isolated_environment do
|
|
1086
1110
|
create_file('dir1/app/models/example1.rb', ['# encoding: utf-8',
|
1087
1111
|
'read_attribute(:test)'])
|
1088
1112
|
create_file('dir1/.rubocop.yml', ['AllCops:',
|
1089
|
-
' RunRailsCops: true'
|
1113
|
+
' RunRailsCops: true',
|
1114
|
+
'',
|
1115
|
+
'ReadAttribute:',
|
1116
|
+
' Include:',
|
1117
|
+
' - dir1/app/models/*.rb'])
|
1090
1118
|
create_file('dir2/app/models/example2.rb', ['# encoding: utf-8',
|
1091
1119
|
'read_attribute(:test)'])
|
1092
1120
|
create_file('dir2/.rubocop.yml', ['AllCops:',
|
1093
|
-
' RunRailsCops: false'
|
1121
|
+
' RunRailsCops: false',
|
1122
|
+
'',
|
1123
|
+
'ReadAttribute:',
|
1124
|
+
' Include:',
|
1125
|
+
' - dir2/app/models/*.rb'])
|
1094
1126
|
expect(cli.run(%w(--format simple dir1 dir2))).to eq(1)
|
1095
1127
|
expect($stdout.string)
|
1096
1128
|
.to eq(['== dir1/app/models/example1.rb ==',
|
@@ -115,4 +115,40 @@ describe Rubocop::Cop::Lint::LiteralInCondition do
|
|
115
115
|
expect(cop.offenses).to be_empty
|
116
116
|
end
|
117
117
|
end
|
118
|
+
|
119
|
+
it 'accepts array literal in case, if it has non-literal elements' do
|
120
|
+
inspect_source(cop,
|
121
|
+
['case [1, 2, x]',
|
122
|
+
'when [1, 2, 5] then top',
|
123
|
+
'end'
|
124
|
+
])
|
125
|
+
expect(cop.offenses).to be_empty
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'accepts array literal in case, if it has non-literal elements' do
|
129
|
+
inspect_source(cop,
|
130
|
+
['case [1, 2, [x, 1]]',
|
131
|
+
'when [1, 2, 5] then top',
|
132
|
+
'end'
|
133
|
+
])
|
134
|
+
expect(cop.offenses).to be_empty
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'registers an offense for case with a primitive array condition' do
|
138
|
+
inspect_source(cop,
|
139
|
+
['case [1, 2, [3, 4]]',
|
140
|
+
'when [1, 2, 5] then top',
|
141
|
+
'end'
|
142
|
+
])
|
143
|
+
expect(cop.offenses.size).to eq(1)
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'accepts dstr literal in case' do
|
147
|
+
inspect_source(cop,
|
148
|
+
['case "#{x}"',
|
149
|
+
'when [1, 2, 5] then top',
|
150
|
+
'end'
|
151
|
+
])
|
152
|
+
expect(cop.offenses).to be_empty
|
153
|
+
end
|
118
154
|
end
|
@@ -10,28 +10,28 @@ describe Rubocop::Cop::Rails::ActionFilter, :config do
|
|
10
10
|
|
11
11
|
described_class::FILTER_METHODS.each do |method|
|
12
12
|
it "registers an offense for #{method}" do
|
13
|
-
|
14
|
-
|
13
|
+
inspect_source_file(cop,
|
14
|
+
["#{method} :name"])
|
15
15
|
expect(cop.offenses.size).to eq(1)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "registers an offense for #{method} with block" do
|
19
|
-
|
20
|
-
|
19
|
+
inspect_source_file(cop,
|
20
|
+
["#{method} { |controller| something }"])
|
21
21
|
expect(cop.offenses.size).to eq(1)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
described_class::ACTION_METHODS.each do |method|
|
26
26
|
it "accepts #{method}" do
|
27
|
-
|
28
|
-
|
27
|
+
inspect_source_file(cop,
|
28
|
+
["#{method} :something"])
|
29
29
|
expect(cop.offenses).to be_empty
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'auto-corrects to preferred method' do
|
34
|
-
new_source =
|
34
|
+
new_source = autocorrect_source_file(cop, 'before_filter :test')
|
35
35
|
expect(new_source).to eq('before_action :test')
|
36
36
|
end
|
37
37
|
end
|
@@ -41,28 +41,28 @@ describe Rubocop::Cop::Rails::ActionFilter, :config do
|
|
41
41
|
|
42
42
|
described_class::ACTION_METHODS.each do |method|
|
43
43
|
it "registers an offense for #{method}" do
|
44
|
-
|
45
|
-
|
44
|
+
inspect_source_file(cop,
|
45
|
+
["#{method} :name"])
|
46
46
|
expect(cop.offenses.size).to eq(1)
|
47
47
|
end
|
48
48
|
|
49
49
|
it "registers an offense for #{method} with block" do
|
50
|
-
|
51
|
-
|
50
|
+
inspect_source_file(cop,
|
51
|
+
["#{method} { |controller| something }"])
|
52
52
|
expect(cop.offenses.size).to eq(1)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
described_class::FILTER_METHODS.each do |method|
|
57
57
|
it "accepts #{method}" do
|
58
|
-
|
59
|
-
|
58
|
+
inspect_source_file(cop,
|
59
|
+
["#{method} :something"])
|
60
60
|
expect(cop.offenses).to be_empty
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'auto-corrects to preferred method' do
|
65
|
-
new_source =
|
65
|
+
new_source = autocorrect_source_file(cop, 'before_action :test')
|
66
66
|
expect(new_source).to eq('before_filter :test')
|
67
67
|
end
|
68
68
|
end
|
@@ -2,10 +2,82 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe Rubocop::Cop::Style::IndentHash
|
5
|
+
describe Rubocop::Cop::Style::IndentHash do
|
6
6
|
subject(:cop) { described_class.new(config) }
|
7
|
+
let(:config) do
|
8
|
+
supported_styles = {
|
9
|
+
'SupportedStyles' => %w(special_inside_parentheses consistent)
|
10
|
+
}
|
11
|
+
Rubocop::Config.new('AlignHash' => align_hash_config,
|
12
|
+
'IndentHash' => cop_config.merge(supported_styles))
|
13
|
+
end
|
14
|
+
let(:align_hash_config) do
|
15
|
+
{
|
16
|
+
'Enabled' => true,
|
17
|
+
'EnforcedColonStyle' => 'key',
|
18
|
+
'EnforcedHashRocketStyle' => 'key'
|
19
|
+
}
|
20
|
+
end
|
7
21
|
let(:cop_config) { { 'EnforcedStyle' => 'special_inside_parentheses' } }
|
8
22
|
|
23
|
+
context 'when the AlignHash style is separator for :' do
|
24
|
+
let(:align_hash_config) do
|
25
|
+
{
|
26
|
+
'Enabled' => true,
|
27
|
+
'EnforcedColonStyle' => 'separator',
|
28
|
+
'EnforcedHashRocketStyle' => 'key'
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'accepts correctly indented first pair' do
|
33
|
+
inspect_source(cop,
|
34
|
+
['a << {',
|
35
|
+
' a: 1,',
|
36
|
+
' aaa: 222',
|
37
|
+
'}'])
|
38
|
+
expect(cop.offenses).to be_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'registers an offense for incorrectly indented first pair with :' do
|
42
|
+
inspect_source(cop,
|
43
|
+
['a << {',
|
44
|
+
' a: 1,',
|
45
|
+
' aaa: 222',
|
46
|
+
'}'])
|
47
|
+
expect(cop.highlights).to eq(['a: 1'])
|
48
|
+
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when the AlignHash style is separator for =>' do
|
53
|
+
let(:align_hash_config) do
|
54
|
+
{
|
55
|
+
'Enabled' => true,
|
56
|
+
'EnforcedColonStyle' => 'key',
|
57
|
+
'EnforcedHashRocketStyle' => 'separator'
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'accepts correctly indented first pair' do
|
62
|
+
inspect_source(cop,
|
63
|
+
['a << {',
|
64
|
+
" 'a' => 1,",
|
65
|
+
" 'aaa' => 222",
|
66
|
+
'}'])
|
67
|
+
expect(cop.offenses).to be_empty
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'registers an offense for incorrectly indented first pair with =>' do
|
71
|
+
inspect_source(cop,
|
72
|
+
['a << {',
|
73
|
+
" 'a' => 1,",
|
74
|
+
" 'aaa' => 222",
|
75
|
+
'}'])
|
76
|
+
expect(cop.highlights).to eq(["'a' => 1"])
|
77
|
+
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
9
81
|
context 'when hash is operand' do
|
10
82
|
it 'accepts correctly indented first pair' do
|
11
83
|
inspect_source(cop,
|
@@ -6,12 +6,20 @@ describe Rubocop::Cop::Style::NumericLiterals, :config do
|
|
6
6
|
subject(:cop) { described_class.new(config) }
|
7
7
|
let(:cop_config) { { 'MinDigits' => 5 } }
|
8
8
|
|
9
|
-
it 'registers an offense for a long integer
|
10
|
-
inspect_source(cop, ['a =
|
9
|
+
it 'registers an offense for a long undelimited integer' do
|
10
|
+
inspect_source(cop, ['a = 12345'])
|
11
11
|
expect(cop.offenses.size).to eq(1)
|
12
12
|
expect(cop.config_to_allow_offenses).to eq('MinDigits' => 6)
|
13
13
|
end
|
14
14
|
|
15
|
+
it 'registers an offense for a float with a long undelimited integer part' do
|
16
|
+
pending 'Though the offense message implies that floats are checked, ' \
|
17
|
+
'currently the cop only detects integers.'
|
18
|
+
inspect_source(cop, ['a = 123456.789'])
|
19
|
+
expect(cop.offenses.size).to eq(1)
|
20
|
+
expect(cop.config_to_allow_offenses).to eq('MinDigits' => 7)
|
21
|
+
end
|
22
|
+
|
15
23
|
it 'registers an offense for an integer with misplaced underscore' do
|
16
24
|
inspect_source(cop, ['a = 123_456_78_90_00'])
|
17
25
|
expect(cop.offenses.size).to eq(1)
|
@@ -236,6 +236,18 @@ describe Rubocop::Cop::Style::PercentLiteralDelimiters, :config do
|
|
236
236
|
expect(new_source).to eq('%r[.*]')
|
237
237
|
end
|
238
238
|
|
239
|
+
it 'fixes a string with interpolation' do
|
240
|
+
original_source = '%Q|#{with_interpolation}|'
|
241
|
+
new_source = autocorrect_source(cop, original_source)
|
242
|
+
expect(new_source).to eq('%Q[#{with_interpolation}]')
|
243
|
+
end
|
244
|
+
|
245
|
+
it 'fixes a regular expression with interpolation' do
|
246
|
+
original_source = '%r|#{with_interpolation}|'
|
247
|
+
new_source = autocorrect_source(cop, original_source)
|
248
|
+
expect(new_source).to eq('%r[#{with_interpolation}]')
|
249
|
+
end
|
250
|
+
|
239
251
|
it 'fixes a regular expression with option' do
|
240
252
|
original_source = '%r(.*)i'
|
241
253
|
new_source = autocorrect_source(cop, original_source)
|
@@ -35,7 +35,7 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
|
|
35
35
|
expect(cop.messages)
|
36
36
|
.to eq(['Use %r only for regular expressions matching more ' \
|
37
37
|
"than 0 '/' characters."])
|
38
|
-
expect(cop.config_to_allow_offenses).to eq('
|
38
|
+
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'accepts %r regexp with one slash' do
|
@@ -92,7 +92,7 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
|
|
92
92
|
expect(cop.messages)
|
93
93
|
.to eq(['Use %r only for regular expressions matching more ' \
|
94
94
|
"than 1 '/' character."] * 2)
|
95
|
-
expect(cop.config_to_allow_offenses).to eq('
|
95
|
+
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'accepts %r regexp with two or more slashes' do
|
@@ -99,6 +99,33 @@ Usage: rubocop [options] [file1, file2, ...]
|
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
|
+
describe 'incompatible cli options' do
|
103
|
+
it 'fails with argument correct error' do
|
104
|
+
msg = 'Incompatible cli options: [:version, :verbose_version]'
|
105
|
+
expect { options.parse %w(-vV) }
|
106
|
+
.to raise_error(ArgumentError, msg)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'fails with argument correct error' do
|
110
|
+
msg = 'Incompatible cli options: [:version, :show_cops]'
|
111
|
+
expect { options.parse %w(-v --show-cops) }
|
112
|
+
.to raise_error(ArgumentError, msg)
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'fails with argument correct error' do
|
116
|
+
msg = 'Incompatible cli options: [:verbose_version, :show_cops]'
|
117
|
+
expect { options.parse %w(-V --show-cops) }
|
118
|
+
.to raise_error(ArgumentError, msg)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'fails with argument correct error' do
|
122
|
+
msg = ['Incompatible cli options: [:version, :verbose_version,',
|
123
|
+
' :show_cops]'].join
|
124
|
+
expect { options.parse %w(-vV --show-cops) }
|
125
|
+
.to raise_error(ArgumentError, msg)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
102
129
|
describe '--only' do
|
103
130
|
it 'exits with error if an incorrect cop name is passed' do
|
104
131
|
expect { options.parse(%w(--only 123)) }
|
data/spec/spec_helper.rb
CHANGED
@@ -60,12 +60,20 @@ module ExitCodeMatchers
|
|
60
60
|
end
|
61
61
|
|
62
62
|
RSpec.configure do |config|
|
63
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
64
|
+
|
65
|
+
# These two settings work together to allow you to limit a spec run
|
66
|
+
# to individual examples or groups you care about by tagging them with
|
67
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
68
|
+
# get run.
|
69
|
+
config.filter_run :focus
|
70
|
+
config.run_all_when_everything_filtered = true
|
71
|
+
|
63
72
|
broken_filter = lambda do |v|
|
64
73
|
v.is_a?(Symbol) ? RUBY_ENGINE == v.to_s : v
|
65
74
|
end
|
66
75
|
config.filter_run_excluding ruby: ->(v) { !RUBY_VERSION.start_with?(v.to_s) }
|
67
76
|
config.filter_run_excluding broken: broken_filter
|
68
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
69
77
|
|
70
78
|
config.expect_with :rspec do |c|
|
71
79
|
c.syntax = :expect # disables `should`
|
@@ -102,9 +110,13 @@ def parse_source(source, file = nil)
|
|
102
110
|
end
|
103
111
|
end
|
104
112
|
|
105
|
-
def
|
113
|
+
def autocorrect_source_file(cop, source)
|
114
|
+
Tempfile.open('tmp') { |f| autocorrect_source(cop, source, f) }
|
115
|
+
end
|
116
|
+
|
117
|
+
def autocorrect_source(cop, source, file = nil)
|
106
118
|
cop.instance_variable_get(:@options)[:auto_correct] = true
|
107
|
-
processed_source = parse_source(source)
|
119
|
+
processed_source = parse_source(source, file)
|
108
120
|
_investigate(cop, processed_source)
|
109
121
|
|
110
122
|
corrector =
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.19.
|
4
|
+
version: 0.19.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rainbow
|
@@ -410,7 +410,8 @@ files:
|
|
410
410
|
- lib/rubocop/target_finder.rb
|
411
411
|
- lib/rubocop/token.rb
|
412
412
|
- lib/rubocop/version.rb
|
413
|
-
- relnotes/
|
413
|
+
- relnotes/v0.19.0.md
|
414
|
+
- relnotes/v0.19.1.md
|
414
415
|
- rubocop-todo.yml
|
415
416
|
- rubocop.gemspec
|
416
417
|
- spec/.rubocop.yml
|