Or maybe just some tips on which direction to start looking :smile:
Maybe taking a look here would help https://github.com/nevir/rubocop-rspec
We never actually got to creating an extension API, but hooking in RuboCop proper is pretty easy. I plan to spin the Rails stuff in a separate gem some day, hopefully this will motivate me to work on an a proper extension API. :-)
That sounds like a cool idea!
rubocop rspec seems cool, and works for me, but I wasn't sure how confident to feel about it as a learning example because it's based on a not-super-recent version of rubocop. I ended up focusing more on looking at the rubocop source code and got one working. I wasn't really sure if I was doing it right along the way but got it working :smile: (that one is kind of a joke)
@bbatsov just to follow-up on this one...
I'm a bit more familiar with rubocop internals than I was when I wrote this, and I've made a few extensions for my use which didn't feel appropriate to submit to the main project. I've just bumped into a limitation that might require the extension API, but I'm not sure.
Let's say I make a custom cop. By inheriting from Rubocop::Cop::Cop, it will get included in the rubocop checking process because it gets added to the CopStore because of this method.
Buuut what if I want to configure my custom cop to only be included for one file? I can try adding a line to my .rubocop.yml file, but then I get an error, because that cop isn't included in the default configuration, so it's not recognized. Is there currently a way to get around that?
I'd be happy to submit a pull request adding some explanation to the README about how to create a custom cop without an extension API for the sake of addressing people who shared my confusion from a few months ago, btw. Or do you think the extension API is coming soon? Happy to try and help on that too :smile:
One pitfall that could be mentioned in the Readme is that you should disable the cache when you're working on your custom cop (AllCops:UseCache)
Happy to add it if you agree @bbatsov :)
Also, I'm still having an 'unrecognized cop' warning with my custom cop even though it's poperly loaded and executed. I'll tell you when I find the solution to this too but if you have suggestions I'm interested !
Buuut what if I want to configure my custom cop to only be included for one file? I can try adding a line to my .rubocop.yml file, but then I get an error, because that cop isn't included in the default configuration, so it's not recognized. Is there currently a way to get around that?
Here's how rubocop-rspec got around it. Bummer that it has to be so invasive, but it works.
Also, I've started work on my own plugin for linting Homebrew-cask files: rubocop-cask. There's only one cop so far, but I'm working on adding more.
Whoa, that's intense
I am commenting because I would also like documentation on how to create custom cops (for those unfamiliar with the codebase). I have been searching and unable to find it.
+1
Wow, those docs look great, great job @jonatas.
I think it would still be valuable to have a section about how to make a cop that lives outside of the bbatsov/rubocop codebase, and rather in a gem like rubocop-rspec. I'm happy to leave this closed if you think that's already clear enough? @bbatsov
Well remembered @maxjacobson. I think we can create another section mentioning rubocop-rspec and other approaches like custom cops for a specific project. For example, you can create a internal folder in your project and create a custom cop for it simply mentioning the require in the .rubocop.yml configuration:
require:
- rubocop-rspec
- ./lib/custom-cop/rubocop/namespace/cop-name
It works in the same way 馃帀
For example, you can create a internal folder in your project and create a custom cop for it simply mentioning the require in the .rubocop.yml configuration.
This works, but there's a downside. You can't refer to it in the configuration file, so you can't make a custom cop and apply it to a single directory, for example. Fixing this is on my TODO list.
Most helpful comment
I am commenting because I would also like documentation on how to create custom cops (for those unfamiliar with the codebase). I have been searching and unable to find it.