Skip to content

Commit

Permalink
Check blank percent literal by `Layout/SpaceInsidePercentLiteralDelim…
Browse files Browse the repository at this point in the history
…iters`
  • Loading branch information
r7kamura committed Nov 3, 2022
1 parent 7007fb1 commit c3bbc6c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/change_check_blank_percent_literal.md
@@ -0,0 +1 @@
* [#11130](https://github.com/rubocop/rubocop/pull/11130): Check blank percent literal by `Layout/SpaceInsidePercentLiteralDelimiters`. ([@r7kamura][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
Expand Up @@ -6,6 +6,9 @@ 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
Expand Down
34 changes: 34 additions & 0 deletions lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
Expand Up @@ -8,14 +8,31 @@ module Layout
#
# @example
#
# # bad
# %i( foo bar baz )
#
# # good
# %i(foo bar baz)
#
# # bad
# %w( foo bar baz )
#
# # good
# %w(foo bar baz)
#
# # bad
# %x( ls -l )
#
# # good
# %x(ls -l)
#
# # bad
# %w( )
# %w(
# )
#
# # good
# %w()
class SpaceInsidePercentLiteralDelimiters < Base
include MatchRange
include PercentLiteral
Expand All @@ -34,11 +51,21 @@ def on_xstr(node)
end

def on_percent_literal(node)
add_offenses_for_blank_spaces(node)
add_offenses_for_unnecessary_spaces(node)
end

private

def add_offenses_for_blank_spaces(node)
range = body_range(node)
return if range.source.empty? || !range.source.strip.empty?

add_offense(range) do |corrector|
corrector.remove(range)
end
end

def add_offenses_for_unnecessary_spaces(node)
return unless node.single_line?

Expand All @@ -54,6 +81,13 @@ def regex_matches(node, &blk)
each_match_range(contents_range(node), regex, &blk)
end
end

def body_range(node)
node.location.expression.with(
begin_pos: node.location.begin.end_pos,
end_pos: node.location.end.begin_pos
)
end
end
end
end
Expand Down
Expand Up @@ -86,6 +86,36 @@ def foo
it 'accepts spaces between entries' do
expect_no_offenses(code_example('a b c'))
end

context 'with space in blank percent literals' do
it 'registers and corrects an offense' do
expect_offense(<<~RUBY)
#{code_example(' ')}
^ #{message}
RUBY

expect_correction("#{code_example('')}\n")
end
end

context 'with spaces in blank percent literals' do
it 'registers and corrects an offense' do
expect_offense(<<~RUBY)
#{code_example(' ')}
^^ #{message}
RUBY

expect_correction("#{code_example('')}\n")
end
end

context 'with newline in blank percent literals' do
it 'registers and corrects an offense' do
expect_offense(code_example("\n").lines.insert(1, " ^{} #{message}\n").join)

expect_correction(code_example('').to_s)
end
end
end
end
end
Expand Down

0 comments on commit c3bbc6c

Please sign in to comment.