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

Documentation request: some guidance on how to write a rubocop extension with custom cops #2040

Closed
maxjacobson opened this issue Jul 14, 2015 · 11 comments

Comments

@maxjacobson
Copy link
Contributor

Or maybe just some tips on which direction to start looking 😄

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 16, 2015

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. :-)

@maxjacobson
Copy link
Contributor Author

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 😄 (that one is kind of a joke)

@maxjacobson
Copy link
Contributor Author

@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 😄

@adipasquale
Copy link

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 !

@jawshooah
Copy link
Contributor

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.

@maxjacobson
Copy link
Contributor Author

Whoa, that's intense

@Dbz
Copy link

Dbz commented Feb 28, 2017

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.

@horaciob
Copy link

horaciob commented Jul 1, 2017

+1

jonatas pushed a commit to jonatas/rubocop that referenced this issue Oct 17, 2017
@maxjacobson
Copy link
Contributor Author

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

@jonatas
Copy link
Contributor

jonatas commented Oct 18, 2017

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 🎉

@Drenmi
Copy link
Collaborator

Drenmi commented Oct 18, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants