Skip to content

Commit 6b470ba

Browse files
committed
Fix RSpec/ExampleWording autocorrection to correctly escape quotes on str node case
1 parent 31c6344 commit 6b470ba

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Support correcting `assert_not_equal` and `assert_not_nil` in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
1313
- Fix a false positive for `RSpec/ExpectActual` when used with rspec-rails routing matchers. ([@naveg])
1414
- Add new `RSpec/RepeatedSubjectCall` cop. ([@drcapulet])
15+
- Fix `RSpec/ExampleWording` autocorrection to correctly escape quotes on str node case. ([@r7kamura])
1516

1617
## 2.26.1 (2024-01-05)
1718

lib/rubocop/cop/rspec/example_wording.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,27 @@ def add_wording_offense(node, message)
9292
add_offense(docstring, message: message) do |corrector|
9393
next if node.heredoc?
9494

95-
corrector.replace(docstring, replacement_text(node))
95+
if node.str_type? && needs_escape?(node)
96+
corrector.replace(node, replacement_text(node).inspect)
97+
else
98+
corrector.replace(docstring, replacement_text(node))
99+
end
96100
end
97101
end
98102

99103
def docstring(node)
100-
expr = node.source_range
104+
if node.str_type? && !node.heredoc?
105+
node.source_range.with(
106+
begin_pos: node.loc.begin.end_pos,
107+
end_pos: node.loc.end.begin_pos
108+
)
109+
else
110+
node.source_range.adjust(begin_pos: 1, end_pos: -1)
111+
end
112+
end
101113

102-
Parser::Source::Range.new(
103-
expr.source_buffer,
104-
expr.begin_pos + 1,
105-
expr.end_pos - 1
106-
)
114+
def needs_escape?(node)
115+
node.value.include?(node.loc.end.source)
107116
end
108117

109118
def replacement_text(node)

spec/rubocop/cop/rspec/example_wording_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,57 @@
351351
end
352352
RUBY
353353
end
354+
355+
it 'corrects escaped single-quote' do
356+
expect_offense(<<~'RUBY')
357+
it 'should return foo\'s bar' do
358+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
359+
end
360+
RUBY
361+
362+
expect_correction(<<~RUBY)
363+
it "returns foo's bar" do
364+
end
365+
RUBY
366+
end
367+
368+
it 'corrects escaped double-quote' do
369+
expect_offense(<<~'RUBY')
370+
it "should return \"foo\"" do
371+
^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
372+
end
373+
RUBY
374+
375+
expect_correction(<<~'RUBY')
376+
it "returns \"foo\"" do
377+
end
378+
RUBY
379+
end
380+
381+
it 'corrects %(...) quote' do
382+
expect_offense(<<~RUBY)
383+
it %q(should return foo (bar)) do
384+
^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
385+
end
386+
RUBY
387+
388+
expect_correction(<<~RUBY)
389+
it "returns foo (bar)" do
390+
end
391+
RUBY
392+
end
393+
394+
it 'corrects %!...! quote' do
395+
expect_offense(<<~'RUBY')
396+
it %!should return foo\!! do
397+
^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
398+
end
399+
RUBY
400+
401+
expect_correction(<<~RUBY)
402+
it "returns foo!" do
403+
end
404+
RUBY
405+
end
354406
end
355407
end

0 commit comments

Comments
 (0)