Browse Source

reattach comments, handle var splitting better

contingency-plan
Rich-Harris 9 years ago
parent
commit
8f3f437e1c
  1. 5
      src/Bundle.js
  2. 31
      src/Module.js
  3. 3
      test/form/exported-empty-vars/_expected/es6.js
  4. 1
      test/form/self-contained-bundle/_expected/amd.js
  5. 1
      test/form/self-contained-bundle/_expected/cjs.js
  6. 1
      test/form/self-contained-bundle/_expected/es6.js
  7. 1
      test/form/self-contained-bundle/_expected/iife.js
  8. 1
      test/form/self-contained-bundle/_expected/umd.js

5
src/Bundle.js

@ -496,7 +496,10 @@ export default class Bundle {
let magicString = new MagicString.Bundle({ separator: '\n\n' });
this.orderedModules.forEach( module => {
magicString.addSource( module.render( allBundleExports, format ) );
const source = module.render( allBundleExports, format );
if ( source.toString().length ) {
magicString.addSource( source );
}
});
// prepend bundle with internal namespaces

31
src/Module.js

@ -539,6 +539,8 @@ export default class Module {
});
let statements = [];
let lastChar = 0;
let commentIndex = 0;
ast.body.forEach( node => {
// special case - top-level var declarations with multiple declarators
@ -550,9 +552,9 @@ export default class Module {
//const magicString = this.magicString.snip( declarator.start, declarator.end ).trim();
const nextDeclarator = node.declarations[ i + 1 ];
if ( nextDeclarator ) {
this.magicString.overwrite( declarator.end, nextDeclarator.start, `;\n${node.kind} ` ); // TODO indentation
}
// if ( nextDeclarator ) {
// this.magicString.overwrite( declarator.end, nextDeclarator.start, `;\n${node.kind} ` ); // TODO indentation
// }
const syntheticNode = {
type: 'VariableDeclaration',
@ -562,15 +564,32 @@ export default class Module {
declarations: [ declarator ]
};
const statement = new Statement( syntheticNode, this, node.start, node.end ); // TODO this is almost certainly wrong...
const start = i === 0 ? node.start : declarator.start;
const end = declarator.end;
const statement = new Statement( syntheticNode, this, start, end ); // TODO this is almost certainly wrong...
statements.push( statement );
});
lastChar = node.end; // TODO account for trailing line comment
}
else {
const statement = new Statement( node, this, node.start, node.end ); // TODO should be comment start, comment end
let comment;
do {
comment = this.comments[ commentIndex ];
if ( !comment ) break;
if ( comment.start > node.start ) break;
commentIndex += 1;
} while ( comment.end < lastChar );
const start = comment ? Math.min( comment.start, node.start ) : node.start;
const end = node.end; // TODO account for trailing line comment
const statement = new Statement( node, this, start, end );
statements.push( statement );
lastChar = end;
}
});

3
test/form/exported-empty-vars/_expected/es6.js

@ -1,8 +1,7 @@
var foo;
foo = 42;
var bar;
var baz;
var bar, baz;
bar = 43;
baz = 44;

1
test/form/self-contained-bundle/_expected/amd.js

@ -8,7 +8,6 @@ define(function () { 'use strict';
return 42;
}
// comment before 1
console.log( 1 );

1
test/form/self-contained-bundle/_expected/cjs.js

@ -8,7 +8,6 @@ function bar () {
return 42;
}
// comment before 1
console.log( 1 );

1
test/form/self-contained-bundle/_expected/es6.js

@ -6,7 +6,6 @@ function bar () {
return 42;
}
// comment before 1
console.log( 1 );

1
test/form/self-contained-bundle/_expected/iife.js

@ -8,7 +8,6 @@
return 42;
}
// comment before 1
console.log( 1 );

1
test/form/self-contained-bundle/_expected/umd.js

@ -12,7 +12,6 @@
return 42;
}
// comment before 1
console.log( 1 );

Loading…
Cancel
Save