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.

Files changed (189) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +3 -1
  4. data/CHANGELOG.md +44 -0
  5. data/CONTRIBUTING.md +40 -8
  6. data/Gemfile +6 -0
  7. data/README.md +65 -20
  8. data/Rakefile +0 -1
  9. data/config/default.yml +15 -3
  10. data/config/enabled.yml +143 -109
  11. data/lib/rubocop.rb +45 -26
  12. data/lib/rubocop/cli.rb +26 -27
  13. data/lib/rubocop/config.rb +0 -1
  14. data/lib/rubocop/config_loader.rb +16 -23
  15. data/lib/rubocop/cop/commissioner.rb +2 -7
  16. data/lib/rubocop/cop/cop.rb +24 -51
  17. data/lib/rubocop/cop/ignored_node.rb +31 -0
  18. data/lib/rubocop/cop/lint/ambiguous_operator.rb +50 -0
  19. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +36 -0
  20. data/lib/rubocop/cop/lint/assignment_in_condition.rb +3 -11
  21. data/lib/rubocop/cop/lint/block_alignment.rb +6 -20
  22. data/lib/rubocop/cop/lint/condition_position.rb +52 -0
  23. data/lib/rubocop/cop/lint/else_layout.rb +57 -0
  24. data/lib/rubocop/cop/lint/end_alignment.rb +33 -8
  25. data/lib/rubocop/cop/lint/invalid_character_literal.rb +37 -0
  26. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +2 -4
  27. data/lib/rubocop/cop/lint/syntax.rb +6 -12
  28. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +25 -0
  29. data/lib/rubocop/cop/mixin/array_syntax.rb +20 -0
  30. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +76 -0
  31. data/lib/rubocop/cop/mixin/check_assignment.rb +26 -0
  32. data/lib/rubocop/cop/{check_methods.rb → mixin/check_methods.rb} +0 -0
  33. data/lib/rubocop/cop/mixin/code_length.rb +33 -0
  34. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +53 -0
  35. data/lib/rubocop/cop/mixin/configurable_max.rb +19 -0
  36. data/lib/rubocop/cop/mixin/configurable_naming.rb +45 -0
  37. data/lib/rubocop/cop/{style → mixin}/if_node.rb +0 -0
  38. data/lib/rubocop/cop/mixin/if_then_else.rb +23 -0
  39. data/lib/rubocop/cop/mixin/negative_conditional.rb +24 -0
  40. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +34 -0
  41. data/lib/rubocop/cop/mixin/safe_assignment.rb +19 -0
  42. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +32 -0
  43. data/lib/rubocop/cop/mixin/space_inside.rb +31 -0
  44. data/lib/rubocop/cop/mixin/statement_modifier.rb +59 -0
  45. data/lib/rubocop/cop/mixin/string_help.rb +32 -0
  46. data/lib/rubocop/cop/mixin/surrounding_space.rb +42 -0
  47. data/lib/rubocop/cop/rails/default_scope.rb +3 -1
  48. data/lib/rubocop/cop/style/accessor_method_name.rb +4 -12
  49. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +16 -1
  50. data/lib/rubocop/cop/style/case_indentation.rb +33 -16
  51. data/lib/rubocop/cop/style/character_literal.rb +10 -0
  52. data/lib/rubocop/cop/style/dot_position.rb +23 -6
  53. data/lib/rubocop/cop/style/empty_lines_around_body.rb +5 -5
  54. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +1 -32
  55. data/lib/rubocop/cop/style/favor_until_over_negated_while.rb +20 -0
  56. data/lib/rubocop/cop/style/hash_syntax.rb +5 -1
  57. data/lib/rubocop/cop/style/if_unless_modifier.rb +34 -0
  58. data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
  59. data/lib/rubocop/cop/style/indentation_consistency.rb +51 -0
  60. data/lib/rubocop/cop/style/indentation_width.rb +0 -26
  61. data/lib/rubocop/cop/style/lambda_call.rb +12 -5
  62. data/lib/rubocop/cop/style/method_def_parentheses.rb +29 -11
  63. data/lib/rubocop/cop/style/multiline_if_then.rb +4 -9
  64. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +22 -0
  65. data/lib/rubocop/cop/style/{ternary_operator.rb → nested_ternary_operator.rb} +0 -15
  66. data/lib/rubocop/cop/style/numeric_literals.rb +30 -2
  67. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -1
  68. data/lib/rubocop/cop/style/parameter_lists.rb +7 -3
  69. data/lib/rubocop/cop/style/parentheses_around_condition.rb +9 -11
  70. data/lib/rubocop/cop/style/predicate_name.rb +4 -12
  71. data/lib/rubocop/cop/style/raise_args.rb +19 -11
  72. data/lib/rubocop/cop/style/regexp_literal.rb +19 -6
  73. data/lib/rubocop/cop/style/space_after_colon.rb +36 -0
  74. data/lib/rubocop/cop/style/space_after_comma.rb +16 -0
  75. data/lib/rubocop/cop/style/space_after_semicolon.rb +16 -0
  76. data/lib/rubocop/cop/style/space_around_block_braces.rb +38 -38
  77. data/lib/rubocop/cop/style/space_around_operators.rb +1 -2
  78. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +6 -2
  79. data/lib/rubocop/cop/style/string_literals.rb +5 -5
  80. data/lib/rubocop/cop/style/trailing_comma.rb +94 -0
  81. data/lib/rubocop/cop/style/unless_else.rb +2 -2
  82. data/lib/rubocop/cop/style/while_until_modifier.rb +32 -0
  83. data/lib/rubocop/cop/style/word_array.rb +9 -1
  84. data/lib/rubocop/cop/util.rb +14 -0
  85. data/lib/rubocop/cop/variable_inspector.rb +11 -6
  86. data/lib/rubocop/cop/variable_inspector/scope.rb +4 -3
  87. data/lib/rubocop/file_inspector.rb +22 -6
  88. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  89. data/lib/rubocop/formatter/colorizable.rb +37 -0
  90. data/lib/rubocop/formatter/disabled_config_formatter.rb +27 -6
  91. data/lib/rubocop/formatter/progress_formatter.rb +1 -1
  92. data/lib/rubocop/formatter/simple_text_formatter.rb +9 -5
  93. data/lib/rubocop/options.rb +19 -4
  94. data/lib/rubocop/target_finder.rb +4 -0
  95. data/lib/rubocop/version.rb +1 -1
  96. data/rubocop-todo.yml +10 -2
  97. data/rubocop.gemspec +3 -2
  98. data/spec/project_spec.rb +12 -7
  99. data/spec/rubocop/cli_spec.rb +262 -99
  100. data/spec/rubocop/config_loader_spec.rb +5 -5
  101. data/spec/rubocop/config_spec.rb +3 -3
  102. data/spec/rubocop/config_store_spec.rb +12 -11
  103. data/spec/rubocop/cop/commissioner_spec.rb +21 -5
  104. data/spec/rubocop/cop/cop_spec.rb +1 -1
  105. data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +113 -0
  106. data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +35 -0
  107. data/spec/rubocop/cop/lint/block_alignment_spec.rb +2 -2
  108. data/spec/rubocop/cop/lint/condition_position_spec.rb +49 -0
  109. data/spec/rubocop/cop/lint/else_layout_spec.rb +65 -0
  110. data/spec/rubocop/cop/lint/end_alignment_spec.rb +41 -1
  111. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +33 -0
  112. data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +3 -3
  113. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +12 -12
  114. data/spec/rubocop/cop/lint/syntax_spec.rb +2 -2
  115. data/spec/rubocop/cop/lint/useless_assignment_spec.rb +72 -54
  116. data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +48 -0
  117. data/spec/rubocop/cop/offence_spec.rb +1 -1
  118. data/spec/rubocop/cop/rails/default_scope_spec.rb +6 -0
  119. data/spec/rubocop/cop/rails/output_spec.rb +2 -1
  120. data/spec/rubocop/cop/style/align_hash_spec.rb +9 -9
  121. data/spec/rubocop/cop/style/align_parameters_spec.rb +1 -1
  122. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +5 -0
  123. data/spec/rubocop/cop/style/case_indentation_spec.rb +53 -2
  124. data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +3 -3
  125. data/spec/rubocop/cop/style/documentation_spec.rb +0 -1
  126. data/spec/rubocop/cop/style/dot_position_spec.rb +18 -3
  127. data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +4 -4
  128. data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +13 -0
  129. data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +1 -1
  130. data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +1 -1
  131. data/spec/rubocop/cop/style/hash_syntax_spec.rb +5 -0
  132. data/spec/rubocop/cop/style/{favor_modifier_spec.rb → if_unless_modifier_spec.rb} +4 -111
  133. data/spec/rubocop/cop/style/indentation_consistency_spec.rb +490 -0
  134. data/spec/rubocop/cop/style/indentation_width_spec.rb +19 -91
  135. data/spec/rubocop/cop/style/lambda_call_spec.rb +18 -0
  136. data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +2 -2
  137. data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +35 -1
  138. data/spec/rubocop/cop/style/method_length_spec.rb +1 -0
  139. data/spec/rubocop/cop/style/method_name_spec.rb +27 -5
  140. data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +4 -4
  141. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +2 -2
  142. data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +18 -0
  143. data/spec/rubocop/cop/style/{ternary_operator_spec.rb → nested_ternary_operator_spec.rb} +0 -15
  144. data/spec/rubocop/cop/style/numeric_literals_spec.rb +18 -1
  145. data/spec/rubocop/cop/style/one_line_conditional_spec.rb +2 -1
  146. data/spec/rubocop/cop/style/parameter_lists_spec.rb +1 -0
  147. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +13 -4
  148. data/spec/rubocop/cop/style/raise_args_spec.rb +22 -0
  149. data/spec/rubocop/cop/style/redundant_self_spec.rb +4 -4
  150. data/spec/rubocop/cop/style/regexp_literal_spec.rb +4 -0
  151. data/spec/rubocop/cop/style/space_after_colon_spec.rb +12 -4
  152. data/spec/rubocop/cop/style/space_after_method_name_spec.rb +2 -2
  153. data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +30 -1
  154. data/spec/rubocop/cop/style/{space_around_equals_in_default_parameter_spec.rb → space_around_equals_in_parameter_default_spec.rb} +0 -0
  155. data/spec/rubocop/cop/style/space_around_operators_spec.rb +2 -1
  156. data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +20 -3
  157. data/spec/rubocop/cop/style/string_literals_spec.rb +33 -0
  158. data/spec/rubocop/cop/style/trailing_comma_spec.rb +200 -0
  159. data/spec/rubocop/cop/style/variable_name_spec.rb +27 -3
  160. data/spec/rubocop/cop/style/while_until_modifier_spec.rb +75 -0
  161. data/spec/rubocop/cop/style/word_array_spec.rb +1 -0
  162. data/spec/rubocop/cop/team_spec.rb +1 -1
  163. data/spec/rubocop/cop/variable_inspector/scope_spec.rb +3 -4
  164. data/spec/rubocop/file_inspector_spec.rb +1 -1
  165. data/spec/rubocop/formatter/base_formatter_spec.rb +12 -11
  166. data/spec/rubocop/formatter/colorizable_spec.rb +107 -0
  167. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +2 -0
  168. data/spec/rubocop/formatter/formatter_set_spec.rb +1 -1
  169. data/spec/rubocop/formatter/json_formatter_spec.rb +4 -3
  170. data/spec/rubocop/formatter/progress_formatter_spec.rb +2 -2
  171. data/spec/rubocop/options_spec.rb +3 -1
  172. data/spec/rubocop/target_finder_spec.rb +13 -11
  173. data/spec/spec_helper.rb +5 -1
  174. data/spec/support/shared_examples.rb +2 -2
  175. data/spec/support/statement_modifier_helper.rb +41 -0
  176. metadata +88 -30
  177. data/lib/rubocop/cop/check_assignment.rb +0 -43
  178. data/lib/rubocop/cop/style/array_syntax.rb +0 -22
  179. data/lib/rubocop/cop/style/autocorrect_alignment.rb +0 -78
  180. data/lib/rubocop/cop/style/code_length.rb +0 -35
  181. data/lib/rubocop/cop/style/configurable_enforced_style.rb +0 -51
  182. data/lib/rubocop/cop/style/configurable_max.rb +0 -17
  183. data/lib/rubocop/cop/style/configurable_naming.rb +0 -41
  184. data/lib/rubocop/cop/style/favor_modifier.rb +0 -118
  185. data/lib/rubocop/cop/style/if_then_else.rb +0 -27
  186. data/lib/rubocop/cop/style/space_after_comma_etc.rb +0 -73
  187. data/lib/rubocop/cop/style/space_inside.rb +0 -33
  188. data/lib/rubocop/cop/style/string_help.rb +0 -30
  189. 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(
@@ -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.stub(:configuration_file_for) do |arg|
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.stub(:configuration_from_file) { |arg| arg }
18
- Rubocop::ConfigLoader.stub(:load_file) { |arg| "#{arg} loaded" }
19
- Rubocop::ConfigLoader.stub(:merge_with_default) do |config, file|
20
- "merged #{config}"
21
- end
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.should_receive(:configuration_file_for).once
33
+ expect(Rubocop::ConfigLoader).to receive(:configuration_file_for).once
33
34
  .with('dir')
34
- Rubocop::ConfigLoader.should_receive(:configuration_file_for).once
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.should_receive(:configuration_from_file).once
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.should_receive(:configuration_file_for).once
47
- Rubocop::ConfigLoader.should_receive(:configuration_from_file).once
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.stub(:offences).and_return([1])
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.should_receive(:on_def)
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.should_receive(:investigate).with(processed_source)
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.stub(:on_def) { fail RuntimeError }
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.stub(:on_def) { fail RuntimeError }
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']
@@ -41,7 +41,7 @@ describe Rubocop::Cop::Cop do
41
41
 
42
42
  it 'will warn if custom severity is invalid' do
43
43
  cop.config[cop.name] = { 'Severity' => 'superbad' }
44
- cop.should_receive(:warn)
44
+ expect(cop).to receive(:warn)
45
45
  cop.add_offence(nil, location, 'message')
46
46
  end
47
47
 
@@ -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