Skip to content

Commit

Permalink
fix for #16 with MagicString (not Bundle)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 2, 2015
1 parent 8c57fa7 commit 2fffedb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Bundle/index.js
Expand Up @@ -93,7 +93,7 @@ class Bundle {
mappings = getSemis( source.content.toString() );
} else {
const sourceIndex = this.uniqueSourceIndexByFilename[ source.filename ];
mappings = source.content.getMappings( options.hires, sourceIndex, offsets );
mappings = source.content.getMappings( options.hires, sourceIndex, offsets, [], {} ); // TODO names and nameLocations
}

return prefix + mappings;
Expand Down
40 changes: 23 additions & 17 deletions src/MagicString/encodeMappings.js
@@ -1,6 +1,6 @@
import { encode } from 'vlq';

export default function encodeMappings ( original, str, mappings, hires, sourcemapLocations, sourceIndex, offsets ) {
export default function encodeMappings ( original, str, mappings, hires, sourcemapLocations, sourceIndex, offsets, names, nameLocations ) {
// store locations, for fast lookup
let lineStart = 0;
const locations = original.split( '\n' ).map( line => {
Expand All @@ -20,6 +20,7 @@ export default function encodeMappings ( original, str, mappings, hires, sourcem
let origin;
let lastOrigin = -1;
let location;
let nameIndex;

let i;

Expand All @@ -28,28 +29,29 @@ export default function encodeMappings ( original, str, mappings, hires, sourcem
char = i + charOffset;
origin = inverseMappings[ char ];

location = ( !~origin && ~lastOrigin ) ?
nameIndex = -1;
location = null;

// if this character has no mapping, but the last one did,
// create a new segment
getLocation( locations, lastOrigin + 1 ) :
// if this character has no mapping, but the last one did,
// create a new segment
if ( !~origin && ~lastOrigin ) {
origin = lastOrigin + 1;
location = getLocation( locations, lastOrigin + 1 );

// otherwise create a new segment if this character is mapped to an origin and
// a) we're in hires mode
// b) the origin isn't just lastOrigin + 1
// c) there's a marked sourcemapLocation
( ~origin && ( hires || ( ~lastOrigin && origin !== lastOrigin + 1 ) || sourcemapLocations[ origin ] ) ) ?
getLocation( locations, origin ) :
if ( origin in nameLocations ) nameIndex = names.indexOf( nameLocations[ origin ] );
}

// otherwise skip it
null;
else if ( ~origin && ( hires || ( ~lastOrigin && origin !== lastOrigin + 1 ) || sourcemapLocations[ origin ] ) ) {
location = getLocation( locations, origin );
}

if ( location ) {
segments.push({
generatedCodeColumn: i,
sourceIndex: sourceIndex,
sourceCodeLine: location.line,
sourceCodeColumn: location.column
sourceCodeColumn: location.column,
sourceCodeName: nameIndex
});
}

Expand All @@ -60,17 +62,16 @@ export default function encodeMappings ( original, str, mappings, hires, sourcem
return segments;
});

offsets = offsets || {};

offsets.sourceIndex = offsets.sourceIndex || 0;
offsets.sourceCodeLine = offsets.sourceCodeLine || 0;
offsets.sourceCodeColumn = offsets.sourceCodeColumn || 0;
offsets.sourceCodeName = offsets.sourceCodeName || 0;

const encoded = lines.map( segments => {
var generatedCodeColumn = 0;

return segments.map( segment => {
const arr = [
let arr = [
segment.generatedCodeColumn - generatedCodeColumn,
segment.sourceIndex - offsets.sourceIndex,
segment.sourceCodeLine - offsets.sourceCodeLine,
Expand All @@ -82,6 +83,11 @@ export default function encodeMappings ( original, str, mappings, hires, sourcem
offsets.sourceCodeLine = segment.sourceCodeLine;
offsets.sourceCodeColumn = segment.sourceCodeColumn;

if ( ~segment.sourceCodeName ) {
arr.push( segment.sourceCodeName - offsets.sourceCodeName );
offsets.sourceCodeName = segment.sourceCodeName;
}

return encode( arr );
}).join( ',' );
}).join( ';' );
Expand Down
21 changes: 16 additions & 5 deletions src/MagicString/index.js
Expand Up @@ -14,6 +14,7 @@ class MagicString {
this.indentExclusionRanges = options.indentExclusionRanges;

this.sourcemapLocations = {};
this.nameLocations = {};

this.indentStr = guessIndent( string );
}
Expand Down Expand Up @@ -58,21 +59,27 @@ class MagicString {
generateMap ( options ) {
options = options || {};

let names = [];
Object.keys( this.nameLocations ).forEach( location => {
const name = this.nameLocations[ location ];
if ( !~names.indexOf( name ) ) names.push( name );
});

return new SourceMap({
file: ( options.file ? options.file.split( /[\/\\]/ ).pop() : null ),
sources: [ options.source ? getRelativePath( options.file || '', options.source ) : null ],
sourcesContent: options.includeContent ? [ this.original ] : [ null ],
names: [],
mappings: this.getMappings( options.hires, 0 )
names,
mappings: this.getMappings( options.hires, 0, {}, names, this.nameLocations )
});
}

getIndentString () {
return this.indentStr === null ? '\t' : this.indentStr;
}

getMappings ( hires, sourceIndex, offsets ) {
return encodeMappings( this.original, this.str, this.mappings, hires, this.sourcemapLocations, sourceIndex, offsets );
getMappings ( hires, sourceIndex, offsets, names, nameLocations ) {
return encodeMappings( this.original, this.str, this.mappings, hires, this.sourcemapLocations, sourceIndex, offsets, names, nameLocations );
}

indent ( indentStr, options ) {
Expand Down Expand Up @@ -238,7 +245,7 @@ class MagicString {
return null;
}

overwrite ( start, end, content ) {
overwrite ( start, end, content, storeName ) {
if ( typeof content !== 'string' ) {
throw new TypeError( 'replacement content must be a string' );
}
Expand All @@ -259,6 +266,10 @@ class MagicString {
);
}

if ( storeName ) {
this.nameLocations[ start ] = this.original.slice( start, end );
}

this.str = this.str.substr( 0, firstChar ) + content + this.str.substring( lastChar + 1 );

d = content.length - ( lastChar + 1 - firstChar );
Expand Down

0 comments on commit 2fffedb

Please sign in to comment.