rubocop 0.86.0 → 0.87.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +46 -4
  4. data/lib/rubocop.rb +8 -1
  5. data/lib/rubocop/cli.rb +0 -2
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +40 -5
  7. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  8. data/lib/rubocop/config_loader.rb +21 -62
  9. data/lib/rubocop/config_obsoletion.rb +0 -1
  10. data/lib/rubocop/cop/autocorrect_logic.rb +13 -23
  11. data/lib/rubocop/cop/base.rb +399 -0
  12. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
  13. data/lib/rubocop/cop/commissioner.rb +48 -50
  14. data/lib/rubocop/cop/cop.rb +85 -236
  15. data/lib/rubocop/cop/corrector.rb +38 -115
  16. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  17. data/lib/rubocop/cop/generator.rb +1 -1
  18. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
  19. data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
  20. data/lib/rubocop/cop/layout/class_structure.rb +2 -37
  21. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -8
  22. data/lib/rubocop/cop/layout/first_argument_indentation.rb +4 -0
  23. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -2
  24. data/lib/rubocop/cop/layout/multiline_block_layout.rb +0 -1
  25. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +19 -25
  26. data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
  27. data/lib/rubocop/cop/legacy/corrector.rb +29 -0
  28. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
  29. data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
  30. data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
  31. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  32. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
  33. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  34. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +27 -23
  35. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
  36. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +8 -0
  37. data/lib/rubocop/cop/lint/syntax.rb +11 -26
  38. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
  39. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
  40. data/lib/rubocop/cop/metrics/block_length.rb +22 -0
  41. data/lib/rubocop/cop/metrics/class_length.rb +25 -2
  42. data/lib/rubocop/cop/metrics/method_length.rb +23 -0
  43. data/lib/rubocop/cop/metrics/module_length.rb +25 -2
  44. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
  45. data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
  46. data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
  47. data/lib/rubocop/cop/mixin/code_length.rb +4 -0
  48. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  49. data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
  50. data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
  51. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
  52. data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -2
  53. data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
  54. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -2
  55. data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
  56. data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
  57. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +2 -2
  58. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  59. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  60. data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
  61. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  62. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  63. data/lib/rubocop/cop/offense.rb +16 -2
  64. data/lib/rubocop/cop/style/accessor_grouping.rb +140 -0
  65. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +145 -0
  66. data/lib/rubocop/cop/style/class_vars.rb +21 -0
  67. data/lib/rubocop/cop/style/date_time.rb +1 -1
  68. data/lib/rubocop/cop/style/dir.rb +2 -2
  69. data/lib/rubocop/cop/style/empty_literal.rb +5 -5
  70. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  71. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
  72. data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
  73. data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
  74. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
  75. data/lib/rubocop/cop/style/proc.rb +1 -1
  76. data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
  77. data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
  78. data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
  79. data/lib/rubocop/cop/style/redundant_fetch_block.rb +26 -7
  80. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  81. data/lib/rubocop/cop/style/redundant_parentheses.rb +7 -1
  82. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -1
  83. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -2
  84. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
  85. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  86. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  87. data/lib/rubocop/cop/style/struct_inheritance.rb +2 -2
  88. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  89. data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
  90. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
  91. data/lib/rubocop/cop/team.rb +97 -81
  92. data/lib/rubocop/cop/utils/format_string.rb +1 -2
  93. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  94. data/lib/rubocop/name_similarity.rb +1 -3
  95. data/lib/rubocop/options.rb +15 -8
  96. data/lib/rubocop/rake_task.rb +6 -9
  97. data/lib/rubocop/rspec/cop_helper.rb +4 -4
  98. data/lib/rubocop/rspec/expect_offense.rb +22 -17
  99. data/lib/rubocop/rspec/shared_contexts.rb +7 -7
  100. data/lib/rubocop/runner.rb +31 -29
  101. data/lib/rubocop/target_ruby.rb +1 -1
  102. data/lib/rubocop/version.rb +1 -1
  103. metadata +16 -7
  104. data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -8,117 +8,25 @@ module RuboCop
8
8
  # Important!
9
9
  # The nodes modified by the corrections should be part of the
10
10
  # AST of the source_buffer.
11
- class Corrector
11
+ class Corrector < ::Parser::Source::TreeRewriter
12
+ # @param source [Parser::Source::Buffer, or anything
13
+ # leading to one via `(processed_source.)buffer`]
12
14
  #
13
- # @param source_buffer [Parser::Source::Buffer]
14
- # @param corrections [Array(#call)]
15
- # Array of Objects that respond to #call. They will receive the
16
- # corrector itself and should use its method to modify the source.
17
- #
18
- # @example
19
- #
20
- # class AndOrCorrector
21
- # def initialize(node)
22
- # @node = node
23
- # end
24
- #
25
- # def call(corrector)
26
- # replacement = (@node.type == :and ? '&&' : '||')
27
- # corrector.replace(@node.loc.operator, replacement)
28
- # end
29
- # end
30
- #
31
- # corrections = [AndOrCorrector.new(node)]
32
- # corrector = Corrector.new(source_buffer, corrections)
33
- def initialize(source_buffer, corrections = [])
34
- @source_buffer = source_buffer
35
- raise 'source_buffer should be a Parser::Source::Buffer' unless \
36
- source_buffer.is_a? Parser::Source::Buffer
37
-
38
- @corrections = corrections
39
- @source_rewriter = Parser::Source::TreeRewriter.new(
40
- source_buffer,
15
+ # corrector = Corrector.new(cop)
16
+ def initialize(source)
17
+ source = self.class.source_buffer(source)
18
+ super(
19
+ source,
41
20
  different_replacements: :raise,
42
21
  swallowed_insertions: :raise,
43
22
  crossing_deletions: :accept
44
23
  )
45
24
 
46
- @diagnostics = []
47
25
  # Don't print warnings to stderr if corrections conflict with each other
48
- @source_rewriter.diagnostics.consumer = lambda do |diagnostic|
49
- @diagnostics << diagnostic
50
- end
51
- end
52
-
53
- attr_reader :corrections, :diagnostics
54
-
55
- # Does the actual rewrite and returns string corresponding to
56
- # the rewritten source.
57
- #
58
- # @return [String]
59
- def rewrite
60
- @corrections.each do |correction|
61
- begin
62
- @source_rewriter.transaction do
63
- correction.call(self)
64
- end
65
- rescue ErrorWithAnalyzedFileLocation => e
66
- raise e unless e.cause.is_a?(::Parser::ClobberingError)
67
- end
68
- end
69
-
70
- @source_rewriter.process
71
- end
72
-
73
- # Removes the source range.
74
- #
75
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
76
- def remove(node_or_range)
77
- range = to_range(node_or_range)
78
- @source_rewriter.remove(range)
79
- end
80
-
81
- # Inserts new code before the given source range.
82
- #
83
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
84
- # @param [String] content
85
- def insert_before(node_or_range, content)
86
- range = to_range(node_or_range)
87
- # TODO: Fix Cops using bad ranges instead
88
- if range.end_pos > @source_buffer.source.size
89
- range = range.with(end_pos: @source_buffer.source.size)
90
- end
91
-
92
- @source_rewriter.insert_before(range, content)
93
- end
94
-
95
- # Inserts new code after the given source range.
96
- #
97
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
98
- # @param [String] content
99
- def insert_after(node_or_range, content)
100
- range = to_range(node_or_range)
101
- @source_rewriter.insert_after(range, content)
26
+ diagnostics.consumer = ->(diagnostic) {}
102
27
  end
103
28
 
104
- # Wraps the given source range with the given before and after texts
105
- #
106
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
107
- # @param [String] before
108
- # @param [String] after
109
- def wrap(node_or_range, before, after)
110
- range = to_range(node_or_range)
111
- @source_rewriter.wrap(range, before, after)
112
- end
113
-
114
- # Replaces the code of the source range `range` with `content`.
115
- #
116
- # @param [Parser::Source::Range, Rubocop::AST::Node] range or node
117
- # @param [String] content
118
- def replace(node_or_range, content)
119
- range = to_range(node_or_range)
120
- @source_rewriter.replace(range, content)
121
- end
29
+ alias rewrite process # Legacy
122
30
 
123
31
  # Removes `size` characters prior to the source range.
124
32
  #
@@ -126,10 +34,11 @@ module RuboCop
126
34
  # @param [Integer] size
127
35
  def remove_preceding(node_or_range, size)
128
36
  range = to_range(node_or_range)
129
- to_remove = Parser::Source::Range.new(range.source_buffer,
130
- range.begin_pos - size,
131
- range.begin_pos)
132
- @source_rewriter.remove(to_remove)
37
+ to_remove = range.with(
38
+ begin_pos: range.begin_pos - size,
39
+ end_pos: range.begin_pos
40
+ )
41
+ remove(to_remove)
133
42
  end
134
43
 
135
44
  # Removes `size` characters from the beginning of the given range.
@@ -140,10 +49,8 @@ module RuboCop
140
49
  # @param [Integer] size
141
50
  def remove_leading(node_or_range, size)
142
51
  range = to_range(node_or_range)
143
- to_remove = Parser::Source::Range.new(range.source_buffer,
144
- range.begin_pos,
145
- range.begin_pos + size)
146
- @source_rewriter.remove(to_remove)
52
+ to_remove = range.with(end_pos: range.begin_pos + size)
53
+ remove(to_remove)
147
54
  end
148
55
 
149
56
  # Removes `size` characters from the end of the given range.
@@ -154,10 +61,22 @@ module RuboCop
154
61
  # @param [Integer] size
155
62
  def remove_trailing(node_or_range, size)
156
63
  range = to_range(node_or_range)
157
- to_remove = Parser::Source::Range.new(range.source_buffer,
158
- range.end_pos - size,
159
- range.end_pos)
160
- @source_rewriter.remove(to_remove)
64
+ to_remove = range.with(begin_pos: range.end_pos - size)
65
+ remove(to_remove)
66
+ end
67
+
68
+ # Duck typing for get to a ::Parser::Source::Buffer
69
+ def self.source_buffer(source)
70
+ source = source.processed_source if source.respond_to?(:processed_source)
71
+ source = source.buffer if source.respond_to?(:buffer)
72
+ source = source.source_buffer if source.respond_to?(:source_buffer)
73
+
74
+ unless source.is_a? ::Parser::Source::Buffer
75
+ raise TypeError, 'Expected argument to lead to a Parser::Source::Buffer ' \
76
+ "but got #{source.inspect}"
77
+ end
78
+
79
+ source
161
80
  end
162
81
 
163
82
  private
@@ -178,8 +97,12 @@ module RuboCop
178
97
  range
179
98
  end
180
99
 
100
+ def check_range_validity(node_or_range)
101
+ super(to_range(node_or_range))
102
+ end
103
+
181
104
  def validate_buffer(buffer)
182
- return if buffer == @source_buffer
105
+ return if buffer == source_buffer
183
106
 
184
107
  unless buffer.is_a?(::Parser::Source::Buffer)
185
108
  # actually this should be enforced by parser gem
@@ -28,7 +28,7 @@ module RuboCop
28
28
  class RubyVersionGlobalsUsage < Cop
29
29
  MSG = 'Do not use `RUBY_VERSION` in gemspec file.'
30
30
 
31
- def_node_matcher :ruby_version?, '(const nil? :RUBY_VERSION)'
31
+ def_node_matcher :ruby_version?, '(const {cbase nil?} :RUBY_VERSION)'
32
32
 
33
33
  def_node_search :gem_specification?, <<~PATTERN
34
34
  (block
@@ -54,7 +54,7 @@ module RuboCop
54
54
  # # good
55
55
  # good_foo_method(args)
56
56
  #
57
- class %<cop_name>s < Cop
57
+ class %<cop_name>s < Base
58
58
  # TODO: Implement the cop in here.
59
59
  #
60
60
  # In many cases, you can use a node matcher for matching node pattern.
@@ -13,7 +13,9 @@ module RuboCop
13
13
  # # good
14
14
  # node.send_type?
15
15
  #
16
- class NodeTypePredicate < Cop
16
+ class NodeTypePredicate < Base
17
+ extend AutoCorrector
18
+
17
19
  MSG = 'Use `#%<type>s_type?` to check node type.'
18
20
 
19
21
  def_node_matcher :node_type_check, <<~PATTERN
@@ -21,21 +23,16 @@ module RuboCop
21
23
  PATTERN
22
24
 
23
25
  def on_send(node)
24
- node_type_check(node) do |_receiver, node_type|
26
+ node_type_check(node) do |receiver, node_type|
25
27
  return unless Parser::Meta::NODE_TYPES.include?(node_type)
26
28
 
27
- add_offense(node, message: format(MSG, type: node_type))
28
- end
29
- end
30
-
31
- def autocorrect(node)
32
- receiver, node_type = node_type_check(node)
33
- range = Parser::Source::Range.new(node.source_range.source_buffer,
34
- receiver.loc.expression.end_pos + 1,
35
- node.loc.expression.end_pos)
36
-
37
- lambda do |corrector|
38
- corrector.replace(range, "#{node_type}_type?")
29
+ message = format(MSG, type: node_type)
30
+ add_offense(node, message: message) do |corrector|
31
+ range = node.loc.expression.with(
32
+ begin_pos: receiver.loc.expression.end_pos + 1
33
+ )
34
+ corrector.replace(range, "#{node_type}_type?")
35
+ end
39
36
  end
40
37
  end
41
38
  end
@@ -67,10 +67,11 @@ module RuboCop
67
67
  # else
68
68
  # y / 3
69
69
  # end
70
- class CaseIndentation < Cop
70
+ class CaseIndentation < Base
71
71
  include Alignment
72
72
  include ConfigurableEnforcedStyle
73
73
  include RangeHelp
74
+ extend AutoCorrector
74
75
 
75
76
  MSG = 'Indent `when` %<depth>s `%<base>s`.'
76
77
 
@@ -82,16 +83,6 @@ module RuboCop
82
83
  end
83
84
  end
84
85
 
85
- def autocorrect(node)
86
- whitespace = whitespace_range(node)
87
-
88
- return false unless whitespace.source.strip.empty?
89
-
90
- lambda do |corrector|
91
- corrector.replace(whitespace, replacement(node))
92
- end
93
- end
94
-
95
86
  private
96
87
 
97
88
  def check_when(when_node)
@@ -114,22 +105,30 @@ module RuboCop
114
105
  end
115
106
 
116
107
  def incorrect_style(when_node)
108
+ add_offense(when_node.loc.keyword) do |corrector|
109
+ detect_incorrect_style(when_node)
110
+
111
+ whitespace = whitespace_range(when_node)
112
+
113
+ corrector.replace(whitespace, replacement(when_node)) if whitespace.source.strip.empty?
114
+ end
115
+ end
116
+
117
+ def detect_incorrect_style(when_node)
117
118
  when_column = when_node.loc.keyword.column
118
119
  base_column = base_column(when_node.parent, alternative_style)
119
120
 
120
- add_offense(when_node, location: :keyword, message: message(style)) do
121
- if when_column == base_column
122
- opposite_style_detected
123
- else
124
- unrecognized_style_detected
125
- end
121
+ if when_column == base_column
122
+ opposite_style_detected
123
+ else
124
+ unrecognized_style_detected
126
125
  end
127
126
  end
128
127
 
129
- def message(base)
128
+ def find_message(*)
130
129
  depth = indent_one_step? ? 'one step more than' : 'as deep as'
131
130
 
132
- format(MSG, depth: depth, base: base)
131
+ format(MSG, depth: depth, base: style)
133
132
  end
134
133
 
135
134
  def base_column(case_node, base)
@@ -134,6 +134,8 @@ module RuboCop
134
134
  #
135
135
  # @see https://rubystyle.guide#consistent-classes
136
136
  class ClassStructure < Cop
137
+ include VisibilityHelp
138
+
137
139
  HUMANIZED_NODE_TYPE = {
138
140
  casgn: :constants,
139
141
  defs: :class_methods,
@@ -141,14 +143,9 @@ module RuboCop
141
143
  sclass: :class_singleton
142
144
  }.freeze
143
145
 
144
- VISIBILITY_SCOPES = %i[private protected public].freeze
145
146
  MSG = '`%<category>s` is supposed to appear before ' \
146
147
  '`%<previous>s`.'
147
148
 
148
- def_node_matcher :visibility_block?, <<~PATTERN
149
- (send nil? { :private :protected :public })
150
- PATTERN
151
-
152
149
  # Validates code style on class declaration.
153
150
  # Add offense when find a node out of expected order.
154
151
  def on_class(class_node)
@@ -243,38 +240,6 @@ module RuboCop
243
240
  expected_order.index(classification).nil?
244
241
  end
245
242
 
246
- def node_visibility(node)
247
- scope = find_visibility_start(node)
248
- scope&.method_name || :public
249
- end
250
-
251
- def find_visibility_start(node)
252
- left_siblings_of(node)
253
- .reverse
254
- .find(&method(:visibility_block?))
255
- end
256
-
257
- # Navigate to find the last protected method
258
- def find_visibility_end(node)
259
- possible_visibilities = VISIBILITY_SCOPES - [node_visibility(node)]
260
- right = right_siblings_of(node)
261
- right.find do |child_node|
262
- possible_visibilities.include?(node_visibility(child_node))
263
- end || right.last
264
- end
265
-
266
- def siblings_of(node)
267
- node.parent.children
268
- end
269
-
270
- def right_siblings_of(node)
271
- siblings_of(node)[node.sibling_index..-1]
272
- end
273
-
274
- def left_siblings_of(node)
275
- siblings_of(node)[0, node.sibling_index]
276
- end
277
-
278
243
  def humanize_node(node)
279
244
  if node.def_type?
280
245
  return :initializer if node.method?(:initialize)
@@ -62,6 +62,7 @@ module RuboCop
62
62
  #
63
63
  class EmptyLinesAroundAttributeAccessor < Cop
64
64
  include RangeHelp
65
+ include AllowedMethods
65
66
 
66
67
  MSG = 'Add an empty line after attribute accessor.'
67
68
 
@@ -114,14 +115,6 @@ module RuboCop
114
115
  def allow_alias_syntax?
115
116
  cop_config.fetch('AllowAliasSyntax', true)
116
117
  end
117
-
118
- def allowed_method?(name)
119
- allowed_methods.include?(name.to_s)
120
- end
121
-
122
- def allowed_methods
123
- cop_config.fetch('AllowedMethods', [])
124
- end
125
118
  end
126
119
  end
127
120
  end
@@ -241,6 +241,10 @@ module RuboCop
241
241
  .select { |c| begins_its_line?(c.loc.expression) }
242
242
  .map { |c| c.loc.line }
243
243
  end
244
+
245
+ def on_new_investigation
246
+ @comment_lines = nil
247
+ end
244
248
  end
245
249
  end
246
250
  end
@@ -219,8 +219,7 @@ module RuboCop
219
219
  correct_node(node, delta)
220
220
  end
221
221
 
222
- attr_accessor :offences_by
223
- attr_accessor :column_deltas
222
+ attr_accessor :offences_by, :column_deltas
224
223
 
225
224
  private
226
225
 
@@ -105,7 +105,6 @@ module RuboCop
105
105
  def add_offense_for_expression(node, expr, msg)
106
106
  expression = expr.source_range
107
107
  range = range_between(expression.begin_pos, expression.end_pos)
108
-
109
108
  add_offense(node, location: range, message: msg)
110
109
  end
111
110
 
@@ -24,9 +24,10 @@ module RuboCop
24
24
  # # good
25
25
  # {}.each { | x, y | puts x }
26
26
  # ->( x, y ) { puts x }
27
- class SpaceAroundBlockParameters < Cop
27
+ class SpaceAroundBlockParameters < Base
28
28
  include ConfigurableEnforcedStyle
29
29
  include RangeHelp
30
+ extend AutoCorrector
30
31
 
31
32
  def on_block(node)
32
33
  arguments = node.arguments
@@ -38,23 +39,6 @@ module RuboCop
38
39
  check_each_arg(arguments)
39
40
  end
40
41
 
41
- # @param target [RuboCop::AST::Node,Parser::Source::Range]
42
- def autocorrect(target)
43
- lambda do |corrector|
44
- if target.is_a?(RuboCop::AST::Node)
45
- if target.parent.children.first == target
46
- corrector.insert_before(target, ' ')
47
- else
48
- corrector.insert_after(target, ' ')
49
- end
50
- elsif /^\s+$/.match?(target.source)
51
- corrector.remove(target)
52
- else
53
- corrector.insert_after(target, ' ')
54
- end
55
- end
56
- end
57
-
58
42
  private
59
43
 
60
44
  def pipes(arguments)
@@ -97,7 +81,7 @@ module RuboCop
97
81
 
98
82
  check_no_space(opening_pipe.end_pos, first.begin_pos,
99
83
  'Space before first')
100
- check_no_space(last_end_pos_inside_pipes(last.end_pos),
84
+ check_no_space(last_end_pos_inside_pipes(last),
101
85
  closing_pipe.begin_pos, 'Space after last')
102
86
  end
103
87
 
@@ -118,7 +102,7 @@ module RuboCop
118
102
 
119
103
  def check_closing_pipe_space(args, closing_pipe)
120
104
  last = args.last.source_range
121
- last_end_pos = last_end_pos_inside_pipes(last.end_pos)
105
+ last_end_pos = last_end_pos_inside_pipes(last)
122
106
 
123
107
  check_space(last_end_pos, closing_pipe.begin_pos, last,
124
108
  'after last block parameter')
@@ -126,8 +110,9 @@ module RuboCop
126
110
  'Extra space after last')
127
111
  end
128
112
 
129
- def last_end_pos_inside_pipes(pos)
130
- processed_source.buffer.source[pos] == ',' ? pos + 1 : pos
113
+ def last_end_pos_inside_pipes(range)
114
+ pos = range.end_pos
115
+ range.source_buffer.source[pos] == ',' ? pos + 1 : pos
131
116
  end
132
117
 
133
118
  def check_each_arg(args)
@@ -151,7 +136,14 @@ module RuboCop
151
136
  return if space_begin_pos != space_end_pos
152
137
 
153
138
  target = node || range
154
- add_offense(target, location: range, message: "Space #{msg} missing.")
139
+ message = "Space #{msg} missing."
140
+ add_offense(target, message: message) do |corrector|
141
+ if node
142
+ corrector.insert_before(node, ' ')
143
+ else
144
+ corrector.insert_after(target, ' ')
145
+ end
146
+ end
155
147
  end
156
148
 
157
149
  def check_no_space(space_begin_pos, space_end_pos, msg)
@@ -160,8 +152,10 @@ module RuboCop
160
152
  range = range_between(space_begin_pos, space_end_pos)
161
153
  return if range.source.include?("\n")
162
154
 
163
- add_offense(range, location: range,
164
- message: "#{msg} block parameter detected.")
155
+ message = "#{msg} block parameter detected."
156
+ add_offense(range, message: message) do |corrector|
157
+ corrector.remove(range)
158
+ end
165
159
  end
166
160
  end
167
161
  end