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
This commit is contained in:
toasted-nutbread 2020-07-03 15:57:17 -04:00 committed by GitHub
parent a07a8dfff6
commit e30bab3324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,7 +18,7 @@
class TextSourceMap { class TextSourceMap {
constructor(source, mapping=null) { constructor(source, mapping=null) {
this._source = source; this._source = source;
this._mapping = (Array.isArray(mapping) ? TextSourceMap._normalizeMapping(mapping) : null); this._mapping = (mapping !== null ? TextSourceMap.normalizeMapping(mapping) : null);
} }
get source() { get source() {
@ -31,19 +31,19 @@ class TextSourceMap {
} }
const source = this._source; const source = this._source;
if (!(other instanceof TextSourceMap && source === other._source)) { if (!(other instanceof TextSourceMap && source === other.source)) {
return false; return false;
} }
let mapping = this._mapping; let mapping = this._mapping;
let otherMapping = other._mapping; let otherMapping = other.getMappingCopy();
if (mapping === null) { if (mapping === null) {
if (otherMapping === null) { if (otherMapping === null) {
return true; return true;
} }
mapping = TextSourceMap._createMapping(source); mapping = TextSourceMap.createMapping(source);
} else if (otherMapping === null) { } else if (otherMapping === null) {
otherMapping = TextSourceMap._createMapping(source); otherMapping = TextSourceMap.createMapping(source);
} }
const mappingLength = mapping.length; const mappingLength = mapping.length;
@ -77,7 +77,7 @@ class TextSourceMap {
if (count <= 0) { return; } if (count <= 0) { return; }
if (this._mapping === null) { if (this._mapping === null) {
this._mapping = TextSourceMap._createMapping(this._source); this._mapping = TextSourceMap.createMapping(this._source);
} }
let sum = this._mapping[index]; let sum = this._mapping[index];
@ -90,17 +90,21 @@ class TextSourceMap {
insert(index, ...items) { insert(index, ...items) {
if (this._mapping === null) { if (this._mapping === null) {
this._mapping = TextSourceMap._createMapping(this._source); this._mapping = TextSourceMap.createMapping(this._source);
} }
this._mapping.splice(index, 0, ...items); 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); return new Array(text.length).fill(1);
} }
static _normalizeMapping(mapping) { static normalizeMapping(mapping) {
const result = []; const result = [];
for (const value of mapping) { for (const value of mapping) {
result.push( result.push(