Skip to content

Commit

Permalink
[Fix rubocop#9894] Handle multiline strings in LineEndStringConcatena…
Browse files Browse the repository at this point in the history
…tionIndentation

A multiline string literal such as

```ruby
%(
  foo
  bar
)
```

is parsed as a `dstr`, and should just be skipped by this cop, as
opposed to strings made up of several literals concatenated with
backslash.
  • Loading branch information
jonas054 committed Jun 30, 2021
1 parent 394d34e commit 4b0569f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_LineEndStringConcatenationIndentation.md
@@ -0,0 +1 @@
* [#9894](https://github.com/rubocop/rubocop/issues/9894): Handle multiline string literals in `Layout/LineEndStringConcatenationIndentation`. ([@jonas054][])
Expand Up @@ -86,10 +86,15 @@ def autocorrect(corrector, node)

def strings_concatenated_with_backslash?(dstr_node)
!dstr_node.heredoc? &&
!single_string_literal?(dstr_node) &&
dstr_node.children.length > 1 &&
dstr_node.children.all? { |c| c.str_type? || c.dstr_type? }
end

def single_string_literal?(dstr_node)
dstr_node.loc.respond_to?(:begin) && dstr_node.loc.begin
end

def always_indented?(dstr_node)
PARENT_TYPES_FOR_INDENTED.include?(dstr_node.parent&.type)
end
Expand Down
Expand Up @@ -14,6 +14,15 @@
let(:cop_indent) { nil } # use indentation width from Layout/IndentationWidth

shared_examples 'common' do
it 'accepts a multiline string literal' do
expect_no_offenses(<<~'RUBY')
puts %(
foo
bar
)
RUBY
end

it 'accepts indented strings in implicit return statement of a block' do
expect_no_offenses(<<~'RUBY')
some_method do
Expand Down

0 comments on commit 4b0569f

Please sign in to comment.