forked from vitejs/vite
/
cleanString.spec.ts
115 lines (101 loc) · 2.48 KB
/
cleanString.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { emptyString, findEmptyStringRawIndex } from '../../node/cleanString'
test('comments', () => {
expect(
emptyString(`
// comment1 // comment
// comment1
/* coment2 */
/*
// coment3
*/
/* // coment3 */
/* // coment3 */ // comment
// comment 4 /* comment 5 */
`).clean.trim()
).toBe('')
})
test('strings', () => {
const clean = emptyString(`
// comment1
const a = 'aaaa'
/* coment2 */
const b = "bbbb"
/*
// coment3
*/
/* // coment3 */
// comment 4 /* comment 5 */
`)
expect(clean.clean).toMatch("const a = '\0\0\0\0'")
expect(clean.clean).toMatch('const b = "\0\0\0\0"')
})
test('strings comment nested', () => {
expect(
emptyString(`
// comment 1 "
const a = "a //"
// comment 2 "
`).clean
).toMatch('const a = "\0\0\0\0"')
expect(
emptyString(`
// comment 1 '
const a = "a //"
// comment 2 '
`).clean
).toMatch('const a = "\0\0\0\0"')
expect(
emptyString(`
// comment 1 \`
const a = "a //"
// comment 2 \`
`).clean
).toMatch('const a = "\0\0\0\0"')
expect(
emptyString(`
const a = "a //"
console.log("console")
`).clean
).toMatch('const a = "\0\0\0\0"')
expect(
emptyString(`
const a = "a /*"
console.log("console")
const b = "b */"
`).clean
).toMatch('const a = "\0\0\0\0"')
expect(
emptyString(`
const a = "a ' "
console.log("console")
const b = "b ' "
`).clean
).toMatch('const a = "\0\0\0\0"')
expect(
emptyString(`
const a = "a \` "
console.log("console")
const b = "b \` "
`).clean
).toMatch('const a = "\0\0\0\0"')
})
test('find empty string flag in raw index', () => {
const str = `
const a = "aaaaa"
const b = "bbbbb"
`
const clean = emptyString(str)
expect(clean.clean).toMatch('const a = "\0\0\0\0\0"')
expect(clean.clean).toMatch('const b = "\0\0\0\0\0"')
const aIndex = str.indexOf('const a = "aaaaa"')
const a = findEmptyStringRawIndex(clean, '\0\0\0\0\0', aIndex)
expect(str.slice(a[0], a[1])).toMatch('aaaaa')
const bIndex = str.indexOf('const b = "bbbbb"')
const b = findEmptyStringRawIndex(clean, '\0\0\0\0\0', bIndex)
expect(str.slice(b[0], b[1])).toMatch('bbbbb')
})
// describe('template string nested', () => {
// const str = "`##${a + b + `##${c + `##${d}`}##`}##`"
// const clean = emptyString(str)
// expect(clean.clean).toMatch('`\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0`')
// })