/
space_inside_array_percent_literal.rb
46 lines (40 loc) · 1.22 KB
/
space_inside_array_percent_literal.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true
module RuboCop
module Cop
module Layout
# Checks for unnecessary additional spaces inside array percent literals
# (i.e. %i/%w).
#
# Note that blank percent literals (e.g. `%i( )`) are checked by
# `Layout/SpaceInsidePercentLiteralDelimiters`.
#
# @example
#
# # bad
# %w(foo bar baz)
# # good
# %i(foo bar baz)
class SpaceInsideArrayPercentLiteral < Base
include MatchRange
include PercentLiteral
extend AutoCorrector
MSG = 'Use only a single space inside array percent literal.'
MULTIPLE_SPACES_BETWEEN_ITEMS_REGEX = /(?:[\S&&[^\\]](?:\\ )*)( {2,})(?=\S)/.freeze
def on_array(node)
process(node, '%i', '%I', '%w', '%W')
end
def on_percent_literal(node)
each_unnecessary_space_match(node) do |range|
add_offense(range) do |corrector|
corrector.replace(range, ' ')
end
end
end
private
def each_unnecessary_space_match(node, &blk)
each_match_range(contents_range(node), MULTIPLE_SPACES_BETWEEN_ITEMS_REGEX, &blk)
end
end
end
end
end