diff --git a/lighthouse-core/lib/cdt/SDK.js b/lighthouse-core/lib/cdt/SDK.js index 8615af63d0af..9c2232bbf1e9 100644 --- a/lighthouse-core/lib/cdt/SDK.js +++ b/lighthouse-core/lib/cdt/SDK.js @@ -14,44 +14,52 @@ const SDK = { ...require('./generated/sdk/SourceMap.js'), }; -// eslint-disable-next-line no-extend-native -Object.defineProperty(Array.prototype, 'upperBound', { - /** - * Return index of the leftmost element that is greater - * than the specimen object. If there's no such element (i.e. all - * elements are smaller or equal to the specimen) returns right bound. - * The function works for sorted array. - * When specified, |left| (inclusive) and |right| (exclusive) indices - * define the search window. - * - * @param {!T} object - * @param {function(!T,!S):number=} comparator - * @param {number=} left - * @param {number=} right - * @return {number} - * @this {Array.} - * @template T,S - */ - value: function(object, comparator, left, right) { - function defaultComparator(a, b) { - return a < b ? -1 : (a > b ? 1 : 0); - } - comparator = comparator || defaultComparator; - let l = left || 0; - let r = right !== undefined ? right : this.length; - while (l < r) { - const m = (l + r) >> 1; - if (comparator(object, this[m]) >= 0) { - l = m + 1; - } else { - r = m; - } +const originalMappings = SDK.TextSourceMap.prototype.mappings; +SDK.TextSourceMap.prototype.mappings = function() { + const mappings = originalMappings.call(this); + mappings.upperBound = upperBound.bind(mappings); + return mappings; +}; + +/** + * Return index of the leftmost element that is greater + * than the specimen object. If there's no such element (i.e. all + * elements are smaller or equal to the specimen) returns right bound. + * The function works for sorted array. + * When specified, |left| (inclusive) and |right| (exclusive) indices + * define the search window. + * + * @param {!T} object + * @param {function(!T,!S):number=} comparator + * @param {number=} left + * @param {number=} right + * @return {number} + * @this {Array.} + * @template T,S + */ +function upperBound(object, comparator, left, right) { + function defaultComparator(a, b) { + return a < b ? -1 : (a > b ? 1 : 0); + } + comparator = comparator || defaultComparator; + let l = left || 0; + let r = right !== undefined ? right : this.length; + while (l < r) { + const m = (l + r) >> 1; + if (comparator(object, this[m]) >= 0) { + l = m + 1; + } else { + r = m; } - return r; - }, -}); + } + return r; +}; -globalThis.cdt.SDK.TextSourceMap.prototype.findExactEntry = function(line, column) { +/** + * @param {number} line + * @param {number} column + */ +SDK.TextSourceMap.prototype.findExactEntry = function(line, column) { // findEntry takes compiled locations and returns original locations. const entry = this.findEntry(line, column); // without an exact hit, we return no match @@ -68,7 +76,7 @@ globalThis.cdt.SDK.TextSourceMap.prototype.findExactEntry = function(line, colum }; // Add `lastColumnNumber` to mappings. -globalThis.cdt.SDK.TextSourceMap.prototype.computeLastGeneratedColumns = function() { +SDK.TextSourceMap.prototype.computeLastGeneratedColumns = function() { const mappings = this.mappings(); if (mappings.length && typeof mappings[0].lastColumnNumber !== 'undefined') return;