Skip to content

Commit 5feef14

Browse files
committed
test: add tests for stripComments function
1 parent 390deb8 commit 5feef14

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

src/Loader/Utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ module.exports = {
349349
makeTemplateId,
350350
injectBeforeEndHead,
351351
injectBeforeEndBody,
352+
stripComments,
352353
encodeReservedChars,
353354
decodeReservedChars,
354355
escapeSequences,

test/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ insert_final_newline = false
1616
# keep nl for expected files from tests with pretty option
1717
[cases/*pretty*/**.html]
1818
insert_final_newline = true
19+
20+
[stripComments.test.js]
21+
trim_trailing_whitespace = false

test/stripComments.test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const { stripComments } = require('../src/Loader/Utils');
2+
3+
describe('stripComments', () => {
4+
test('removes single-line comments', () => {
5+
expect(stripComments('const x = 1; // a comment')).toBe('const x = 1; ');
6+
});
7+
8+
test('removes multi-line (block) comments', () => {
9+
expect(stripComments('let y = 2; /* block comment */ y++;')).toBe('let y = 2; y++;');
10+
});
11+
12+
test('preserves comment-like patterns inside single quotes', () => {
13+
expect(stripComments("const s = 'foo // bar'; // real comment")).toBe("const s = 'foo // bar'; ");
14+
});
15+
16+
test('preserves comment-like patterns inside double quotes', () => {
17+
expect(stripComments('const s = "// not a comment"; // real comment')).toBe('const s = "// not a comment"; ');
18+
});
19+
20+
test('preserves comment-like patterns inside template strings', () => {
21+
expect(stripComments('const s = `/* not a comment */`; // real comment')).toBe('const s = `/* not a comment */`; ');
22+
});
23+
24+
test('handles strings with escaped quotes', () => {
25+
expect(stripComments('const s = "a \\" // not comment"; // actual comment')).toBe(
26+
'const s = "a \\" // not comment"; '
27+
);
28+
});
29+
30+
test('line comment disables block comments and code after //', () => {
31+
expect(stripComments('a = 1; // c1 /* c2 */ b = 2; // c3')).toBe('a = 1; ');
32+
});
33+
34+
test('removes multiline block comments', () => {
35+
expect(stripComments('a = 1; /* comment\nstill comment\nend*/ b = 2;')).toBe('a = 1; b = 2;');
36+
});
37+
38+
test('removes only comments, not code that looks like comments', () => {
39+
expect(stripComments('const x = "//"; /* real */')).toBe('const x = "//"; ');
40+
expect(stripComments("const y = '/* not a comment */'; // real")).toBe("const y = '/* not a comment */'; ");
41+
});
42+
43+
test('handles block comments with stars inside', () => {
44+
expect(stripComments('/* ***\ncomment\n***/const x=2;')).toBe('const x=2;');
45+
});
46+
47+
test('handles comments at end of input', () => {
48+
expect(stripComments('const x=1;// comment')).toBe('const x=1;');
49+
expect(stripComments('const x=1;/* comment */')).toBe('const x=1;');
50+
});
51+
52+
test('preserves newlines for removed comments', () => {
53+
expect(stripComments('let a=1; // foo\nlet b=2;')).toBe('let a=1; \nlet b=2;');
54+
expect(stripComments('let a=1; /* foo */\nlet b=2;')).toBe('let a=1; \nlet b=2;');
55+
});
56+
57+
test('handles nested strings with escaped escapes', () => {
58+
expect(stripComments('const s = "\\\\\\"/*"; // comment')).toBe('const s = "\\\\\\"/*"; ');
59+
});
60+
61+
test('handles no comments', () => {
62+
expect(stripComments('let a = 42;')).toBe('let a = 42;');
63+
});
64+
65+
test('handles code with only comments', () => {
66+
expect(stripComments('// only comment')).toBe('');
67+
expect(stripComments('/* only block comment */')).toBe('');
68+
expect(stripComments('// one\n/* two */')).toBe('\n');
69+
});
70+
71+
test('handles empty string', () => {
72+
expect(stripComments('')).toBe('');
73+
});
74+
75+
test('handles sequence of slashes and stars not being comments', () => {
76+
expect(stripComments('const s = "/**/"; // real')).toBe('const s = "/**/"; ');
77+
expect(stripComments("const s = '//'; /* real */")).toBe("const s = '//'; ");
78+
});
79+
80+
test('removes multiple block and line comments together', () => {
81+
const code = `
82+
// line 1
83+
let a = 1; /* block 1 */
84+
let b = 2; // line 2
85+
/* block 2 */
86+
let c = 3;
87+
`;
88+
const expected = `
89+
90+
let a = 1;
91+
let b = 2;
92+
93+
let c = 3;
94+
`;
95+
expect(stripComments(code)).toBe(expected);
96+
});
97+
98+
test('does not remove slashes inside regex literals (known limitation)', () => {
99+
// NOTE: This will fail! The function does not support regex detection.
100+
// Documented as a limitation.
101+
const code = `const re = /\\/\\/.*$/; // regex`;
102+
// The actual output will be incorrect.
103+
// This test documents the limitation (not a failure if you comment it out).
104+
// expect(stripComments(code)).toBe(`const re = /\\/\\/.*$/; `);
105+
});
106+
});

0 commit comments

Comments
 (0)