rubocop 0.19.1 → 0.20.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.

Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -0
  3. data/CHANGELOG.md +60 -1
  4. data/CONTRIBUTING.md +2 -1
  5. data/README.md +9 -7
  6. data/config/default.yml +3 -3
  7. data/config/disabled.yml +4 -0
  8. data/config/enabled.yml +45 -21
  9. data/lib/rubocop.rb +30 -9
  10. data/lib/rubocop/cli.rb +1 -1
  11. data/lib/rubocop/comment_config.rb +4 -2
  12. data/lib/rubocop/config.rb +16 -22
  13. data/lib/rubocop/config_loader.rb +29 -26
  14. data/lib/rubocop/cop/commissioner.rb +1 -1
  15. data/lib/rubocop/cop/cop.rb +6 -6
  16. data/lib/rubocop/cop/lint/assignment_in_condition.rb +1 -1
  17. data/lib/rubocop/cop/lint/condition_position.rb +1 -1
  18. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  19. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +9 -7
  20. data/lib/rubocop/cop/lint/else_layout.rb +1 -1
  21. data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
  22. data/lib/rubocop/cop/lint/empty_interpolation.rb +22 -0
  23. data/lib/rubocop/cop/lint/end_in_method.rb +1 -1
  24. data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
  25. data/lib/rubocop/cop/lint/eval.rb +1 -1
  26. data/lib/rubocop/cop/lint/literal_in_condition.rb +2 -2
  27. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +9 -0
  28. data/lib/rubocop/cop/lint/loop.rb +2 -2
  29. data/lib/rubocop/cop/lint/require_parentheses.rb +0 -4
  30. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  31. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
  32. data/lib/rubocop/cop/lint/space_before_first_arg.rb +36 -0
  33. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +14 -2
  34. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -0
  35. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  36. data/lib/rubocop/cop/lint/void.rb +3 -3
  37. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  38. data/lib/rubocop/cop/offense.rb +3 -21
  39. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  40. data/lib/rubocop/cop/rails/default_scope.rb +1 -1
  41. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  42. data/lib/rubocop/cop/rails/read_write_attribute.rb +43 -0
  43. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  44. data/lib/rubocop/cop/rails/validation.rb +1 -1
  45. data/lib/rubocop/cop/severity.rb +76 -0
  46. data/lib/rubocop/cop/style/access_modifier_indentation.rb +1 -1
  47. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  48. data/lib/rubocop/cop/style/alias.rb +1 -1
  49. data/lib/rubocop/cop/style/align_hash.rb +1 -1
  50. data/lib/rubocop/cop/style/and_or.rb +1 -1
  51. data/lib/rubocop/cop/style/{favor_join.rb → array_join.rb} +2 -2
  52. data/lib/rubocop/cop/style/begin_block.rb +1 -1
  53. data/lib/rubocop/cop/style/block_nesting.rb +12 -8
  54. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +1 -1
  55. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  56. data/lib/rubocop/cop/style/case_indentation.rb +5 -5
  57. data/lib/rubocop/cop/style/class_methods.rb +19 -3
  58. data/lib/rubocop/cop/style/class_vars.rb +1 -1
  59. data/lib/rubocop/cop/style/collection_methods.rb +17 -7
  60. data/lib/rubocop/cop/style/colon_method_call.rb +1 -1
  61. data/lib/rubocop/cop/style/{def_parentheses.rb → def_with_parentheses.rb} +0 -0
  62. data/lib/rubocop/cop/style/{hash_methods.rb → deprecated_hash_methods.rb} +5 -4
  63. data/lib/rubocop/cop/style/double_negation.rb +1 -1
  64. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
  65. data/lib/rubocop/cop/style/end_block.rb +1 -1
  66. data/lib/rubocop/cop/style/even_odd.rb +2 -2
  67. data/lib/rubocop/cop/style/file_name.rb +1 -1
  68. data/lib/rubocop/cop/style/for.rb +2 -2
  69. data/lib/rubocop/cop/style/format_string.rb +1 -1
  70. data/lib/rubocop/cop/style/guard_clause.rb +69 -0
  71. data/lib/rubocop/cop/style/hash_syntax.rb +6 -10
  72. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  73. data/lib/rubocop/cop/style/lambda.rb +2 -2
  74. data/lib/rubocop/cop/style/line_end_concatenation.rb +16 -9
  75. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  76. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +2 -2
  77. data/lib/rubocop/cop/style/negated_if.rb +37 -0
  78. data/lib/rubocop/cop/style/negated_while.rb +33 -0
  79. data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
  80. data/lib/rubocop/cop/style/nil_comparison.rb +14 -10
  81. data/lib/rubocop/cop/style/non_nil_check.rb +70 -0
  82. data/lib/rubocop/cop/style/not.rb +25 -1
  83. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  84. data/lib/rubocop/cop/style/op_method.rb +4 -4
  85. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
  86. data/lib/rubocop/cop/style/predicate_name.rb +1 -1
  87. data/lib/rubocop/cop/style/proc.rb +1 -1
  88. data/lib/rubocop/cop/style/raise_args.rb +3 -2
  89. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  90. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  91. data/lib/rubocop/cop/style/signal_exception.rb +6 -3
  92. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  93. data/lib/rubocop/cop/style/single_line_methods.rb +6 -5
  94. data/lib/rubocop/cop/style/single_space_before_first_arg.rb +41 -0
  95. data/lib/rubocop/cop/style/space_around_operators.rb +1 -2
  96. data/lib/rubocop/cop/style/special_global_vars.rb +8 -8
  97. data/lib/rubocop/cop/style/trailing_comma.rb +1 -1
  98. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  99. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  100. data/lib/rubocop/cop/style/variable_interpolation.rb +10 -5
  101. data/lib/rubocop/cop/style/when_then.rb +1 -1
  102. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  103. data/lib/rubocop/cop/style/word_array.rb +1 -1
  104. data/lib/rubocop/cop/team.rb +12 -13
  105. data/lib/rubocop/cop/util.rb +4 -0
  106. data/lib/rubocop/cop/variable_inspector/locatable.rb +1 -1
  107. data/lib/rubocop/cop/variable_inspector/variable_table.rb +1 -1
  108. data/lib/rubocop/file_inspector.rb +46 -10
  109. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  110. data/lib/rubocop/formatter/disabled_lines_formatter.rb +56 -0
  111. data/lib/rubocop/formatter/emacs_style_formatter.rb +1 -1
  112. data/lib/rubocop/formatter/formatter_set.rb +2 -1
  113. data/lib/rubocop/formatter/fuubar_style_formatter.rb +2 -4
  114. data/lib/rubocop/formatter/json_formatter.rb +3 -7
  115. data/lib/rubocop/formatter/progress_formatter.rb +1 -3
  116. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  117. data/lib/rubocop/options.rb +29 -10
  118. data/lib/rubocop/path_util.rb +2 -1
  119. data/lib/rubocop/processed_source.rb +8 -0
  120. data/lib/rubocop/target_finder.rb +33 -12
  121. data/lib/rubocop/version.rb +1 -1
  122. data/relnotes/v0.20.0.md +69 -0
  123. data/rubocop-todo.yml +2 -2
  124. data/spec/rubocop/cli_spec.rb +269 -94
  125. data/spec/rubocop/config_loader_spec.rb +14 -14
  126. data/spec/rubocop/config_spec.rb +8 -8
  127. data/spec/rubocop/cop/lint/deprecated_class_methods_spec.rb +11 -6
  128. data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +18 -0
  129. data/spec/rubocop/cop/lint/eval_spec.rb +2 -4
  130. data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +10 -0
  131. data/spec/rubocop/cop/lint/rescue_exception_spec.rb +0 -8
  132. data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +4 -4
  133. data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +48 -0
  134. data/spec/rubocop/cop/lint/string_conversion_in_interpolation_spec.rb +10 -0
  135. data/spec/rubocop/cop/lint/useless_access_modifier_spec.rb +154 -0
  136. data/spec/rubocop/cop/offense_spec.rb +1 -1
  137. data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +19 -0
  138. data/spec/rubocop/cop/severity_spec.rb +113 -0
  139. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +10 -10
  140. data/spec/rubocop/cop/style/alias_spec.rb +2 -2
  141. data/spec/rubocop/cop/style/and_or_spec.rb +2 -2
  142. data/spec/rubocop/cop/style/{favor_join_spec.rb → array_join_spec.rb} +1 -3
  143. data/spec/rubocop/cop/style/block_nesting_spec.rb +4 -4
  144. data/spec/rubocop/cop/style/case_equality_spec.rb +1 -0
  145. data/spec/rubocop/cop/style/case_indentation_spec.rb +12 -9
  146. data/spec/rubocop/cop/style/class_methods_spec.rb +23 -0
  147. data/spec/rubocop/cop/style/collection_methods_spec.rb +2 -2
  148. data/spec/rubocop/cop/style/{hash_methods_spec.rb → deprecated_hash_methods_spec.rb} +3 -3
  149. data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +2 -2
  150. data/spec/rubocop/cop/style/even_odd_spec.rb +8 -8
  151. data/spec/rubocop/cop/style/file_name_spec.rb +55 -42
  152. data/spec/rubocop/cop/style/for_spec.rb +4 -4
  153. data/spec/rubocop/cop/style/format_string_spec.rb +10 -10
  154. data/spec/rubocop/cop/style/guard_clause_spec.rb +77 -0
  155. data/spec/rubocop/cop/style/hash_syntax_spec.rb +4 -2
  156. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +4 -4
  157. data/spec/rubocop/cop/style/lambda_spec.rb +2 -2
  158. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +21 -0
  159. data/spec/rubocop/cop/style/multiline_if_then_spec.rb +1 -1
  160. data/spec/rubocop/cop/style/{favor_unless_over_negated_if_spec.rb → negated_if_spec.rb} +8 -3
  161. data/spec/rubocop/cop/style/{favor_until_over_negated_while_spec.rb → negated_while_spec.rb} +8 -3
  162. data/spec/rubocop/cop/style/nil_comparison_spec.rb +7 -13
  163. data/spec/rubocop/cop/style/non_nil_check_spec.rb +35 -0
  164. data/spec/rubocop/cop/style/not_spec.rb +11 -0
  165. data/spec/rubocop/cop/style/numeric_literals_spec.rb +0 -2
  166. data/spec/rubocop/cop/style/op_method_spec.rb +10 -2
  167. data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +2 -2
  168. data/spec/rubocop/cop/style/predicate_name_spec.rb +2 -1
  169. data/spec/rubocop/cop/style/raise_args_spec.rb +5 -0
  170. data/spec/rubocop/cop/style/rescue_modifier_spec.rb +2 -2
  171. data/spec/rubocop/cop/style/self_assignment_spec.rb +4 -4
  172. data/spec/rubocop/cop/style/signal_exception_spec.rb +24 -0
  173. data/spec/rubocop/cop/style/single_line_block_params_spec.rb +2 -0
  174. data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +63 -0
  175. data/spec/rubocop/cop/style/special_global_vars_spec.rb +6 -5
  176. data/spec/rubocop/cop/style/trivial_accessors_spec.rb +6 -3
  177. data/spec/rubocop/cop/style/unless_else_spec.rb +2 -4
  178. data/spec/rubocop/cop/style/variable_interpolation_spec.rb +15 -6
  179. data/spec/rubocop/cop/style/when_then_spec.rb +3 -4
  180. data/spec/rubocop/cop/team_spec.rb +4 -18
  181. data/spec/rubocop/file_inspector_spec.rb +4 -0
  182. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +1 -1
  183. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +69 -0
  184. data/spec/rubocop/options_spec.rb +5 -0
  185. data/spec/rubocop/target_finder_spec.rb +42 -11
  186. data/spec/support/shared_context.rb +1 -1
  187. data/spec/support/statement_modifier_helper.rb +1 -1
  188. metadata +75 -50
  189. data/lib/rubocop/cop/rails/read_attribute.rb +0 -28
  190. data/lib/rubocop/cop/style/favor_unless_over_negated_if.rb +0 -24
  191. data/lib/rubocop/cop/style/favor_until_over_negated_while.rb +0 -20
  192. data/spec/rubocop/cop/rails/read_attribute_spec.rb +0 -13
@@ -9,7 +9,7 @@ describe Rubocop::Cop::Style::Lambda do
9
9
  inspect_source(cop, ['f = lambda { |x| x }'])
10
10
  expect(cop.offenses.size).to eq(1)
11
11
  expect(cop.messages)
12
- .to eq(['Use the new lambda literal syntax ->(params) {...}.'])
12
+ .to eq(['Use the new lambda literal syntax `->(params) {...}`.'])
13
13
  end
14
14
 
15
15
  it 'accepts the new lambda literal with single-line body' do
@@ -24,7 +24,7 @@ describe Rubocop::Cop::Style::Lambda do
24
24
  'end'])
25
25
  expect(cop.offenses.size).to eq(1)
26
26
  expect(cop.messages)
27
- .to eq(['Use the lambda method for multi-line lambdas.'])
27
+ .to eq(['Use the `lambda` method for multi-line lambdas.'])
28
28
  end
29
29
 
30
30
  it 'accepts the old lambda syntax with multi-line body' do
@@ -12,6 +12,13 @@ describe Rubocop::Cop::Style::LineEndConcatenation do
12
12
  expect(cop.offenses.size).to eq(1)
13
13
  end
14
14
 
15
+ it 'registers an offense for string concat with << at line end' do
16
+ inspect_source(cop,
17
+ ['top = "test" <<',
18
+ '"top"'])
19
+ expect(cop.offenses.size).to eq(1)
20
+ end
21
+
15
22
  it 'registers an offense for dynamic string concat at line end' do
16
23
  inspect_source(cop,
17
24
  ['top = "test#{x}" +',
@@ -19,6 +26,13 @@ describe Rubocop::Cop::Style::LineEndConcatenation do
19
26
  expect(cop.offenses.size).to eq(1)
20
27
  end
21
28
 
29
+ it 'registers an offense for dynamic string concat with << at line end' do
30
+ inspect_source(cop,
31
+ ['top = "test#{x}" <<',
32
+ '"top"'])
33
+ expect(cop.offenses.size).to eq(1)
34
+ end
35
+
22
36
  it 'accepts string concat on the same line' do
23
37
  inspect_source(cop,
24
38
  ['top = "test" + "top"'])
@@ -32,6 +46,13 @@ describe Rubocop::Cop::Style::LineEndConcatenation do
32
46
  expect(cop.offenses).to be_empty
33
47
  end
34
48
 
49
+ it 'accepts string concat at line end when % literals are involved' do
50
+ inspect_source(cop,
51
+ ['top = %(test) +',
52
+ '"top"'])
53
+ expect(cop.offenses).to be_empty
54
+ end
55
+
35
56
  it 'autocorrects by replacing + with \\' do
36
57
  corrected = autocorrect_source(cop,
37
58
  ['top = "test" +',
@@ -72,7 +72,7 @@ describe Rubocop::Cop::Style::MultilineIfThen do
72
72
  inspect_source(cop, ['unless cond then',
73
73
  'end'])
74
74
  expect(cop.messages).to eq(
75
- ['Never use then for multi-line unless.'])
75
+ ['Never use `then` for multi-line `unless`.'])
76
76
  end
77
77
 
78
78
  it 'accepts multiline unless without then' do
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::FavorUnlessOverNegatedIf do
5
+ describe Rubocop::Cop::Style::NegatedIf do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
8
  it 'registers an offense for if with exclamation point condition' do
@@ -13,7 +13,7 @@ describe Rubocop::Cop::Style::FavorUnlessOverNegatedIf do
13
13
  'some_method if !a_condition'
14
14
  ])
15
15
  expect(cop.messages).to eq(
16
- ['Favor unless (or control flow or) over if for negative ' \
16
+ ['Favor `unless` over `if` for negative ' \
17
17
  'conditions.'] * 2)
18
18
  end
19
19
 
@@ -24,7 +24,7 @@ describe Rubocop::Cop::Style::FavorUnlessOverNegatedIf do
24
24
  'end',
25
25
  'some_method if not a_condition'])
26
26
  expect(cop.messages).to eq(
27
- ['Favor unless (or control flow or) over if for negative ' \
27
+ ['Favor `unless` over `if` for negative ' \
28
28
  'conditions.'] * 2)
29
29
  expect(cop.offenses.map(&:line)).to eq([1, 4])
30
30
  end
@@ -73,4 +73,9 @@ describe Rubocop::Cop::Style::FavorUnlessOverNegatedIf do
73
73
  inspect_source(cop, 'a ? b : c')
74
74
  expect(cop.offenses).to be_empty
75
75
  end
76
+
77
+ it 'autocorrects by replacing if not with unless' do
78
+ corrected = autocorrect_source(cop, 'something if !x.even?')
79
+ expect(corrected).to eq 'something unless x.even?'
80
+ end
76
81
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::FavorUntilOverNegatedWhile do
5
+ describe Rubocop::Cop::Style::NegatedWhile do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
8
  it 'registers an offense for while with exclamation point condition' do
@@ -13,7 +13,7 @@ describe Rubocop::Cop::Style::FavorUntilOverNegatedWhile do
13
13
  'some_method while !a_condition'
14
14
  ])
15
15
  expect(cop.messages).to eq(
16
- ['Favor until over while for negative conditions.'] * 2)
16
+ ['Favor `until` over `while` for negative conditions.'] * 2)
17
17
  end
18
18
 
19
19
  it 'registers an offense for while with "not" condition' do
@@ -23,7 +23,7 @@ describe Rubocop::Cop::Style::FavorUntilOverNegatedWhile do
23
23
  'end',
24
24
  'some_method while not a_condition'])
25
25
  expect(cop.messages).to eq(
26
- ['Favor until over while for negative conditions.'] * 2)
26
+ ['Favor `until` over `while` for negative conditions.'] * 2)
27
27
  expect(cop.offenses.map(&:line)).to eq([1, 4])
28
28
  end
29
29
 
@@ -38,4 +38,9 @@ describe Rubocop::Cop::Style::FavorUntilOverNegatedWhile do
38
38
  'some_method while not a_condition or other_cond'])
39
39
  expect(cop.messages).to be_empty
40
40
  end
41
+
42
+ it 'autocorrects by replacing while not with until' do
43
+ corrected = autocorrect_source(cop, 'something while !x.even?')
44
+ expect(corrected).to eq 'something until x.even?'
45
+ end
41
46
  end
@@ -6,25 +6,19 @@ describe Rubocop::Cop::Style::NilComparison do
6
6
  subject(:cop) { described_class.new }
7
7
 
8
8
  it 'registers an offense for == nil' do
9
- inspect_source(cop,
10
- ['x == nil'])
9
+ inspect_source(cop, 'x == nil')
11
10
  expect(cop.offenses.size).to eq(1)
11
+ expect(cop.highlights).to eq(['=='])
12
12
  end
13
13
 
14
14
  it 'registers an offense for === nil' do
15
- inspect_source(cop,
16
- ['x === nil'])
15
+ inspect_source(cop, 'x === nil')
17
16
  expect(cop.offenses.size).to eq(1)
17
+ expect(cop.highlights).to eq(['==='])
18
18
  end
19
19
 
20
- it 'registers an offense for === nil' do
21
- inspect_source(cop,
22
- ['x != nil'])
23
- expect(cop.offenses.size).to eq(1)
24
- end
25
-
26
- it 'works with lambda.()' do
27
- inspect_source(cop, ['a.(x) == nil'])
28
- expect(cop.offenses.size).to eq(1)
20
+ it 'autocorrects by replacing == nil with .nil?' do
21
+ corrected = autocorrect_source(cop, 'x == nil')
22
+ expect(corrected).to eq 'x.nil?'
29
23
  end
30
24
  end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::NonNilCheck do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'registers an offense for != nil' do
9
+ inspect_source(cop, 'x != nil')
10
+ expect(cop.offenses.size).to eq(1)
11
+ expect(cop.highlights).to eq(['!='])
12
+ end
13
+
14
+ it 'registers an offense for !x.nil?' do
15
+ inspect_source(cop, '!x.nil?')
16
+ expect(cop.offenses.size).to eq(1)
17
+ expect(cop.highlights).to eq(['!x.nil?'])
18
+ end
19
+
20
+ it 'registers an offense for not x.nil?' do
21
+ inspect_source(cop, 'not x.nil?')
22
+ expect(cop.offenses.size).to eq(1)
23
+ expect(cop.highlights).to eq(['not x.nil?'])
24
+ end
25
+
26
+ it 'autocorrects by removing != nil' do
27
+ corrected = autocorrect_source(cop, 'x != nil')
28
+ expect(corrected).to eq 'x'
29
+ end
30
+
31
+ it 'autocorrects by removing non-nil (!x.nil?) check' do
32
+ corrected = autocorrect_source(cop, '!x.nil?')
33
+ expect(corrected).to eq 'x'
34
+ end
35
+ end
@@ -14,4 +14,15 @@ describe Rubocop::Cop::Style::Not do
14
14
  inspect_source(cop, '!test')
15
15
  expect(cop.offenses).to be_empty
16
16
  end
17
+
18
+ it 'auto-corrects "not" with !' do
19
+ new_source = autocorrect_source(cop, 'x = 10 if not y')
20
+ expect(new_source).to eq('x = 10 if !y')
21
+ end
22
+
23
+ it 'leaves "not" as is if auto-correction changes the meaning' do
24
+ src = 'not x < y'
25
+ new_source = autocorrect_source(cop, src)
26
+ expect(new_source).to eq(src)
27
+ end
17
28
  end
@@ -13,8 +13,6 @@ describe Rubocop::Cop::Style::NumericLiterals, :config do
13
13
  end
14
14
 
15
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
16
  inspect_source(cop, ['a = 123456.789'])
19
17
  expect(cop.offenses.size).to eq(1)
20
18
  expect(cop.config_to_allow_offenses).to eq('MinDigits' => 7)
@@ -12,7 +12,7 @@ describe Rubocop::Cop::Style::OpMethod do
12
12
  'end'])
13
13
  expect(cop.offenses.size).to eq(1)
14
14
  expect(cop.messages)
15
- .to eq(['When defining the + operator, name its argument *other*.'])
15
+ .to eq(['When defining the `+` operator, name its argument `other`.'])
16
16
  end
17
17
 
18
18
  it 'works properly even if the argument not surrounded with braces' do
@@ -22,7 +22,7 @@ describe Rubocop::Cop::Style::OpMethod do
22
22
  'end'])
23
23
  expect(cop.offenses.size).to eq(1)
24
24
  expect(cop.messages)
25
- .to eq(['When defining the + operator, name its argument *other*.'])
25
+ .to eq(['When defining the `+` operator, name its argument `other`.'])
26
26
  end
27
27
 
28
28
  it 'does not register an offense for arg named other' do
@@ -33,6 +33,14 @@ describe Rubocop::Cop::Style::OpMethod do
33
33
  expect(cop.offenses).to be_empty
34
34
  end
35
35
 
36
+ it 'does not register an offense for arg named _other' do
37
+ inspect_source(cop,
38
+ ['def <=>(_other)',
39
+ ' 0',
40
+ 'end'])
41
+ expect(cop.offenses).to be_empty
42
+ end
43
+
36
44
  it 'does not register an offense for []' do
37
45
  inspect_source(cop,
38
46
  ['def [](index)',
@@ -23,9 +23,9 @@ describe Rubocop::Cop::Style::ParenthesesAroundCondition, :config do
23
23
  ])
24
24
  expect(cop.offenses.size).to eq(9)
25
25
  expect(cop.messages.first)
26
- .to eq("Don't use parentheses around the condition of an if.")
26
+ .to eq("Don't use parentheses around the condition of an `if`.")
27
27
  expect(cop.messages.last)
28
- .to eq("Don't use parentheses around the condition of a while.")
28
+ .to eq("Don't use parentheses around the condition of a `while`.")
29
29
  end
30
30
 
31
31
  it 'accepts parentheses if there is no space between the keyword and (.' do
@@ -6,12 +6,13 @@ describe Rubocop::Cop::Style::PredicateName, :config do
6
6
  subject(:cop) { described_class.new(config) }
7
7
  let(:cop_config) { { 'NamePrefixBlacklist' => %w(has_ is_) } }
8
8
 
9
- %w(has_ is_).each do |prefix|
9
+ %w(has is).each do |prefix|
10
10
  it 'registers an offense for blacklisted method_name' do
11
11
  inspect_source(cop, ["def #{prefix}_attr",
12
12
  ' # ...',
13
13
  'end'])
14
14
  expect(cop.offenses.size).to eq(1)
15
+ expect(cop.messages).to eq(["Rename `#{prefix}_attr` to `attr?`."])
15
16
  expect(cop.highlights).to eq(["#{prefix}_attr"])
16
17
  end
17
18
  end
@@ -21,6 +21,8 @@ describe Rubocop::Cop::Style::RaiseArgs, :config do
21
21
  ' raise Ex.new(msg)',
22
22
  'end'])
23
23
  expect(cop.offenses.size).to eq(1)
24
+ expect(cop.messages)
25
+ .to eq(['Provide an exception object as an argument to `raise`.'])
24
26
  expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
25
27
  end
26
28
 
@@ -46,6 +48,9 @@ describe Rubocop::Cop::Style::RaiseArgs, :config do
46
48
  it 'reports an offense for a raise with exception object' do
47
49
  inspect_source(cop, ['raise Ex.new(msg)'])
48
50
  expect(cop.offenses.size).to eq(1)
51
+ expect(cop.messages)
52
+ .to eq(['Provide an exception class and message ' \
53
+ 'as arguments to `raise`.'])
49
54
  expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'compact')
50
55
  end
51
56
 
@@ -10,7 +10,7 @@ describe Rubocop::Cop::Style::RescueModifier do
10
10
  ['method rescue handle'])
11
11
  expect(cop.offenses.size).to eq(1)
12
12
  expect(cop.messages)
13
- .to eq(['Avoid using rescue in its modifier form.'])
13
+ .to eq(['Avoid using `rescue` in its modifier form.'])
14
14
  end
15
15
 
16
16
  it 'handles more complex expression with modifier rescue' do
@@ -18,7 +18,7 @@ describe Rubocop::Cop::Style::RescueModifier do
18
18
  ['method1 or method2 rescue handle'])
19
19
  expect(cop.offenses.size).to eq(1)
20
20
  expect(cop.messages)
21
- .to eq(['Avoid using rescue in its modifier form.'])
21
+ .to eq(['Avoid using `rescue` in its modifier form.'])
22
22
  end
23
23
 
24
24
  it 'handles modifier rescue in normal rescue' do
@@ -11,7 +11,7 @@ describe Rubocop::Cop::Style::SelfAssignment do
11
11
  ["#{var} = #{var} #{op} y"])
12
12
  expect(cop.offenses.size).to eq(1)
13
13
  expect(cop.messages)
14
- .to eq(["Use self-assignment shorthand #{op}=."])
14
+ .to eq(["Use self-assignment shorthand `#{op}=`."])
15
15
  end
16
16
 
17
17
  it "accepts shorthand assignment for #{op} and #{var}" do
@@ -19,7 +19,7 @@ describe Rubocop::Cop::Style::SelfAssignment do
19
19
  ["#{var} = #{var} #{op} y"])
20
20
  expect(cop.offenses.size).to eq(1)
21
21
  expect(cop.messages)
22
- .to eq(["Use self-assignment shorthand #{op}=."])
22
+ .to eq(["Use self-assignment shorthand `#{op}=`."])
23
23
  end
24
24
  end
25
25
 
@@ -29,7 +29,7 @@ describe Rubocop::Cop::Style::SelfAssignment do
29
29
  ["#{var} = #{var} #{op} y"])
30
30
  expect(cop.offenses.size).to eq(1)
31
31
  expect(cop.messages)
32
- .to eq(["Use self-assignment shorthand #{op}=."])
32
+ .to eq(["Use self-assignment shorthand `#{op}=`."])
33
33
  end
34
34
 
35
35
  it "accepts shorthand assignment for #{op} and #{var}" do
@@ -37,7 +37,7 @@ describe Rubocop::Cop::Style::SelfAssignment do
37
37
  ["#{var} = #{var} #{op} y"])
38
38
  expect(cop.offenses.size).to eq(1)
39
39
  expect(cop.messages)
40
- .to eq(["Use self-assignment shorthand #{op}=."])
40
+ .to eq(["Use self-assignment shorthand `#{op}=`."])
41
41
  end
42
42
  end
43
43
  end
@@ -54,6 +54,18 @@ describe Rubocop::Cop::Style::SignalException, :config do
54
54
  expect(cop.offenses).to be_empty
55
55
  end
56
56
 
57
+ it 'accepts raise in def with multiple rescues' do
58
+ inspect_source(cop,
59
+ ['def test',
60
+ ' fail',
61
+ 'rescue StandardError',
62
+ ' # handle error',
63
+ 'rescue Exception',
64
+ ' raise',
65
+ 'end'])
66
+ expect(cop.offenses).to be_empty
67
+ end
68
+
57
69
  it 'registers an offense for fail in def rescue section' do
58
70
  inspect_source(cop,
59
71
  ['def test',
@@ -66,6 +78,18 @@ describe Rubocop::Cop::Style::SignalException, :config do
66
78
  .to eq(['Use `raise` instead of `fail` to rethrow exceptions.'])
67
79
  end
68
80
 
81
+ it 'registers an offense for fail in second rescue' do
82
+ inspect_source(cop,
83
+ ['def test',
84
+ ' fail',
85
+ 'rescue StandardError',
86
+ ' # handle error',
87
+ 'rescue Exception',
88
+ ' fail',
89
+ 'end'])
90
+ expect(cop.offenses.size).to eq(1)
91
+ end
92
+
69
93
  it 'registers only offense for one raise that should be fail' do
70
94
  # This is a special case that has caused double reporting.
71
95
  inspect_source(cop,
@@ -24,6 +24,8 @@ describe Rubocop::Cop::Style::SingleLineBlockParams, :config do
24
24
  'end'])
25
25
  expect(cop.offenses.size).to eq(7)
26
26
  expect(cop.offenses.map(&:line).sort).to eq((2..8).to_a)
27
+ expect(cop.messages.first)
28
+ .to eq('Name `reduce` block params `|a, e|`.')
27
29
  end
28
30
 
29
31
  it 'allows calls with proper argument names' do
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::SingleSpaceBeforeFirstArg do
6
+ subject(:cop) { described_class.new }
7
+
8
+ context 'for method calls without parentheses' do
9
+ it 'registers an offense for method call with two spaces before the ' \
10
+ 'first arg' do
11
+ inspect_source(cop, ['something x',
12
+ 'a.something y, z'])
13
+ expect(cop.messages)
14
+ .to eq(['Put one space between the method name and the first ' \
15
+ 'argument.'] * 2)
16
+ expect(cop.highlights).to eq([' ', ' '])
17
+ end
18
+
19
+ it 'auto-corrects extra space' do
20
+ new_source = autocorrect_source(cop, ['something x',
21
+ 'a.something y, z'])
22
+ expect(new_source).to eq(['something x',
23
+ 'a.something y, z'].join("\n"))
24
+ end
25
+
26
+ it 'accepts a method call with one space before the first arg' do
27
+ inspect_source(cop, ['something x',
28
+ 'a.something y, z'])
29
+ expect(cop.offenses).to be_empty
30
+ end
31
+
32
+ it 'accepts + operator' do
33
+ inspect_source(cop, ['something +',
34
+ ' x'])
35
+ expect(cop.offenses).to be_empty
36
+ end
37
+
38
+ it 'accepts setter call' do
39
+ inspect_source(cop, ['something.x =',
40
+ ' y'])
41
+ expect(cop.offenses).to be_empty
42
+ end
43
+
44
+ it 'accepts multiple space containing line break' do
45
+ inspect_source(cop, ['something \\',
46
+ ' x'])
47
+ expect(cop.offenses).to be_empty
48
+ end
49
+ end
50
+
51
+ context 'for method calls with parentheses' do
52
+ it 'accepts a method call without space' do
53
+ inspect_source(cop, ['something(x)',
54
+ 'a.something(y, z)'])
55
+ expect(cop.offenses).to be_empty
56
+ end
57
+
58
+ it 'accepts a method call with space after the left parenthesis' do
59
+ inspect_source(cop, ['something( x )'])
60
+ expect(cop.offenses).to be_empty
61
+ end
62
+ end
63
+ end