upgrade magic-string, fix sourcemaps

pull/97/head
Rich-Harris 8 years ago
parent 695cfd01c9
commit 57f94d4d0b

@ -86,7 +86,7 @@ export default function generate ( parsed, source, options ) {
const { name } = flattenReference( node ); const { name } = flattenReference( node );
if ( parent && parent.type === 'CallExpression' && node === parent.callee ) { if ( parent && parent.type === 'CallExpression' && node === parent.callee ) {
if ( generator.helpers[ name ] ) generator.code.insertRight( node.start, `template.helpers.` ); if ( generator.helpers[ name ] ) generator.code.prependRight( node.start, `template.helpers.` );
return; return;
} }
@ -102,7 +102,7 @@ export default function generate ( parsed, source, options ) {
if ( !~usedContexts.indexOf( context ) ) usedContexts.push( context ); if ( !~usedContexts.indexOf( context ) ) usedContexts.push( context );
} else { } else {
dependencies.push( node.name ); dependencies.push( node.name );
generator.code.insertRight( node.start, `root.` ); generator.code.prependRight( node.start, `root.` );
if ( !~usedContexts.indexOf( 'root' ) ) usedContexts.push( 'root' ); if ( !~usedContexts.indexOf( 'root' ) ) usedContexts.push( 'root' );
} }
@ -204,19 +204,19 @@ export default function generate ( parsed, source, options ) {
while ( /\s/.test( source[ i - 1 ] ) ) i--; while ( /\s/.test( source[ i - 1 ] ) ) i--;
const indentation = source.slice( i, defaultExport.start ); const indentation = source.slice( i, defaultExport.start );
generator.code.insertLeft( finalNode.end, `\n\n${indentation}return template;` ); generator.code.appendLeft( finalNode.end, `\n\n${indentation}return template;` );
} }
defaultExport.declaration.properties.forEach( prop => { defaultExport.declaration.properties.forEach( prop => {
templateProperties[ prop.key.name ] = prop.value; templateProperties[ prop.key.name ] = prop.value;
}); });
generator.code.insertRight( parsed.js.content.start, 'var template = (function () {' ); generator.code.prependRight( parsed.js.content.start, 'var template = (function () {' );
} else { } else {
generator.code.insertRight( parsed.js.content.start, '(function () {' ); generator.code.prependRight( parsed.js.content.start, '(function () {' );
} }
generator.code.insertLeft( parsed.js.content.end, '}());' ); generator.code.appendLeft( parsed.js.content.end, '}());' );
[ 'helpers', 'events', 'components' ].forEach( key => { [ 'helpers', 'events', 'components' ].forEach( key => {
if ( templateProperties[ key ] ) { if ( templateProperties[ key ] ) {
@ -512,24 +512,27 @@ export default function generate ( parsed, source, options ) {
}); });
} }
addString( getIntro( format, options, imports ) ); const intro = getIntro( format, options, imports );
if ( intro ) addString( intro );
// a filename is necessary for sourcemap generation // a filename is necessary for sourcemap generation
const filename = options.filename || 'SvelteComponent.html'; const filename = options.filename || 'SvelteComponent.html';
parts.forEach( str => { parts.forEach( str => {
const chunk = str.replace( pattern, '' );
if ( chunk ) addString( chunk );
const match = pattern.exec( str ); const match = pattern.exec( str );
addString( str.replace( pattern, '' ) ); const snippet = generator.code.snip( +match[1], +match[2] );
compiled.addSource({ compiled.addSource({
filename, filename,
content: generator.code.snip( +match[1], +match[2] ) content: snippet
}); });
}); });
compiled.append( finalChunk ); addString( finalChunk );
addString( '\n\n' + getOutro( format, constructorName, options, imports ) ); addString( '\n\n' + getOutro( format, constructorName, options, imports ) );
return { return {

@ -80,7 +80,7 @@ export default function addComponentAttributes ( generator, node, local ) {
else if ( attribute.type === 'EventHandler' ) { else if ( attribute.type === 'EventHandler' ) {
// TODO verify that it's a valid callee (i.e. built-in or declared method) // TODO verify that it's a valid callee (i.e. built-in or declared method)
generator.addSourcemapLocations( attribute.expression ); generator.addSourcemapLocations( attribute.expression );
generator.code.insertRight( attribute.expression.start, 'component.' ); generator.code.prependRight( attribute.expression.start, 'component.' );
const usedContexts = new Set(); const usedContexts = new Set();
attribute.expression.arguments.forEach( arg => { attribute.expression.arguments.forEach( arg => {

@ -116,7 +116,7 @@ export default function addElementAttributes ( generator, node, local ) {
else if ( attribute.type === 'EventHandler' ) { else if ( attribute.type === 'EventHandler' ) {
// TODO verify that it's a valid callee (i.e. built-in or declared method) // TODO verify that it's a valid callee (i.e. built-in or declared method)
generator.addSourcemapLocations( attribute.expression ); generator.addSourcemapLocations( attribute.expression );
generator.code.insertRight( attribute.expression.start, 'component.' ); generator.code.prependRight( attribute.expression.start, 'component.' );
const usedContexts = new Set(); const usedContexts = new Set();
attribute.expression.arguments.forEach( arg => { attribute.expression.arguments.forEach( arg => {

@ -47,7 +47,7 @@
"fuzzyset.js": "0.0.1", "fuzzyset.js": "0.0.1",
"jsdom": "^9.8.3", "jsdom": "^9.8.3",
"locate-character": "^2.0.0", "locate-character": "^2.0.0",
"magic-string": "^0.16.0", "magic-string": "^0.19.0",
"mocha": "^3.1.2", "mocha": "^3.1.2",
"node-resolve": "^1.3.3", "node-resolve": "^1.3.3",
"nyc": "^9.0.1", "nyc": "^9.0.1",

@ -1,9 +1 @@
{{foo.bar.baz}} {{foo.bar.baz}}
<script>
export default {
onrender () {
console.log( 42 );
}
};
</script>

@ -1,8 +1,26 @@
export function test ({ assert, smc, locateInSource, locateInGenerated }) { export function test ({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource( 'foo.bar.baz' ); const expected = locateInSource( 'foo.bar.baz' );
const loc = locateInGenerated( 'foo.bar.baz' );
const actual = smc.originalPositionFor({ let loc;
let actual;
loc = locateInGenerated( 'foo.bar.baz' );
actual = smc.originalPositionFor({
line: loc.line + 1,
column: loc.column
});
assert.deepEqual( actual, {
source: 'SvelteComponent.html',
name: null,
line: expected.line + 1,
column: expected.column
});
loc = locateInGenerated( 'foo.bar.baz', loc.character + 1 );
actual = smc.originalPositionFor({
line: loc.line + 1, line: loc.line + 1,
column: loc.column column: loc.column
}); });
@ -10,7 +28,7 @@ export function test ({ assert, smc, locateInSource, locateInGenerated }) {
assert.deepEqual( actual, { assert.deepEqual( actual, {
source: 'SvelteComponent.html', source: 'SvelteComponent.html',
name: null, name: null,
line: expected.line, line: expected.line + 1,
column: expected.column column: expected.column
}); });
} }

@ -0,0 +1,9 @@
<div></div>
<script>
export default {
onrender () {
console.log( 42 );
}
}
</script>

@ -0,0 +1,16 @@
export function test ({ assert, smc, locateInSource, locateInGenerated }) {
const expected = locateInSource( '42' );
const loc = locateInGenerated( '42' );
const actual = smc.originalPositionFor({
line: loc.line + 1,
column: loc.column
});
assert.deepEqual( actual, {
source: 'SvelteComponent.html',
name: null,
line: expected.line + 1,
column: expected.column
});
}
Loading…
Cancel
Save