diff --git a/packages/comment/src/index.js b/packages/comment/src/index.js index af50c7f..beb5d6c 100644 --- a/packages/comment/src/index.js +++ b/packages/comment/src/index.js @@ -6,25 +6,26 @@ export default { init(jsep) { // treat all comments as whitespace to remove from parsing - jsep.hooks.add('gobble-spaces', function gobbleComment(env) { + jsep.hooks.add('gobble-spaces', function gobbleComment() { if (this.code === FSLSH_CODE) { let ch = this.expr.charCodeAt(this.index + 1); if (ch === FSLSH_CODE) { // '//': read to end of line/input - this.index += 2; + this.index += 1; while (ch !== jsep.LF_CODE && !isNaN(ch)) { - ch = this.expr.charCodeAt(this.index++); + ch = this.expr.charCodeAt(++this.index); } + this.gobbleSpaces(); } else if (ch === ASTSK_CODE) { // read to */ or end of input - env.index += 2; + this.index += 2; while (!isNaN(ch)) { - ch = this.expr.charCodeAt(++this.index); + ch = this.expr.charCodeAt(this.index++); if (ch === ASTSK_CODE) { - ch = this.expr.charCodeAt(++this.index); + ch = this.expr.charCodeAt(this.index++); if (ch === FSLSH_CODE) { - this.index += 1; + this.gobbleSpaces(); return; } } diff --git a/packages/comment/test/index.test.js b/packages/comment/test/index.test.js index 5370ffc..bf7e513 100644 --- a/packages/comment/test/index.test.js +++ b/packages/comment/test/index.test.js @@ -11,10 +11,14 @@ const { test } = QUnit; [ 'a /* ignore this */> 1 // ignore this too', + 'a/* ignore this */ > 1 // ignore this too', + '(a/* ignore this */ )>/**/ 1 // ignore this too', 'a /* ignore *\r\n *this */> 1 // ignore this too', + 'a /*ignore *\r\n *this *///\n> 1 // ignore this too', 'a // ignore this\r\n> 1', 'a /** {param} \r\n */> 1', '// a\na > 1', + '// a\n //\n a > 1', ].forEach(expr => test(`should parse out comments from expression ${expr}`, (assert) => { testParser(expr, { type: 'BinaryExpression',