Skip to content

Commit

Permalink
🐛 makes subgraph names unique in generated dot
Browse files Browse the repository at this point in the history
this fixes the problem that dot complains about > 1 cluster having the same name when two or more folders in different spots in the dependency graph have the same name
  • Loading branch information
sverweij committed Nov 25, 2016
1 parent fc06dbc commit 3a25097
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/report/dot.template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ digraph "dependency-cruiser output"{
edge [color=black arrowhead=normal fontname="Helvetica" fontsize="9"]

{{#each things}}
{{#if folder}}{{#each path}}subgraph "cluster_{{{.}}}" {label="{{{.}}}" style="rounded" {{/each}}"{{{source}}}" [label="{{{label}}}"]{{#each path}} }{{/each}}
{{#if folder}}{{#each path}}subgraph "cluster_{{{aggregateDir}}}" {label="{{{dir}}}" style="rounded" {{/each}}"{{{source}}}" [label="{{{label}}}"]{{#each path}} }{{/each}}
{{else~}}
"{{{source}}}"
{{/if}}
Expand Down
6 changes: 3 additions & 3 deletions src/report/dot.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ templates['dot.template.hbs'] = template({"1":function(container,depth0,helpers,
+ ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.path : depth0),{"name":"each","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "\n";
},"3":function(container,depth0,helpers,partials,data) {
var stack1, alias1=container.lambda;
var stack1, helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function";

return "subgraph \"cluster_"
+ ((stack1 = alias1(depth0, depth0)) != null ? stack1 : "")
+ ((stack1 = ((helper = (helper = helpers.aggregateDir || (depth0 != null ? depth0.aggregateDir : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"aggregateDir","hash":{},"data":data}) : helper))) != null ? stack1 : "")
+ "\" {label=\""
+ ((stack1 = alias1(depth0, depth0)) != null ? stack1 : "")
+ ((stack1 = ((helper = (helper = helpers.dir || (depth0 != null ? depth0.dir : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"dir","hash":{},"data":data}) : helper))) != null ? stack1 : "")
+ "\" style=\"rounded\" ";
},"5":function(container,depth0,helpers,partials,data) {
return " }";
Expand Down
16 changes: 10 additions & 6 deletions src/report/dotReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ const Handlebars = require("handlebars/dist/cjs/handlebars.runtime");

require("./dot.template");

Handlebars.registerPartial(
'dot.template.hbs',
Handlebars.templates['dot.template.hbs']
);

function compareOnSource(pOne, pTwo) {
return pOne.source > pTwo.source ? 1 : -1;
}

let toFullPath = (pAll, pCurrent) => `${pAll}${path.sep}${pCurrent}`;

function agg (pFolder, i, ary){
return {
dir: pFolder,
aggregateDir: `${ary.slice(0, i).reduce(toFullPath, '')}${path.sep}${pFolder}`
};
}

function folderify(pDependencyItem) {
let lAdditions = {};
let lDirName = path.dirname(pDependencyItem.source);

if (lDirName !== ".") {
lAdditions.folder = lDirName;
lAdditions.path = lDirName.split(path.sep);
lAdditions.path = lDirName.split(path.sep).map(agg);
}

lAdditions.label = path.basename(pDependencyItem.source);
Expand Down
11 changes: 11 additions & 0 deletions test/cli.index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ const testPairs = [
expect: "{{moduleType}}.dir.filtered.dot",
cleanup: true
},
{
description: "dot - duplicate subs",
dirOrFile: "test/fixtures/duplicate-subs",
options: {
outputTo: path.join(OUT_DIR, "duplicate-subs.dot"),
outputType: "dot",
exclude: "node_modules"
},
expect: "duplicate-subs.dot",
cleanup: true
},
{
description: "csv",
dirOrFile: "test/fixtures/{{moduleType}}",
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/cjs.dir.filtered.dot
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ digraph "dependency-cruiser output"{
node [shape=box style="rounded, filled" fillcolor="#ffffcc" height=0.2 fontname=Helvetica fontsize=9]
edge [color=black arrowhead=normal fontname="Helvetica" fontsize="9"]

subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" "test/fixtures/cjs/one_only_one.js" [label="one_only_one.js"] } } }
subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" "test/fixtures/cjs/one_only_two.js" [label="one_only_two.js"] } } }
subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" "test/fixtures/cjs/root_one.js" [label="root_one.js"] } } }
subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" "test/fixtures/cjs/root_two.js" [label="root_two.js"] } } }
subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" "test/fixtures/cjs/shared.js" [label="shared.js"] } } }
subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" subgraph "cluster_sub" {label="sub" style="rounded" "test/fixtures/cjs/sub/depindir.js" [label="depindir.js"] } } } }
subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" subgraph "cluster_sub" {label="sub" style="rounded" "test/fixtures/cjs/sub/dir.js" [label="dir.js"] } } } }
subgraph "cluster_test" {label="test" style="rounded" subgraph "cluster_fixtures" {label="fixtures" style="rounded" subgraph "cluster_cjs" {label="cjs" style="rounded" "test/fixtures/cjs/two_only_one.js" [label="two_only_one.js"] } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" "test/fixtures/cjs/one_only_one.js" [label="one_only_one.js"] } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" "test/fixtures/cjs/one_only_two.js" [label="one_only_two.js"] } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" "test/fixtures/cjs/root_one.js" [label="root_one.js"] } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" "test/fixtures/cjs/root_two.js" [label="root_two.js"] } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" "test/fixtures/cjs/shared.js" [label="shared.js"] } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" subgraph "cluster_/test/fixtures/cjs/sub" {label="sub" style="rounded" "test/fixtures/cjs/sub/depindir.js" [label="depindir.js"] } } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" subgraph "cluster_/test/fixtures/cjs/sub" {label="sub" style="rounded" "test/fixtures/cjs/sub/dir.js" [label="dir.js"] } } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/cjs" {label="cjs" style="rounded" "test/fixtures/cjs/two_only_one.js" [label="two_only_one.js"] } } }

"test/fixtures/cjs/one_only_one.js" -> "path" [color="grey"]
"test/fixtures/cjs/one_only_two.js" -> "path" [color="grey"]
Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/duplicate-subs.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
digraph "dependency-cruiser output"{
ordering=out
rankdir=LR
splines=true
overlap=false
nodesep=0.16
fontname="Helvetica"
fontsize="9"
compound=true
node [shape=box style="rounded, filled" fillcolor="#ffffcc" height=0.2 fontname=Helvetica fontsize=9]
edge [color=black arrowhead=normal fontname="Helvetica" fontsize="9"]

subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs" {label="duplicate-subs" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/nested-sub" {label="nested-sub" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/nested-sub/sub" {label="sub" style="rounded" "test/fixtures/duplicate-subs/nested-sub/sub/index.js" [label="index.js"] } } } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs" {label="duplicate-subs" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/nested-sub" {label="nested-sub" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/nested-sub/sub" {label="sub" style="rounded" "test/fixtures/duplicate-subs/nested-sub/sub/sub.js" [label="sub.js"] } } } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs" {label="duplicate-subs" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/nested-sub" {label="nested-sub" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/nested-sub/sub" {label="sub" style="rounded" "test/fixtures/duplicate-subs/nested-sub/sub/wakka.js" [label="wakka.js"] } } } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs" {label="duplicate-subs" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/sub" {label="sub" style="rounded" "test/fixtures/duplicate-subs/sub/index.js" [label="index.js"] } } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs" {label="duplicate-subs" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/sub" {label="sub" style="rounded" "test/fixtures/duplicate-subs/sub/more-in-sub.js" [label="more-in-sub.js"] } } } }
subgraph "cluster_/test" {label="test" style="rounded" subgraph "cluster_/test/fixtures" {label="fixtures" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs" {label="duplicate-subs" style="rounded" subgraph "cluster_/test/fixtures/duplicate-subs/sub" {label="sub" style="rounded" "test/fixtures/duplicate-subs/sub/most-in-sub.js" [label="most-in-sub.js"] } } } }

"test/fixtures/duplicate-subs/nested-sub/sub/index.js" -> "test/fixtures/duplicate-subs/nested-sub/sub/sub.js"
"test/fixtures/duplicate-subs/nested-sub/sub/index.js" -> "test/fixtures/duplicate-subs/nested-sub/sub/wakka.js"
"test/fixtures/duplicate-subs/nested-sub/sub/sub.js" -> "test/fixtures/duplicate-subs/nested-sub/sub/wakka.js"
"test/fixtures/duplicate-subs/sub/index.js" -> "test/fixtures/duplicate-subs/nested-sub/sub/index.js"
}
4 changes: 4 additions & 0 deletions test/fixtures/duplicate-subs/nested-sub/sub/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const wakka = require('./wakka');
const sub = require('./sub');

console.log('yo from nested-sub/sub');
1 change: 1 addition & 0 deletions test/fixtures/duplicate-subs/nested-sub/sub/sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let sub = require('./wakka');
1 change: 1 addition & 0 deletions test/fixtures/duplicate-subs/nested-sub/sub/wakka.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.yo = () => 'nothing, really';
2 changes: 2 additions & 0 deletions test/fixtures/duplicate-subs/sub/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const sub = require('../nested-sub/sub');
console.log('yo from sub');
Empty file.
Empty file.
10 changes: 5 additions & 5 deletions test/report.dotReporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const elFixture = `digraph "dependency-cruiser output"{
node [shape=box style="rounded, filled" fillcolor="#ffffcc" height=0.2 fontname=Helvetica fontsize=9]
edge [color=black arrowhead=normal fontname="Helvetica" fontsize="9"]
subgraph "cluster_node_modules" {label="node_modules" style="rounded" subgraph "cluster_somemodule" {label="somemodule" style="rounded" subgraph "cluster_node_modules" {label="node_modules" style="rounded" subgraph "cluster_someothermodule" {label="someothermodule" style="rounded" "node_modules/somemodule/node_modules/someothermodule/main.js" [label="main.js"] } } } }
subgraph "cluster_node_modules" {label="node_modules" style="rounded" subgraph "cluster_somemodule" {label="somemodule" style="rounded" subgraph "cluster_src" {label="src" style="rounded" "node_modules/somemodule/src/moar-javascript.js" [label="moar-javascript.js"] } } }
subgraph "cluster_node_modules" {label="node_modules" style="rounded" subgraph "cluster_somemodule" {label="somemodule" style="rounded" subgraph "cluster_src" {label="src" style="rounded" "node_modules/somemodule/src/somemodule.js" [label="somemodule.js"] } } }
subgraph "cluster_/node_modules" {label="node_modules" style="rounded" subgraph "cluster_/node_modules/somemodule" {label="somemodule" style="rounded" subgraph "cluster_/node_modules/somemodule/node_modules" {label="node_modules" style="rounded" subgraph "cluster_/node_modules/somemodule/node_modules/someothermodule" {label="someothermodule" style="rounded" "node_modules/somemodule/node_modules/someothermodule/main.js" [label="main.js"] } } } }
subgraph "cluster_/node_modules" {label="node_modules" style="rounded" subgraph "cluster_/node_modules/somemodule" {label="somemodule" style="rounded" subgraph "cluster_/node_modules/somemodule/src" {label="src" style="rounded" "node_modules/somemodule/src/moar-javascript.js" [label="moar-javascript.js"] } } }
subgraph "cluster_/node_modules" {label="node_modules" style="rounded" subgraph "cluster_/node_modules/somemodule" {label="somemodule" style="rounded" subgraph "cluster_/node_modules/somemodule/src" {label="src" style="rounded" "node_modules/somemodule/src/somemodule.js" [label="somemodule.js"] } } }
"one_only_one.js"
"one_only_two.js"
"root_one.js"
"root_two.js"
"shared.js"
subgraph "cluster_sub" {label="sub" style="rounded" "sub/depindir.js" [label="depindir.js"] }
subgraph "cluster_sub" {label="sub" style="rounded" "sub/dir.js" [label="dir.js"] }
subgraph "cluster_/sub" {label="sub" style="rounded" "sub/depindir.js" [label="depindir.js"] }
subgraph "cluster_/sub" {label="sub" style="rounded" "sub/dir.js" [label="dir.js"] }
"two_only_one.js"
"node_modules/somemodule/src/somemodule.js" -> "node_modules/somemodule/src/moar-javascript.js" [color="red" penwidth=2.0]
Expand Down

0 comments on commit 3a25097

Please sign in to comment.