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
Add AllowedPatterns
configuration option to RSpec/ContextWording
#1358
Add AllowedPatterns
configuration option to RSpec/ContextWording
#1358
Conversation
7fa2a1c
to
25ed74f
Compare
end | ||
|
||
# good | ||
context 'when this test is success' 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'd really prefer this to be in an RTL language, as with LTR it's hard to imagine anyone willing to constrain descriptions to be suffixed with a limited set of words.
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 agree, an RTL language example would work much better here.
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.
If it is an RTL language, it appears that if /olloeh$/ is specified, it will correctly check if it ends in olloeh as an RTL language.
# bad
context '!sknaht olleh'
end
# good
context 'olleh !sknaht'
end
How about using Japanese as an example, as in the case of the original Issue?
Should we consider changing the cop to be configured with regular expressions instead of prefixes and suffixes? Then people can use them however they like. The current defaults would need to be changed to e.g. RSpec/ContextWording:
Prefixes:
- "^when\b"
- "^with\b"
- "^without\b" |
Certainly we might consider changing the cop to be composed of regular expressions. Would it be better to allow regular expressions to be set with names like |
I'd keep |
7e390b8
to
6fd5d9b
Compare
Suffixes
configuration option to RSpec/ContextWording
AllowedPatterns
configuration option to RSpec/ContextWording
The following PR is needed: |
e496c23
to
c6a0b8d
Compare
c6a0b8d
to
9f7cdd7
Compare
Conflicts have been resolved. |
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 we also need a spec for when Prefixes is empty, but AllowedPatterns is not.
end | ||
|
||
# good | ||
context 'when this test is success' 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 agree, an RTL language example would work much better here.
67125e3
to
b6ba8de
Compare
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 am sorry for stalling this pull request, but there are still a few things I would like to change 😅
I think we should stop treating the Prefixes as anything special. I think if we override the #allowed_patterns
method to include the prefixes, we can get rid of PREFIX_MSG
, BOTH_MSG
and a bunch of methods. E.g.
def allowed_patterns
@allowed_patterns ||= begin
prefix_regexes = prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ }
super.concat(prefix_regexes)
end
end
(Without the memoization it seems that the list grows unbounded. I didn’t have time to check why that happens)
Another thing to be aware of is that “#inspect actually produces the more natural version of the [Regex] string than #to_s”. So, by using "'#{value.inspect}'"
instead of "'#{value}'"
, we can have offense messages with '/とき$/'
instead of '(?-mix:とき$)'
. (That's also why I chose to map
the Regexp.escape
d prefixes – that way they look better in offense messages).
This is of course up for discussion. I’m looking forward to hearing your opinion.
b6ba8de
to
d7c629a
Compare
If it makes it better, even if it takes longer, it's worth it ;-) The approach of overriding the I also think it would be a good idea to use |
1030731
to
8ea4ae0
Compare
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 so much @ydah 🙏🏼
8ea4ae0
to
1efcaae
Compare
@@ -212,8 +212,9 @@ RSpec/ContextWording: | |||
- when | |||
- with | |||
- without | |||
AllowedPatterns: [] |
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.
Should we have default allowed patterns, matching the prefixes, and deprecate the prefixes option?
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 am certainly beginning to think that is a good.
@bquorning @pirj What do you think? I'd like to hear your opinion.
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 we can do that in a follow-up PR. We should also consider rewriting the cop documentation to mention AllowedPatterns before Prefixes.
@@ -13,15 +15,15 @@ | |||
it 'finds context without `when` at the beginning' do | |||
expect_offense(<<-RUBY) | |||
context 'the display name not present' do | |||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Start context description with 'when', or 'with'. | |||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Context description should match /^when\\b/, or /^with\\b/. |
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.
with love to see an example with 3 patterns
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.
Indeed it is. I updated it:
rubocop-rspec/spec/rubocop/cop/rspec/context_wording_spec.rb
Lines 15 to 21 in c017ffd
it 'finds context without `when` at the beginning' do | |
expect_offense(<<-RUBY) | |
context 'the display name not present' do | |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Context description should match /^when\\b/, /^with\\b/, or /^without\\b/. | |
end | |
RUBY | |
end |
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
1efcaae
to
c017ffd
Compare
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.
Happy to see the cop works for RTL now, too.
Just a small optional subjective note, maybe an idea for a follow-up if it gets some support.
MSG = 'Start context description with %<prefixes>s.' | ||
include AllowedPattern | ||
|
||
MSG = 'Context description should match %<patterns>s.' |
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'm uncertain that I'm happy with this change.
From my perspective, the cognitive gap between "Start description with" and "Description should match /@$&#%&$$/" is noticeable.
Wondering if it's easy to use the old message when only Prefixes
are defined, and the new one when AllowedPatterns
are defined, too.
Or a mix of like "Context description should either start with % or match %".
Resolve: #1356
This PR is add the
AllowedPattern
option.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
indefault/config.yml
to the next minor version.If you have modified an existing cop's configuration options:
VersionChanged
inconfig/default.yml
to the next major version.