1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Optionally allow replacement of existing mappings in SourceMap#addMapping().

This commit is contained in:
Jason Walton 2013-03-01 10:18:37 -05:00
parent 844549954a
commit ea86e3e7b1
4 changed files with 22 additions and 12 deletions

View file

@ -61,7 +61,9 @@
fragment = fragments[_j];
if (options.sourceMap) {
if (fragment.locationData) {
options.sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn]);
options.sourceMap.addMapping([fragment.locationData.first_line, fragment.locationData.first_column], [currentLine, currentColumn], {
noReplace: true
});
}
newLines = count(fragment.code, "\n");
currentLine += newLines;

View file

@ -10,10 +10,13 @@
this.columnMappings = [];
}
LineMapping.prototype.addMapping = function(generatedColumn, _arg) {
LineMapping.prototype.addMapping = function(generatedColumn, _arg, options) {
var sourceColumn, sourceLine;
sourceLine = _arg[0], sourceColumn = _arg[1];
if (this.columnMap[generatedColumn]) {
if (options == null) {
options = {};
}
if (this.columnMap[generatedColumn] && options.noReplace) {
return;
}
this.columnMap[generatedColumn] = {
@ -56,14 +59,17 @@
this.generatedLines = [];
}
SourceMap.prototype.addMapping = function(sourceLocation, generatedLocation) {
SourceMap.prototype.addMapping = function(sourceLocation, generatedLocation, options) {
var generatedColumn, generatedLine, lineMapping;
if (options == null) {
options = {};
}
generatedLine = generatedLocation[0], generatedColumn = generatedLocation[1];
lineMapping = this.generatedLines[generatedLine];
if (!lineMapping) {
lineMapping = this.generatedLines[generatedLine] = new LineMapping(generatedLine);
}
return lineMapping.addMapping(generatedColumn, sourceLocation);
return lineMapping.addMapping(generatedColumn, sourceLocation, options);
};
SourceMap.prototype.getSourcePosition = function(_arg) {

View file

@ -53,7 +53,8 @@ exports.compile = compile = (code, options = {}) ->
if fragment.locationData
options.sourceMap.addMapping(
[fragment.locationData.first_line, fragment.locationData.first_column],
[currentLine, currentColumn])
[currentLine, currentColumn],
{noReplace: true})
newLines = count fragment.code, "\n"
currentLine += newLines
currentColumn = fragment.code.length - (if newLines then fragment.code.lastIndexOf "\n" else 0)

View file

@ -10,8 +10,8 @@ class LineMapping
# columnMappings is an array of all column mappings, sorted by generated-column.
@columnMappings = []
addMapping: (generatedColumn, [sourceLine, sourceColumn]) ->
if @columnMap[generatedColumn]
addMapping: (generatedColumn, [sourceLine, sourceColumn], options={}) ->
if @columnMap[generatedColumn] and options.noReplace
# We already have a mapping for this column.
return
@ -52,16 +52,17 @@ class exports.SourceMap
# Adds a mapping to this SourceMap.
#
# `sourceLocation` and `generatedLocation` are both [line, column] arrays.
# If there is already a mapping for the specified `generatedLine` and
# `generatedColumn`, then this will have no effect.
addMapping: (sourceLocation, generatedLocation) ->
#
# If `options.noReplace` is true, then if there is already a mapping for
# the specified `generatedLine` and `generatedColumn`, this will have no effect.
addMapping: (sourceLocation, generatedLocation, options={}) ->
[generatedLine, generatedColumn] = generatedLocation
lineMapping = @generatedLines[generatedLine]
if not lineMapping
lineMapping = @generatedLines[generatedLine] = new LineMapping(generatedLine)
lineMapping.addMapping generatedColumn, sourceLocation
lineMapping.addMapping generatedColumn, sourceLocation, options
# Returns [sourceLine, sourceColumn], or null if no mapping could be found.
getSourcePosition: ([generatedLine, generatedColumn]) ->