Skip to content

Instantly share code, notes, and snippets.

@DNNX DNNX/ README.md
Last active Jan 8, 2016

Embed
What would you like to do?
Rubocop between-cop interdependencies

I wrote quick&dirty script to see the dependencies between cops (i.e. when a cop calls config.for_cop(another_cop)).

Strangely enough, Lint/AmbiguousOperator needs everyone else's configs. I excluded it from the diagram in order to keep it clean.

Other than that, the dependency graph has 28 edges.

diff --git a/lib/rubocop/cop/cop.rb b/lib/rubocop/cop/cop.rb
index 1859218..d781f98 100644
--- a/lib/rubocop/cop/cop.rb
+++ b/lib/rubocop/cop/cop.rb
@@ -114,8 +114,23 @@ module RuboCop
given_names.include?(cop_type.to_s.capitalize)
end
+require 'set'
+KNOWN_CONFIGS = File.readlines('ress.csv').map {|x| x.chomp.split(',')}.to_set
+DEPS = Set.new
+
def initialize(config = nil, options = nil)
- @config = config || Config.new
+ @config = c = config || Config.new
+ name = self.class.cop_name
+ if KNOWN_CONFIGS.add?(c)
+ c.define_singleton_method(:for_cop) do |cop|
+ d = [name, cop.respond_to?(:cop_name) ? cop.cop_name : cop]
+ if DEPS.add?(d)
+ File.open('ress.csv', 'a+') { |f| f.puts(d.join(',')) }
+ end
+ super(cop)
+ end
+ end
+
@options = options || { debug: false }
@offenses = []
dot -Tpng rubo.dot -o rubocop.png
digraph Rubocop {
rankdir=RL
"Metrics/LineLength" -> "Style/IfUnlessModifier"
"Style/SpaceInsideBlockBraces" -> "Style/SpaceAfterSemicolon"
"Style/BlockDelimiters" -> "Style/SpaceInsideBlockBraces"
"IndentationWidth" -> "Style/MultilineOperationIndentation"
"Style/IndentationWidth" -> "Style/MultilineOperationIndentation"
"Lint/EndAlignment" -> "Style/ElseAlignment"
"Style/MissingElse" -> "Style/EmptyElse"
"Style/CollectionMethods" -> "Performance/Detect"
"Style/BlockDelimiters" -> "Style/SpaceBeforeBlockBraces"
"IndentationWidth" -> "Style/MultilineMethodCallIndentation"
"Style/IndentationWidth" -> "Style/MultilineMethodCallIndentation"
"Style/PercentLiteralDelimiters" -> "Style/RegexpLiteral"
"Style/UnlessElse" -> "Style/MissingElse"
"Style/EmptyElse" -> "Style/MissingElse"
"IndentationWidth" -> "Style/AccessModifierIndentation"
"IndentationWidth" -> "Style/CaseIndentation"
"Style/PercentLiteralDelimiters" -> "Style/CommandLiteral"
"IndentationWidth" -> "Style/IndentArray"
"IndentationWidth" -> "Style/FirstParameterIndentation"
"IndentationWidth" -> "Style/IndentAssignment"
"Style/AlignHash" -> "Style/SpaceAroundOperators"
"IndentationWidth" -> "Style/AlignParameters"
"Style/IndentationConsistency" -> "Style/IndentationWidth"
"Style/CommentAnnotation" -> "Style/Documentation"
"Style/AlignHash" -> "Style/IndentHash"
"IndentationWidth" -> "Style/IndentHash"
"IndentationWidth" -> "Style/SingleLineMethods"
"Style/StringLiterals" -> "Style/EmptyLiteral"
}
# copied from irb
data = File.readlines('ress.csv').map {|x| x.chomp.split(',')}.reject{|x,y| x == y || x == 'Lint/AmbiguousOperator' }
File.open('rubo.dot', 'w') { |f| f.puts('digraph Rubocop {'); f.puts(" rankdir=LR"); f.puts(data.map {|x,y| " \"#{x}\" -> \"#{y}\"" }); f.puts('}') }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.