Browse Source

prevent comments inside statement being later appended to it

contingency-plan
Rich Harris 10 years ago
parent
commit
954408e53f
  1. 12
      src/ast/analyse.js
  2. 4
      test/form/block-comments/_config.js
  3. 13
      test/form/block-comments/foo.js
  4. 3
      test/form/block-comments/main.js
  5. 8
      test/function/iife-comments/_config.js
  6. 13
      test/function/iife-comments/main.js

12
src/ast/analyse.js

@ -47,9 +47,19 @@ export default function analyse ( ast, magicString, module ) {
let trailing = !!previousStatement;
// TODO surely this can be neater
// attach leading comment
do {
const comment = module.comments[ commentIndex ];
let comment = module.comments[ commentIndex ];
// prevent comments inside the previous statement being
// appended to it
if ( previousStatement ) {
while ( comment && comment.start < previousStatement.end ) {
commentIndex += 1;
comment = module.comments[ commentIndex ];
}
}
if ( !comment || ( comment.end > statement.start ) ) break;

4
test/form/block-comments/_config.js

@ -0,0 +1,4 @@
module.exports = {
description: 'block comments are printed correctly',
skip: true // work on this later
};

13
test/form/block-comments/foo.js

@ -0,0 +1,13 @@
export default function foo () {
return embiggen( 6, 7 );
}
/**
* Embiggens a number
* @param {number} num - the number to embiggen
* @param {number} factor - the factor to embiggen it by
* @returns {number}
*/
function embiggen ( num, factor ) {
return num * factor;
}

3
test/form/block-comments/main.js

@ -0,0 +1,3 @@
import foo from './foo';
alert( foo() );

8
test/function/iife-comments/_config.js

@ -0,0 +1,8 @@
var assert = require( 'assert' );
module.exports = {
description: 'does not wrongly append comments',
exports: function ( exports ) {
assert.equal( exports, 42 );
}
}

13
test/function/iife-comments/main.js

@ -0,0 +1,13 @@
(function () {
console.log( '1' ) // not an ID. should fix!
/*
BLOCK COMMENT
*/
console.log( '2' );
// standalone comment
})();
export default 42;
Loading…
Cancel
Save