Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSpec/ExampleWording unintentionally removes escapes #1799

Open
r7kamura opened this issue Feb 6, 2024 · 2 comments
Open

RSpec/ExampleWording unintentionally removes escapes #1799

r7kamura opened this issue Feb 6, 2024 · 2 comments
Labels

Comments

@r7kamura
Copy link
Contributor

r7kamura commented Feb 6, 2024

I noticed that it autocorrects as follows, which causes syntax error:

# before
it 'should return foo\'s bar' do
end

# after
it 'returns foo's bar' do
end

This can be confirmed by the following test code.

diff --git a/spec/rubocop/cop/rspec/example_wording_spec.rb b/spec/rubocop/cop/rspec/example_wording_spec.rb
index 100ae719..ae83c6ac 100644
--- a/spec/rubocop/cop/rspec/example_wording_spec.rb
+++ b/spec/rubocop/cop/rspec/example_wording_spec.rb
@@ -351,5 +351,18 @@ RSpec.describe RuboCop::Cop::RSpec::ExampleWording do
         end
       RUBY
     end
+
+    it 'keeps escaped single-quote after autocorrection' do
+      expect_offense(<<~'RUBY')
+        it 'should return foo\'s bar' do
+            ^^^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
+        end
+      RUBY
+
+      expect_correction(<<~'RUBY')
+        it 'returns foo\'s bar' do
+        end
+      RUBY
+    end
   end
 end
Failures:

  1) RuboCop::Cop::RSpec::ExampleWording when `DisallowedExamples: Workz` keeps escaped single-quote after autocorrection
     Failure/Error:
       expect_correction(<<~'RUBY')
         it 'returns foo\'s bar' do
         end
       RUBY
     
       expected: "it 'returns foo\\'s bar' do\nend\n"
            got: "it 'returns foo's bar' do\nend\n"
     
       (compared using ==)
     
       Diff:
       @@ -1 +1 @@
       -it 'returns foo\'s bar' do
       +it 'returns foo's bar' do
       
     # ./spec/rubocop/cop/rspec/example_wording_spec.rb:362:in `block (3 levels) in <top (required)>'

Since this cop supports not only str but also dstr, it would be difficult to simply fix it with String#inspect 🤔

@pirj
Copy link
Member

pirj commented Feb 6, 2024

We’ve been discussing this in #1189 and closed it as won’t fix.

this cop supports not only str but also dstr, it would be difficult to simply fix it with String#inspect

Would it be possible to at least fix the str cases? Would you want to give the fix a try, @r7kamura ?

@r7kamura
Copy link
Contributor Author

r7kamura commented Feb 7, 2024

We’ve been discussing this in #1189 and closed it as won’t fix.

Sorry, I didn't realize there was already an issue in the past.

I created a pull request to work on this issue:

@ydah ydah added the bug label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants