/
index.spec.ts
277 lines (255 loc) · 7.4 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import test from 'ava'
import { js, parseFiles, ts, tsx, html } from '../index'
const { parse, kind } = js
let parseMulti = countedPromise(parseFiles)
test('find from native code', t => {
const sg = parse('console.log(123)')
const match = sg.root().find('console.log')
t.deepEqual(match!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 11, index: 11 },
})
const node = match!.find('console')
t.deepEqual(node!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 7, index: 7 },
})
})
test('parse async', async t => {
const sg = await js.parseAsync('console.log(123)')
const match = sg.root().find('console.log')
t.deepEqual(match!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 11, index: 11 },
})
const node = match!.find('console')
t.deepEqual(node!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 7, index: 7 },
})
})
test('find multiple nodes', t => {
const sg = parse('a(1, 2, 3)')
const match = sg.root().find('a($$$B)')
t.deepEqual(match!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 10, index: 10 },
})
const matchedVar = match!.getMultipleMatches('B')
let start = matchedVar[0].range().start;
let end = matchedVar[matchedVar.length - 1].range().end;
t.deepEqual(start, { line: 0, column: 2, index: 2 })
t.deepEqual(end, { line: 0, column: 9, index: 9 })
})
test('find unicode', t => {
const str = `console.log("Hello, 世界")
print("ザ・ワールド")`
const sg = parse(str)
const match = sg.root().find('console.log($_)')
t.deepEqual(match!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 24, index: 24 },
})
const node = sg.root().find('print($_)')
t.deepEqual(node!.range(), {
start: { line: 1, column: 2, index: 27 },
end: { line: 1, column: 17, index: 42 },
})
})
test('find with transformation', t => {
const str = `console.log("Hello, 世界")`
const sg = parse(str)
const match = sg.root().find({
rule: {
pattern: 'console.log($A)',
},
transform: {
NEW_ARG: {
substring: {
source: '$A',
startChar: 1,
endChar: -1,
}
},
}
})!
t.deepEqual(match.getTransformed('NEW_ARG'), 'Hello, 世界')
t.deepEqual(match.getMatch('A')?.text(), '"Hello, 世界"')
})
test('findAll from native code', t => {
const sg = parse('console.log(123); let a = console.log.bind(console);')
const match = sg.root().findAll('console.log')
t.deepEqual(match.length, 2)
t.deepEqual(match[0].range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 11, index: 11 },
})
t.deepEqual(match[1].range(), {
start: { line: 0, column: 26, index: 26 },
end: { line: 0, column: 37, index: 37 },
})
})
test('find not match', t => {
const sg = parse('console.log(123)')
const match = sg.root().find('notExist')
t.is(match, null)
})
test('get variable', t => {
const sg = parse('console.log("hello world")')
const match = sg.root().find('console.log($MATCH)')
t.is(match!.getMatch('MATCH')!.text(), '"hello world"')
})
test('find by kind', t => {
const sg = parse('console.log("hello world")')
const match = sg.root().find(kind('member_expression'))
t.deepEqual(match!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 11, index: 11 },
})
})
test('find by config', t => {
const sg = parse('console.log("hello world")')
const match = sg.root().find({
rule: {kind: 'member_expression'},
})
t.deepEqual(match!.range(), {
start: { line: 0, column: 0, index: 0 },
end: { line: 0, column: 11, index: 11 },
})
})
test('test find files', async t => {
await parseMulti(['./__test__/index.spec.ts'], (err, tree) => {
t.is(err, null)
t.is(tree.filename(), './__test__/index.spec.ts')
t.assert(tree.root() !== null)
})
})
test('test file count', async t => {
let i = 0
let fileCount = await parseMulti(['./'], (err, _) => {
// ZZZ... sleep a while to mock expensive operation
let start = Date.now()
while (Date.now() - start < 1) continue
t.is(err, null)
i++
})
t.is(i, fileCount)
})
test('test languageGlobs', async t => {
let i = 0
let foundVue = false
let fileCount = await parseMulti({
paths: ['./'],
languageGlobs: {
'html': ['*.vue']
},
}, (err, root) => {
t.is(err, null)
if (root.filename().endsWith('.vue')) {
foundVue = true
}
i++
})
t.is(i, fileCount)
t.true(foundVue)
})
test('show good error message for invalid arg', async t => {
const sg = parse('console.log(123)')
t.throws(() => sg.root().find({rule: {regex: '('}}), {
message: /Rule contains invalid regex matcher/
})
t.throws(() => sg.root().find({
rule: { all: [{any: [{ kind: '33'}]}]}
}), {
message: /Rule contains invalid kind matcher/
})
})
test('find in files', async t => {
let findInFiles = countedPromise(ts.findInFiles)
await findInFiles({
paths: ['./'],
matcher: {
rule: {kind: 'member_expression'}
},
}, (err, n) => {
// ZZZ... sleep a while to mock expensive operation
let start = Date.now()
while (Date.now() - start < 1) continue
t.is(err, null)
t.assert(n.length > 0)
t.assert(n[0].text().includes('.'))
})
})
test('find in files with filename', async t => {
let findInFiles = countedPromise(ts.findInFiles)
await findInFiles({
paths: ['./'],
matcher: {
rule: {kind: 'member_expression'},
},
}, (err, n) => {
t.is(err, null)
const root = n[0].getRoot();
t.is(root.filename().replace('\\', '/'), './__test__/index.spec.ts')
})
})
test('tsx should not find ts file', async t => {
await tsx.findInFiles({
paths: ['./'],
matcher: {
rule: {kind: 'member_expression'},
},
}, () => {
t.assert(false)
})
t.assert(true)
})
test('find with language globs', async t => {
let findInFiles = countedPromise(tsx.findInFiles)
await findInFiles({
paths: ['./'],
matcher: {
rule: {kind: 'member_expression'},
},
languageGlobs: ['*.ts'],
}, (err, n) => {
t.is(err, null)
const root = n[0].getRoot();
t.is(root.filename().replace('\\', '/'), './__test__/index.spec.ts')
})
})
test('find with language globs can parse with correct language', async t => {
let findInFiles = countedPromise(html.findInFiles)
await findInFiles({
paths: ['./'],
matcher: {
rule: {pattern: '<template>$A</template>'},
},
languageGlobs: ['*.vue'],
}, (err, n) => {
t.is(err, null)
const root = n[0].getRoot();
t.is(root.filename().replace('\\', '/'), './__test__/test.vue')
const div = root.root().find('<h1>$A</h1>')?.getMatch('A')?.text()
t.is(div, '{{ greeting }}')
})
})
function countedPromise<F extends (t: any, cb: any) => Promise<number>>(func: F) {
type P = Parameters<F>
return async (t: P[0], cb: P[1]) => {
let i = 0
let fileCount: number | undefined = undefined
let resolve = () => {} // will be called after all files are processed
function wrapped(...args: any[]) {
let ret = cb(...args)
if (++i === fileCount) resolve()
return ret
}
fileCount = await func(t, wrapped as P[1])
// all files are not processed, wait the resolve function to be called
if (fileCount > i) {
await new Promise<void>(r => resolve = r)
}
return fileCount
}
}