File tree Expand file tree Collapse file tree 3 files changed +69
-7
lines changed Expand file tree Collapse file tree 3 files changed +69
-7
lines changed Original file line number Diff line number Diff line change 12
12
- Support correcting ` assert_not_equal ` and ` assert_not_nil ` in ` RSpec/Rails/MinitestAssertions ` . ([ @G-Rath ] )
13
13
- Fix a false positive for ` RSpec/ExpectActual ` when used with rspec-rails routing matchers. ([ @naveg ] )
14
14
- Add new ` RSpec/RepeatedSubjectCall ` cop. ([ @drcapulet ] )
15
+ - Fix ` RSpec/ExampleWording ` autocorrection to correctly escape quotes on str node case. ([ @r7kamura ] )
15
16
16
17
## 2.26.1 (2024-01-05)
17
18
Original file line number Diff line number Diff line change @@ -92,18 +92,27 @@ def add_wording_offense(node, message)
92
92
add_offense ( docstring , message : message ) do |corrector |
93
93
next if node . heredoc?
94
94
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
96
100
end
97
101
end
98
102
99
103
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
101
113
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 )
107
116
end
108
117
109
118
def replacement_text ( node )
Original file line number Diff line number Diff line change 351
351
end
352
352
RUBY
353
353
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
354
406
end
355
407
end
You can’t perform that action at this time.
0 commit comments