rubocop 0.9.1 → 0.10.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.
- data/.travis.yml +3 -1
- data/CHANGELOG.md +38 -0
- data/README.md +34 -0
- data/Rakefile +3 -0
- data/config/default.yml +14 -1
- data/config/enabled.yml +30 -7
- data/lib/rubocop.rb +15 -0
- data/lib/rubocop/cli.rb +48 -154
- data/lib/rubocop/config.rb +19 -22
- data/lib/rubocop/config_store.rb +2 -4
- data/lib/rubocop/cop/commissioner.rb +90 -0
- data/lib/rubocop/cop/cop.rb +38 -31
- data/lib/rubocop/cop/corrector.rb +84 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +0 -3
- data/lib/rubocop/cop/lint/block_alignment.rb +151 -0
- data/lib/rubocop/cop/lint/empty_ensure.rb +18 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +0 -124
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +3 -3
- data/lib/rubocop/cop/lint/eval.rb +0 -2
- data/lib/rubocop/cop/lint/handle_exceptions.rb +0 -2
- data/lib/rubocop/cop/lint/literal_in_condition.rb +0 -10
- data/lib/rubocop/cop/lint/loop.rb +0 -2
- data/lib/rubocop/cop/lint/rescue_exception.rb +0 -2
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +0 -2
- data/lib/rubocop/cop/lint/unused_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +0 -2
- data/lib/rubocop/cop/offence.rb +9 -0
- data/lib/rubocop/cop/rails/validation.rb +2 -1
- data/lib/rubocop/cop/style/access_control.rb +4 -3
- data/lib/rubocop/cop/style/alias.rb +2 -4
- data/lib/rubocop/cop/style/align_parameters.rb +0 -2
- data/lib/rubocop/cop/style/and_or.rb +4 -6
- data/lib/rubocop/cop/style/ascii_comments.rb +2 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -2
- data/lib/rubocop/cop/style/attr.rb +0 -2
- data/lib/rubocop/cop/style/avoid_class_vars.rb +0 -1
- data/lib/rubocop/cop/style/avoid_for.rb +0 -2
- data/lib/rubocop/cop/style/avoid_global_vars.rb +3 -7
- data/lib/rubocop/cop/style/avoid_perl_backrefs.rb +0 -2
- data/lib/rubocop/cop/style/avoid_perlisms.rb +2 -4
- data/lib/rubocop/cop/style/begin_block.rb +0 -2
- data/lib/rubocop/cop/style/block_comments.rb +2 -2
- data/lib/rubocop/cop/style/block_nesting.rb +3 -3
- data/lib/rubocop/cop/style/blocks.rb +0 -2
- data/lib/rubocop/cop/style/case_equality.rb +0 -2
- data/lib/rubocop/cop/style/case_indentation.rb +0 -2
- data/lib/rubocop/cop/style/character_literal.rb +10 -6
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +0 -4
- data/lib/rubocop/cop/style/class_methods.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +3 -5
- data/lib/rubocop/cop/style/colon_method_call.rb +3 -3
- data/lib/rubocop/cop/style/comment_annotation.rb +44 -0
- data/lib/rubocop/cop/style/constant_name.rb +0 -2
- data/lib/rubocop/cop/style/def_parentheses.rb +0 -8
- data/lib/rubocop/cop/style/documentation.rb +6 -2
- data/lib/rubocop/cop/style/dot_position.rb +0 -2
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +0 -2
- data/lib/rubocop/cop/style/empty_lines.rb +10 -8
- data/lib/rubocop/cop/style/empty_literal.rb +3 -1
- data/lib/rubocop/cop/style/encoding.rb +7 -6
- data/lib/rubocop/cop/style/end_block.rb +0 -2
- data/lib/rubocop/cop/style/end_of_line.rb +4 -3
- data/lib/rubocop/cop/style/favor_join.rb +0 -2
- data/lib/rubocop/cop/style/favor_modifier.rb +9 -9
- data/lib/rubocop/cop/style/favor_sprintf.rb +0 -2
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +0 -2
- data/lib/rubocop/cop/style/if_then_else.rb +0 -2
- data/lib/rubocop/cop/style/lambda.rb +0 -2
- data/lib/rubocop/cop/style/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/style/line_continuation.rb +4 -3
- data/lib/rubocop/cop/style/line_length.rb +4 -3
- data/lib/rubocop/cop/style/method_and_variable_snake_case.rb +4 -3
- data/lib/rubocop/cop/style/method_call_parentheses.rb +0 -2
- data/lib/rubocop/cop/style/method_length.rb +0 -4
- data/lib/rubocop/cop/style/not.rb +0 -2
- data/lib/rubocop/cop/style/op_method.rb +0 -2
- data/lib/rubocop/cop/style/parameter_lists.rb +0 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -6
- data/lib/rubocop/cop/style/proc.rb +0 -2
- data/lib/rubocop/cop/style/reduce_arguments.rb +0 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +45 -0
- data/lib/rubocop/cop/style/redundant_return.rb +59 -0
- data/lib/rubocop/cop/style/redundant_self.rb +83 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +0 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +13 -21
- data/lib/rubocop/cop/style/semicolon.rb +15 -9
- data/lib/rubocop/cop/style/single_line_methods.rb +0 -4
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +2 -2
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +0 -1
- data/lib/rubocop/cop/style/string_literals.rb +5 -2
- data/lib/rubocop/cop/style/surrounding_space.rb +106 -91
- data/lib/rubocop/cop/style/tab.rb +4 -3
- data/lib/rubocop/cop/style/ternary_operator.rb +0 -4
- data/lib/rubocop/cop/style/trailing_whitespace.rb +4 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +51 -6
- data/lib/rubocop/cop/style/unless_else.rb +0 -2
- data/lib/rubocop/cop/style/variable_interpolation.rb +0 -2
- data/lib/rubocop/cop/style/when_then.rb +3 -3
- data/lib/rubocop/cop/style/while_until_do.rb +3 -5
- data/lib/rubocop/cop/style/word_array.rb +0 -2
- data/lib/rubocop/cop/util.rb +0 -4
- data/lib/rubocop/formatter/file_list_formatter.rb +18 -0
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/processed_source.rb +27 -0
- data/lib/rubocop/rake_task.rb +50 -0
- data/lib/rubocop/source_parser.rb +105 -0
- data/lib/rubocop/target_finder.rb +67 -0
- data/lib/rubocop/token.rb +22 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop.gemspec +5 -3
- data/spec/project_spec.rb +0 -11
- data/spec/rubocop/cli_spec.rb +112 -6
- data/spec/rubocop/config_spec.rb +13 -17
- data/spec/rubocop/config_store_spec.rb +8 -23
- data/spec/rubocop/cops/commissioner_spec.rb +72 -0
- data/spec/rubocop/cops/corrector_spec.rb +63 -0
- data/spec/rubocop/cops/lint/assignment_in_condition_spec.rb +2 -2
- data/spec/rubocop/cops/lint/block_alignment_spec.rb +357 -0
- data/spec/rubocop/cops/lint/empty_ensure_spec.rb +33 -0
- data/spec/rubocop/cops/lint/end_alignment_spec.rb +0 -263
- data/spec/rubocop/cops/lint/ensure_return_spec.rb +6 -9
- data/spec/rubocop/cops/offence_spec.rb +28 -0
- data/spec/rubocop/cops/style/and_or_spec.rb +21 -11
- data/spec/rubocop/cops/style/ascii_identifiers_spec.rb +14 -0
- data/spec/rubocop/cops/style/avoid_global_vars_spec.rb +10 -14
- data/spec/rubocop/cops/style/character_literal_spec.rb +17 -2
- data/spec/rubocop/cops/style/colon_method_call_spec.rb +20 -15
- data/spec/rubocop/cops/style/comment_annotation_spec.rb +62 -0
- data/spec/rubocop/cops/style/encoding_spec.rb +7 -0
- data/spec/rubocop/cops/style/parentheses_around_condition_spec.rb +37 -9
- data/spec/rubocop/cops/style/redundant_begin_spec.rb +63 -0
- data/spec/rubocop/cops/style/redundant_return_spec.rb +64 -0
- data/spec/rubocop/cops/style/redundant_self_spec.rb +76 -0
- data/spec/rubocop/cops/style/string_literals_spec.rb +18 -13
- data/spec/rubocop/cops/style/trivial_accessors_spec.rb +110 -52
- data/spec/rubocop/cops/style/when_then_spec.rb +14 -7
- data/spec/rubocop/cops/style/while_until_do_spec.rb +12 -0
- data/spec/rubocop/cops/variable_inspector_spec.rb +3 -5
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
- data/spec/rubocop/processed_source_spec.rb +67 -0
- data/spec/rubocop/source_parser_spec.rb +141 -0
- data/spec/rubocop/target_finder_spec.rb +180 -0
- data/spec/rubocop/token_spec.rb +27 -0
- data/spec/spec_helper.rb +24 -4
- metadata +108 -18
- checksums.yaml +0 -7
@@ -0,0 +1,63 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module Style
|
8
|
+
describe RedundantBegin do
|
9
|
+
let(:cop) { RedundantBegin.new }
|
10
|
+
|
11
|
+
it 'reports an offence for def with redundant begin block' do
|
12
|
+
src = ['def func',
|
13
|
+
' begin',
|
14
|
+
' ala',
|
15
|
+
' rescue => e',
|
16
|
+
' bala',
|
17
|
+
' end',
|
18
|
+
'end']
|
19
|
+
inspect_source(cop, src)
|
20
|
+
expect(cop.offences).to have(1).item
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'reports an offence for defs with redundant begin block' do
|
24
|
+
src = ['def Test.func',
|
25
|
+
' begin',
|
26
|
+
' ala',
|
27
|
+
' rescue => e',
|
28
|
+
' bala',
|
29
|
+
' end',
|
30
|
+
'end']
|
31
|
+
inspect_source(cop, src)
|
32
|
+
expect(cop.offences).to have(1).item
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'accepts a def with required begin block' do
|
36
|
+
src = ['def func',
|
37
|
+
' begin',
|
38
|
+
' ala',
|
39
|
+
' rescue => e',
|
40
|
+
' bala',
|
41
|
+
' end',
|
42
|
+
' something',
|
43
|
+
'end']
|
44
|
+
inspect_source(cop, src)
|
45
|
+
expect(cop.offences).to be_empty
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'accepts a defs with required begin block' do
|
49
|
+
src = ['def Test.func',
|
50
|
+
' begin',
|
51
|
+
' ala',
|
52
|
+
' rescue => e',
|
53
|
+
' bala',
|
54
|
+
' end',
|
55
|
+
' something',
|
56
|
+
'end']
|
57
|
+
inspect_source(cop, src)
|
58
|
+
expect(cop.offences).to be_empty
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module Style
|
8
|
+
describe RedundantReturn do
|
9
|
+
let(:cop) { RedundantReturn.new }
|
10
|
+
|
11
|
+
it 'reports an offence for def with only a return' do
|
12
|
+
src = ['def func',
|
13
|
+
' return something',
|
14
|
+
'end']
|
15
|
+
inspect_source(cop, src)
|
16
|
+
expect(cop.offences).to have(1).item
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'reports an offence for defs with only a return' do
|
20
|
+
src = ['def Test.func',
|
21
|
+
' return something',
|
22
|
+
'end']
|
23
|
+
inspect_source(cop, src)
|
24
|
+
expect(cop.offences).to have(1).item
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'reports an offence for def ending with return' do
|
28
|
+
src = ['def func',
|
29
|
+
' one',
|
30
|
+
' two',
|
31
|
+
' return something',
|
32
|
+
'end']
|
33
|
+
inspect_source(cop, src)
|
34
|
+
expect(cop.offences).to have(1).item
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'reports an offence for defs ending with return' do
|
38
|
+
src = ['def func',
|
39
|
+
' one',
|
40
|
+
' two',
|
41
|
+
' return something',
|
42
|
+
'end']
|
43
|
+
inspect_source(cop, src)
|
44
|
+
expect(cop.offences).to have(1).item
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'accepts return in a non-final position' do
|
48
|
+
src = ['def func',
|
49
|
+
' return something if something_else',
|
50
|
+
'end']
|
51
|
+
inspect_source(cop, src)
|
52
|
+
expect(cop.offences).to be_empty
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'does not blow up on empty method body' do
|
56
|
+
src = ['def func',
|
57
|
+
'end']
|
58
|
+
inspect_source(cop, src)
|
59
|
+
expect(cop.offences).to be_empty
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Rubocop
|
6
|
+
module Cop
|
7
|
+
module Style
|
8
|
+
describe RedundantSelf do
|
9
|
+
let(:cop) { RedundantSelf.new }
|
10
|
+
|
11
|
+
it 'reports an offence a self receiver on an rvalue' do
|
12
|
+
src = ['a = self.b']
|
13
|
+
inspect_source(cop, src)
|
14
|
+
expect(cop.offences).to have(1).item
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accepts a self receiver on an lvalue of an assignment' do
|
18
|
+
src = ['self.a = b']
|
19
|
+
inspect_source(cop, src)
|
20
|
+
expect(cop.offences).to be_empty
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'accepts a self receiver on an lvalue of an or-assignment' do
|
24
|
+
src = ['self.logger ||= Rails.logger']
|
25
|
+
inspect_source(cop, src)
|
26
|
+
expect(cop.offences).to be_empty
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'accepts a self receiver on an lvalue of an and-assignment' do
|
30
|
+
src = ['self.flag &&= value']
|
31
|
+
inspect_source(cop, src)
|
32
|
+
expect(cop.offences).to be_empty
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'accepts a self receiver on an lvalue of a plus-assignment' do
|
36
|
+
src = ['self.sum += 10']
|
37
|
+
inspect_source(cop, src)
|
38
|
+
expect(cop.offences).to be_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'accepts a self receiver with the square bracket operator' do
|
42
|
+
src = ['self[a]']
|
43
|
+
inspect_source(cop, src)
|
44
|
+
expect(cop.offences).to be_empty
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'accepts a self receiver with the double less-than operator' do
|
48
|
+
src = ['self << a']
|
49
|
+
inspect_source(cop, src)
|
50
|
+
expect(cop.offences).to be_empty
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'accepts a self receiver for methods named like ruby keywords' do
|
54
|
+
src = ['a = self.class',
|
55
|
+
'self.for(deps, [], true)'
|
56
|
+
]
|
57
|
+
inspect_source(cop, src)
|
58
|
+
expect(cop.offences).to be_empty
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'accepts a self receiver used to distinguish from local variable' do
|
62
|
+
src = ['def requested_specs',
|
63
|
+
' @requested_specs ||= begin',
|
64
|
+
' groups = self.groups - Bundler.settings.without',
|
65
|
+
' groups.map! { |g| g.to_sym }',
|
66
|
+
' specs_for(groups)',
|
67
|
+
' end',
|
68
|
+
'end',
|
69
|
+
]
|
70
|
+
inspect_source(cop, src)
|
71
|
+
expect(cop.offences).to be_empty
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -6,14 +6,14 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe StringLiterals do
|
9
|
-
let(:
|
9
|
+
let(:cop) { StringLiterals.new }
|
10
10
|
|
11
11
|
it 'registers offence for double quotes when single quotes suffice' do
|
12
|
-
inspect_source(
|
12
|
+
inspect_source(cop, ['s = "abc"',
|
13
13
|
'x = "a\\\\b"',
|
14
14
|
'y ="\\\\b"',
|
15
15
|
'z = "a\\\\"'])
|
16
|
-
expect(
|
16
|
+
expect(cop.offences.size).to eq(4)
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'accepts double quotes when they are needed' do
|
@@ -25,13 +25,13 @@ module Rubocop
|
|
25
25
|
'e = "#$test"',
|
26
26
|
'f = "\e"',
|
27
27
|
'g = "#@@test"']
|
28
|
-
inspect_source(
|
29
|
-
expect(
|
28
|
+
inspect_source(cop, src)
|
29
|
+
expect(cop.offences).to be_empty
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'accepts double quotes at the start of regexp literals' do
|
33
|
-
inspect_source(
|
34
|
-
expect(
|
33
|
+
inspect_source(cop, ['s = /"((?:[^\\"]|\\.)*)"/'])
|
34
|
+
expect(cop.offences).to be_empty
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'accepts double quotes with some other special symbols' do
|
@@ -39,22 +39,27 @@ module Rubocop
|
|
39
39
|
# http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html
|
40
40
|
src = ['g = "\xf9"',
|
41
41
|
'copyright = "\u00A9"']
|
42
|
-
inspect_source(
|
43
|
-
expect(
|
42
|
+
inspect_source(cop, src)
|
43
|
+
expect(cop.offences).to be_empty
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'can handle double quotes within embedded expression' do
|
47
47
|
src = ['"#{"A"}"']
|
48
|
-
inspect_source(
|
49
|
-
expect(
|
48
|
+
inspect_source(cop, src)
|
49
|
+
expect(cop.offences).to be_empty
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'can handle a built-in constant parsed as string' do
|
53
53
|
# Parser will produce str nodes for constants such as __FILE__.
|
54
54
|
src = ['if __FILE__ == $PROGRAM_NAME',
|
55
55
|
'end']
|
56
|
-
inspect_source(
|
57
|
-
expect(
|
56
|
+
inspect_source(cop, src)
|
57
|
+
expect(cop.offences).to be_empty
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'auto-corrects " with \'' do
|
61
|
+
new_source = autocorrect_source(cop, 's = "abc"')
|
62
|
+
expect(new_source).to eq("s = 'abc'")
|
58
63
|
end
|
59
64
|
end
|
60
65
|
end
|
@@ -6,14 +6,11 @@ module Rubocop
|
|
6
6
|
module Cop
|
7
7
|
module Style
|
8
8
|
describe TrivialAccessors do
|
9
|
-
|
10
|
-
|
11
|
-
before :each do
|
12
|
-
trivial_accessors_finder.offences.clear
|
13
|
-
end
|
9
|
+
TrivialAccessors.config = { 'ExactNameMatch' => false }
|
10
|
+
let(:cop) { TrivialAccessors.new }
|
14
11
|
|
15
12
|
it 'finds trivial reader' do
|
16
|
-
inspect_source(
|
13
|
+
inspect_source(cop,
|
17
14
|
['def foo',
|
18
15
|
' @foo',
|
19
16
|
'end',
|
@@ -21,13 +18,13 @@ module Rubocop
|
|
21
18
|
'def Foo',
|
22
19
|
' @Foo',
|
23
20
|
'end'])
|
24
|
-
expect(
|
25
|
-
expect(
|
21
|
+
expect(cop.offences.size).to eq(2)
|
22
|
+
expect(cop.offences
|
26
23
|
.map(&:line).sort).to eq([1, 5])
|
27
24
|
end
|
28
25
|
|
29
26
|
it 'finds trivial reader in a class' do
|
30
|
-
inspect_source(
|
27
|
+
inspect_source(cop,
|
31
28
|
['class TrivialFoo',
|
32
29
|
' def foo',
|
33
30
|
' @foo',
|
@@ -36,13 +33,28 @@ module Rubocop
|
|
36
33
|
' !foo',
|
37
34
|
' end',
|
38
35
|
'end'])
|
39
|
-
expect(
|
40
|
-
expect(
|
36
|
+
expect(cop.offences.size).to eq(1)
|
37
|
+
expect(cop.offences
|
38
|
+
.map(&:line).sort).to eq([2])
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'finds trivial reader in a class method' do
|
42
|
+
inspect_source(cop,
|
43
|
+
['class TrivialFoo',
|
44
|
+
' def self.foo',
|
45
|
+
' @foo',
|
46
|
+
' end',
|
47
|
+
' def bar',
|
48
|
+
' !foo',
|
49
|
+
' end',
|
50
|
+
'end'])
|
51
|
+
expect(cop.offences.size).to eq(1)
|
52
|
+
expect(cop.offences
|
41
53
|
.map(&:line).sort).to eq([2])
|
42
54
|
end
|
43
55
|
|
44
56
|
it 'finds trivial reader in a nested class' do
|
45
|
-
inspect_source(
|
57
|
+
inspect_source(cop,
|
46
58
|
['class TrivialFoo',
|
47
59
|
' class Nested',
|
48
60
|
' def foo',
|
@@ -50,13 +62,13 @@ module Rubocop
|
|
50
62
|
' end',
|
51
63
|
' end',
|
52
64
|
'end'])
|
53
|
-
expect(
|
54
|
-
expect(
|
65
|
+
expect(cop.offences.size).to eq(1)
|
66
|
+
expect(cop.offences
|
55
67
|
.map(&:line).sort).to eq([3])
|
56
68
|
end
|
57
69
|
|
58
70
|
it 'finds trivial readers in a little less trivial class' do
|
59
|
-
inspect_source(
|
71
|
+
inspect_source(cop,
|
60
72
|
['class TrivialFoo',
|
61
73
|
' def foo',
|
62
74
|
' @foo',
|
@@ -110,39 +122,39 @@ module Rubocop
|
|
110
122
|
' @val',
|
111
123
|
' end',
|
112
124
|
'end'])
|
113
|
-
expect(
|
114
|
-
expect(
|
125
|
+
expect(cop.offences.size).to eq(2)
|
126
|
+
expect(cop.offences
|
115
127
|
.map(&:line).sort).to eq([2, 8])
|
116
128
|
end
|
117
129
|
|
118
130
|
it 'finds trivial reader with braces' do
|
119
|
-
inspect_source(
|
131
|
+
inspect_source(cop,
|
120
132
|
['class Test',
|
121
133
|
' # trivial reader with braces',
|
122
134
|
' def name()',
|
123
135
|
' @name',
|
124
136
|
' end',
|
125
137
|
'end'])
|
126
|
-
expect(
|
127
|
-
expect(
|
138
|
+
expect(cop.offences.size).to eq(1)
|
139
|
+
expect(cop.offences
|
128
140
|
.map(&:line).sort).to eq([3])
|
129
141
|
end
|
130
142
|
|
131
143
|
it 'finds trivial writer without braces' do
|
132
|
-
inspect_source(
|
144
|
+
inspect_source(cop,
|
133
145
|
['class Test',
|
134
146
|
' # trivial writer without braces',
|
135
147
|
' def name= name',
|
136
148
|
' @name = name',
|
137
149
|
' end',
|
138
150
|
'end'])
|
139
|
-
expect(
|
140
|
-
expect(
|
151
|
+
expect(cop.offences.size).to eq(1)
|
152
|
+
expect(cop.offences
|
141
153
|
.map(&:line).sort).to eq([3])
|
142
154
|
end
|
143
155
|
|
144
156
|
it 'does not find trivial writer with function calls' do
|
145
|
-
inspect_source(
|
157
|
+
inspect_source(cop,
|
146
158
|
['class TrivialTest',
|
147
159
|
' def test=(val)',
|
148
160
|
' @test = val',
|
@@ -150,11 +162,11 @@ module Rubocop
|
|
150
162
|
' or_more_of_them',
|
151
163
|
' end',
|
152
164
|
'end'])
|
153
|
-
expect(
|
165
|
+
expect(cop.offences).to be_empty
|
154
166
|
end
|
155
167
|
|
156
168
|
it 'finds trivials with less peculiar methods' do
|
157
|
-
inspect_source(
|
169
|
+
inspect_source(cop,
|
158
170
|
['class NilStats',
|
159
171
|
'def most_traded_pair',
|
160
172
|
'end',
|
@@ -189,40 +201,40 @@ module Rubocop
|
|
189
201
|
' #do stuff',
|
190
202
|
'end',
|
191
203
|
'end'])
|
192
|
-
expect(
|
204
|
+
expect(cop.offences).to be_empty
|
193
205
|
end
|
194
206
|
|
195
207
|
it 'finds oneliner trivials' do
|
196
|
-
inspect_source(
|
208
|
+
inspect_source(cop,
|
197
209
|
['class Oneliner',
|
198
210
|
' def foo; @foo; end',
|
199
211
|
' def foo= foo; @foo = foo; end',
|
200
212
|
'end'])
|
201
|
-
expect(
|
202
|
-
expect(
|
213
|
+
expect(cop.offences.size).to eq(2)
|
214
|
+
expect(cop.offences
|
203
215
|
.map(&:line).sort).to eq([2, 3])
|
204
216
|
end
|
205
217
|
|
206
218
|
it 'does not find a trivial reader' do
|
207
|
-
inspect_source(
|
219
|
+
inspect_source(cop,
|
208
220
|
['def bar',
|
209
221
|
' @bar + foo',
|
210
222
|
'end'])
|
211
|
-
expect(
|
223
|
+
expect(cop.offences).to be_empty
|
212
224
|
end
|
213
225
|
|
214
226
|
it 'finds trivial writer' do
|
215
|
-
inspect_source(
|
227
|
+
inspect_source(cop,
|
216
228
|
['def foo=(val)',
|
217
229
|
' @foo = val',
|
218
230
|
'end'])
|
219
|
-
expect(
|
220
|
-
expect(
|
231
|
+
expect(cop.offences.size).to eq(1)
|
232
|
+
expect(cop.offences
|
221
233
|
.map(&:line).sort).to eq([1])
|
222
234
|
end
|
223
235
|
|
224
236
|
it 'finds trivial writer in a class' do
|
225
|
-
inspect_source(
|
237
|
+
inspect_source(cop,
|
226
238
|
['class TrivialFoo',
|
227
239
|
' def foo=(val)',
|
228
240
|
' @foo = val',
|
@@ -242,13 +254,13 @@ module Rubocop
|
|
242
254
|
' end',
|
243
255
|
' end',
|
244
256
|
'end'])
|
245
|
-
expect(
|
246
|
-
expect(
|
257
|
+
expect(cop.offences.size).to eq(1)
|
258
|
+
expect(cop.offences
|
247
259
|
.map(&:line).sort).to eq([2])
|
248
260
|
end
|
249
261
|
|
250
262
|
it 'finds trivial accessors in a little less trivial class' do
|
251
|
-
inspect_source(
|
263
|
+
inspect_source(cop,
|
252
264
|
['class TrivialFoo',
|
253
265
|
' def foo',
|
254
266
|
' @foo',
|
@@ -263,21 +275,21 @@ module Rubocop
|
|
263
275
|
' @bar = bar_value',
|
264
276
|
' end',
|
265
277
|
'end'])
|
266
|
-
expect(
|
267
|
-
expect(
|
278
|
+
expect(cop.offences.size).to eq(3)
|
279
|
+
expect(cop.offences
|
268
280
|
.map(&:line).sort).to eq([2, 8, 11])
|
269
281
|
end
|
270
282
|
|
271
283
|
it 'does not find a trivial writer' do
|
272
|
-
inspect_source(
|
284
|
+
inspect_source(cop,
|
273
285
|
['def bar=(value)',
|
274
286
|
' @bar = value + 42',
|
275
287
|
'end'])
|
276
|
-
expect(
|
288
|
+
expect(cop.offences).to be_empty
|
277
289
|
end
|
278
290
|
|
279
291
|
it 'finds trivial writers in a little less trivial class' do
|
280
|
-
inspect_source(
|
292
|
+
inspect_source(cop,
|
281
293
|
['class TrivialFoo',
|
282
294
|
' def foo_bar=(foo, bar)',
|
283
295
|
' @foo_bar = foo + bar',
|
@@ -289,13 +301,13 @@ module Rubocop
|
|
289
301
|
' @bar = bar_value',
|
290
302
|
' end',
|
291
303
|
'end'])
|
292
|
-
expect(
|
293
|
-
expect(
|
304
|
+
expect(cop.offences.size).to eq(2)
|
305
|
+
expect(cop.offences
|
294
306
|
.map(&:line).sort).to eq([5, 8])
|
295
307
|
end
|
296
308
|
|
297
309
|
it 'does not find trivial accessors with method calls' do
|
298
|
-
inspect_source(
|
310
|
+
inspect_source(cop,
|
299
311
|
['class TrivialFoo',
|
300
312
|
' def foo_bar(foo)',
|
301
313
|
' foo_bar = foo + 42',
|
@@ -309,11 +321,11 @@ module Rubocop
|
|
309
321
|
' foo_method',
|
310
322
|
' end',
|
311
323
|
'end'])
|
312
|
-
expect(
|
324
|
+
expect(cop.offences).to be_empty
|
313
325
|
end
|
314
326
|
|
315
327
|
it 'does not find trivial writer with exceptions' do
|
316
|
-
inspect_source(
|
328
|
+
inspect_source(cop,
|
317
329
|
[' def expiration_formatted=(value)',
|
318
330
|
' begin',
|
319
331
|
' @expiration = foo_stuff',
|
@@ -322,15 +334,61 @@ module Rubocop
|
|
322
334
|
' end',
|
323
335
|
' self[:expiration] = @expiration',
|
324
336
|
' end'])
|
325
|
-
expect(
|
337
|
+
expect(cop.offences).to be_empty
|
326
338
|
end
|
327
339
|
|
328
340
|
it 'accepts an initialize method looking like a writer' do
|
329
|
-
inspect_source(
|
341
|
+
inspect_source(cop,
|
330
342
|
[' def initialize(value)',
|
331
343
|
' @top = value',
|
332
344
|
' end'])
|
333
|
-
expect(
|
345
|
+
expect(cop.offences).to be_empty
|
346
|
+
end
|
347
|
+
|
348
|
+
context 'exact name match required' do
|
349
|
+
before { TrivialAccessors.config['ExactNameMatch'] = true }
|
350
|
+
|
351
|
+
it 'finds only 1 trivial reader' do
|
352
|
+
inspect_source(cop,
|
353
|
+
['def foo',
|
354
|
+
' @foo',
|
355
|
+
'end',
|
356
|
+
'',
|
357
|
+
'def bar',
|
358
|
+
' @barr',
|
359
|
+
'end'])
|
360
|
+
expect(cop.offences.size).to eq(1)
|
361
|
+
expect(cop.offences
|
362
|
+
.map(&:line).sort).to eq([1])
|
363
|
+
end
|
364
|
+
|
365
|
+
it 'finds only 1 trivial writer' do
|
366
|
+
inspect_source(cop,
|
367
|
+
['def foo=(foo)',
|
368
|
+
' @foo = foo',
|
369
|
+
'end',
|
370
|
+
'',
|
371
|
+
'def bar=(bar)',
|
372
|
+
' @barr = bar',
|
373
|
+
'end'])
|
374
|
+
expect(cop.offences.size).to eq(1)
|
375
|
+
expect(cop.offences
|
376
|
+
.map(&:line).sort).to eq([1])
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
context 'with predicates allowed' do
|
381
|
+
before do
|
382
|
+
described_class.config['AllowPredicates'] = true
|
383
|
+
end
|
384
|
+
|
385
|
+
it 'ignores accessors ending with a question mark' do
|
386
|
+
inspect_source(cop,
|
387
|
+
[' def foo?',
|
388
|
+
' @foo',
|
389
|
+
' end'])
|
390
|
+
expect(cop.offences).to be_empty
|
391
|
+
end
|
334
392
|
end
|
335
393
|
end
|
336
394
|
end
|