rubocop 0.16.0 → 0.17.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 (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
@@ -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 if/unless.'])
75
+ ['Never use then for multi-line unless.'])
76
76
  end
77
77
 
78
78
  it 'accepts multiline unless without then' do
@@ -83,7 +83,7 @@ describe Rubocop::Cop::Style::MultilineIfThen do
83
83
 
84
84
  it 'does not get confused by a postfix unless' do
85
85
  inspect_source(cop,
86
- ['two unless one',
86
+ ['two unless one'
87
87
  ])
88
88
  expect(cop.offences).to be_empty
89
89
  end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Rubocop::Cop::Style::MultilineTernaryOperator do
6
+ subject(:cop) { described_class.new }
7
+
8
+ it 'registers offence for a multiline ternary operator expression' do
9
+ inspect_source(cop, ['a = cond ?',
10
+ ' b : c'])
11
+ expect(cop.offences.size).to eq(1)
12
+ end
13
+
14
+ it 'accepts a single line ternary operator expression' do
15
+ inspect_source(cop, ['a = cond ? b : c'])
16
+ expect(cop.offences).to be_empty
17
+ end
18
+ end
@@ -2,21 +2,6 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- describe Rubocop::Cop::Style::MultilineTernaryOperator do
6
- subject(:cop) { described_class.new }
7
-
8
- it 'registers offence for a multiline ternary operator expression' do
9
- inspect_source(cop, ['a = cond ?',
10
- ' b : c'])
11
- expect(cop.offences.size).to eq(1)
12
- end
13
-
14
- it 'accepts a single line ternary operator expression' do
15
- inspect_source(cop, ['a = cond ? b : c'])
16
- expect(cop.offences).to be_empty
17
- end
18
- end
19
-
20
5
  describe Rubocop::Cop::Style::NestedTernaryOperator do
21
6
  subject(:cop) { described_class.new }
22
7
 
@@ -9,11 +9,13 @@ describe Rubocop::Cop::Style::NumericLiterals, :config do
9
9
  it 'registers an offence for a long integer without underscores' do
10
10
  inspect_source(cop, ['a = 123456'])
11
11
  expect(cop.offences.size).to eq(1)
12
+ expect(cop.config_to_allow_offences).to eq('MinDigits' => 6)
12
13
  end
13
14
 
14
- it 'registers an offence for an integer with misplaced' do
15
+ it 'registers an offence for an integer with misplaced underscore' do
15
16
  inspect_source(cop, ['a = 123_456_78_90_00'])
16
17
  expect(cop.offences.size).to eq(1)
18
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
17
19
  end
18
20
 
19
21
  it 'accepts long numbers with underscore' do
@@ -44,4 +46,19 @@ describe Rubocop::Cop::Style::NumericLiterals, :config do
44
46
  'c = 0xab11111111bb'])
45
47
  expect(cop.offences).to be_empty
46
48
  end
49
+
50
+ it 'autocorrects a long integer offence' do
51
+ corrected = autocorrect_source(cop, ['a = 123456'])
52
+ expect(corrected).to eq 'a = 123_456'
53
+ end
54
+
55
+ it 'autocorrects an integer with misplaced underscore' do
56
+ corrected = autocorrect_source(cop, ['a = 123_456_78_90_00'])
57
+ expect(corrected).to eq 'a = 123_456_789_000'
58
+ end
59
+
60
+ it 'autocorrects negative numbers' do
61
+ corrected = autocorrect_source(cop, ['a = -123456'])
62
+ expect(corrected).to eq 'a = -123_456'
63
+ end
47
64
  end
@@ -7,6 +7,7 @@ describe Rubocop::Cop::Style::OneLineConditional do
7
7
 
8
8
  it 'registers an offence for one line if/then/end' do
9
9
  inspect_source(cop, ['if cond then run else dont end'])
10
- expect(cop.messages).to eq([cop.error_message])
10
+ expect(cop.messages).to eq(['Favor the ternary operator (?:)' \
11
+ ' over if/then/else/end constructs.'])
11
12
  end
12
13
  end
@@ -15,6 +15,7 @@ describe Rubocop::Cop::Style::ParameterLists, :config do
15
15
  inspect_source(cop, ['def meth(a, b, c, d, e)',
16
16
  'end'])
17
17
  expect(cop.offences.size).to eq(1)
18
+ expect(cop.config_to_allow_offences).to eq('Max' => 5)
18
19
  end
19
20
 
20
21
  it 'accepts a method def with 4 parameters' do
@@ -18,10 +18,14 @@ describe Rubocop::Cop::Style::ParenthesesAroundCondition, :config do
18
18
  'end',
19
19
  'x += 1 if (x < 10)',
20
20
  'x += 1 unless (x < 10)',
21
- 'x += 1 while (x < 10)',
22
21
  'x += 1 until (x < 10)',
22
+ 'x += 1 while (x < 10)'
23
23
  ])
24
24
  expect(cop.offences.size).to eq(9)
25
+ expect(cop.messages.first)
26
+ .to eq("Don't use parentheses around the condition of an if.")
27
+ expect(cop.messages.last)
28
+ .to eq("Don't use parentheses around the condition of a while.")
25
29
  end
26
30
 
27
31
  it 'auto-corrects parentheses around condition' do
@@ -37,7 +41,7 @@ describe Rubocop::Cop::Style::ParenthesesAroundCondition, :config do
37
41
  'x += 1 if (x < 10)',
38
42
  'x += 1 unless (x < 10)',
39
43
  'x += 1 while (x < 10)',
40
- 'x += 1 until (x < 10)',
44
+ 'x += 1 until (x < 10)'
41
45
  ])
42
46
  expect(corrected).to eq ['if x > 10',
43
47
  'elsif x < 3',
@@ -51,7 +55,7 @@ describe Rubocop::Cop::Style::ParenthesesAroundCondition, :config do
51
55
  'x += 1 if x < 10',
52
56
  'x += 1 unless x < 10',
53
57
  'x += 1 while x < 10',
54
- 'x += 1 until x < 10',
58
+ 'x += 1 until x < 10'
55
59
  ].join("\n")
56
60
  end
57
61
 
@@ -67,11 +71,16 @@ describe Rubocop::Cop::Style::ParenthesesAroundCondition, :config do
67
71
  'x += 1 if x < 10',
68
72
  'x += 1 unless x < 10',
69
73
  'x += 1 while x < 10',
70
- 'x += 1 until x < 10',
74
+ 'x += 1 until x < 10'
71
75
  ])
72
76
  expect(cop.offences).to be_empty
73
77
  end
74
78
 
79
+ it 'accepts parentheses around condition in a ternary' do
80
+ inspect_source(cop, '(a == 0) ? b : a')
81
+ expect(cop.offences).to be_empty
82
+ end
83
+
75
84
  it 'is not confused by leading parenthesis in subexpression' do
76
85
  inspect_source(cop, ['(a > b) && other ? one : two'])
77
86
  expect(cop.offences).to be_empty
@@ -11,6 +11,17 @@ describe Rubocop::Cop::Style::RaiseArgs, :config do
11
11
  it 'reports an offence for a raise with 2 args' do
12
12
  inspect_source(cop, ['raise RuntimeError, msg'])
13
13
  expect(cop.offences.size).to eq(1)
14
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'exploded')
15
+ end
16
+
17
+ it 'reports an offence for correct + opposite' do
18
+ inspect_source(cop, ['if a',
19
+ ' raise RuntimeError, msg',
20
+ 'else',
21
+ ' raise Ex.new(msg)',
22
+ 'end'])
23
+ expect(cop.offences.size).to eq(1)
24
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
14
25
  end
15
26
 
16
27
  it 'reports an offence for a raise with 3 args' do
@@ -35,6 +46,17 @@ describe Rubocop::Cop::Style::RaiseArgs, :config do
35
46
  it 'reports an offence for a raise with exception object' do
36
47
  inspect_source(cop, ['raise Ex.new(msg)'])
37
48
  expect(cop.offences.size).to eq(1)
49
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'compact')
50
+ end
51
+
52
+ it 'reports an offence for opposite + correct' do
53
+ inspect_source(cop, ['if a',
54
+ ' raise RuntimeError, msg',
55
+ 'else',
56
+ ' raise Ex.new(msg)',
57
+ 'end'])
58
+ expect(cop.offences.size).to eq(1)
59
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
38
60
  end
39
61
 
40
62
  it 'accepts exception constructor with more than 1 argument' do
@@ -92,7 +92,7 @@ describe Rubocop::Cop::Style::RedundantSelf do
92
92
  it 'accepts a self receiver used to distinguish from blockarg' do
93
93
  src = ['def requested_specs(&groups)',
94
94
  ' some_method(self.groups)',
95
- 'end',
95
+ 'end'
96
96
  ]
97
97
  inspect_source(cop, src)
98
98
  expect(cop.offences).to be_empty
@@ -101,7 +101,7 @@ describe Rubocop::Cop::Style::RedundantSelf do
101
101
  it 'accepts a self receiver used to distinguish from argument' do
102
102
  src = ['def requested_specs(groups)',
103
103
  ' some_method(self.groups)',
104
- 'end',
104
+ 'end'
105
105
  ]
106
106
  inspect_source(cop, src)
107
107
  expect(cop.offences).to be_empty
@@ -110,7 +110,7 @@ describe Rubocop::Cop::Style::RedundantSelf do
110
110
  it 'accepts a self receiver used to distinguish from argument' do
111
111
  src = ['def requested_specs(final = true)',
112
112
  ' something if self.final != final',
113
- 'end',
113
+ 'end'
114
114
  ]
115
115
  inspect_source(cop, src)
116
116
  expect(cop.offences).to be_empty
@@ -123,7 +123,7 @@ describe Rubocop::Cop::Style::RedundantSelf do
123
123
  ' groups.map! { |g| g.to_sym }',
124
124
  ' specs_for(groups)',
125
125
  ' end',
126
- 'end',
126
+ 'end'
127
127
  ]
128
128
  inspect_source(cop, src)
129
129
  expect(cop.offences).to be_empty
@@ -14,6 +14,7 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
14
14
  expect(cop.messages)
15
15
  .to eq(['Use %r for regular expressions matching more ' +
16
16
  "than 1 '/' character."] * 2)
17
+ expect(cop.config_to_allow_offences).to eq('MaxSlashes' => 2)
17
18
  end
18
19
 
19
20
  it 'accepts zero or one slash in regexp' do
@@ -33,6 +34,7 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
33
34
  expect(cop.messages)
34
35
  .to eq(['Use %r for regular expressions matching more ' +
35
36
  "than 0 '/' characters."])
37
+ expect(cop.config_to_allow_offences).to eq('MaxSlashes' => 1)
36
38
  end
37
39
 
38
40
  it 'accepts zero slashes in regexp' do
@@ -45,6 +47,7 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
45
47
  expect(cop.messages)
46
48
  .to eq(['Use %r only for regular expressions matching more ' +
47
49
  "than 0 '/' characters."])
50
+ expect(cop.config_to_allow_offences).to eq('MaxSlashes' => 1)
48
51
  end
49
52
 
50
53
  it 'accepts regexp with one slash' do
@@ -67,6 +70,7 @@ describe Rubocop::Cop::Style::RegexpLiteral, :config do
67
70
  expect(cop.messages)
68
71
  .to eq(['Use %r only for regular expressions matching more ' +
69
72
  "than 1 '/' character."] * 2)
73
+ expect(cop.config_to_allow_offences).to eq('MaxSlashes' => 2)
70
74
  end
71
75
 
72
76
  it 'accepts regexp with two or more slashes' do
@@ -9,16 +9,24 @@ describe Rubocop::Cop::Style::SpaceAfterColon do
9
9
  # TODO: There is double reporting of the last colon (also from
10
10
  # SpaceAroundOperators).
11
11
  inspect_source(cop, ['x = w ? {a:3}:4'])
12
- expect(cop.messages).to eq(
13
- ['Space missing after colon.'] * 2)
12
+ expect(cop.messages).to eq(['Space missing after colon.'] * 2)
13
+ expect(cop.highlights).to eq([':'] * 2)
14
14
  end
15
15
 
16
- it 'allows the colons in symbols' do
16
+ it 'accepts colons in symbols' do
17
17
  inspect_source(cop, ['x = :a'])
18
18
  expect(cop.messages).to be_empty
19
19
  end
20
20
 
21
- it 'allows colons in strings' do
21
+ if RUBY_VERSION >= '2.1'
22
+ it 'accepts colons denoting required keyword argument' do
23
+ inspect_source(cop, ['def initialize(table:, nodes:)',
24
+ 'end'])
25
+ expect(cop.messages).to be_empty
26
+ end
27
+ end
28
+
29
+ it 'accepts colons in strings' do
22
30
  inspect_source(cop, ["str << ':'"])
23
31
  expect(cop.messages).to be_empty
24
32
  end
@@ -10,7 +10,7 @@ describe Rubocop::Cop::Style::SpaceAfterMethodName do
10
10
  ['def func (x)',
11
11
  ' a',
12
12
  'end'])
13
- expect(cop.offences).to have(1).item
13
+ expect(cop.offences.size).to eq(1)
14
14
  end
15
15
 
16
16
  it 'registers an offence for defs with space before the parenthesis' do
@@ -18,7 +18,7 @@ describe Rubocop::Cop::Style::SpaceAfterMethodName do
18
18
  ['def self.func (x)',
19
19
  ' a',
20
20
  'end'])
21
- expect(cop.offences).to have(1).item
21
+ expect(cop.offences.size).to eq(1)
22
22
  end
23
23
 
24
24
  it 'accepts a def without arguments' do
@@ -7,8 +7,10 @@ describe Rubocop::Cop::Style::SpaceAroundBlockBraces do
7
7
 
8
8
  subject(:cop) { described_class.new(config) }
9
9
  let(:config) do
10
+ merged = Rubocop::ConfigLoader
11
+ .default_configuration['SpaceAroundBlockBraces'].merge(cop_config)
10
12
  Rubocop::Config.new('Blocks' => { 'Enabled' => false },
11
- 'SpaceAroundBlockBraces' => cop_config)
13
+ 'SpaceAroundBlockBraces' => merged)
12
14
  end
13
15
  let(:cop_config) do
14
16
  {
@@ -96,6 +98,23 @@ describe Rubocop::Cop::Style::SpaceAroundBlockBraces do
96
98
  inspect_source(cop, ['each { puts}'])
97
99
  expect(cop.messages).to eq(['Space missing inside }.'])
98
100
  expect(cop.highlights).to eq(['}'])
101
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
102
+ end
103
+
104
+ it 'registers offences for both braces without inner space' do
105
+ inspect_source(cop, ['a {}',
106
+ 'b { }',
107
+ 'each {puts}'])
108
+ expect(cop.messages).to eq(['Space inside empty braces detected.',
109
+ 'Space missing inside {.',
110
+ 'Space missing inside }.'])
111
+ expect(cop.highlights).to eq([' ', 'p', '}'])
112
+
113
+ # Both correct and incorrect code has been found in relation to
114
+ # EnforcedStyleForEmptyBraces, but that doesn't matter. EnforcedStyle can
115
+ # be changed to get rid of the EnforcedStyle offences.
116
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' =>
117
+ 'no_space_inside_braces')
99
118
  end
100
119
 
101
120
  it 'auto-corrects missing space' do
@@ -190,6 +209,7 @@ describe Rubocop::Cop::Style::SpaceAroundBlockBraces do
190
209
  inspect_source(cop, ['each { puts}'])
191
210
  expect(cop.messages).to eq(['Space inside { detected.'])
192
211
  expect(cop.highlights).to eq([' '])
212
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
193
213
  end
194
214
 
195
215
  it 'registers an offence for right brace with inner space' do
@@ -198,6 +218,15 @@ describe Rubocop::Cop::Style::SpaceAroundBlockBraces do
198
218
  expect(cop.highlights).to eq([' '])
199
219
  end
200
220
 
221
+ it 'registers offences for both braces with inner space' do
222
+ inspect_source(cop, ['each { puts }'])
223
+ expect(cop.messages).to eq(['Space inside { detected.',
224
+ 'Space inside } detected.'])
225
+ expect(cop.highlights).to eq([' ', ' '])
226
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' =>
227
+ 'space_inside_braces')
228
+ end
229
+
201
230
  it 'registers an offence for left brace without outer space' do
202
231
  inspect_source(cop, ['each{puts}'])
203
232
  expect(cop.messages).to eq(['Space missing to the left of {.'])
@@ -281,7 +281,7 @@ describe Rubocop::Cop::Style::SpaceAroundOperators do
281
281
 
282
282
  it 'registers an offence for various assignments without space' do
283
283
  inspect_source(cop, ['x||=0', 'y&&=0', 'z*=2',
284
- '@a=0', 'a,b=0', 'A=0', 'x[3]=0', '$A=0'])
284
+ '@a=0', '@@a=0', 'a,b=0', 'A=0', 'x[3]=0', '$A=0'])
285
285
  expect(cop.messages)
286
286
  .to eq(["Surrounding space missing for operator '||='.",
287
287
  "Surrounding space missing for operator '&&='.",
@@ -290,6 +290,7 @@ describe Rubocop::Cop::Style::SpaceAroundOperators do
290
290
  "Surrounding space missing for operator '='.",
291
291
  "Surrounding space missing for operator '='.",
292
292
  "Surrounding space missing for operator '='.",
293
+ "Surrounding space missing for operator '='.",
293
294
  "Surrounding space missing for operator '='."])
294
295
  end
295
296
 
@@ -51,11 +51,20 @@ describe Rubocop::Cop::Style::SpaceInsideHashLiteralBraces, :config do
51
51
  it 'registers an offence for hashes with no spaces if so configured' do
52
52
  inspect_source(cop,
53
53
  ['h = {a: 1, b: 2}',
54
- 'h = {a => 1 }'])
54
+ 'h = {a => 1}'])
55
55
  expect(cop.messages).to eq(['Space inside { missing.',
56
56
  'Space inside } missing.',
57
- 'Space inside { missing.'])
58
- expect(cop.highlights).to eq(['{', '}', '{'])
57
+ 'Space inside { missing.',
58
+ 'Space inside } missing.'])
59
+ expect(cop.highlights).to eq(['{', '}', '{', '}'])
60
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'no_space')
61
+ end
62
+
63
+ it 'registers an offence for correct + opposite' do
64
+ inspect_source(cop,
65
+ ['h = { a: 1}'])
66
+ expect(cop.messages).to eq(['Space inside } missing.'])
67
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
59
68
  end
60
69
 
61
70
  it 'auto-corrects missing space' do
@@ -74,6 +83,14 @@ describe Rubocop::Cop::Style::SpaceInsideHashLiteralBraces, :config do
74
83
  expect(cop.messages).to eq(['Space inside { detected.',
75
84
  'Space inside } detected.'])
76
85
  expect(cop.highlights).to eq([' ', ' '])
86
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'space')
87
+ end
88
+
89
+ it 'registers an offence for opposite + correct' do
90
+ inspect_source(cop,
91
+ ['h = {a: 1 }'])
92
+ expect(cop.messages).to eq(['Space inside } detected.'])
93
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
77
94
  end
78
95
 
79
96
  it 'auto-corrects unwanted space' do
@@ -21,6 +21,17 @@ describe Rubocop::Cop::Style::StringLiterals, :config do
21
21
  expect(cop.messages)
22
22
  .to eq(["Prefer single-quoted strings when you don't need " +
23
23
  'string interpolation or special symbols.'] * 4)
24
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' =>
25
+ 'double_quotes')
26
+ end
27
+
28
+ it 'registers offence for correct + opposite' do
29
+ inspect_source(cop, ['s = "abc"',
30
+ "x = 'abc'"])
31
+ expect(cop.messages)
32
+ .to eq(["Prefer single-quoted strings when you don't need " +
33
+ 'string interpolation or special symbols.'])
34
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
24
35
  end
25
36
 
26
37
  it 'accepts single quotes' do
@@ -33,6 +44,11 @@ describe Rubocop::Cop::Style::StringLiterals, :config do
33
44
  expect(cop.offences).to be_empty
34
45
  end
35
46
 
47
+ it 'accepts % quotes' do
48
+ inspect_source(cop, ['a = %(x)'])
49
+ expect(cop.offences).to be_empty
50
+ end
51
+
36
52
  it 'accepts heredocs' do
37
53
  inspect_source(cop,
38
54
  ['execute <<-SQL',
@@ -116,6 +132,18 @@ describe Rubocop::Cop::Style::StringLiterals, :config do
116
132
  .to eq(['Prefer double-quoted strings unless you need ' +
117
133
  'single quotes to avoid extra backslashes for ' +
118
134
  'escaping.'])
135
+ expect(cop.config_to_allow_offences).to eq('EnforcedStyle' =>
136
+ 'single_quotes')
137
+ end
138
+
139
+ it 'registers offence for opposite + correct' do
140
+ inspect_source(cop, ['s = "abc"',
141
+ "x = 'abc'"])
142
+ expect(cop.messages)
143
+ .to eq(['Prefer double-quoted strings unless you need ' +
144
+ 'single quotes to avoid extra backslashes for ' +
145
+ 'escaping.'])
146
+ expect(cop.config_to_allow_offences).to eq('Enabled' => false)
119
147
  end
120
148
 
121
149
  it 'accepts double quotes' do
@@ -128,6 +156,11 @@ describe Rubocop::Cop::Style::StringLiterals, :config do
128
156
  expect(cop.offences).to be_empty
129
157
  end
130
158
 
159
+ it 'accepts % quotes' do
160
+ inspect_source(cop, ['a = %(x)'])
161
+ expect(cop.offences).to be_empty
162
+ end
163
+
131
164
  it 'accepts heredocs' do
132
165
  inspect_source(cop,
133
166
  ['execute <<-SQL',