Skip to content

Commit

Permalink
Last JS for #1 custom props: length of dependency graph
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaVerou committed Oct 5, 2020
1 parent 975fdbb commit bfe128b
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions vars/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default function compute() {
function walkElements(node, callback, parent) {
if (Array.isArray(node)) {
for (let n of node) {
walkElements(n, callback, node);
walkElements(n, callback, parent);
}
}
else {
Expand All @@ -15,46 +15,50 @@ function walkElements(node, callback, parent) {
}
}

let ret = {
let ret = {max_length: 0};

};
function countDependencyLength(node, property) {
if (!node) {
return 0;
}

function countDependencyLength(declarations, property) {
let o = declarations[property];
let declarations = node.declarations;

if (o === undefined) {
return 0;
if (!declarations || !(property in declarations)) {
return countDependencyLength(node.parent, property);
}

let o = declarations[property];

if (!o.references || o.references.length === 0) {
return 0;
}

let lengths = o.references.map(p => countDependencyLength(declarations, p));
let lengths = o.references.map(p => countDependencyLength(node, p));

return 1 + Math.max(...lengths);
}

walkElements(vars.computed, (node, parent) => {
if (node.declarations) {
// Make inheritance explicit
if (parent && parent.declarations) {
for (let property in parent.declarations) {
if (!(property in node.declarations)) {
node.declarations[property] = Object.assign({inherited: true}, parent.declarations[property]);
}
}
}
if (parent && !node.parent) {
node.parent = parent;
}

if (node.declarations) {
for (let property in node.declarations) {

let o = node.declarations[property];
if (o.computed && o.computed.trim() === "initial" && o.value.trim() !== "initial") {
if (o.computed && o.computed.trim() !== o.value.trim() && (o.computed === "initial" || o.computed === "null")) {
// Cycle or missing ref
incrementByKey(ret, "initial");
}
else if (o.references) {
let depth = countDependencyLength(node.declarations, property);
else {
let depth = countDependencyLength(node, property);

if (depth > ret.max_length) {
ret.max_length = depth;
}

incrementByKey(ret, depth);
}
}
Expand Down

0 comments on commit bfe128b

Please sign in to comment.