Skip to content

Commit 35ffb03

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

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-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` in escaping and `%` string literals. ([@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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,95 @@
351351
end
352352
RUBY
353353
end
354+
355+
context "when message includes `'` in `'...'`" do
356+
it 'corrects message with `String#inspect`' do
357+
expect_offense(<<~'RUBY')
358+
it 'should return foo\'s bar' do
359+
^^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
360+
end
361+
RUBY
362+
363+
expect_correction(<<~RUBY)
364+
it "returns foo's bar" do
365+
end
366+
RUBY
367+
end
368+
end
369+
370+
context 'when message includes `"` in `"..."`' do
371+
it 'corrects message with `String#inspect`' do
372+
expect_offense(<<~'RUBY')
373+
it "should return \"foo\"" do
374+
^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
375+
end
376+
RUBY
377+
378+
expect_correction(<<~'RUBY')
379+
it "returns \"foo\"" do
380+
end
381+
RUBY
382+
end
383+
end
384+
385+
context 'when message includes `!` in `%!...!`' do
386+
it 'corrects message with `String#inspect`' do
387+
expect_offense(<<~'RUBY')
388+
it %!should return foo\!! do
389+
^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
390+
end
391+
RUBY
392+
393+
expect_correction(<<~RUBY)
394+
it "returns foo!" do
395+
end
396+
RUBY
397+
end
398+
end
399+
400+
context 'when message includes `)` in `%q(...)`' do
401+
it 'corrects message with `String#inspect`' do
402+
expect_offense(<<~RUBY)
403+
it %q(should return foo (bar)) do
404+
^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
405+
end
406+
RUBY
407+
408+
expect_correction(<<~RUBY)
409+
it "returns foo (bar)" do
410+
end
411+
RUBY
412+
end
413+
end
414+
415+
context 'when message includes `"` in `%q(...)`' do
416+
it 'corrects message with direct substring replacement' do
417+
expect_offense(<<~RUBY)
418+
it %q(should return "foo") do
419+
^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
420+
end
421+
RUBY
422+
423+
expect_correction(<<~RUBY)
424+
it %q(returns "foo") do
425+
end
426+
RUBY
427+
end
428+
end
429+
430+
context 'when message includes `"` and `)` in `%q(...)`' do
431+
it 'corrects message with `String#inspect`' do
432+
expect_offense(<<~RUBY)
433+
it %q(should return "foo (bar)") do
434+
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
435+
end
436+
RUBY
437+
438+
expect_correction(<<~'RUBY')
439+
it "returns \"foo (bar)\"" do
440+
end
441+
RUBY
442+
end
443+
end
354444
end
355445
end

0 commit comments

Comments
 (0)