-
-
Notifications
You must be signed in to change notification settings - Fork 269
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,41 +35,72 @@ module RSpec | |
# # ... | ||
# end | ||
# | ||
# This cop can be customized allowed context description pattern | ||
# with `AllowedPatterns`. By default, there are no checking by pattern. | ||
# | ||
# @example `AllowedPatterns` configuration | ||
# | ||
# # .rubocop.yml | ||
# # RSpec/ContextWording: | ||
# # AllowedPatterns: | ||
# # - /とき$/ | ||
# | ||
# @example | ||
# # bad | ||
# context '条件を満たす' do | ||
# # ... | ||
# end | ||
# | ||
# # good | ||
# context '条件を満たすとき' do | ||
# # ... | ||
# end | ||
# | ||
class ContextWording < Base | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I'm uncertain that I'm happy with this change. Wondering if it's easy to use the old message when only |
||
|
||
# @!method context_wording(node) | ||
def_node_matcher :context_wording, <<-PATTERN | ||
(block (send #rspec? { :context :shared_context } $(str #bad_prefix?) ...) ...) | ||
(block (send #rspec? { :context :shared_context } $(str $_) ...) ...) | ||
PATTERN | ||
|
||
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler | ||
context_wording(node) do |context| | ||
add_offense(context, | ||
message: format(MSG, prefixes: joined_prefixes)) | ||
context_wording(node) do |context, description| | ||
if bad_pattern?(description) | ||
message = format(MSG, patterns: expect_patterns) | ||
add_offense(context, message: message) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def bad_prefix?(description) | ||
!prefix_regex.match?(description) | ||
def allowed_patterns | ||
super + prefix_regexes | ||
end | ||
|
||
def joined_prefixes | ||
quoted = prefixes.map { |prefix| "'#{prefix}'" } | ||
return quoted.first if quoted.size == 1 | ||
def prefix_regexes | ||
@prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ } | ||
end | ||
|
||
def bad_pattern?(description) | ||
return false if allowed_patterns.empty? | ||
|
||
quoted << "or #{quoted.pop}" | ||
quoted.join(', ') | ||
!matches_allowed_pattern?(description) | ||
end | ||
|
||
def prefixes | ||
cop_config['Prefixes'] || [] | ||
def expect_patterns | ||
inspected = allowed_patterns.map(&:inspect) | ||
return inspected.first if inspected.size == 1 | ||
|
||
inspected << "or #{inspected.pop}" | ||
inspected.join(', ') | ||
end | ||
|
||
def prefix_regex | ||
/^#{Regexp.union(prefixes)}\b/ | ||
def prefixes | ||
Array(cop_config.fetch('Prefixes', [])) | ||
end | ||
end | ||
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.
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.