Skip to content

Commit

Permalink
Merge pull request #29 from Rich-Harris/gh-28
Browse files Browse the repository at this point in the history
prevent syntax errors when combining comments
  • Loading branch information
Rich-Harris committed Jan 27, 2020
2 parents e8fcc30 + 3818be0 commit 8cec439
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/print/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,17 @@ const handle_body = (nodes: Node[], state: State) => {
indent: state.indent
});

let add_newline = false;

while (state.comments.length) {
const comment = state.comments.shift();
const prefix = add_newline ? `\n${state.indent}` : ` `;

chunks.push(c(comment.type === 'Block'
? ` /*${comment.value}*/`
: ` //${comment.value}`));
? `${prefix}/*${comment.value}*/`
: `${prefix}//${comment.value}`));

add_newline = (comment.type === 'Line');
}

return chunks;
Expand Down
6 changes: 6 additions & 0 deletions test/samples/comment-mixed-trailing/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function foo() {

} // hey1
/*
hey2
*/
1 change: 1 addition & 0 deletions test/samples/comment-mixed-trailing/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/samples/comment-mixed-trailing/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = ({ b }) => b`
function foo() {
// hey1
/*
hey2
*/
}
`;
2 changes: 1 addition & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ describe('codered', () => {
fs.writeFileSync(`test/samples/${dir}/_actual.js`, actual.code);
fs.writeFileSync(`test/samples/${dir}/_actual.js.map`, actual.map.toString());

assert.equal(actual.code, expected.code);
assert.equal(actual.code.replace(/\t+$/gm, ''), expected.code.replace(/\t+$/gm, ''));
assert.deepEqual(actual.map, expected.map);
});
});
Expand Down

0 comments on commit 8cec439

Please sign in to comment.