From 80f54421fcffeda269fa387d86a297455b615b05 Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Wed, 26 Apr 2017 14:07:45 -0700 Subject: [PATCH] fix some closure warnings. --- lib/utils/array-splice.html | 468 ++++++++++++------------ lib/utils/flattened-nodes-observer.html | 23 +- lib/utils/import-href.html | 6 +- 3 files changed, 250 insertions(+), 247 deletions(-) diff --git a/lib/utils/array-splice.html b/lib/utils/array-splice.html index 9008ebffe8..b9ac24cd71 100644 --- a/lib/utils/array-splice.html +++ b/lib/utils/array-splice.html @@ -26,254 +26,250 @@ const EDIT_ADD = 2; const EDIT_DELETE = 3; - const ArraySplice = { - - // Note: This function is *based* on the computation of the Levenshtein - // "edit" distance. The one change is that "updates" are treated as two - // edits - not one. With Array splices, an update is really a delete - // followed by an add. By retaining this, we optimize for "keeping" the - // maximum array items in the original array. For example: - // - // 'xxxx123' -> '123yyyy' - // - // With 1-edit updates, the shortest path would be just to update all seven - // characters. With 2-edit updates, we delete 4, leave 3, and add 4. This - // leaves the substring '123' intact. - calcEditDistances(current, currentStart, currentEnd, - old, oldStart, oldEnd) { - // "Deletion" columns - let rowCount = oldEnd - oldStart + 1; - let columnCount = currentEnd - currentStart + 1; - let distances = new Array(rowCount); - - // "Addition" rows. Initialize null column. - for (let i = 0; i < rowCount; i++) { - distances[i] = new Array(columnCount); - distances[i][0] = i; - } + // Note: This function is *based* on the computation of the Levenshtein + // "edit" distance. The one change is that "updates" are treated as two + // edits - not one. With Array splices, an update is really a delete + // followed by an add. By retaining this, we optimize for "keeping" the + // maximum array items in the original array. For example: + // + // 'xxxx123' -> '123yyyy' + // + // With 1-edit updates, the shortest path would be just to update all seven + // characters. With 2-edit updates, we delete 4, leave 3, and add 4. This + // leaves the substring '123' intact. + function calcEditDistances(current, currentStart, currentEnd, + old, oldStart, oldEnd) { + // "Deletion" columns + let rowCount = oldEnd - oldStart + 1; + let columnCount = currentEnd - currentStart + 1; + let distances = new Array(rowCount); + + // "Addition" rows. Initialize null column. + for (let i = 0; i < rowCount; i++) { + distances[i] = new Array(columnCount); + distances[i][0] = i; + } - // Initialize null row - for (let j = 0; j < columnCount; j++) - distances[0][j] = j; - - for (let i = 1; i < rowCount; i++) { - for (let j = 1; j < columnCount; j++) { - if (this.equals(current[currentStart + j - 1], old[oldStart + i - 1])) - distances[i][j] = distances[i - 1][j - 1]; - else { - let north = distances[i - 1][j] + 1; - let west = distances[i][j - 1] + 1; - distances[i][j] = north < west ? north : west; - } + // Initialize null row + for (let j = 0; j < columnCount; j++) + distances[0][j] = j; + + for (let i = 1; i < rowCount; i++) { + for (let j = 1; j < columnCount; j++) { + if (equals(current[currentStart + j - 1], old[oldStart + i - 1])) + distances[i][j] = distances[i - 1][j - 1]; + else { + let north = distances[i - 1][j] + 1; + let west = distances[i][j - 1] + 1; + distances[i][j] = north < west ? north : west; } } + } - return distances; - }, - - // This starts at the final weight, and walks "backward" by finding - // the minimum previous weight recursively until the origin of the weight - // matrix. - spliceOperationsFromEditDistances(distances) { - let i = distances.length - 1; - let j = distances[0].length - 1; - let current = distances[i][j]; - let edits = []; - while (i > 0 || j > 0) { - if (i == 0) { - edits.push(EDIT_ADD); - j--; - continue; - } - if (j == 0) { - edits.push(EDIT_DELETE); - i--; - continue; - } - let northWest = distances[i - 1][j - 1]; - let west = distances[i - 1][j]; - let north = distances[i][j - 1]; - - let min; - if (west < north) - min = west < northWest ? west : northWest; - else - min = north < northWest ? north : northWest; - - if (min == northWest) { - if (northWest == current) { - edits.push(EDIT_LEAVE); - } else { - edits.push(EDIT_UPDATE); - current = northWest; - } - i--; - j--; - } else if (min == west) { - edits.push(EDIT_DELETE); - i--; - current = west; + return distances; + } + + // This starts at the final weight, and walks "backward" by finding + // the minimum previous weight recursively until the origin of the weight + // matrix. + function spliceOperationsFromEditDistances(distances) { + let i = distances.length - 1; + let j = distances[0].length - 1; + let current = distances[i][j]; + let edits = []; + while (i > 0 || j > 0) { + if (i == 0) { + edits.push(EDIT_ADD); + j--; + continue; + } + if (j == 0) { + edits.push(EDIT_DELETE); + i--; + continue; + } + let northWest = distances[i - 1][j - 1]; + let west = distances[i - 1][j]; + let north = distances[i][j - 1]; + + let min; + if (west < north) + min = west < northWest ? west : northWest; + else + min = north < northWest ? north : northWest; + + if (min == northWest) { + if (northWest == current) { + edits.push(EDIT_LEAVE); } else { - edits.push(EDIT_ADD); - j--; - current = north; + edits.push(EDIT_UPDATE); + current = northWest; } + i--; + j--; + } else if (min == west) { + edits.push(EDIT_DELETE); + i--; + current = west; + } else { + edits.push(EDIT_ADD); + j--; + current = north; } + } - edits.reverse(); - return edits; - }, + edits.reverse(); + return edits; + } - /** - * Splice Projection functions: - * - * A splice map is a representation of how a previous array of items - * was transformed into a new array of items. Conceptually it is a list of - * tuples of - * - * - * - * which are kept in ascending index order of. The tuple represents that at - * the |index|, |removed| sequence of items were removed, and counting forward - * from |index|, |addedCount| items were added. - */ + /** + * Splice Projection functions: + * + * A splice map is a representation of how a previous array of items + * was transformed into a new array of items. Conceptually it is a list of + * tuples of + * + * + * + * which are kept in ascending index order of. The tuple represents that at + * the |index|, |removed| sequence of items were removed, and counting forward + * from |index|, |addedCount| items were added. + */ - /** - * Lacking individual splice mutation information, the minimal set of - * splices can be synthesized given the previous state and final state of an - * array. The basic approach is to calculate the edit distance matrix and - * choose the shortest path through it. - * - * Complexity: O(l * p) - * l: The length of the current array - * p: The length of the old array - * - * @param {Array} current The current "changed" array for which to - * calculate splices. - * @param {number} currentStart Starting index in the `current` array for - * which splices are calculated. - * @param {number} currentEnd Ending index in the `current` array for - * which splices are calculated. - * @param {Array} old The original "unchanged" array to compare `current` - * against to determine splices. - * @param {number} oldStart Starting index in the `old` array for - * which splices are calculated. - * @param {number} oldEnd Ending index in the `old` array for - * which splices are calculated. - * @return {Array} Returns an array of splice record objects. Each of these - * contains: `index` the location where the splice occurred; `removed` - * the array of removed items from this location; `addedCount` the number - * of items added at this location. - */ - calcSplices(current, currentStart, currentEnd, - old, oldStart, oldEnd) { - let prefixCount = 0; - let suffixCount = 0; - let splice; - - let minLength = Math.min(currentEnd - currentStart, oldEnd - oldStart); - if (currentStart == 0 && oldStart == 0) - prefixCount = this.sharedPrefix(current, old, minLength); - - if (currentEnd == current.length && oldEnd == old.length) - suffixCount = this.sharedSuffix(current, old, minLength - prefixCount); - - currentStart += prefixCount; - oldStart += prefixCount; - currentEnd -= suffixCount; - oldEnd -= suffixCount; - - if (currentEnd - currentStart == 0 && oldEnd - oldStart == 0) - return []; - - if (currentStart == currentEnd) { - splice = newSplice(currentStart, [], 0); - while (oldStart < oldEnd) - splice.removed.push(old[oldStart++]); - - return [ splice ]; - } else if (oldStart == oldEnd) - return [ newSplice(currentStart, [], currentEnd - currentStart) ]; - - let ops = this.spliceOperationsFromEditDistances( - this.calcEditDistances(current, currentStart, currentEnd, - old, oldStart, oldEnd)); - - splice = undefined; - let splices = []; - let index = currentStart; - let oldIndex = oldStart; - for (let i = 0; i < ops.length; i++) { - switch(ops[i]) { - case EDIT_LEAVE: - if (splice) { - splices.push(splice); - splice = undefined; - } - - index++; - oldIndex++; - break; - case EDIT_UPDATE: - if (!splice) - splice = newSplice(index, [], 0); - - splice.addedCount++; - index++; - - splice.removed.push(old[oldIndex]); - oldIndex++; - break; - case EDIT_ADD: - if (!splice) - splice = newSplice(index, [], 0); - - splice.addedCount++; - index++; - break; - case EDIT_DELETE: - if (!splice) - splice = newSplice(index, [], 0); - - splice.removed.push(old[oldIndex]); - oldIndex++; - break; - } - } + /** + * Lacking individual splice mutation information, the minimal set of + * splices can be synthesized given the previous state and final state of an + * array. The basic approach is to calculate the edit distance matrix and + * choose the shortest path through it. + * + * Complexity: O(l * p) + * l: The length of the current array + * p: The length of the old array + * + * @param {Array} current The current "changed" array for which to + * calculate splices. + * @param {number} currentStart Starting index in the `current` array for + * which splices are calculated. + * @param {number} currentEnd Ending index in the `current` array for + * which splices are calculated. + * @param {Array} old The original "unchanged" array to compare `current` + * against to determine splices. + * @param {number} oldStart Starting index in the `old` array for + * which splices are calculated. + * @param {number} oldEnd Ending index in the `old` array for + * which splices are calculated. + * @return {Array} Returns an array of splice record objects. Each of these + * contains: `index` the location where the splice occurred; `removed` + * the array of removed items from this location; `addedCount` the number + * of items added at this location. + */ + function calcSplices(current, currentStart, currentEnd, + old, oldStart, oldEnd) { + let prefixCount = 0; + let suffixCount = 0; + let splice; + + let minLength = Math.min(currentEnd - currentStart, oldEnd - oldStart); + if (currentStart == 0 && oldStart == 0) + prefixCount = sharedPrefix(current, old, minLength); + + if (currentEnd == current.length && oldEnd == old.length) + suffixCount = sharedSuffix(current, old, minLength - prefixCount); + + currentStart += prefixCount; + oldStart += prefixCount; + currentEnd -= suffixCount; + oldEnd -= suffixCount; + + if (currentEnd - currentStart == 0 && oldEnd - oldStart == 0) + return []; + + if (currentStart == currentEnd) { + splice = newSplice(currentStart, [], 0); + while (oldStart < oldEnd) + splice.removed.push(old[oldStart++]); + + return [ splice ]; + } else if (oldStart == oldEnd) + return [ newSplice(currentStart, [], currentEnd - currentStart) ]; + + let ops = spliceOperationsFromEditDistances( + calcEditDistances(current, currentStart, currentEnd, + old, oldStart, oldEnd)); + + splice = undefined; + let splices = []; + let index = currentStart; + let oldIndex = oldStart; + for (let i = 0; i < ops.length; i++) { + switch(ops[i]) { + case EDIT_LEAVE: + if (splice) { + splices.push(splice); + splice = undefined; + } - if (splice) { - splices.push(splice); + index++; + oldIndex++; + break; + case EDIT_UPDATE: + if (!splice) + splice = newSplice(index, [], 0); + + splice.addedCount++; + index++; + + splice.removed.push(old[oldIndex]); + oldIndex++; + break; + case EDIT_ADD: + if (!splice) + splice = newSplice(index, [], 0); + + splice.addedCount++; + index++; + break; + case EDIT_DELETE: + if (!splice) + splice = newSplice(index, [], 0); + + splice.removed.push(old[oldIndex]); + oldIndex++; + break; } - return splices; - }, - - sharedPrefix(current, old, searchLength) { - for (let i = 0; i < searchLength; i++) - if (!this.equals(current[i], old[i])) - return i; - return searchLength; - }, - - sharedSuffix(current, old, searchLength) { - let index1 = current.length; - let index2 = old.length; - let count = 0; - while (count < searchLength && this.equals(current[--index1], old[--index2])) - count++; - - return count; - }, - - calculateSplices(current, previous) { - return this.calcSplices(current, 0, current.length, previous, 0, - previous.length); - }, - - equals(currentValue, previousValue) { - return currentValue === previousValue; } - }; + if (splice) { + splices.push(splice); + } + return splices; + } + + function sharedPrefix(current, old, searchLength) { + for (let i = 0; i < searchLength; i++) + if (!equals(current[i], old[i])) + return i; + return searchLength; + } + + function sharedSuffix(current, old, searchLength) { + let index1 = current.length; + let index2 = old.length; + let count = 0; + while (count < searchLength && equals(current[--index1], old[--index2])) + count++; + + return count; + } + + function calculateSplices(current, previous) { + return calcSplices(current, 0, current.length, previous, 0, + previous.length); + } + + function equals(currentValue, previousValue) { + return currentValue === previousValue; + } /** * @namespace @@ -312,9 +308,7 @@ * the array of removed items from this location; `addedCount` the number * of items added at this location. */ - calculateSplices(current, previous) { - return ArraySplice.calculateSplices(current, previous); - } + calculateSplices } })(); diff --git a/lib/utils/flattened-nodes-observer.html b/lib/utils/flattened-nodes-observer.html index c1f75d038b..da8292d47a 100644 --- a/lib/utils/flattened-nodes-observer.html +++ b/lib/utils/flattened-nodes-observer.html @@ -14,6 +14,12 @@ (function() { 'use strict'; + /** + * Returns true if `node` is a slot element + * @param {HTMLElement} node Node to test. + * @return {boolean} Returns true if the given `node` is a slot + * @private + */ function isSlot(node) { return (node.localName === 'slot'); } @@ -39,9 +45,6 @@ * are asynchronous. * * @memberof Polymer - * @param {Node} target Node on which to listen for changes. - * @param {Function} callback Function called when there are additions - * or removals from the target's list of flattened nodes. * @summary Class that listens for changes (additions or removals) to * "flattened nodes" on a given `node`. */ @@ -56,16 +59,16 @@ * nodes list is `
`. If the `` has other * `` elements assigned to it, these are flattened as well. * - * @param {Node} node The node for which to return the list of flattened nodes. + * @param {HTMLElement|HTMLSlotElement} node The node for which to return the list of flattened nodes. * @return {Array} The list of flattened nodes for the given `node`. */ static getFlattenedNodes(node) { if (isSlot(node)) { - return node.assignedNodes({flatten: true}); + return /** @type {HTMLSlotElement} */ (node).assignedNodes({flatten: true}); } else { return Array.from(node.childNodes).map(node => { if (isSlot(node)) { - return node.assignedNodes({flatten: true}); + return /** @type {HTMLSlotElement} */ (node).assignedNodes({flatten: true}); } else { return [node]; } @@ -73,6 +76,11 @@ } } + /** + * @param {Node} target Node on which to listen for changes. + * @param {Function} callback Function called when there are additions + * or removals from the target's list of flattened nodes. + */ constructor(target, callback) { /** @type {MutationObserver} */ this._shadyChildrenObserver = null; @@ -84,6 +92,7 @@ this._effectiveNodes = []; this._observer = null; this._scheduled = false; + /** @type {function()} */ this._boundSchedule = () => { this._schedule(); } @@ -175,7 +184,7 @@ */ flush() { if (!this._connected) { - return; + return false; } if (window.ShadyDOM) { ShadyDOM.flush(); diff --git a/lib/utils/import-href.html b/lib/utils/import-href.html index 77933a4e43..65e35dfe33 100644 --- a/lib/utils/import-href.html +++ b/lib/utils/import-href.html @@ -45,10 +45,10 @@ * @return {HTMLLinkElement} The link element for the URL to be loaded. */ Polymer.importHref = function(href, onload, onerror, optAsync) { - let link = - document.head.querySelector('link[href="' + href + '"][import-href]'); + let link = /** @type {HTMLLinkElement} */ + (document.head.querySelector('link[href="' + href + '"][import-href]')); if (!link) { - link = document.createElement('link'); + link = /** @type {HTMLLinkElement} */ (document.createElement('link')); link.rel = 'import'; link.href = href; link.setAttribute('import-href', '');