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
@@ -10,6 +10,7 @@ module RuboCop
10
10
  # The maximum level of nesting allowed is configurable.
11
11
  class BlockNesting < Cop
12
12
  include ConfigurableMax
13
+ include IfNode
13
14
 
14
15
  NESTING_BLOCKS = [
15
16
  :case, :if, :while, :while_post,
@@ -26,10 +27,7 @@ module RuboCop
26
27
 
27
28
  def check_nesting_level(node, max, current_level)
28
29
  if NESTING_BLOCKS.include?(node.type)
29
- unless node.loc.respond_to?(:keyword) &&
30
- node.loc.keyword.is?('elsif')
31
- current_level += 1
32
- end
30
+ current_level += 1 unless elsif?(node)
33
31
  if current_level > max
34
32
  self.max = current_level
35
33
  unless part_of_ignored_node?(node)
@@ -23,7 +23,7 @@ module RuboCop
23
23
  class_body_line_numbers = line_range(node).to_a[1...-1]
24
24
 
25
25
  target_line_numbers = class_body_line_numbers -
26
- line_numbers_of_inner_classes(node)
26
+ line_numbers_of_inner_classes(node)
27
27
 
28
28
  target_line_numbers.reduce(0) do |length, line_number|
29
29
  source_line = processed_source[line_number]
@@ -29,13 +29,10 @@ module RuboCop
29
29
  max
30
30
  end
31
31
 
32
- range = source_range(processed_source.buffer,
33
- index + 1,
32
+ range = source_range(processed_source.buffer, index + 1,
34
33
  excessive_position...(line.length))
35
34
 
36
- add_offense(nil, range, message) do
37
- self.max = line.length
38
- end
35
+ add_offense(nil, range, message) { self.max = line.length }
39
36
  end
40
37
  end
41
38
 
@@ -7,12 +7,12 @@ module RuboCop
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
9
  class MethodLength < Cop
10
- include OnMethod
10
+ include OnMethodDef
11
11
  include CodeLength
12
12
 
13
13
  private
14
14
 
15
- def on_method(node, _method_name, _args, _body)
15
+ def on_method_def(node, _method_name, _args, _body)
16
16
  check_code_length(node)
17
17
  end
18
18
 
@@ -6,6 +6,10 @@ module RuboCop
6
6
  # the left or to the right, amount being determined by the instance
7
7
  # variable @column_delta.
8
8
  module AutocorrectAlignment
9
+ def configured_indentation_width
10
+ config.for_cop('IndentationWidth')['Width']
11
+ end
12
+
9
13
  def check_alignment(items, base_column = nil)
10
14
  base_column ||= items.first.loc.column unless items.empty?
11
15
  prev_line = -1
@@ -26,33 +30,38 @@ module RuboCop
26
30
  heredoc_ranges = heredoc_ranges(arg)
27
31
  expr = arg.respond_to?(:loc) ? arg.loc.expression : arg
28
32
 
29
- # We can't use the instance variable inside the lambda. That would
30
- # just give each lambda the same reference and they would all get
31
- # the last value of @column_delta. A local variable fixes the
32
- # problem.
33
+ # We can't use the instance variable inside the lambda. That would just
34
+ # give each lambda the same reference and they would all get the last
35
+ # value of @column_delta. A local variable fixes the problem.
33
36
  column_delta = @column_delta
34
37
 
35
38
  fail CorrectionNotPossible if block_comment_within?(expr)
36
39
 
37
40
  @corrections << lambda do |corrector|
38
41
  each_line(expr) do |line_begin_pos|
39
- range = calculate_range(expr, line_begin_pos, column_delta)
40
- # We must not change indentation of heredoc stings.
41
- next if heredoc_ranges.any? { |h| within?(range, h) }
42
-
43
- if column_delta > 0
44
- unless range.source == "\n"
45
- corrector.insert_before(range, ' ' * column_delta)
46
- end
47
- else
48
- remove(range, corrector) if range.source =~ /^[ \t]+$/
49
- end
42
+ autocorrect_line(corrector, line_begin_pos, expr, column_delta,
43
+ heredoc_ranges)
50
44
  end
51
45
  end
52
46
  end
53
47
 
54
48
  private
55
49
 
50
+ def autocorrect_line(corrector, line_begin_pos, expr, column_delta,
51
+ heredoc_ranges)
52
+ range = calculate_range(expr, line_begin_pos, column_delta)
53
+ # We must not change indentation of heredoc stings.
54
+ return if heredoc_ranges.any? { |h| within?(range, h) }
55
+
56
+ if column_delta > 0
57
+ unless range.source == "\n"
58
+ corrector.insert_before(range, ' ' * column_delta)
59
+ end
60
+ else
61
+ remove(range, corrector) if range.source =~ /^[ \t]+$/
62
+ end
63
+ end
64
+
56
65
  def heredoc_ranges(arg)
57
66
  return [] unless arg.is_a?(Parser::AST::Node)
58
67
 
@@ -7,7 +7,6 @@ module RuboCop
7
7
  # abstract syntax tree, it is not done.
8
8
  module AutocorrectUnlessChangingAST
9
9
  def autocorrect(node)
10
- c = correction(node)
11
10
  new_source = rewrite_node(node)
12
11
 
13
12
  # Make the correction only if it doesn't change the AST. Regenerate the
@@ -17,7 +16,11 @@ module RuboCop
17
16
  fail CorrectionNotPossible
18
17
  end
19
18
 
20
- @corrections << c
19
+ if syntax_error?(node.loc.expression, new_source)
20
+ fail CorrectionNotPossible
21
+ end
22
+
23
+ @corrections << correction(node)
21
24
  end
22
25
 
23
26
  private
@@ -31,6 +34,16 @@ module RuboCop
31
34
  c = correction(processed_source.ast)
32
35
  Corrector.new(processed_source.buffer, [c]).rewrite
33
36
  end
37
+
38
+ # Return true if the change would introduce a syntax error in the buffer
39
+ # source.
40
+ def syntax_error?(replaced_range, new_source)
41
+ current_buffer_src = processed_source.buffer.source
42
+ pre = current_buffer_src[0...replaced_range.begin_pos]
43
+ post = current_buffer_src[replaced_range.end_pos..-1]
44
+ new_buffer_src = pre + new_source + post
45
+ !ProcessedSource.new(new_buffer_src).valid_syntax?
46
+ end
34
47
  end
35
48
  end
36
49
  end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Common functionality for checking if presence/absence of empty lines
7
+ # around some kind of body matches the configuration.
8
+ module EmptyLinesAroundBody
9
+ include ConfigurableEnforcedStyle
10
+
11
+ MSG_EXTRA = 'Extra empty line detected at %s body %s.'
12
+ MSG_MISSING = 'Empty line missing at %s body %s.'
13
+
14
+ def autocorrect(range)
15
+ @corrections << lambda do |corrector|
16
+ case style
17
+ when :no_empty_lines then corrector.remove(range)
18
+ when :empty_lines then corrector.insert_before(range, "\n")
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def check(node)
26
+ start_line = node.loc.keyword.line
27
+ end_line = node.loc.end.line
28
+
29
+ return if start_line == end_line
30
+
31
+ check_source(start_line, end_line)
32
+ end
33
+
34
+ def check_source(start_line, end_line)
35
+ case style
36
+ when :no_empty_lines
37
+ check_both(start_line, end_line, MSG_EXTRA) do |line|
38
+ line.empty?
39
+ end
40
+ when :empty_lines
41
+ check_both(start_line, end_line, MSG_MISSING) do |line|
42
+ !line.empty?
43
+ end
44
+ end
45
+ end
46
+
47
+ def check_both(start_line, end_line, msg, &block)
48
+ kind = self.class::KIND
49
+ check_line(start_line, format(msg, kind, 'beginning'), &block)
50
+ check_line(end_line - 2, format(msg, kind, 'end'), &block)
51
+ end
52
+
53
+ def check_line(line, msg)
54
+ return unless yield processed_source.lines[line]
55
+
56
+ offset = style == :empty_lines && msg.include?('end.') ? 2 : 1
57
+ range = source_range(processed_source.buffer, line + offset, 0)
58
+ add_offense(range, range, msg)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -23,7 +23,7 @@ module RuboCop
23
23
  kw_loc = node.loc.keyword
24
24
 
25
25
  if kw_loc.line != end_loc.line &&
26
- kw_loc.column != end_loc.column + offset
26
+ kw_loc.column != end_loc.column + offset
27
27
  add_offense(nil, end_loc,
28
28
  format(MSG, end_loc.line, end_loc.column,
29
29
  alignment_base, kw_loc.line, kw_loc.column)) do
@@ -5,7 +5,9 @@ module RuboCop
5
5
  # Common functionality for checking if nodes.
6
6
  module IfNode
7
7
  def modifier_if?(node)
8
- node.loc.respond_to?(:end) && node.loc.end.nil?
8
+ node.loc.respond_to?(:keyword) &&
9
+ %w(if unless).include?(node.loc.keyword.source) &&
10
+ node.loc.respond_to?(:end) && node.loc.end.nil?
9
11
  end
10
12
 
11
13
  def ternary_op?(node)
@@ -4,19 +4,19 @@ module RuboCop
4
4
  module Cop
5
5
  # This module handles measurement and reporting of complexity in methods.
6
6
  module MethodComplexity
7
- include OnMethod
7
+ include OnMethodDef
8
8
  include ConfigurableMax
9
9
 
10
10
  private
11
11
 
12
- def on_method(node, method_name, _args, _body)
12
+ def on_method_def(node, method_name, _args, _body)
13
13
  max = cop_config['Max']
14
14
  complexity = complexity(node)
15
15
  return unless complexity > max
16
16
 
17
17
  add_offense(node, :keyword,
18
18
  format(self.class::MSG, method_name, complexity, max)) do
19
- self.max = complexity
19
+ self.max = complexity.ceil
20
20
  end
21
21
  end
22
22
 
@@ -3,15 +3,15 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  # Common functionality for checking instance methods and singeton methods.
6
- module OnMethod
6
+ module OnMethodDef
7
7
  def on_def(node)
8
8
  method_name, args, body = *node
9
- on_method(node, method_name, args, body)
9
+ on_method_def(node, method_name, args, body)
10
10
  end
11
11
 
12
12
  def on_defs(node)
13
13
  _scope, method_name, args, body = *node
14
- on_method(node, method_name, args, body)
14
+ on_method_def(node, method_name, args, body)
15
15
  end
16
16
 
17
17
  private
@@ -10,8 +10,8 @@ module RuboCop
10
10
  def investigate(processed_source)
11
11
  processed_source.tokens.each_cons(2) do |t1, t2|
12
12
  next unless kind(t1) && t1.pos.line == t2.pos.line &&
13
- t2.pos.column == t1.pos.column + offset &&
14
- ![:tRPAREN, :tRBRACK].include?(t2.type)
13
+ t2.pos.column == t1.pos.column + offset &&
14
+ ![:tRPAREN, :tRBRACK].include?(t2.type)
15
15
 
16
16
  add_offense(t1, t1.pos, format(MSG, kind(t1)))
17
17
  end
@@ -10,7 +10,7 @@ module RuboCop
10
10
  def investigate(processed_source)
11
11
  processed_source.tokens.each_cons(2) do |t1, t2|
12
12
  next unless kind(t2) && t1.pos.line == t2.pos.line &&
13
- t2.pos.begin_pos > t1.pos.end_pos
13
+ t2.pos.begin_pos > t1.pos.end_pos
14
14
  buffer = processed_source.buffer
15
15
  pos_before_punctuation = Parser::Source::Range.new(buffer,
16
16
  t1.pos.end_pos,
@@ -27,13 +27,13 @@ module RuboCop
27
27
  cond_length = cond.loc.expression.size
28
28
  space = 1
29
29
  total = indentation + body_length + space + kw_length + space +
30
- cond_length
30
+ cond_length
31
31
  total <= max_line_length && !body_has_comment?(body)
32
32
  end
33
33
 
34
34
  def max_line_length
35
35
  cop_config && cop_config['MaxLineLength'] ||
36
- config.for_cop('Metrics/LineLength')['Max']
36
+ config.for_cop('Metrics/LineLength')['Max']
37
37
  end
38
38
 
39
39
  def length(node)
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Common functionality for cops checking single/double quotes.
6
+ module StringLiteralsHelp
7
+ include StringHelp
8
+
9
+ def wrong_quotes?(node, style)
10
+ src = node.loc.expression.source
11
+ return false if src.start_with?('%') || src.start_with?('?')
12
+ if style == :single_quotes
13
+ src !~ /'/ && src !~ StringHelp::ESCAPED_CHAR_REGEXP
14
+ else
15
+ src !~ /" | \\/x
16
+ end
17
+ end
18
+
19
+ def autocorrect(node)
20
+ @corrections << lambda do |corrector|
21
+ replacement = node.loc.begin.is?('"') ? "'" : '"'
22
+ corrector.replace(node.loc.begin, replacement)
23
+ corrector.replace(node.loc.end, replacement)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -29,13 +29,13 @@ module RuboCop
29
29
  # foo.bar
30
30
  # end
31
31
  class Delegate < Cop
32
- include OnMethod
32
+ include OnMethodDef
33
33
 
34
34
  MSG = 'Use `delegate` to define delegations.'
35
35
 
36
36
  private
37
37
 
38
- def on_method(node, method_name, args, body)
38
+ def on_method_def(node, method_name, args, body)
39
39
  return unless trivial_delegate?(method_name, args, body)
40
40
  return if private_or_protected_delegation(node)
41
41
  add_offense(node, :keyword, MSG)
@@ -73,13 +73,13 @@ module RuboCop
73
73
  end
74
74
 
75
75
  def expected_indent_offset
76
- style == :outdent ? 0 : IndentationWidth::CORRECT_INDENTATION
76
+ style == :outdent ? 0 : configured_indentation_width
77
77
  end
78
78
 
79
79
  # An offset that is not expected, but correct if the configuration is
80
80
  # changed.
81
81
  def unexpected_indent_offset
82
- IndentationWidth::CORRECT_INDENTATION - expected_indent_offset
82
+ configured_indentation_width - expected_indent_offset
83
83
  end
84
84
  end
85
85
  end
@@ -18,11 +18,11 @@ module RuboCop
18
18
  # # good
19
19
  # def attribute ...
20
20
  class AccessorMethodName < 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
  if bad_reader_name?(method_name.to_s, args)
27
27
  add_offense(node, :name,
28
28
  'Do not prefix reader method names with `get_`.')
@@ -41,7 +41,7 @@ module RuboCop
41
41
  separator_delta = separator_delta(first_pair, current_separator,
42
42
  key_delta)
43
43
  value_delta = value_delta(first_pair, current_pair) -
44
- key_delta - separator_delta
44
+ key_delta - separator_delta
45
45
 
46
46
  { key: key_delta, separator: separator_delta, value: value_delta }
47
47
  end
@@ -106,7 +106,8 @@ module RuboCop
106
106
  first_key, _ = *first_pair
107
107
  _, current_value = *current_pair
108
108
  correct_value_column = first_key.loc.column +
109
- spaced_separator(current_pair).length + @max_key_width
109
+ spaced_separator(current_pair).length +
110
+ @max_key_width
110
111
  correct_value_column - current_value.loc.column
111
112
  end
112
113
 
@@ -148,7 +149,8 @@ module RuboCop
148
149
 
149
150
  def on_send(node)
150
151
  return unless (last_child = node.children.last) &&
151
- hash?(last_child) && ignore_last_argument_hash?(last_child)
152
+ hash?(last_child) &&
153
+ ignore_last_argument_hash?(last_child)
152
154
 
153
155
  ignore_node(last_child)
154
156
  end
@@ -162,15 +164,20 @@ module RuboCop
162
164
  new_alignment('EnforcedHashRocketStyle')
163
165
  @alignment_for_colons ||= new_alignment('EnforcedColonStyle')
164
166
 
165
- first_pair = node.children.first
166
-
167
167
  unless @alignment_for_hash_rockets.checkable_layout(node) &&
168
- @alignment_for_colons.checkable_layout(node)
168
+ @alignment_for_colons.checkable_layout(node)
169
169
  return
170
170
  end
171
171
 
172
+ check_pairs(node)
173
+ end
174
+
175
+ private
176
+
177
+ def check_pairs(node)
178
+ first_pair = node.children.first
172
179
  @column_deltas = alignment_for(first_pair)
173
- .deltas_for_first_pair(first_pair, node)
180
+ .deltas_for_first_pair(first_pair, node)
174
181
  add_offense(first_pair, :expression) unless good_alignment?
175
182
 
176
183
  node.children.each_cons(2) do |prev, current|
@@ -180,8 +187,6 @@ module RuboCop
180
187
  end
181
188
  end
182
189
 
183
- private
184
-
185
190
  def ignore_last_argument_hash?(node)
186
191
  case cop_config['EnforcedLastArgumentHashStyle']
187
192
  when 'always_inspect' then false
@@ -199,12 +204,11 @@ module RuboCop
199
204
  node.loc.begin
200
205
  end
201
206
 
202
- # Returns true if the hash spans multiple lines, and each key-value
203
- # pair following the first is on a new line.
207
+ # Returns true if the hash spans multiple lines
204
208
  def multiline?(node)
205
209
  return false unless node.loc.expression.source.include?("\n")
206
210
 
207
- return false if node.children[1..-1].find do |child|
211
+ return false if node.children[1..-1].all? do |child|
208
212
  !begins_its_line?(child.loc.expression)
209
213
  end
210
214