-
-
Notifications
You must be signed in to change notification settings - Fork 277
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
Fix RSpec/ExampleWording
in escaping and %
string literals
#1800
base: master
Are you sure you want to change the base?
Conversation
This cop has not been able to handle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Thank you.
@@ -106,6 +110,11 @@ def docstring(node) | |||
) | |||
end | |||
|
|||
def needs_escape?(node) | |||
node.value.include?(node.loc.begin.source) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For quoted strings, both begin and end are the same. Do we need to check twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for %q(...)
, %Q(...)
, or %<...>
etc.
irb(#<RuboCop::Cop::RSpec::Exampl...):001> node.loc.begin.source
=> "%q("
irb(#<RuboCop::Cop::RSpec::Exampl...):002> node.loc.end.source
=> ")"
But on second thought, we must check against node.loc.begin.source[-1]
, not node.loc.begin.source
. I'll try to fix that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, since escaping should only be necessary if begin and end are different (e.g. %!...!
may require escaping like \!
, while %(...)
does not), it seems that only one of them needs to be checked after all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
I’d love to extensively test this, but only the quoting autocorrection part. Wondering how to do that.
6b470ba
to
ffc6a8c
Compare
RSpec/ExampleWording
autocorrection to correctly escape quotes on str node caseRSpec/ExampleWording
in escaping and %
string literals
I've added a few more improvements:
Since the handling of |
35ffb03
to
2ef3761
Compare
@@ -351,5 +351,95 @@ | |||
end | |||
RUBY | |||
end | |||
|
|||
context "when message includes `'` in `'...'`" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these examples are in the wrong context ('when
DisallowedExamples: Workz'
).
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
it "returns foo's bar" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it’s a silly question, but why are we changing the type of quotes from '
to "
here? If we kept the same quote type, we wouldn’t need to concern ourselves with escaping, would we? (unless we insert an apostrophe/single quote)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for implementation convenience. If there is an implementation like string.inspect(quote_type: 'single_quote')
, I would like to use it. If there is a way to rewrite only substrings while preserving quoting and escaping, that would be great, but do you have any other ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a just idea, but, if the offense range had been like this, we might not have to go through all this trouble?
it "should return foo's bar"
^^^^^^^^^^^^^
then we would only need to replace this range without changing the unrelated part that may contain troublesome
escapes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic idea!
…on str node case
2ef3761
to
d860600
Compare
This change resolves some of the following Issue:
RSpec/ExampleWording
unintentionally removes escapes #1799It still does not handle quotes well for dstr node, but at least it will handle them well for str node.
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).CHANGELOG.md
if the new code introduces user-observable changes.bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml
.Enabled: pending
inconfig/default.yml
.Enabled: true
in.rubocop.yml
.VersionAdded: "<<next>>"
indefault/config.yml
.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"
inconfig/default.yml
.