Skip to content

Commit

Permalink
Merge pull request rubocop#797 from pirj/fix-example-wording-cop-mult…
Browse files Browse the repository at this point in the history
…iline-literal

Fix autocorrect of multi-line docstrings in RSpec/ExampleWording
  • Loading branch information
Darhazer committed Aug 8, 2019
2 parents 6b689df + e646eb8 commit 6a1336a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

* Fix `RSpec/DescribedClass`'s error when a `described_class` is part of the namespace. ([@pirj][])
* Fix `RSpec/ExampleWording` autocorrect of multi-line docstrings. ([@pirj][])

## 1.35.0 (2019-08-02)

Expand Down
40 changes: 28 additions & 12 deletions lib/rubocop/cop/rspec/example_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,30 @@ def on_block(node)
end
end

def autocorrect(range)
->(corrector) { corrector.replace(range, replacement_text(range)) }
def autocorrect(node)
lambda do |corrector|
corrector.replace(docstring(node), replacement_text(node))
end
end

private

def add_wording_offense(node, message)
expr = node.loc.expression
add_offense(node, location: docstring(node), message: message)
end

docstring =
Parser::Source::Range.new(
expr.source_buffer,
expr.begin_pos + 1,
expr.end_pos - 1
)
def docstring(node)
expr = node.loc.expression

add_offense(docstring, location: docstring, message: message)
Parser::Source::Range.new(
expr.source_buffer,
expr.begin_pos + 1,
expr.end_pos - 1
)
end

def replacement_text(range)
text = range.source
def replacement_text(node)
text = text(node)

if text =~ SHOULD_PREFIX
RuboCop::RSpec::Wording.new(
Expand All @@ -86,6 +89,19 @@ def replacement_text(range)
end
end

# Recursive processing is required to process nested dstr nodes
# that is the case for \-separated multiline strings with interpolation.
def text(node)
case node.type
when :dstr
node.node_parts.map { |child_node| text(child_node) }.join
when :str
node.value
when :begin
node.source
end
end

def custom_transform
cop_config.fetch('CustomTransform', {})
end
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/rspec/example_wording_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,34 @@
end
RUBY
end

it 'flags \-separated multiline strings' do
expect_offense(<<-RUBY)
it 'should do something '\\
^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
'and correctly fix' do
end
RUBY

expect_correction(<<-RUBY)
it 'does something and correctly fix' do
end
RUBY
end

it 'flags \-separated multiline interpolated strings' do
expect_offense(<<-'RUBY')
it "should do something "\
^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
"with #{object}" do
end
RUBY

expect_correction(<<-'RUBY')
it "does something with #{object}" do
end
RUBY
end
end

context 'when configuration is empty' do
Expand Down

0 comments on commit 6a1336a

Please sign in to comment.