From ee937c25f7e147fef539714b7a14b32cde6f00a7 Mon Sep 17 00:00:00 2001 From: Lea Verou Date: Mon, 5 Oct 2020 14:30:37 +0300 Subject: [PATCH] [#20] Remove optional chaining operator (unsupported by BigQuery) --- js/20-gradients.js | 8 ++++-- runtime/var-tree.js | 4 +-- vars/dependencies.js | 66 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 vars/dependencies.js diff --git a/js/20-gradients.js b/js/20-gradients.js index bd1df9b..9457b55 100644 --- a/js/20-gradients.js +++ b/js/20-gradients.js @@ -49,10 +49,12 @@ walkDeclarations(ast, ({property, value}) => { // Separate color and position(s) stops = stops.map(s => { - let color = s.match(/#[a-f0-9]+|(?:rgba?|hsla?|color)\((\(.*?\)|.)+?\)/)?.[0]; + let match = s.match(/#[a-f0-9]+|(?:rgba?|hsla?|color)\((\(.*?\)|.)+?\)/); + let color = match && match[0]; if (!color) { - color = s.match(keywordRegex)?.[0]; + let match = s.match(keywordRegex); + color = match && match[0]; } let pos = s.replace(color, "").trim().split(/\s+/); @@ -79,7 +81,7 @@ walkDeclarations(ast, ({property, value}) => { } // Two positions of which the first is 0 or the same as the last one else if (s.pos.length === 2) { - if (parseFLoat(s.pos[0]) === 0 || prev.pos.length === 2 && s.pos[0] === prev.pos?.[1] || prev.pos.length === 1 && s.pos[0] === s.pos[0]) { + if (parseFLoat(s.pos[0]) === 0 || prev.pos.length === 2 && s.pos[0] === (prev.pos && prev.pos[1]) || prev.pos.length === 1 && s.pos[0] === s.pos[0]) { ret.hard_stops++; } } diff --git a/runtime/var-tree.js b/runtime/var-tree.js index 0ae6730..0479289 100644 --- a/runtime/var-tree.js +++ b/runtime/var-tree.js @@ -305,8 +305,6 @@ walkElements(computed, node => { } }) - - return {summary, computed}; }; @@ -340,7 +338,7 @@ function serializeElement(element) { return str; } - + return element; } diff --git a/vars/dependencies.js b/vars/dependencies.js new file mode 100644 index 0000000..ed11556 --- /dev/null +++ b/vars/dependencies.js @@ -0,0 +1,66 @@ +export default function compute() { + +function walkElements(node, callback, parent) { + if (Array.isArray(node)) { + for (let n of node) { + walkElements(n, callback, node); + } + } + else { + callback(node, parent); + + if (node.children) { + walkElements(node.children, callback, node); + } + } +} + +let ret = { + +}; + +function countDependencyLength(declarations, property) { + let o = declarations[property]; + + if (o === undefined) { + return 0; + } + + if (!o.references || o.references.length === 0) { + return 0; + } + + let lengths = o.references.map(p => countDependencyLength(declarations, 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]); + } + } + } + + for (let property in node.declarations) { + + let o = node.declarations[property]; + if (o.computed && o.computed.trim() === "initial" && o.value.trim() !== "initial") { + // Cycle or missing ref + incrementByKey(ret, "initial"); + } + else if (o.references) { + let depth = countDependencyLength(node.declarations, property); + incrementByKey(ret, depth); + } + } + } +}); + +return ret; + +}