rubocop 0.26.1 → 0.27.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 (155) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -0
  3. data/.rubocop_todo.yml +10 -6
  4. data/.travis.yml +2 -0
  5. data/CHANGELOG.md +30 -0
  6. data/README.md +9 -2
  7. data/assets/logo.png +0 -0
  8. data/assets/output.html.erb +68 -65
  9. data/config/default.yml +42 -7
  10. data/config/disabled.yml +5 -0
  11. data/config/enabled.yml +32 -7
  12. data/lib/rubocop.rb +10 -2
  13. data/lib/rubocop/comment_config.rb +11 -17
  14. data/lib/rubocop/config.rb +20 -16
  15. data/lib/rubocop/config_loader.rb +8 -12
  16. data/lib/rubocop/cop/cop.rb +13 -12
  17. data/lib/rubocop/cop/lint/block_alignment.rb +4 -6
  18. data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -2
  19. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  20. data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -3
  21. data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
  22. data/lib/rubocop/cop/metrics/abc_size.rb +27 -0
  23. data/lib/rubocop/cop/metrics/block_nesting.rb +2 -4
  24. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  25. data/lib/rubocop/cop/metrics/line_length.rb +2 -5
  26. data/lib/rubocop/cop/metrics/method_length.rb +2 -2
  27. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +24 -15
  28. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +15 -2
  29. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +63 -0
  30. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  31. data/lib/rubocop/cop/mixin/if_node.rb +3 -1
  32. data/lib/rubocop/cop/mixin/method_complexity.rb +3 -3
  33. data/lib/rubocop/cop/mixin/{on_method.rb → on_method_def.rb} +3 -3
  34. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -2
  35. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  36. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  37. data/lib/rubocop/cop/mixin/string_literals_help.rb +28 -0
  38. data/lib/rubocop/cop/rails/delegate.rb +2 -2
  39. data/lib/rubocop/cop/style/access_modifier_indentation.rb +2 -2
  40. data/lib/rubocop/cop/style/accessor_method_name.rb +2 -2
  41. data/lib/rubocop/cop/style/align_hash.rb +16 -12
  42. data/lib/rubocop/cop/style/align_parameters.rb +1 -1
  43. data/lib/rubocop/cop/style/and_or.rb +14 -6
  44. data/lib/rubocop/cop/style/array_join.rb +1 -1
  45. data/lib/rubocop/cop/style/block_comments.rb +16 -8
  46. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -30
  47. data/lib/rubocop/cop/style/case_indentation.rb +20 -12
  48. data/lib/rubocop/cop/style/collection_methods.rb +4 -4
  49. data/lib/rubocop/cop/style/colon_method_call.rb +9 -0
  50. data/lib/rubocop/cop/style/comment_annotation.rb +1 -1
  51. data/lib/rubocop/cop/style/comment_indentation.rb +22 -22
  52. data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -2
  53. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +1 -1
  54. data/lib/rubocop/cop/style/double_negation.rb +6 -1
  55. data/lib/rubocop/cop/style/else_alignment.rb +93 -0
  56. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -1
  57. data/lib/rubocop/cop/style/empty_lines.rb +1 -1
  58. data/lib/rubocop/cop/style/empty_lines_around_class_body.rb +34 -0
  59. data/lib/rubocop/cop/style/empty_lines_around_method_body.rb +37 -0
  60. data/lib/rubocop/cop/style/empty_lines_around_module_body.rb +30 -0
  61. data/lib/rubocop/cop/style/encoding.rb +1 -1
  62. data/lib/rubocop/cop/style/format_string.rb +4 -4
  63. data/lib/rubocop/cop/style/indent_array.rb +2 -2
  64. data/lib/rubocop/cop/style/indent_hash.rb +17 -12
  65. data/lib/rubocop/cop/style/indentation_width.rb +27 -19
  66. data/lib/rubocop/cop/style/method_call_parentheses.rb +3 -3
  67. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +1 -1
  68. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -11
  69. data/lib/rubocop/cop/style/method_name.rb +1 -1
  70. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +174 -0
  71. data/lib/rubocop/cop/style/non_nil_check.rb +12 -15
  72. data/lib/rubocop/cop/style/not.rb +1 -1
  73. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +12 -17
  74. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  75. data/lib/rubocop/cop/style/predicate_name.rb +2 -2
  76. data/lib/rubocop/cop/style/redundant_begin.rb +2 -2
  77. data/lib/rubocop/cop/style/redundant_return.rb +3 -3
  78. data/lib/rubocop/cop/style/redundant_self.rb +3 -3
  79. data/lib/rubocop/cop/style/regexp_literal.rb +17 -13
  80. data/lib/rubocop/cop/style/rescue_modifier.rb +2 -2
  81. data/lib/rubocop/cop/style/single_line_methods.rb +7 -4
  82. data/lib/rubocop/cop/style/space_after_method_name.rb +2 -2
  83. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +17 -11
  84. data/lib/rubocop/cop/style/space_before_block_braces.rb +1 -1
  85. data/lib/rubocop/cop/style/space_inside_block_braces.rb +17 -14
  86. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +10 -6
  87. data/lib/rubocop/cop/style/string_literals.rb +13 -16
  88. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +41 -0
  89. data/lib/rubocop/cop/style/trailing_comma.rb +1 -3
  90. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -3
  91. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  92. data/lib/rubocop/cop/style/unneeded_percent_q.rb +2 -2
  93. data/lib/rubocop/cop/style/word_array.rb +23 -19
  94. data/lib/rubocop/cop/team.rb +13 -26
  95. data/lib/rubocop/cop/util.rb +5 -0
  96. data/lib/rubocop/cop/variable_force/locatable.rb +7 -13
  97. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  98. data/lib/rubocop/formatter/formatter_set.rb +9 -1
  99. data/lib/rubocop/formatter/html_formatter.rb +83 -55
  100. data/lib/rubocop/formatter/simple_text_formatter.rb +2 -2
  101. data/lib/rubocop/formatter/text_util.rb +25 -0
  102. data/lib/rubocop/options.rb +14 -7
  103. data/lib/rubocop/path_util.rb +11 -7
  104. data/lib/rubocop/runner.rb +7 -2
  105. data/lib/rubocop/version.rb +1 -1
  106. data/relnotes/v0.27.0.md +77 -0
  107. data/rubocop.gemspec +1 -1
  108. data/spec/fixtures/html_formatter/expected.html +495 -0
  109. data/spec/fixtures/html_formatter/project/app/controllers/application_controller.rb +5 -0
  110. data/spec/fixtures/html_formatter/project/app/controllers/books_controller.rb +74 -0
  111. data/spec/fixtures/html_formatter/project/app/models/book.rb +5 -0
  112. data/spec/rubocop/cli_spec.rb +56 -13
  113. data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +1 -1
  114. data/spec/rubocop/cop/metrics/abc_size_spec.rb +99 -0
  115. data/spec/rubocop/cop/rails/action_filter_spec.rb +1 -0
  116. data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +23 -1
  117. data/spec/rubocop/cop/style/align_hash_spec.rb +13 -0
  118. data/spec/rubocop/cop/style/align_parameters_spec.rb +44 -33
  119. data/spec/rubocop/cop/style/blocks_spec.rb +8 -0
  120. data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +9 -9
  121. data/spec/rubocop/cop/style/case_indentation_spec.rb +3 -2
  122. data/spec/rubocop/cop/style/colon_method_call_spec.rb +5 -0
  123. data/spec/rubocop/cop/style/comment_indentation_spec.rb +6 -1
  124. data/spec/rubocop/cop/style/else_alignment_spec.rb +437 -0
  125. data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +75 -0
  126. data/spec/rubocop/cop/style/{empty_lines_around_body_spec.rb → empty_lines_around_method_body_spec.rb} +9 -50
  127. data/spec/rubocop/cop/style/empty_lines_around_module_body_spec.rb +79 -0
  128. data/spec/rubocop/cop/style/file_name_spec.rb +1 -1
  129. data/spec/rubocop/cop/style/format_string_spec.rb +12 -0
  130. data/spec/rubocop/cop/style/indent_array_spec.rb +6 -1
  131. data/spec/rubocop/cop/style/indent_hash_spec.rb +2 -1
  132. data/spec/rubocop/cop/style/indentation_width_spec.rb +765 -722
  133. data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +414 -0
  134. data/spec/rubocop/cop/style/non_nil_check_spec.rb +86 -55
  135. data/spec/rubocop/cop/style/single_line_methods_spec.rb +5 -1
  136. data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +2 -1
  137. data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +2 -1
  138. data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +63 -0
  139. data/spec/rubocop/cop/style/string_literals_spec.rb +2 -2
  140. data/spec/rubocop/cop/style/word_array_spec.rb +15 -1
  141. data/spec/rubocop/formatter/base_formatter_spec.rb +1 -1
  142. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +0 -1
  143. data/spec/rubocop/formatter/formatter_set_spec.rb +9 -0
  144. data/spec/rubocop/formatter/html_formatter_spec.rb +25 -122
  145. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +0 -1
  146. data/spec/rubocop/runner_spec.rb +1 -1
  147. data/spec/spec_helper.rb +12 -130
  148. data/spec/support/cop_helper.rb +72 -0
  149. data/spec/support/coverage.rb +15 -0
  150. data/spec/support/{offenses_matcher.rb → custom_matchers.rb} +28 -0
  151. data/spec/support/jruby_workaround.rb +15 -0
  152. data/spec/support/{isolated_environment.rb → shared_contexts.rb} +19 -0
  153. metadata +49 -14
  154. data/lib/rubocop/cop/style/empty_lines_around_body.rb +0 -75
  155. data/spec/support/shared_context.rb +0 -20
@@ -24,40 +24,37 @@ module RuboCop
24
24
  # !current_user.nil?
25
25
  # end
26
26
  class NonNilCheck < Cop
27
- MSG = 'Explicit non-nil checks are usually redundant.'
27
+ include OnMethodDef
28
28
 
29
29
  NIL_NODE = s(:nil)
30
30
 
31
- def on_def(node)
32
- method_name, _args, body = *node
33
- process_method(method_name, body)
34
- end
35
-
36
- def on_defs(node)
37
- _scope, method_name, _args, body = *node
38
- process_method(method_name, body)
39
- end
40
-
41
31
  def on_send(node)
42
32
  return if ignored_node?(node)
43
33
  receiver, method, args = *node
44
34
 
45
- return unless [:!=, :!].include?(method)
46
-
47
35
  if method == :!=
48
36
  add_offense(node, :selector) if args == NIL_NODE
49
- elsif include_semantic_changes? && method == :!
37
+ elsif method == :! && include_semantic_changes?
50
38
  add_offense(node, :expression) if nil_check?(receiver)
51
39
  end
52
40
  end
53
41
 
54
42
  private
55
43
 
44
+ def message(node)
45
+ _receiver, method, _args = *node
46
+ if method == :!=
47
+ 'Prefer `!expression.nil?` over `expression != nil`.'
48
+ else
49
+ 'Explicit non-nil checks are usually redundant.'
50
+ end
51
+ end
52
+
56
53
  def include_semantic_changes?
57
54
  cop_config['IncludeSemanticChanges']
58
55
  end
59
56
 
60
- def process_method(name, body)
57
+ def on_method_def(_node, name, _args, body)
61
58
  # only predicate methods are handled differently
62
59
  return unless name.to_s.end_with?('?')
63
60
  return unless body
@@ -14,7 +14,7 @@ module RuboCop
14
14
 
15
15
  # not does not take any arguments
16
16
  return unless args.empty? && method_name == :! &&
17
- node.loc.selector.is?('not')
17
+ node.loc.selector.is?('not')
18
18
 
19
19
  add_offense(node, :selector)
20
20
  end
@@ -56,17 +56,14 @@ module RuboCop
56
56
  closing_indentation + closing_delimiter + reg_opt
57
57
 
58
58
  @corrections << lambda do |corrector|
59
- corrector.replace(
60
- node.loc.expression,
61
- corrected_source
62
- )
59
+ corrector.replace(node.loc.expression, corrected_source)
63
60
  end
64
61
  end
65
62
 
66
63
  def on_percent_literal(node)
67
64
  type = type(node)
68
65
  return if uses_preferred_delimiter?(node, type) ||
69
- contains_preferred_delimiter?(node, type)
66
+ contains_preferred_delimiter?(node, type)
70
67
 
71
68
  add_offense(node, :expression)
72
69
  end
@@ -94,20 +91,18 @@ module RuboCop
94
91
  if node.type == :regexp
95
92
  *_, next_to_last_child = *middle
96
93
  next_to_last_child ||= first_child
97
- [
98
- source(node, first_child, next_to_last_child),
99
- last_child.loc.expression.source
100
- ]
94
+ expression = source(node, first_child, next_to_last_child)
95
+ reg_opt = last_child.loc.expression.source
101
96
  else
102
- [
103
- if first_child.is_a?(Parser::AST::Node)
104
- source(node, first_child, last_child)
105
- else
106
- first_child.to_s
107
- end,
108
- ''
109
- ]
97
+ expression = if first_child.is_a?(Parser::AST::Node)
98
+ source(node, first_child, last_child)
99
+ else
100
+ first_child.to_s
101
+ end
102
+ reg_opt = ''
110
103
  end
104
+
105
+ [expression, reg_opt]
111
106
  end
112
107
 
113
108
  def source(node, begin_node, end_node)
@@ -31,7 +31,7 @@ module RuboCop
31
31
  # Report offense only if changing case doesn't change semantics,
32
32
  # i.e., if the string would become dynamic or has special characters.
33
33
  return if node.children !=
34
- ProcessedSource.new(corrected(src)).ast.children
34
+ ProcessedSource.new(corrected(src)).ast.children
35
35
 
36
36
  add_offense(node, :begin, msg)
37
37
  end
@@ -18,11 +18,11 @@ module RuboCop
18
18
  # # good
19
19
  # def value? ...
20
20
  class PredicateName < Cop
21
- include OnMethod
21
+ include OnMethodDef
22
22
 
23
23
  private
24
24
 
25
- def on_method(node, method_name, _args, _body)
25
+ def on_method_def(node, method_name, _args, _body)
26
26
  predicate_prefices.each do |prefix|
27
27
  method_name = method_name.to_s
28
28
  next unless method_name.start_with?(prefix)
@@ -25,11 +25,11 @@ module RuboCop
25
25
  # something
26
26
  # end
27
27
  class RedundantBegin < Cop
28
- include OnMethod
28
+ include OnMethodDef
29
29
 
30
30
  MSG = 'Redundant `begin` block detected.'
31
31
 
32
- def on_method(_node, _method_name, _args, body)
32
+ def on_method_def(_node, _method_name, _args, body)
33
33
  return unless body && body.type == :kwbegin
34
34
 
35
35
  add_offense(body, :begin)
@@ -21,7 +21,7 @@ module RuboCop
21
21
  # It should be extended to handle methods whose body is if/else
22
22
  # or a case expression with a default branch.
23
23
  class RedundantReturn < Cop
24
- include OnMethod
24
+ include OnMethodDef
25
25
 
26
26
  MSG = 'Redundant `return` detected.'
27
27
 
@@ -39,7 +39,7 @@ module RuboCop
39
39
  end
40
40
  end
41
41
 
42
- def on_method(_node, _method_name, _args, body)
42
+ def on_method_def(_node, _method_name, _args, body)
43
43
  return unless body
44
44
 
45
45
  if body.type == :return
@@ -56,7 +56,7 @@ module RuboCop
56
56
 
57
57
  def check_return_node(node)
58
58
  return if cop_config['AllowMultipleReturnValues'] &&
59
- node.children.size > 1
59
+ node.children.size > 1
60
60
 
61
61
  add_offense(node, :keyword)
62
62
  end
@@ -94,9 +94,9 @@ module RuboCop
94
94
  receiver, method_name, *_args = *node
95
95
  return unless receiver && receiver.type == :self
96
96
  return if operator?(method_name) || keyword?(method_name) ||
97
- constant_name?(method_name) ||
98
- @allowed_send_nodes.include?(node) ||
99
- @local_variables.include?(method_name)
97
+ constant_name?(method_name) ||
98
+ @allowed_send_nodes.include?(node) ||
99
+ @local_variables.include?(method_name)
100
100
 
101
101
  add_offense(node, :expression)
102
102
  end
@@ -53,19 +53,23 @@ module RuboCop
53
53
  max = self.class.slash_count['/'].max
54
54
  min = self.class.slash_count['%'].min
55
55
 
56
- self.config_to_allow_offenses = if max > max_slashes
57
- if max < min
58
- { 'MaxSlashes' => max }
59
- else
60
- { 'Enabled' => false }
61
- end
62
- elsif min < max_slashes + 1
63
- if max < min
64
- { 'MaxSlashes' => min - 1 }
65
- else
66
- { 'Enabled' => false }
67
- end
68
- end
56
+ self.config_to_allow_offenses = calculate_config(max, min)
57
+ end
58
+
59
+ def calculate_config(max, min)
60
+ if max > max_slashes
61
+ if max < min
62
+ { 'MaxSlashes' => max }
63
+ else
64
+ { 'Enabled' => false }
65
+ end
66
+ elsif min < max_slashes + 1
67
+ if max < min
68
+ { 'MaxSlashes' => min - 1 }
69
+ else
70
+ { 'Enabled' => false }
71
+ end
72
+ end
69
73
  end
70
74
 
71
75
  def error_message(word)
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Style
6
6
  # This cop checks for uses of rescue in its modifier form.
7
7
  class RescueModifier < Cop
8
- include OnMethod
8
+ include OnMethodDef
9
9
 
10
10
  MSG = 'Avoid using `rescue` in its modifier form.'
11
11
 
@@ -20,7 +20,7 @@ module RuboCop
20
20
  check(body)
21
21
  end
22
22
 
23
- def on_method(_node, _method_name, _args, body)
23
+ def on_method_def(_node, _method_name, _args, body)
24
24
  check(body)
25
25
  end
26
26
 
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # This cop checks for single-line method definitions.
7
7
  # It can optionally accept single-line methods with no body.
8
8
  class SingleLineMethods < Cop
9
- include OnMethod
9
+ include OnMethodDef
10
10
 
11
11
  MSG = 'Avoid single-line method definitions.'
12
12
 
@@ -16,7 +16,7 @@ module RuboCop
16
16
 
17
17
  private
18
18
 
19
- def on_method(node, _method_name, _args, body)
19
+ def on_method_def(node, _method_name, _args, body)
20
20
  start_line = node.loc.keyword.line
21
21
  end_line = node.loc.end.line
22
22
 
@@ -51,11 +51,14 @@ module RuboCop
51
51
  corrector.insert_before(
52
52
  range,
53
53
  "\n" + ' ' * (node.loc.keyword.column +
54
- indent_steps *
55
- IndentationWidth::CORRECT_INDENTATION)
54
+ indent_steps * configured_indentation_width)
56
55
  )
57
56
  end
58
57
 
58
+ def configured_indentation_width
59
+ config.for_cop('IndentationWidth')['Width']
60
+ end
61
+
59
62
  def move_comment(eol_comment, node, corrector)
60
63
  text = eol_comment.loc.expression.source
61
64
  corrector.insert_before(node.loc.expression,
@@ -13,12 +13,12 @@ module RuboCop
13
13
  # # good
14
14
  # def func(x) ... end
15
15
  class SpaceAfterMethodName < Cop
16
- include OnMethod
16
+ include OnMethodDef
17
17
 
18
18
  MSG = 'Never put a space between a method name and the opening ' \
19
19
  'parenthesis.'
20
20
 
21
- def on_method(_node, _method_name, args, _body)
21
+ def on_method_def(_node, _method_name, args, _body)
22
22
  return unless args.loc.begin && args.loc.begin.is?('(')
23
23
  expr = args.loc.expression
24
24
  pos_before_left_paren = Parser::Source::Range.new(expr.source_buffer,
@@ -22,19 +22,25 @@ module RuboCop
22
22
  no_surrounding_space = no_surrounding_space?(arg, equals, value)
23
23
 
24
24
  if style == :space && space_on_both_sides ||
25
- style == :no_space && no_surrounding_space
25
+ style == :no_space && no_surrounding_space
26
26
  correct_style_detected
27
27
  else
28
- range = Parser::Source::Range.new(processed_source.buffer,
29
- arg.pos.end_pos,
30
- value.pos.begin_pos)
31
- add_offense(range, range) do
32
- if style == :space && no_surrounding_space ||
33
- style == :no_space && space_on_both_sides
34
- opposite_style_detected
35
- else
36
- unrecognized_style_detected
37
- end
28
+ incorrect_style_detected(arg, value, space_on_both_sides,
29
+ no_surrounding_space)
30
+ end
31
+ end
32
+
33
+ def incorrect_style_detected(arg, value, space_on_both_sides,
34
+ no_surrounding_space)
35
+ range = Parser::Source::Range.new(processed_source.buffer,
36
+ arg.pos.end_pos,
37
+ value.pos.begin_pos)
38
+ add_offense(range, range) do
39
+ if style == :space && no_surrounding_space ||
40
+ style == :no_space && space_on_both_sides
41
+ opposite_style_detected
42
+ else
43
+ unrecognized_style_detected
38
44
  end
39
45
  end
40
46
  end
@@ -16,7 +16,7 @@ module RuboCop
16
16
  # auto-correct, so reporting space issues is not useful, and it
17
17
  # creates auto-correct conflicts.
18
18
  if config.for_cop('Style/Blocks')['Enabled'] &&
19
- Util.block_length(node) > 0
19
+ Util.block_length(node) > 0
20
20
  return
21
21
  end
22
22
 
@@ -19,7 +19,7 @@ module RuboCop
19
19
  # auto-correct, so reporting space issues is not useful, and it
20
20
  # creates auto-correct conflicts.
21
21
  if config.for_cop('Style/Blocks')['Enabled'] &&
22
- Util.block_length(node) > 0
22
+ Util.block_length(node) > 0
23
23
  return
24
24
  end
25
25
 
@@ -34,25 +34,28 @@ module RuboCop
34
34
  sb = node.loc.expression.source_buffer
35
35
 
36
36
  if left_brace.end_pos == right_brace.begin_pos
37
- if style_for_empty_braces == :space
38
- offense(sb, left_brace.begin_pos, right_brace.end_pos,
39
- 'Space missing inside empty braces.')
40
- end
41
- else
37
+ adjacent_braces(sb, left_brace, right_brace)
38
+ elsif left_brace.line == right_brace.line
42
39
  range = Parser::Source::Range.new(sb, left_brace.end_pos,
43
40
  right_brace.begin_pos)
44
41
  inner = range.source
45
- unless inner =~ /\n/
46
- if inner =~ /\S/
47
- braces_with_contents_inside(node, inner, sb)
48
- elsif style_for_empty_braces == :no_space
49
- offense(sb, range.begin_pos, range.end_pos,
50
- 'Space inside empty braces detected.')
51
- end
42
+
43
+ if inner =~ /\S/
44
+ braces_with_contents_inside(node, inner, sb)
45
+ elsif style_for_empty_braces == :no_space
46
+ offense(sb, range.begin_pos, range.end_pos,
47
+ 'Space inside empty braces detected.')
52
48
  end
53
49
  end
54
50
  end
55
51
 
52
+ def adjacent_braces(sb, left_brace, right_brace)
53
+ return if style_for_empty_braces != :space
54
+
55
+ offense(sb, left_brace.begin_pos, right_brace.end_pos,
56
+ 'Space missing inside empty braces.')
57
+ end
58
+
56
59
  def braces_with_contents_inside(node, inner, sb)
57
60
  _method, args, _body = *node
58
61
  left_brace, right_brace = node.loc.begin, node.loc.end
@@ -75,7 +78,7 @@ module RuboCop
75
78
  def no_space_inside_left_brace(left_brace, args_delimiter, sb)
76
79
  if pipe?(args_delimiter)
77
80
  if left_brace.end_pos == args_delimiter.begin_pos &&
78
- cop_config['SpaceBeforeBlockParameters']
81
+ cop_config['SpaceBeforeBlockParameters']
79
82
  offense(sb, left_brace.begin_pos, args_delimiter.end_pos,
80
83
  'Space between { and | missing.')
81
84
  end
@@ -37,17 +37,21 @@ module RuboCop
37
37
  style == :space
38
38
  end
39
39
  if offense?(t1, t2, expect_space)
40
- brace = (t1.text == '{' ? t1 : t2).pos
41
- range = expect_space ? brace : space_range(brace)
42
- add_offense(range, range, message(brace, is_empty_braces,
43
- expect_space)) do
44
- opposite_style_detected
45
- end
40
+ incorrect_style_detected(t1, t2, expect_space, is_empty_braces)
46
41
  else
47
42
  correct_style_detected
48
43
  end
49
44
  end
50
45
 
46
+ def incorrect_style_detected(t1, t2, expect_space, is_empty_braces)
47
+ brace = (t1.text == '{' ? t1 : t2).pos
48
+ range = expect_space ? brace : space_range(brace)
49
+ add_offense(range, range,
50
+ message(brace, is_empty_braces, expect_space)) do
51
+ opposite_style_detected
52
+ end
53
+ end
54
+
51
55
  def offense?(t1, t2, expect_space)
52
56
  has_space = space_between?(t1, t2)
53
57
  expect_space ? !has_space : has_space
@@ -6,7 +6,18 @@ module RuboCop
6
6
  # Checks if uses of quotes match the configured preference.
7
7
  class StringLiterals < Cop
8
8
  include ConfigurableEnforcedStyle
9
- include StringHelp
9
+ include StringLiteralsHelp
10
+
11
+ def on_dstr(node)
12
+ # A dstr node with dstr and str children is a concatenated
13
+ # string. Don't ignore the whole thing.
14
+ return if node.children.find { |child| child.type == :str }
15
+
16
+ # Dynamic strings can not use single quotes, and quotes inside
17
+ # interpolation expressions are checked by the
18
+ # StringLiteralsInInterpolation cop, so ignore.
19
+ ignore_node(node)
20
+ end
10
21
 
11
22
  private
12
23
 
@@ -21,21 +32,7 @@ module RuboCop
21
32
  end
22
33
 
23
34
  def offense?(node)
24
- src = node.loc.expression.source
25
- return false if src =~ /^(%[qQ]?|\?|<<-)/i
26
- if style == :single_quotes
27
- src !~ /'/ && src !~ StringHelp::ESCAPED_CHAR_REGEXP
28
- else
29
- src !~ /" | \\/x
30
- end
31
- end
32
-
33
- def autocorrect(node)
34
- @corrections << lambda do |corrector|
35
- replacement = node.loc.begin.is?('"') ? "'" : '"'
36
- corrector.replace(node.loc.begin, replacement)
37
- corrector.replace(node.loc.end, replacement)
38
- end
35
+ wrong_quotes?(node, style)
39
36
  end
40
37
  end
41
38
  end