I am working on a tool to analyze how rubocop is used (enabled/disabled) and fixed (excluded into todo lists) in certain areas of the codebase. Given the multitude of config options, I think I am looking for a way to programmatically ask RuboCop whether given a file or folder, a certain rule is enabled and whether this file is excluded via a todo or not. Is there an API for this?
I have so far spelunked in https://github.com/rubocop/rubocop to see if I could find an API... Searched via SO and google and can't find anything.
I don't think there's public interface for what you want. This
mobilize_teammethod in the Runner is what instantiates aRuboCop::Cop::Team. The team object determines the cops to use in thisroundup_relevant_copsmethod. Both of these methods are private, but you could usesendto work around that.For example, say that we have a project with two files called
test_one.rbandtest_two.rb.The
.rubocop_todo.ymlsays that one of the files still needs to be handled:The
.rubocop.ymlconfig says that we just care aboutStyle/FrozenStringLiteralComment:We can see the cops that RuboCop will apply to each file if we do this:
This will show this output:
Because of this special condition in
enable_cop?theLint/Syntaxcop is always enabled. And sincetest_one.rbisn't included in the TODO list, it is the only file that qualifies for theStyle/FrozenStringLiteralCommentcop.