-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathstrip-literal.test.ts
More file actions
125 lines (111 loc) · 4.47 KB
/
strip-literal.test.ts
File metadata and controls
125 lines (111 loc) · 4.47 KB
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
116
117
118
119
120
121
122
123
124
125
import { describe, expect, it } from 'vitest'
import { stripLiteral } from 'strip-literal'
describe('stripLiteral for viteRsc API detection', () => {
it('should strip comments with import.meta.viteRsc.loadModule', () => {
const code = `
// This is a comment with import.meta.viteRsc.loadModule("test")
/* block comment import.meta.viteRsc.loadModule("test") */
import.meta.viteRsc.loadModule("actual");
`
const stripped = stripLiteral(code)
const matches = [
...stripped.matchAll(/import\.meta\.viteRsc\.loadModule\(([\s\S]*?)\)/dg),
]
// Should only match the actual call, not the ones in comments
expect(matches.length).toBe(1)
// Extract argument using indices from original code
const [argStart, argEnd] = matches[0]!.indices![1]!
const argCode = code.slice(argStart, argEnd).trim()
expect(argCode).toBe('"actual"')
})
it('should strip strings with import.meta.viteRsc.loadModule', () => {
const code = `
const x = "string with import.meta.viteRsc.loadModule('test')";
const y = 'another string import.meta.viteRsc.loadModule("test")';
import.meta.viteRsc.loadModule("actual");
`
const stripped = stripLiteral(code)
const matches = [
...stripped.matchAll(/import\.meta\.viteRsc\.loadModule\(([\s\S]*?)\)/dg),
]
// Should only match the actual call, not the ones in strings
expect(matches.length).toBe(1)
const [argStart, argEnd] = matches[0]!.indices![1]!
const argCode = code.slice(argStart, argEnd).trim()
expect(argCode).toBe('"actual"')
})
it('should strip comments with import.meta.viteRsc.loadCss', () => {
const code = `
// This is a comment with import.meta.viteRsc.loadCss("test")
/* block comment import.meta.viteRsc.loadCss("test") */
import.meta.viteRsc.loadCss("actual");
`
const stripped = stripLiteral(code)
const matches = [
...stripped.matchAll(/import\.meta\.viteRsc\.loadCss\(([\s\S]*?)\)/dg),
]
// Should only match the actual call, not the ones in comments
expect(matches.length).toBe(1)
const [argStart, argEnd] = matches[0]!.indices![1]!
const argCode = code.slice(argStart, argEnd).trim()
expect(argCode).toBe('"actual"')
})
it('should strip strings with import.meta.viteRsc.loadCss', () => {
const code = `
const x = "string with import.meta.viteRsc.loadCss('test')";
import.meta.viteRsc.loadCss("actual");
`
const stripped = stripLiteral(code)
const matches = [
...stripped.matchAll(/import\.meta\.viteRsc\.loadCss\(([\s\S]*?)\)/dg),
]
// Should only match the actual call, not the ones in strings
expect(matches.length).toBe(1)
const [argStart, argEnd] = matches[0]!.indices![1]!
const argCode = code.slice(argStart, argEnd).trim()
expect(argCode).toBe('"actual"')
})
it('should strip comments with import.meta.viteRsc.loadBootstrapScriptContent', () => {
const code = `
// This is a comment with import.meta.viteRsc.loadBootstrapScriptContent("test")
/* block import.meta.viteRsc.loadBootstrapScriptContent("test") */
import.meta.viteRsc.loadBootstrapScriptContent("actual");
`
const stripped = stripLiteral(code)
const matches = [
...stripped.matchAll(
/import\s*\.\s*meta\s*\.\s*viteRsc\s*\.\s*loadBootstrapScriptContent\(([\s\S]*?)\)/dg,
),
]
// Should only match the actual call, not the ones in comments
expect(matches.length).toBe(1)
const [argStart, argEnd] = matches[0]!.indices![1]!
const argCode = code.slice(argStart, argEnd).trim()
expect(argCode).toBe('"actual"')
})
it('should handle mixed comments and strings', () => {
const code = `
// Comment with import.meta.viteRsc.loadModule("comment")
const x = "string with import.meta.viteRsc.loadModule('string')";
/*
* Multi-line comment
* import.meta.viteRsc.loadModule("multiline")
*/
import.meta.viteRsc.loadModule("first-actual");
const y = \`template with import.meta.viteRsc.loadModule('template')\`;
import.meta.viteRsc.loadModule("second-actual");
`
const stripped = stripLiteral(code)
const matches = [
...stripped.matchAll(/import\.meta\.viteRsc\.loadModule\(([\s\S]*?)\)/dg),
]
// Should only match the actual calls
expect(matches.length).toBe(2)
const [argStart1, argEnd1] = matches[0]!.indices![1]!
const argCode1 = code.slice(argStart1, argEnd1).trim()
expect(argCode1).toBe('"first-actual"')
const [argStart2, argEnd2] = matches[1]!.indices![1]!
const argCode2 = code.slice(argStart2, argEnd2).trim()
expect(argCode2).toBe('"second-actual"')
})
})