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

Add AllowedPatterns configuration option to RSpec/ContextWording #1358

Merged

Conversation

ydah
Copy link
Member

@ydah ydah commented Aug 5, 2022

Resolve: #1356

This PR is add the AllowedPattern option.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (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:

  • [-] Added the new cop to config/default.yml.
  • [-] The cop is configured as Enabled: pending in config/default.yml.
  • [-] The cop is configured as Enabled: true in .rubocop.yml.
  • [-] The cop documents examples of good and bad code.
  • [-] The tests assert both that bad code is reported and that good code is not reported.
  • [-] Set VersionAdded in default/config.yml to the next minor version.

If you have modified an existing cop's configuration options:

  • Set VersionChanged in config/default.yml to the next major version.

@ydah ydah force-pushed the feature/suffix_support_context_wording branch from 7fa2a1c to 25ed74f Compare August 5, 2022 11:36
end

# good
context 'when this test is success' do
Copy link
Member

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.

Copy link
Collaborator

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.

Copy link
Member Author

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?

@bquorning
Copy link
Collaborator

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"

@ydah
Copy link
Member Author

ydah commented Aug 15, 2022

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 AllowedPattern instead of the option names Prefixes and Suffixes?
In that case, it seems to me that it would be better to make Prefixes obsoleted but not removed, and migrate slowly while ensuring backward compatibility.

@pirj
Copy link
Member

pirj commented Aug 16, 2022

I'd keep Prefixes and introduced a AllowedPatterns to support both suffixes and prefixes. WDYT?

@ydah ydah force-pushed the feature/suffix_support_context_wording branch 2 times, most recently from 7e390b8 to 6fd5d9b Compare August 16, 2022 08:00
@ydah ydah changed the title Add Suffixes configuration option to RSpec/ContextWording Add AllowedPatterns configuration option to RSpec/ContextWording Aug 16, 2022
@ydah
Copy link
Member Author

ydah commented Aug 16, 2022

The following PR is needed:

@ydah ydah force-pushed the feature/suffix_support_context_wording branch 2 times, most recently from e496c23 to c6a0b8d Compare August 25, 2022 11:37
@ydah ydah requested a review from pirj August 25, 2022 11:40
@pirj pirj requested review from Darhazer and bquorning and removed request for pirj August 26, 2022 08:05
@ydah ydah force-pushed the feature/suffix_support_context_wording branch from c6a0b8d to 9f7cdd7 Compare August 30, 2022 10:50
@ydah
Copy link
Member Author

ydah commented Aug 30, 2022

Conflicts have been resolved.

Copy link
Collaborator

@bquorning bquorning left a 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
Copy link
Collaborator

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.

spec/rubocop/cop/rspec/context_wording_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/context_wording_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/context_wording_spec.rb Outdated Show resolved Hide resolved
@ydah ydah force-pushed the feature/suffix_support_context_wording branch 2 times, most recently from 67125e3 to b6ba8de Compare August 31, 2022 08:51
Copy link
Collaborator

@bquorning bquorning left a 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.escaped prefixes – that way they look better in offense messages).

This is of course up for discussion. I’m looking forward to hearing your opinion.

@ydah ydah force-pushed the feature/suffix_support_context_wording branch from b6ba8de to d7c629a Compare August 31, 2022 22:08
@ydah
Copy link
Member Author

ydah commented Aug 31, 2022

I am sorry for stalling this pull request, but there are still a few things I would like to change 😅

If it makes it better, even if it takes longer, it's worth it ;-)
I sincerely appreciate your always thoughtful reviews. @bquorning @Darhazer @pirj

The approach of overriding the #allowed_patterns method without special attention to Prefixes seems so good.
It also keeps the code very simple and seems to improve the outlook of the ContextWording class, which was a bit bloated.
This would also unify the messages and make judging very simple.

I also think it would be a good idea to use Object#inspect in the violation message.
In that case, there is no need to further quoted string like a #{"'v.inspect'"}, so I put the result of Object#inspect directly into the offense message, what do you think?

@ydah ydah requested review from bquorning and pirj and removed request for Darhazer, bquorning and pirj August 31, 2022 22:47
@bquorning bquorning dismissed their stale review September 1, 2022 07:46

Changes were applied

@ydah ydah force-pushed the feature/suffix_support_context_wording branch 2 times, most recently from 1030731 to 8ea4ae0 Compare September 1, 2022 08:00
@bquorning bquorning requested a review from pirj September 1, 2022 08:34
Copy link
Collaborator

@bquorning bquorning left a 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 🙏🏼

@ydah ydah force-pushed the feature/suffix_support_context_wording branch from 8ea4ae0 to 1efcaae Compare September 1, 2022 10:42
@@ -212,8 +212,9 @@ RSpec/ContextWording:
- when
- with
- without
AllowedPatterns: []
Copy link
Member

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?

Copy link
Member Author

@ydah ydah Sep 1, 2022

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.

Copy link
Collaborator

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/.
Copy link
Member

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

Copy link
Member Author

@ydah ydah Sep 1, 2022

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:

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@ydah ydah force-pushed the feature/suffix_support_context_wording branch from 1efcaae to c017ffd Compare September 1, 2022 10:55
@Darhazer Darhazer merged commit bfaa66e into rubocop:master Sep 1, 2022
Copy link
Member

@pirj pirj left a 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.'
Copy link
Member

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 %".

@ydah ydah deleted the feature/suffix_support_context_wording branch January 23, 2023 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Support suffix for RSpec/ContextWording
4 participants