From 954408e53f2e874b65b56c1d1f47cfbc286a4d5f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 21 May 2015 14:35:32 -0400 Subject: [PATCH] prevent comments inside statement being later appended to it --- src/ast/analyse.js | 12 +++++++++++- test/form/block-comments/_config.js | 4 ++++ test/form/block-comments/foo.js | 13 +++++++++++++ test/form/block-comments/main.js | 3 +++ test/function/iife-comments/_config.js | 8 ++++++++ test/function/iife-comments/main.js | 13 +++++++++++++ 6 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/form/block-comments/_config.js create mode 100644 test/form/block-comments/foo.js create mode 100644 test/form/block-comments/main.js create mode 100644 test/function/iife-comments/_config.js create mode 100644 test/function/iife-comments/main.js diff --git a/src/ast/analyse.js b/src/ast/analyse.js index a111cc4..a0a582e 100644 --- a/src/ast/analyse.js +++ b/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; diff --git a/test/form/block-comments/_config.js b/test/form/block-comments/_config.js new file mode 100644 index 0000000..fc82b3f --- /dev/null +++ b/test/form/block-comments/_config.js @@ -0,0 +1,4 @@ +module.exports = { + description: 'block comments are printed correctly', + skip: true // work on this later +}; diff --git a/test/form/block-comments/foo.js b/test/form/block-comments/foo.js new file mode 100644 index 0000000..e43388e --- /dev/null +++ b/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; +} diff --git a/test/form/block-comments/main.js b/test/form/block-comments/main.js new file mode 100644 index 0000000..b4ad555 --- /dev/null +++ b/test/form/block-comments/main.js @@ -0,0 +1,3 @@ +import foo from './foo'; + +alert( foo() ); diff --git a/test/function/iife-comments/_config.js b/test/function/iife-comments/_config.js new file mode 100644 index 0000000..6b991a8 --- /dev/null +++ b/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 ); + } +} diff --git a/test/function/iife-comments/main.js b/test/function/iife-comments/main.js new file mode 100644 index 0000000..0da7960 --- /dev/null +++ b/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;