rubocop 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +3 -1
- data/CHANGELOG.md +44 -0
- data/CONTRIBUTING.md +40 -8
- data/Gemfile +6 -0
- data/README.md +65 -20
- data/Rakefile +0 -1
- data/config/default.yml +15 -3
- data/config/enabled.yml +143 -109
- data/lib/rubocop.rb +45 -26
- data/lib/rubocop/cli.rb +26 -27
- data/lib/rubocop/config.rb +0 -1
- data/lib/rubocop/config_loader.rb +16 -23
- data/lib/rubocop/cop/commissioner.rb +2 -7
- data/lib/rubocop/cop/cop.rb +24 -51
- data/lib/rubocop/cop/ignored_node.rb +31 -0
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +50 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +36 -0
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +3 -11
- data/lib/rubocop/cop/lint/block_alignment.rb +6 -20
- data/lib/rubocop/cop/lint/condition_position.rb +52 -0
- data/lib/rubocop/cop/lint/else_layout.rb +57 -0
- data/lib/rubocop/cop/lint/end_alignment.rb +33 -8
- data/lib/rubocop/cop/lint/invalid_character_literal.rb +37 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +2 -4
- data/lib/rubocop/cop/lint/syntax.rb +6 -12
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +25 -0
- data/lib/rubocop/cop/mixin/array_syntax.rb +20 -0
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +76 -0
- data/lib/rubocop/cop/mixin/check_assignment.rb +26 -0
- data/lib/rubocop/cop/{check_methods.rb → mixin/check_methods.rb} +0 -0
- data/lib/rubocop/cop/mixin/code_length.rb +33 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +53 -0
- data/lib/rubocop/cop/mixin/configurable_max.rb +19 -0
- data/lib/rubocop/cop/mixin/configurable_naming.rb +45 -0
- data/lib/rubocop/cop/{style → mixin}/if_node.rb +0 -0
- data/lib/rubocop/cop/mixin/if_then_else.rb +23 -0
- data/lib/rubocop/cop/mixin/negative_conditional.rb +24 -0
- data/lib/rubocop/cop/mixin/parser_diagnostic.rb +34 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +19 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +32 -0
- data/lib/rubocop/cop/mixin/space_inside.rb +31 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +59 -0
- data/lib/rubocop/cop/mixin/string_help.rb +32 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +42 -0
- data/lib/rubocop/cop/rails/default_scope.rb +3 -1
- data/lib/rubocop/cop/style/accessor_method_name.rb +4 -12
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +16 -1
- data/lib/rubocop/cop/style/case_indentation.rb +33 -16
- data/lib/rubocop/cop/style/character_literal.rb +10 -0
- data/lib/rubocop/cop/style/dot_position.rb +23 -6
- data/lib/rubocop/cop/style/empty_lines_around_body.rb +5 -5
- data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -32
- data/lib/rubocop/cop/style/favor_until_over_negated_while.rb +20 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +5 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +34 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
- data/lib/rubocop/cop/style/indentation_consistency.rb +51 -0
- data/lib/rubocop/cop/style/indentation_width.rb +0 -26
- data/lib/rubocop/cop/style/lambda_call.rb +12 -5
- data/lib/rubocop/cop/style/method_def_parentheses.rb +29 -11
- data/lib/rubocop/cop/style/multiline_if_then.rb +4 -9
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +22 -0
- data/lib/rubocop/cop/style/{ternary_operator.rb → nested_ternary_operator.rb} +0 -15
- data/lib/rubocop/cop/style/numeric_literals.rb +30 -2
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -1
- data/lib/rubocop/cop/style/parameter_lists.rb +7 -3
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +9 -11
- data/lib/rubocop/cop/style/predicate_name.rb +4 -12
- data/lib/rubocop/cop/style/raise_args.rb +19 -11
- data/lib/rubocop/cop/style/regexp_literal.rb +19 -6
- data/lib/rubocop/cop/style/space_after_colon.rb +36 -0
- data/lib/rubocop/cop/style/space_after_comma.rb +16 -0
- data/lib/rubocop/cop/style/space_after_semicolon.rb +16 -0
- data/lib/rubocop/cop/style/space_around_block_braces.rb +38 -38
- data/lib/rubocop/cop/style/space_around_operators.rb +1 -2
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +6 -2
- data/lib/rubocop/cop/style/string_literals.rb +5 -5
- data/lib/rubocop/cop/style/trailing_comma.rb +94 -0
- data/lib/rubocop/cop/style/unless_else.rb +2 -2
- data/lib/rubocop/cop/style/while_until_modifier.rb +32 -0
- data/lib/rubocop/cop/style/word_array.rb +9 -1
- data/lib/rubocop/cop/util.rb +14 -0
- data/lib/rubocop/cop/variable_inspector.rb +11 -6
- data/lib/rubocop/cop/variable_inspector/scope.rb +4 -3
- data/lib/rubocop/file_inspector.rb +22 -6
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/colorizable.rb +37 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +27 -6
- data/lib/rubocop/formatter/progress_formatter.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +9 -5
- data/lib/rubocop/options.rb +19 -4
- data/lib/rubocop/target_finder.rb +4 -0
- data/lib/rubocop/version.rb +1 -1
- data/rubocop-todo.yml +10 -2
- data/rubocop.gemspec +3 -2
- data/spec/project_spec.rb +12 -7
- data/spec/rubocop/cli_spec.rb +262 -99
- data/spec/rubocop/config_loader_spec.rb +5 -5
- data/spec/rubocop/config_spec.rb +3 -3
- data/spec/rubocop/config_store_spec.rb +12 -11
- data/spec/rubocop/cop/commissioner_spec.rb +21 -5
- data/spec/rubocop/cop/cop_spec.rb +1 -1
- data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +113 -0
- data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +35 -0
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +2 -2
- data/spec/rubocop/cop/lint/condition_position_spec.rb +49 -0
- data/spec/rubocop/cop/lint/else_layout_spec.rb +65 -0
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +41 -1
- data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +33 -0
- data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +3 -3
- data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +12 -12
- data/spec/rubocop/cop/lint/syntax_spec.rb +2 -2
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +72 -54
- data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +48 -0
- data/spec/rubocop/cop/offence_spec.rb +1 -1
- data/spec/rubocop/cop/rails/default_scope_spec.rb +6 -0
- data/spec/rubocop/cop/rails/output_spec.rb +2 -1
- data/spec/rubocop/cop/style/align_hash_spec.rb +9 -9
- data/spec/rubocop/cop/style/align_parameters_spec.rb +1 -1
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +5 -0
- data/spec/rubocop/cop/style/case_indentation_spec.rb +53 -2
- data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +3 -3
- data/spec/rubocop/cop/style/documentation_spec.rb +0 -1
- data/spec/rubocop/cop/style/dot_position_spec.rb +18 -3
- data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +4 -4
- data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +13 -0
- data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +1 -1
- data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +1 -1
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +5 -0
- data/spec/rubocop/cop/style/{favor_modifier_spec.rb → if_unless_modifier_spec.rb} +4 -111
- data/spec/rubocop/cop/style/indentation_consistency_spec.rb +490 -0
- data/spec/rubocop/cop/style/indentation_width_spec.rb +19 -91
- data/spec/rubocop/cop/style/lambda_call_spec.rb +18 -0
- data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +2 -2
- data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +35 -1
- data/spec/rubocop/cop/style/method_length_spec.rb +1 -0
- data/spec/rubocop/cop/style/method_name_spec.rb +27 -5
- data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +4 -4
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +2 -2
- data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +18 -0
- data/spec/rubocop/cop/style/{ternary_operator_spec.rb → nested_ternary_operator_spec.rb} +0 -15
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +18 -1
- data/spec/rubocop/cop/style/one_line_conditional_spec.rb +2 -1
- data/spec/rubocop/cop/style/parameter_lists_spec.rb +1 -0
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +13 -4
- data/spec/rubocop/cop/style/raise_args_spec.rb +22 -0
- data/spec/rubocop/cop/style/redundant_self_spec.rb +4 -4
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +4 -0
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +12 -4
- data/spec/rubocop/cop/style/space_after_method_name_spec.rb +2 -2
- data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +30 -1
- data/spec/rubocop/cop/style/{space_around_equals_in_default_parameter_spec.rb → space_around_equals_in_parameter_default_spec.rb} +0 -0
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +2 -1
- data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +20 -3
- data/spec/rubocop/cop/style/string_literals_spec.rb +33 -0
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +200 -0
- data/spec/rubocop/cop/style/variable_name_spec.rb +27 -3
- data/spec/rubocop/cop/style/while_until_modifier_spec.rb +75 -0
- data/spec/rubocop/cop/style/word_array_spec.rb +1 -0
- data/spec/rubocop/cop/team_spec.rb +1 -1
- data/spec/rubocop/cop/variable_inspector/scope_spec.rb +3 -4
- data/spec/rubocop/file_inspector_spec.rb +1 -1
- data/spec/rubocop/formatter/base_formatter_spec.rb +12 -11
- data/spec/rubocop/formatter/colorizable_spec.rb +107 -0
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -0
- data/spec/rubocop/formatter/formatter_set_spec.rb +1 -1
- data/spec/rubocop/formatter/json_formatter_spec.rb +4 -3
- data/spec/rubocop/formatter/progress_formatter_spec.rb +2 -2
- data/spec/rubocop/options_spec.rb +3 -1
- data/spec/rubocop/target_finder_spec.rb +13 -11
- data/spec/spec_helper.rb +5 -1
- data/spec/support/shared_examples.rb +2 -2
- data/spec/support/statement_modifier_helper.rb +41 -0
- metadata +88 -30
- data/lib/rubocop/cop/check_assignment.rb +0 -43
- data/lib/rubocop/cop/style/array_syntax.rb +0 -22
- data/lib/rubocop/cop/style/autocorrect_alignment.rb +0 -78
- data/lib/rubocop/cop/style/code_length.rb +0 -35
- data/lib/rubocop/cop/style/configurable_enforced_style.rb +0 -51
- data/lib/rubocop/cop/style/configurable_max.rb +0 -17
- data/lib/rubocop/cop/style/configurable_naming.rb +0 -41
- data/lib/rubocop/cop/style/favor_modifier.rb +0 -118
- data/lib/rubocop/cop/style/if_then_else.rb +0 -27
- data/lib/rubocop/cop/style/space_after_comma_etc.rb +0 -73
- data/lib/rubocop/cop/style/space_inside.rb +0 -33
- data/lib/rubocop/cop/style/string_help.rb +0 -30
- data/lib/rubocop/cop/style/surrounding_space.rb +0 -44
@@ -86,12 +86,12 @@ describe Rubocop::ConfigLoader do
|
|
86
86
|
create_file('.rubocop.yml',
|
87
87
|
['AllCops:',
|
88
88
|
' Excludes:',
|
89
|
-
' - vendor/**'
|
89
|
+
' - vendor/**'
|
90
90
|
])
|
91
91
|
|
92
92
|
create_file(file_path,
|
93
93
|
['AllCops:',
|
94
|
-
' Excludes: []'
|
94
|
+
' Excludes: []'
|
95
95
|
])
|
96
96
|
end
|
97
97
|
|
@@ -109,7 +109,7 @@ describe Rubocop::ConfigLoader do
|
|
109
109
|
['AllCops:',
|
110
110
|
' Excludes:',
|
111
111
|
' - vendor/**',
|
112
|
-
' - !ruby/regexp /[A-Z]/'
|
112
|
+
' - !ruby/regexp /[A-Z]/'
|
113
113
|
])
|
114
114
|
|
115
115
|
create_file(file_path, ['inherit_from: ../.rubocop.yml'])
|
@@ -142,7 +142,7 @@ describe Rubocop::ConfigLoader do
|
|
142
142
|
create_file('src/.rubocop.yml',
|
143
143
|
['AllCops:',
|
144
144
|
' Excludes:',
|
145
|
-
' - vendor/**'
|
145
|
+
' - vendor/**'
|
146
146
|
])
|
147
147
|
|
148
148
|
create_file(file_path, ['inherit_from: ../src/.rubocop.yml'])
|
@@ -251,7 +251,7 @@ describe Rubocop::ConfigLoader do
|
|
251
251
|
it 'returns a configuration loaded from the passed path' do
|
252
252
|
create_file(configuration_path, [
|
253
253
|
'Encoding:',
|
254
|
-
' Enabled: true'
|
254
|
+
' Enabled: true'
|
255
255
|
])
|
256
256
|
configuration = load_file
|
257
257
|
expect(configuration['Encoding']).to eq(
|
data/spec/rubocop/config_spec.rb
CHANGED
@@ -27,7 +27,7 @@ describe Rubocop::Config do
|
|
27
27
|
create_file(configuration_path, [
|
28
28
|
'LyneLenth:',
|
29
29
|
' Enabled: true',
|
30
|
-
' Max: 100'
|
30
|
+
' Max: 100'
|
31
31
|
])
|
32
32
|
end
|
33
33
|
|
@@ -43,7 +43,7 @@ describe Rubocop::Config do
|
|
43
43
|
create_file(configuration_path, [
|
44
44
|
'LineLength:',
|
45
45
|
' Enabled: true',
|
46
|
-
' Min: 10'
|
46
|
+
' Min: 10'
|
47
47
|
])
|
48
48
|
end
|
49
49
|
|
@@ -64,7 +64,7 @@ describe Rubocop::Config do
|
|
64
64
|
' - lib/file.rb',
|
65
65
|
' Include:',
|
66
66
|
' - lib/file.xyz',
|
67
|
-
' Severity: warning'
|
67
|
+
' Severity: warning'
|
68
68
|
])
|
69
69
|
end
|
70
70
|
|
@@ -6,7 +6,7 @@ describe Rubocop::ConfigStore do
|
|
6
6
|
subject(:config_store) { described_class.new }
|
7
7
|
|
8
8
|
before do
|
9
|
-
Rubocop::ConfigLoader.
|
9
|
+
allow(Rubocop::ConfigLoader).to receive(:configuration_file_for) do |arg|
|
10
10
|
# File tree:
|
11
11
|
# file1
|
12
12
|
# dir/.rubocop.yml
|
@@ -14,11 +14,12 @@ describe Rubocop::ConfigStore do
|
|
14
14
|
# dir/subdir/file3
|
15
15
|
(arg =~ /dir/ ? 'dir' : '.') + '/.rubocop.yml'
|
16
16
|
end
|
17
|
-
Rubocop::ConfigLoader
|
18
|
-
|
19
|
-
Rubocop::ConfigLoader
|
20
|
-
"
|
21
|
-
|
17
|
+
allow(Rubocop::ConfigLoader)
|
18
|
+
.to receive(:configuration_from_file) { |arg| arg }
|
19
|
+
allow(Rubocop::ConfigLoader)
|
20
|
+
.to receive(:load_file) { |arg| "#{arg} loaded" }
|
21
|
+
allow(Rubocop::ConfigLoader)
|
22
|
+
.to receive(:merge_with_default) { |config, file| "merged #{config}" }
|
22
23
|
end
|
23
24
|
|
24
25
|
describe '.for' do
|
@@ -29,12 +30,12 @@ describe Rubocop::ConfigStore do
|
|
29
30
|
|
30
31
|
context 'when no config specified in command line' do
|
31
32
|
it 'gets config path and config from cache if available' do
|
32
|
-
Rubocop::ConfigLoader.
|
33
|
+
expect(Rubocop::ConfigLoader).to receive(:configuration_file_for).once
|
33
34
|
.with('dir')
|
34
|
-
Rubocop::ConfigLoader.
|
35
|
+
expect(Rubocop::ConfigLoader).to receive(:configuration_file_for).once
|
35
36
|
.with('dir/subdir')
|
36
37
|
# The stub returns the same config path for dir and dir/subdir.
|
37
|
-
Rubocop::ConfigLoader.
|
38
|
+
expect(Rubocop::ConfigLoader).to receive(:configuration_from_file).once
|
38
39
|
.with('dir/.rubocop.yml')
|
39
40
|
|
40
41
|
config_store.for('dir/file2')
|
@@ -43,8 +44,8 @@ describe Rubocop::ConfigStore do
|
|
43
44
|
end
|
44
45
|
|
45
46
|
it 'searches for config path if not available in cache' do
|
46
|
-
Rubocop::ConfigLoader.
|
47
|
-
Rubocop::ConfigLoader.
|
47
|
+
expect(Rubocop::ConfigLoader).to receive(:configuration_file_for).once
|
48
|
+
expect(Rubocop::ConfigLoader).to receive(:configuration_from_file).once
|
48
49
|
config_store.for('file1')
|
49
50
|
end
|
50
51
|
end
|
@@ -8,7 +8,7 @@ describe Rubocop::Cop::Commissioner do
|
|
8
8
|
let(:cop) { double(Rubocop::Cop, offences: []).as_null_object }
|
9
9
|
|
10
10
|
it 'returns all offences found by the cops' do
|
11
|
-
cop.
|
11
|
+
allow(cop).to receive(:offences).and_return([1])
|
12
12
|
|
13
13
|
commissioner = described_class.new([cop])
|
14
14
|
source = []
|
@@ -17,8 +17,24 @@ describe Rubocop::Cop::Commissioner do
|
|
17
17
|
expect(commissioner.investigate(processed_source)).to eq [1]
|
18
18
|
end
|
19
19
|
|
20
|
+
context 'when a cop has no interest in the file' do
|
21
|
+
it 'returns all offences except the ones of the cop' do
|
22
|
+
cops = []
|
23
|
+
cops << double('cop A', offences: %w(foo), relevant_file?: true)
|
24
|
+
cops << double('cop B', offences: %w(bar), relevant_file?: false)
|
25
|
+
cops << double('cop C', offences: %w(baz), relevant_file?: true)
|
26
|
+
cops.each(&:as_null_object)
|
27
|
+
|
28
|
+
commissioner = described_class.new(cops)
|
29
|
+
source = []
|
30
|
+
processed_source = parse_source(source)
|
31
|
+
|
32
|
+
expect(commissioner.investigate(processed_source)).to eq %w(foo baz)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
20
36
|
it 'traverses the AST and invoke cops specific callbacks' do
|
21
|
-
cop.
|
37
|
+
expect(cop).to receive(:on_def)
|
22
38
|
|
23
39
|
commissioner = described_class.new([cop])
|
24
40
|
source = ['def method', '1', 'end']
|
@@ -30,7 +46,7 @@ describe Rubocop::Cop::Commissioner do
|
|
30
46
|
it 'passes the input params to all cops that implement their own #investigate method' do
|
31
47
|
source = []
|
32
48
|
processed_source = parse_source(source)
|
33
|
-
cop.
|
49
|
+
expect(cop).to receive(:investigate).with(processed_source)
|
34
50
|
|
35
51
|
commissioner = described_class.new([cop])
|
36
52
|
|
@@ -38,7 +54,7 @@ describe Rubocop::Cop::Commissioner do
|
|
38
54
|
end
|
39
55
|
|
40
56
|
it 'stores all errors raised by the cops' do
|
41
|
-
cop.
|
57
|
+
allow(cop).to receive(:on_def) { fail RuntimeError }
|
42
58
|
|
43
59
|
commissioner = described_class.new([cop])
|
44
60
|
source = ['def method', '1', 'end']
|
@@ -52,7 +68,7 @@ describe Rubocop::Cop::Commissioner do
|
|
52
68
|
|
53
69
|
context 'when passed :raise_error option' do
|
54
70
|
it 're-raises the exception received while processing' do
|
55
|
-
cop.
|
71
|
+
allow(cop).to receive(:on_def) { fail RuntimeError }
|
56
72
|
|
57
73
|
commissioner = described_class.new([cop], raise_error: true)
|
58
74
|
source = ['def method', '1', 'end']
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Lint::AmbiguousOperator do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
before do
|
9
|
+
inspect_source(cop, source)
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'with a splat operator in the first argument' do
|
13
|
+
context 'without parentheses' do
|
14
|
+
context 'without whitespaces on the right of the operator' do
|
15
|
+
let(:source) do
|
16
|
+
[
|
17
|
+
'array = [1, 2, 3]',
|
18
|
+
'puts *array'
|
19
|
+
]
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'registers an offence' do
|
23
|
+
expect(cop.offences.size).to eq(1)
|
24
|
+
expect(cop.offences.first.message).to eq(
|
25
|
+
'Ambiguous splat operator. ' +
|
26
|
+
"Parenthesize the method arguments if it's surely a splat " +
|
27
|
+
'operator, ' +
|
28
|
+
'or add a whitespace to the right of the * if it should be a ' +
|
29
|
+
'multiplication.'
|
30
|
+
)
|
31
|
+
expect(cop.highlights).to eq(['*'])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'with a whitespace on the right of the operator' do
|
36
|
+
let(:source) do
|
37
|
+
[
|
38
|
+
'array = [1, 2, 3]',
|
39
|
+
'puts * array'
|
40
|
+
]
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'accepts' do
|
44
|
+
expect(cop.offences).to be_empty
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with parentheses' do
|
50
|
+
let(:source) do
|
51
|
+
[
|
52
|
+
'array = [1, 2, 3]',
|
53
|
+
'puts(*array)'
|
54
|
+
]
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'accepts' do
|
58
|
+
expect(cop.offences).to be_empty
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'with a block ampersand in the first argument' do
|
64
|
+
context 'without parentheses' do
|
65
|
+
context 'without whitespaces on the right of the operator' do
|
66
|
+
let(:source) do
|
67
|
+
[
|
68
|
+
'process = proc { do_something }',
|
69
|
+
'2.times &process'
|
70
|
+
]
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'registers an offence' do
|
74
|
+
expect(cop.offences.size).to eq(1)
|
75
|
+
expect(cop.offences.first.message).to eq(
|
76
|
+
'Ambiguous block operator. ' +
|
77
|
+
"Parenthesize the method arguments if it's surely a block " +
|
78
|
+
'operator, ' +
|
79
|
+
'or add a whitespace to the right of the & if it should be a ' +
|
80
|
+
'binary AND.'
|
81
|
+
)
|
82
|
+
expect(cop.highlights).to eq(['&'])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'with a whitespace on the right of the operator' do
|
87
|
+
let(:source) do
|
88
|
+
[
|
89
|
+
'process = proc { do_something }',
|
90
|
+
'2.times & process'
|
91
|
+
]
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'accepts' do
|
95
|
+
expect(cop.offences).to be_empty
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'with parentheses' do
|
101
|
+
let(:source) do
|
102
|
+
[
|
103
|
+
'process = proc { do_something }',
|
104
|
+
'2.times(&process)'
|
105
|
+
]
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'accepts' do
|
109
|
+
expect(cop.offences).to be_empty
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Lint::AmbiguousRegexpLiteral do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
before do
|
9
|
+
inspect_source(cop, source)
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'with a regexp literal in the first argument' do
|
13
|
+
context 'without parentheses' do
|
14
|
+
let(:source) { 'p /pattern/' }
|
15
|
+
|
16
|
+
it 'registers an offence' do
|
17
|
+
expect(cop.offences.size).to eq(1)
|
18
|
+
expect(cop.offences.first.message).to eq(
|
19
|
+
'Ambiguous regexp literal. Parenthesize the method arguments ' +
|
20
|
+
"if it's surely a regexp literal, or add a whitespace to the " +
|
21
|
+
'right of the / if it should be a division.'
|
22
|
+
)
|
23
|
+
expect(cop.highlights).to eq(['/'])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with parentheses' do
|
28
|
+
let(:source) { 'p(/pattern/)' }
|
29
|
+
|
30
|
+
it 'accepts' do
|
31
|
+
expect(cop.offences).to be_empty
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -22,7 +22,7 @@ describe Rubocop::Cop::Lint::BlockAlignment do
|
|
22
22
|
' type_check_value(subvalue, array_type)',
|
23
23
|
'end',
|
24
24
|
'a || b do',
|
25
|
-
'end'
|
25
|
+
'end'
|
26
26
|
])
|
27
27
|
expect(cop.offences).to be_empty
|
28
28
|
end
|
@@ -49,7 +49,7 @@ describe Rubocop::Cop::Lint::BlockAlignment do
|
|
49
49
|
it 'accepts end aligned with the first variable' do
|
50
50
|
inspect_source(cop,
|
51
51
|
['a = b = c = test do |ala|',
|
52
|
-
'end'
|
52
|
+
'end'
|
53
53
|
])
|
54
54
|
expect(cop.offences).to be_empty
|
55
55
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Lint::ConditionPosition do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
%w(if unless while until).each do |keyword|
|
9
|
+
it 'registers an offence for condition on the next line' do
|
10
|
+
inspect_source(cop,
|
11
|
+
["#{keyword}",
|
12
|
+
'x == 10',
|
13
|
+
'end'
|
14
|
+
])
|
15
|
+
expect(cop.offences.size).to eq(1)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'accepts condition on the same line' do
|
19
|
+
inspect_source(cop,
|
20
|
+
["#{keyword} x == 10",
|
21
|
+
' bala',
|
22
|
+
'end'
|
23
|
+
])
|
24
|
+
expect(cop.offences).to be_empty
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'registers an offence for elsif condition on the next line' do
|
29
|
+
inspect_source(cop,
|
30
|
+
['if something',
|
31
|
+
' test',
|
32
|
+
'elsif',
|
33
|
+
' something',
|
34
|
+
' test',
|
35
|
+
'end'
|
36
|
+
])
|
37
|
+
expect(cop.offences.size).to eq(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'handles ternary ops' do
|
41
|
+
inspect_source(cop, ['x ? a : b'])
|
42
|
+
expect(cop.offences).to be_empty
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'handles modifier forms' do
|
46
|
+
inspect_source(cop, ['x if something'])
|
47
|
+
expect(cop.offences).to be_empty
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Lint::ElseLayout do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
it 'registers an offence for expr on same line as else' do
|
9
|
+
inspect_source(cop,
|
10
|
+
['if something',
|
11
|
+
' test',
|
12
|
+
'else ala',
|
13
|
+
' something',
|
14
|
+
' test',
|
15
|
+
'end'
|
16
|
+
])
|
17
|
+
expect(cop.offences.size).to eq(1)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'accepts proper else' do
|
21
|
+
inspect_source(cop,
|
22
|
+
['if something',
|
23
|
+
' test',
|
24
|
+
'else',
|
25
|
+
' something',
|
26
|
+
' test',
|
27
|
+
'end'
|
28
|
+
])
|
29
|
+
expect(cop.offences).to be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'accepts single-expr else regardless of layout' do
|
33
|
+
inspect_source(cop,
|
34
|
+
['if something',
|
35
|
+
' test',
|
36
|
+
'else bala',
|
37
|
+
'end'
|
38
|
+
])
|
39
|
+
expect(cop.offences).to be_empty
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'can handle elsifs' do
|
43
|
+
inspect_source(cop,
|
44
|
+
['if something',
|
45
|
+
' test',
|
46
|
+
'elsif something',
|
47
|
+
' bala',
|
48
|
+
'else ala',
|
49
|
+
' something',
|
50
|
+
' test',
|
51
|
+
'end'
|
52
|
+
])
|
53
|
+
expect(cop.offences.size).to eq(1)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'handles ternary ops' do
|
57
|
+
inspect_source(cop, 'x ? a : b')
|
58
|
+
expect(cop.offences).to be_empty
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'handles modifier forms' do
|
62
|
+
inspect_source(cop, 'x if something')
|
63
|
+
expect(cop.offences).to be_empty
|
64
|
+
end
|
65
|
+
end
|