Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

getChain bug #5

Closed
wants to merge 1 commit into from

2 participants

@yuest

No description provided.

Yuest Wang getChain bug 38793c9
@TrevorBurnham

Could you provide a test case where adding the when parent in deps clause affects the result?

@yuest

yuest@YuestAtWork:~/Dropbox/github/dep-graph(master)$ cake test
The "sys" module is now called "util". It should have a similar interface.
execvp(): No such file or directory

/home/yuest/Dropbox/github/dep-graph/node_modules/watch-tree/lib/watchers/stat.js:148
throw err;
^
Error: ENOTDIR, not a directory 'src/dep-graph.coffee'

@TrevorBurnham

Yeah, watch-tree was broken by Node 0.6. Fixed by b244d1f. Pull that and try again.

@yuest

I'll send another pull request

@yuest yuest closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 23, 2012
  1. getChain bug

    Yuest Wang authored
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 19 deletions.
  1. +27 −16 lib/dep-graph.js
  2. +2 −3 src/dep-graph.coffee
View
43 lib/dep-graph.js
@@ -1,16 +1,16 @@
+// Generated by CoffeeScript 1.3.3
(function() {
- var DepGraph, _;
- var __indexOf = Array.prototype.indexOf || function(item) {
- for (var i = 0, l = this.length; i < l; i++) {
- if (this[i] === item) return i;
- }
- return -1;
- }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ var DepGraph, _,
+ __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
+
_ = require('underscore');
+
DepGraph = (function() {
+
function DepGraph() {
this.map = {};
}
+
DepGraph.prototype.add = function(id, depId) {
var _base, _ref;
if ((_ref = (_base = this.map)[id]) == null) {
@@ -22,24 +22,28 @@
this.map[id].push(depId);
return this.map[id];
};
+
DepGraph.prototype.getChain = function(id) {
- var chain, deps, leafNode, visit, visited, _i, _len, _ref;
+ var chain, deps, leafNode, visit, visited, _i, _len, _ref,
+ _this = this;
deps = this.descendantsOf(id);
chain = [];
visited = {};
- visit = __bind(function(node) {
+ visit = function(node) {
var parent, _i, _len, _ref;
if (visited[node] || node === id) {
return;
}
visited[node] = true;
- _ref = this.parentsOf(node);
+ _ref = _this.parentsOf(node);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
parent = _ref[_i];
- visit(parent);
+ if (__indexOf.call(deps, parent) >= 0) {
+ visit(parent);
+ }
}
return chain.unshift(node);
- }, this);
+ };
_ref = _.intersection(deps, this.leafNodes()).reverse();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
leafNode = _ref[_i];
@@ -47,6 +51,7 @@
}
return chain;
};
+
DepGraph.prototype.leafNodes = function() {
var allNodes, node, _i, _len, _ref, _results;
allNodes = _.uniq(_.flatten(_.values(this.map)));
@@ -59,6 +64,7 @@
}
return _results;
};
+
DepGraph.prototype.parentsOf = function(child) {
var node, _i, _len, _ref, _results;
_ref = _.keys(this.map);
@@ -71,8 +77,9 @@
}
return _results;
};
+
DepGraph.prototype.descendantsOf = function(parent, descendants, branch) {
- var child, _i, _len, _ref, _ref2;
+ var child, _i, _len, _ref, _ref1;
if (descendants == null) {
descendants = [];
}
@@ -81,9 +88,9 @@
}
descendants.push(parent);
branch.push(parent);
- _ref2 = (_ref = this.map[parent]) != null ? _ref : [];
- for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
- child = _ref2[_i];
+ _ref1 = (_ref = this.map[parent]) != null ? _ref : [];
+ for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
+ child = _ref1[_i];
if (__indexOf.call(branch, child) >= 0) {
throw new Error("Cyclic dependency from " + parent + " to " + child);
}
@@ -94,11 +101,15 @@
}
return descendants.slice(1);
};
+
return DepGraph;
+
})();
+
if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
module.exports = DepGraph;
} else {
this.DepGraph = DepGraph;
}
+
}).call(this);
View
5 src/dep-graph.coffee
@@ -27,8 +27,7 @@ class DepGraph
visit = (node) =>
return if visited[node] or node is id
visited[node] = true
- for parent in @parentsOf(node)
- visit parent
+ visit parent for parent in @parentsOf(node) when parent in deps
chain.unshift node
for leafNode in _.intersection(deps, @leafNodes()).reverse()
@@ -57,4 +56,4 @@ class DepGraph
if module?.exports?
module.exports = DepGraph
else
- @DepGraph = DepGraph
+ @DepGraph = DepGraph
Something went wrong with that request. Please try again.