From e30bab33249a9a99b59a11f8ea582f31651b4710 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 3 Jul 2020 15:57:17 -0400 Subject: [PATCH] Refactor text source map (#649) * Use a null check instead of array check * Convert statisc to non-private * Use public source * Add public function for getting the mapping --- ext/bg/js/text-source-map.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/ext/bg/js/text-source-map.js b/ext/bg/js/text-source-map.js index 1776ae07..6ed553e6 100644 --- a/ext/bg/js/text-source-map.js +++ b/ext/bg/js/text-source-map.js @@ -18,7 +18,7 @@ class TextSourceMap { constructor(source, mapping=null) { this._source = source; - this._mapping = (Array.isArray(mapping) ? TextSourceMap._normalizeMapping(mapping) : null); + this._mapping = (mapping !== null ? TextSourceMap.normalizeMapping(mapping) : null); } get source() { @@ -31,19 +31,19 @@ class TextSourceMap { } const source = this._source; - if (!(other instanceof TextSourceMap && source === other._source)) { + if (!(other instanceof TextSourceMap && source === other.source)) { return false; } let mapping = this._mapping; - let otherMapping = other._mapping; + let otherMapping = other.getMappingCopy(); if (mapping === null) { if (otherMapping === null) { return true; } - mapping = TextSourceMap._createMapping(source); + mapping = TextSourceMap.createMapping(source); } else if (otherMapping === null) { - otherMapping = TextSourceMap._createMapping(source); + otherMapping = TextSourceMap.createMapping(source); } const mappingLength = mapping.length; @@ -77,7 +77,7 @@ class TextSourceMap { if (count <= 0) { return; } if (this._mapping === null) { - this._mapping = TextSourceMap._createMapping(this._source); + this._mapping = TextSourceMap.createMapping(this._source); } let sum = this._mapping[index]; @@ -90,17 +90,21 @@ class TextSourceMap { insert(index, ...items) { if (this._mapping === null) { - this._mapping = TextSourceMap._createMapping(this._source); + this._mapping = TextSourceMap.createMapping(this._source); } this._mapping.splice(index, 0, ...items); } - static _createMapping(text) { + getMappingCopy() { + return this._mapping !== null ? [...this._mapping] : null; + } + + static createMapping(text) { return new Array(text.length).fill(1); } - static _normalizeMapping(mapping) { + static normalizeMapping(mapping) { const result = []; for (const value of mapping) { result.push(