UNPKG

als-remove-js-comments

Version:

A utility for removing JavaScript comments, supporting both single-line and multi-line comments with proper handling of string literals.

131 lines (111 loc) 5.34 kB
const { describe, it } = require('node:test'); const assert = require('node:assert'); const removeComments = require('./remove-comments'); describe('removeComments', () => { it('should remove single-line comments', () => { const input = `const a = 5; // This is a comment\nconst b = 10; // Another comment`; const expectedOutput = `const a = 5; \nconst b = 10; `; assert.strictEqual(removeComments(input), expectedOutput); }); it('should remove multi-line comments', () => { const input = `const a = 5; /* This is a\nmulti-line comment */\nconst b = 10;`; const expectedOutput = `const a = 5; \nconst b = 10;`; assert.strictEqual(removeComments(input), expectedOutput); }); it('should handle comments on separate lines', () => { const input = `// This is a comment\nconst a = 5;`; const expectedOutput = `\nconst a = 5;`; assert.strictEqual(removeComments(input), expectedOutput); }); it('should handle strings with escaped quotes', () => { const input = `const str = "This is a string with an escaped quote: \\" and a // comment";`; assert.strictEqual(removeComments(input), input); }); it('should handle empty strings', () => { const input = ``; const expectedOutput = ``; assert.strictEqual(removeComments(input), expectedOutput); }); it('should handle code without comments', () => { const input = `const a = 5;\nconst b = 10;`; const expectedOutput = `const a = 5;\nconst b = 10;`; assert.strictEqual(removeComments(input), expectedOutput); }); it('should remove mixed single-line and multi-line comments', () => { const input = `const a = 5; // single-line\n/* multi-line\ncomment */ const b = 10;`; const expectedOutput = `const a = 5; \n const b = 10;`; assert.strictEqual(removeComments(input), expectedOutput); }); it('should remove single-line comment with // inside quotes', () => { const input = `ic.outerHTML = ic.outerHTML + '<svg version="1.1" id="iconBigCursorSvg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="position: absolute;width: 19px;height: 19px;top: 9px; left: 9px;" xml:space="preserve"><path d="M 423.547 323.115 l -320 -320 c -3.051 -3.051 -7.637 -3.947 -11.627 -2.304 s -6.592 5.547 -6.592 9.856 V 480 c 0 4.501 2.837 8.533 7.083 10.048 c 4.224 1.536 8.981 0.192 11.84 -3.285 l 85.205 -104.128 l 56.853 123.179 c 1.792 3.883 5.653 6.187 9.685 6.187 c 1.408 0 2.837 -0.277 4.203 -0.875 l 74.667 -32 c 2.645 -1.131 4.736 -3.285 5.76 -5.973 c 1.024 -2.688 0.939 -5.675 -0.277 -8.299 l -57.024 -123.52 h 132.672 c 4.309 0 8.213 -2.603 9.856 -6.592 C 427.515 330.752 426.598 326.187 423.547 323.115 Z"/></svg>';`; assert.strictEqual(removeComments(input), input); }); }); describe('removeComments - Additional Tests', () => { it('should remove multiline comments that are not properly closed', () => { const input = `const a = 5; /* unclosed comment`; const expectedOutput = `const a = 5; `; assert.strictEqual(removeComments(input), expectedOutput); }); it('should not remove URLs containing // or /* */', () => { const input = `const url = 'http://example.com/*not-a-comment*/';`; assert.strictEqual(removeComments(input), input); }); it('should handle JSX inline attributes', () => { const input = ` const Button = () => ( <button title="Click me // not a comment"> {/* Click here */} Click Me </button> );`; const expectedOutput = ` const Button = () => ( <button title="Click me // not a comment"> Click Me </button> );`; assert.strictEqual(removeComments(input), expectedOutput); }); it('should remove comments after JSX components', () => { const input = ` <div> <Component /> // comment after component </div>`; const expectedOutput = ` <div> <Component /> </div>`; assert.strictEqual(removeComments(input), expectedOutput); }); it('should preserve comments inside strings', () => { const input = `const str = "This is a string with /* not a comment */ inside";`; assert.strictEqual(removeComments(input), input); }); it('should handle JSX with comments', () => { const input = ` const App = () => { return ( <div> {/* This is a JSX comment */} Hello World! </div> ); };`; const expectedOutput = ` const App = () => { return ( <div> Hello World! </div> ); };`; const result = removeComments(input) assert.strictEqual(result, expectedOutput); }); it.skip('should handle nested comments gracefully', () => { const input = `const a = 5; /* outer /* inner */ still outer */ const b = 10;`; const expectedOutput = `const a = 5; const b = 10;`; assert.strictEqual(removeComments(input), expectedOutput); }); });