-
-
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
WIP: Rspec aliases #921
WIP: Rspec aliases #921
Conversation
Since some of patterns like `hook?` should be available not only in cops, but also in "concepts", e.g. `ExampleGroup` - the solution would be to put config accessor shortcuts into mixin.
It fixes ScatteredSetup cop specs as well.
Another implementation is here Probably it's a really edge case when you need to specify different aliases in different directories. I would even say no one would do that. So maybe it worths just to go with top level config for aliases. In this case all the patterns may be kept unchanged, so only |
Agree 👍 |
Good headstart! |
Thank you, @pirj! I've made several proofs of concepts, but decided not to open multiple PRs against this repository's master and just refer PRs here instead.
|
Sorry again for taking so long to review. Wow, that is impressive! I really like them all, let's discuss which approach to take. There's a generic problem I've noticed while playing around with this thing, not related to your code, but to how RuboCop works. We can't load the config using regular RuboCop's mechanisms before our classes are fully loaded. Config validation is an inherent part of its config loading, along with checking if cops configured in the config do exist. Another thing to keep in mind is that there are multiple ways how config is defined, it can be a It's definitely not a trivial task to keep up with all of that. I really like how the config file is structured. It's a regular RuboCop config file. This will work fine for a typical RuboCop installation. This solution is so simple and elegant I'm inclined to go with this without even looking at the other options.
Agree 👍 One thing that makes me think is that we're putting the responsibility of keeping their
However, it's not a showstopper, as it will take a while to spread the word about those new configuration options. I believe we'll have to add a "config_loaded" callback to RuboCop or lazily initialize our
Seems similar to 3), but it implies that an additional config file is used. This limits us in the ability to push the responsibility to Pundit and other gems. Hope you can grab something from my random thoughts. Please don't hesitate to share your thoughts on early stages here or on the ticket. |
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.
A couple of minor implementation notes.
When it comes to the Language module, we don't have any complex patterns, we mostly check the method name. So we can easily replace those with dynamic checks on the name, and checking for the node type (if needed). I was even thinking on extending it to provide on_example_group, on_hook, etc., like we have on_top_level_describe, but didn't find enough cops that would benefit (in terms of code simplification) from such a change. But with the customizable language, it may yield results.
@@ -27,12 +27,14 @@ class EmptyHook < Cop | |||
|
|||
MSG = 'Empty hook detected.' | |||
|
|||
def_node_matcher :empty_hook?, <<~PATTERN | |||
(block $#{Hooks::ALL.send_pattern} _ nil?) | |||
def_node_matcher :empty_block?, <<~PATTERN |
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.
Do we really need a matcher to tell that a block is empty?
add_offense(hook) is node.body.nil?
should do the job
@@ -15,11 +15,18 @@ module NodePattern | |||
|
|||
def_node_matcher :example?, Examples::ALL.block_pattern | |||
|
|||
def_node_matcher :hook?, Hooks::ALL.block_pattern | |||
def hook?(node) | |||
block_pattern(node) do |hook| |
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 don't think you need a pattern for this.
return false unless node.block_type?
hooks.include?(node.method_name)
Amazing! I'll try to look into that way definitely. |
@sl4vr Do you need any help here? |
@pirj Not yet, just trying to find time to proceed. Thanks for help offering, I'll report here if I'll face any new difficulties. |
Closing in favour of #956 |
This is an attempt of implementation of #333. Allowing to configure 3rd partly DSLs as valid RSpec syntax.
Suggested configuration:
The problem is that if we need to be able to consider configuration regarding to every file - we need somehow build dynamic matchers for hooks, example_groups, etc.
AFAICU
def_node_matcher
defines matcher in class on load, so we cannot mutate it during runtime (at least it looks complicated), so instead I tried to rewrite some matchers to be more common, and then inside them I check was it, for example, hook or not.Now I want to understand is this approach is viable or should I look into some other options?
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).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
.