Skip to content

Commit

Permalink
do not pollute global array
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Dec 7, 2019
1 parent fa07a80 commit 7ad9389
Showing 1 changed file with 45 additions and 37 deletions.
82 changes: 45 additions & 37 deletions lighthouse-core/lib/cdt/SDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.<!S>}
* @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.<!S>}
* @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
Expand All @@ -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;

Expand Down

0 comments on commit 7ad9389

Please sign in to comment.