Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
cspotcode committed Jul 21, 2021
commit e94cfd11b702ed29c410afb04230e1663873274e
153 changes: 68 additions & 85 deletions lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const INTERNAL = Symbol("smcInternal");

class SourceMapConsumer {
constructor(aSourceMap, aSourceMapURL) {
if(aSourceMap === INTERNAL) return this;
if (aSourceMap === INTERNAL) return this;
return _factory(aSourceMap, aSourceMapURL);
}

Expand Down Expand Up @@ -228,7 +228,7 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
// the source root, if the source root is absolute. Not doing this would
// be particularly problematic when the source root is a prefix of the
// source (valid, but why??). See github issue #199 and bugzil.la/1188982.
.map(function(source) {
.map(function (source) {
return sourceRoot && util.isAbsolute(sourceRoot) && util.isAbsolute(source)
? util.relative(sourceRoot, source)
: source;
Expand All @@ -241,7 +241,7 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
this._names = ArraySet.fromArray(names.map(String), true);
this._sources = ArraySet.fromArray(sources, true);

this._absoluteSources = that._sources.toArray().map(function(s) {
this._absoluteSources = that._sources.toArray().map(function (s) {
return util.computeSourceURL(sourceRoot, s, aSourceMapURL);
});

Expand Down Expand Up @@ -374,14 +374,14 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
},
() => {
switch (order) {
case SourceMapConsumer.GENERATED_ORDER:
this._wasm.exports.by_generated_location(this._getMappingsPtr());
break;
case SourceMapConsumer.ORIGINAL_ORDER:
this._wasm.exports.by_original_location(this._getMappingsPtr());
break;
default:
throw new Error("Unknown order of iteration.");
case SourceMapConsumer.GENERATED_ORDER:
this._wasm.exports.by_generated_location(this._getMappingsPtr());
break;
case SourceMapConsumer.ORIGINAL_ORDER:
this._wasm.exports.by_original_location(this._getMappingsPtr());
break;
default:
throw new Error("Unknown order of iteration.");
}
}
);
Expand Down Expand Up @@ -544,7 +544,7 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
return false;
}
return this.sourcesContent.length >= this._sources.size() &&
!this.sourcesContent.some(function(sc) { return sc == null; });
!this.sourcesContent.some(function (sc) { return sc == null; });
}

/**
Expand All @@ -569,19 +569,19 @@ class BasicSourceMapConsumer extends SourceMapConsumer {

let url;
if (this.sourceRoot != null
&& (url = util.urlParse(this.sourceRoot))) {
&& (url = util.urlParse(this.sourceRoot))) {
// XXX: file:// URIs and absolute paths lead to unexpected behavior for
// many users. We can help them out when they expect file:// URIs to
// behave like it would if they were running a local HTTP server. See
// https://bugzilla.mozilla.org/show_bug.cgi?id=885597.
const fileUriAbsPath = relativeSource.replace(/^file:\/\//, "");
if (url.scheme == "file"
&& this._sources.has(fileUriAbsPath)) {
&& this._sources.has(fileUriAbsPath)) {
return this.sourcesContent[this._sources.indexOf(fileUriAbsPath)];
}

if ((!url.path || url.path == "/")
&& this._sources.has("/" + relativeSource)) {
&& this._sources.has("/" + relativeSource)) {
return this.sourcesContent[this._sources.indexOf("/" + relativeSource)];
}
}
Expand Down Expand Up @@ -750,60 +750,43 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
throw new Error("Unsupported version: " + version);
}

this._sources = new ArraySet();
this._names = new ArraySet();
this.__generatedMappings = null;
this.__originalMappings = null;
this.__generatedMappingsUnsorted = null;
this.__originalMappingsUnsorted = null;

let lastOffset = {
line: -1,
column: 0
};
const s = sections.map(s => {
this._sections = sections.map(s => {
if (s.url) {
// The url field will require support for asynchronicity.
// See https://github.com/mozilla/source-map/issues/16
throw new Error(
"Support for url field in sections not implemented."
);
throw new Error("Support for url field in sections not implemented.");
}
const offset = util.getArg(s, "offset");
const offsetLine = util.getArg(offset, "line");
const offsetColumn = util.getArg(offset, "column");

this._sources = new ArraySet();
this._names = new ArraySet();
this.__generatedMappings = null;
this.__originalMappings = null;
this.__generatedMappingsUnsorted = null;
this.__originalMappingsUnsorted = null;
if (offsetLine < lastOffset.line ||
(offsetLine === lastOffset.line && offsetColumn < lastOffset.column)) {
throw new Error("Section offsets must be ordered and non-overlapping.");
}
lastOffset = offset;

let lastOffset = {
line: -1,
column: 0
const consumer = new SourceMapConsumer(util.getArg(s, "map"), aSourceMapURL);
return {
generatedOffset: {
// The offset fields are 0-based, but we use 1-based indices when
// encoding/decoding from VLQ.
generatedLine: offsetLine + 1,
generatedColumn: offsetColumn + 1
},
consumer
};
this._sections = sections.map(s => {
if (s.url) {
// The url field will require support for asynchronicity.
// See https://github.com/mozilla/source-map/issues/16
throw new Error("Support for url field in sections not implemented.");
}
const offset = util.getArg(s, "offset");
const offsetLine = util.getArg(offset, "line");
const offsetColumn = util.getArg(offset, "column");

if (offsetLine < lastOffset.line ||
(offsetLine === lastOffset.line && offsetColumn < lastOffset.column)) {
throw new Error("Section offsets must be ordered and non-overlapping.");
}
lastOffset = offset;

const consumer = new SourceMapConsumer(util.getArg(s, "map"), aSourceMapURL);
return {
generatedOffset: {
// The offset fields are 0-based, but we use 1-based indices when
// encoding/decoding from VLQ.
generatedLine: offsetLine + 1,
generatedColumn: offsetColumn + 1
},
consumer
};
});
});
}

Expand Down Expand Up @@ -837,7 +820,7 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
//
// `_originalMappings` is ordered by the original positions.
get _generatedMappings() {
if (!this.__generatedMappings) {
if(!this.__generatedMappings) {
this._sortGeneratedMappings();
}

Expand Down Expand Up @@ -921,14 +904,14 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
// Find the section containing the generated position we're trying to map
// to an original position.
const sectionIndex = binarySearch.search(needle, this._sections,
function(aNeedle, section) {
function (aNeedle, section) {
const cmp = aNeedle.generatedLine - section.generatedOffset.generatedLine;
if (cmp) {
return cmp;
}

return (aNeedle.generatedColumn -
section.generatedOffset.generatedColumn);
section.generatedOffset.generatedColumn);
});
const section = this._sections[sectionIndex];

Expand All @@ -946,8 +929,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
(section.generatedOffset.generatedLine - 1),
column: needle.generatedColumn -
(section.generatedOffset.generatedLine === needle.generatedLine
? section.generatedOffset.generatedColumn - 1
: 0),
? section.generatedOffset.generatedColumn - 1
: 0),
bias: aArgs.bias
});
}
Expand All @@ -957,7 +940,7 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
* map, false otherwise.
*/
hasContentsOfAllSources() {
return this._sections.every(function (s) {
return this._sections.every(function(s) {
return s.consumer.hasContentsOfAllSources();
});
}
Expand Down Expand Up @@ -1016,8 +999,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
(section.generatedOffset.generatedLine - 1),
column: generatedPosition.column +
(section.generatedOffset.generatedLine === generatedPosition.line
? section.generatedOffset.generatedColumn - 1
: 0)
? section.generatedOffset.generatedColumn - 1
: 0)
};
return ret;
}
Expand Down Expand Up @@ -1070,8 +1053,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
(section.generatedOffset.generatedLine - 1),
generatedColumn: mapping.generatedColumn +
(section.generatedOffset.generatedLine === mapping.generatedLine
? section.generatedOffset.generatedColumn - 1
: 0),
? section.generatedOffset.generatedColumn - 1
: 0),
originalLine: mapping.originalLine,
originalColumn: mapping.originalColumn,
name
Expand All @@ -1091,14 +1074,14 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {

let mappings;
switch (order) {
case SourceMapConsumer.GENERATED_ORDER:
mappings = this._generatedMappings;
break;
case SourceMapConsumer.ORIGINAL_ORDER:
mappings = this._originalMappings;
break;
default:
throw new Error("Unknown order of iteration.");
case SourceMapConsumer.GENERATED_ORDER:
mappings = this._generatedMappings;
break;
case SourceMapConsumer.ORIGINAL_ORDER:
mappings = this._originalMappings;
break;
default:
throw new Error("Unknown order of iteration.");
}

const sourceRoot = this.sourceRoot;
Expand All @@ -1124,19 +1107,19 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
* we are searching for in the given "haystack" of mappings.
*/
_findMapping(aNeedle, aMappings, aLineName,
aColumnName, aComparator, aBias) {
aColumnName, aComparator, aBias) {
// To return the position we are searching for, we must first find the
// mapping for the given position and then return the opposite position it
// points to. Because the mappings are sorted, we can use binary search to
// find the best mapping.

if (aNeedle[aLineName] <= 0) {
throw new TypeError("Line must be greater than or equal to 1, got "
+ aNeedle[aLineName]);
+ aNeedle[aLineName]);
}
if (aNeedle[aColumnName] < 0) {
throw new TypeError("Column must be greater than or equal to 0, got "
+ aNeedle[aColumnName]);
+ aNeedle[aColumnName]);
}

return binarySearch.search(aNeedle, aMappings, aComparator, aBias);
Expand Down Expand Up @@ -1171,11 +1154,11 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
const mappings = [];

let index = this._findMapping(needle,
this._originalMappings,
"originalLine",
"originalColumn",
util.compareByOriginalPositions,
binarySearch.LEAST_UPPER_BOUND);
this._originalMappings,
"originalLine",
"originalColumn",
util.compareByOriginalPositions,
binarySearch.LEAST_UPPER_BOUND);
if (index >= 0) {
let mapping = this._originalMappings[index];

Expand Down Expand Up @@ -1207,8 +1190,8 @@ class IndexedSourceMapConsumer extends SourceMapConsumer {
// Since mappings are sorted, this is guaranteed to find all mappings for
// the line we are searching for.
while (mapping &&
mapping.originalLine === line &&
mapping.originalColumn == originalColumn) {
mapping.originalLine === line &&
mapping.originalColumn == originalColumn) {
let lastColumn = mapping.lastGeneratedColumn;
if (this._computedColumnSpans && lastColumn === null) {
lastColumn = Infinity;
Expand Down Expand Up @@ -1246,8 +1229,8 @@ function _factory(aSourceMap, aSourceMapURL) {
}

const consumer = sourceMap.sections != null
? new IndexedSourceMapConsumer(sourceMap, aSourceMapURL)
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
? new IndexedSourceMapConsumer(sourceMap, aSourceMapURL)
: new BasicSourceMapConsumer(sourceMap, aSourceMapURL);
return consumer;
}

Expand Down