forked from evanw/esbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestructuring-fuzzer.js
402 lines (349 loc) · 10.1 KB
/
destructuring-fuzzer.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
function generateTestCase(assign) {
let sideEffectCount = 0
let patternCount = 0
let limit = 10
let depth = 0
function choice(n) {
return Math.random() * n | 0
}
function patternAndValue() {
patternCount++
switch (choice(3)) {
case 0: return array()
case 1: return object()
case 2: return [assign(), choice(10), choice(10)]
}
}
function sideEffect(result) {
return `s(${sideEffectCount++}${result ? `, ${result}` : ''})`
}
function indent(open, items, close) {
let tab = ' '
items = items.map(i => `\n${tab.repeat(depth + 1)}${i}`).join(',')
return `${open}${items}\n${tab.repeat(depth)}${close}`
}
function id() {
return String.fromCharCode('a'.charCodeAt(0) + choice(3))
}
function array() {
let count = 1 + choice(2)
let pattern = []
let value = []
depth++
for (let i = 0; i < count; i++) {
if (patternCount > limit) break
let [pat, val, defVal] = patternAndValue()
switch (choice(3)) {
case 0:
pattern.push(pat)
value.push(val)
break
case 1:
pattern.push(`${pat} = ${sideEffect(defVal)}`)
value.push(val)
break
case 2:
pattern.push(`${pat} = ${sideEffect(defVal)}`)
value.push(defVal)
break
}
if (choice(10) < 8) value.push(val)
}
if (choice(2)) {
pattern.push(`...${assign()}`)
if (choice(10) < 8) value.push(choice(10))
}
depth--
return [
indent('[', pattern, ']'),
indent('[', value, ']'),
'[]',
]
}
function object() {
let count = 1 + choice(2)
let pattern = []
let value = []
let valKeys = new Set()
depth++
for (let i = 0; i < count; i++) {
if (patternCount > limit) break
let valKey = id()
if (valKeys.has(valKey)) continue
valKeys.add(valKey)
let patKey = choice() ? valKey : `[${sideEffect(`'${valKey}'`)}]`
let [pat, val, defVal] = patternAndValue()
switch (choice(3)) {
case 0:
pattern.push(`${patKey}: ${pat}`)
value.push(`${valKey}: ${val}`)
break
case 1:
pattern.push(`${patKey}: ${pat} = ${sideEffect(defVal)}`)
value.push(`${valKey}: ${val}`)
break
case 2:
pattern.push(`${patKey}: ${pat} = ${sideEffect(defVal)}`)
value.push(`${valKey}: ${defVal}`)
break
}
}
if (choice(2)) {
pattern.push(`...${assign()}`)
if (choice(10) < 8) value.push(`${id()}: ${choice(10)}`)
}
depth--
return [
indent('{', pattern, '}'),
indent('{', value, '}'),
'{}',
]
}
return choice(2) ? array() : object()
}
function evaluate(code) {
let effectTrace = []
let assignTarget = {}
let sideEffect = (id, value) => (effectTrace.push(id), value)
new Function('a', 's', code)(assignTarget, sideEffect)
return JSON.stringify({ assignTarget, effectTrace })
}
function generateTestCases(trials) {
let testCases = []
while (testCases.length < trials) {
let ids = []
let assignCount = 0
let [pattern, value] = generateTestCase(() => {
let id = `_${assignCount++}`
ids.push(id)
return id
})
try {
evaluate(`(${pattern.replace(/_/g, 'a._')} = ${value});`)
testCases.push([pattern, value, ids])
} catch (e) {
}
}
return testCases
}
function AssignmentOperator([pattern, value]) {
let ts = `(${pattern.replace(/_/g, 'a._')} = ${value});`
let js = ts
return { js, ts }
}
function NamespaceExport([pattern, value]) {
let ts = `namespace a { export const ${`${pattern} = ${value}`} }`
let js = `(${pattern.replace(/_/g, 'a._')} = ${value});`
return { js, ts }
}
function ConstDeclaration([pattern, value, ids]) {
let ts = `const ${pattern} = ${value};${ids.map(id => `\na.${id} = ${id};`).join('')}`
let js = ts
return { js, ts }
}
function LetDeclaration([pattern, value, ids]) {
let ts = `let ${pattern} = ${value};${ids.map(id => `\na.${id} = ${id};`).join('')}`
let js = ts
return { js, ts }
}
function VarDeclaration([pattern, value, ids]) {
let ts = `var ${pattern} = ${value};${ids.map(id => `\na.${id} = ${id};`).join('')}`
let js = ts
return { js, ts }
}
function TryCatchBinding([pattern, value, ids]) {
let ts = `try { throw ${value} } catch (${pattern}) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }`
let js = ts
return { js, ts }
}
function FunctionStatementArguments([pattern, value, ids]) {
let ts = `function foo(${pattern}) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }\nfoo(${value});`
let js = ts
return { js, ts }
}
function FunctionExpressionArguments([pattern, value, ids]) {
let ts = `(function(${pattern}) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} })(${value});`
let js = ts
return { js, ts }
}
function ArrowFunctionArguments([pattern, value, ids]) {
let ts = `((${pattern}) => { ${ids.map(id => `a.${id} = ${id};`).join('\n')} })(${value});`
let js = ts
return { js, ts }
}
function ObjectMethodArguments([pattern, value, ids]) {
let ts = `({ foo(${pattern}) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} } }).foo(${value});`
let js = ts
return { js, ts }
}
function ClassStatementMethodArguments([pattern, value, ids]) {
let ts = `class Foo { foo(${pattern}) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} } }\nnew Foo().foo(${value});`
let js = ts
return { js, ts }
}
function ClassExpressionMethodArguments([pattern, value, ids]) {
let ts = `(new (class { foo(${pattern}) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} } })).foo(${value});`
let js = ts
return { js, ts }
}
function ForLoopConst([pattern, value, ids]) {
let ts = `var i; for (const ${pattern} = ${value}; i < 1; i++) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }`
let js = ts
return { js, ts }
}
function ForLoopLet([pattern, value, ids]) {
let ts = `for (let ${pattern} = ${value}, i = 0; i < 1; i++) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }`
let js = ts
return { js, ts }
}
function ForLoopVar([pattern, value, ids]) {
let ts = `for (var ${pattern} = ${value}, i = 0; i < 1; i++) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }`
let js = ts
return { js, ts }
}
function ForLoop([pattern, value]) {
let ts = `for (${pattern.replace(/_/g, 'a._')} = ${value}; 0; ) ;`
let js = ts
return { js, ts }
}
function ForOfLoopConst([pattern, value, ids]) {
let ts = `for (const ${pattern} of [${value}]) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }`
let js = ts
return { js, ts }
}
function ForOfLoopLet([pattern, value, ids]) {
let ts = `for (let ${pattern} of [${value}]) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }`
let js = ts
return { js, ts }
}
function ForOfLoopVar([pattern, value, ids]) {
let ts = `for (var ${pattern} of [${value}]) { ${ids.map(id => `a.${id} = ${id};`).join('\n')} }`
let js = ts
return { js, ts }
}
function ForOfLoop([pattern, value]) {
let ts = `for (${pattern.replace(/_/g, 'a._')} of [${value}]) ;`
let js = ts
return { js, ts }
}
async function verify(test, transform, testCases) {
let verbose = process.argv.indexOf('--verbose') >= 0
let indent = t => t.replace(/\n/g, '\n ')
let newline = false
console.log(`${test.name} (${transform.name}):`)
await concurrentMap(testCases, 20, async (testCase) => {
let { js, ts } = test(testCase)
let expected
try {
expected = evaluate(js)
} catch (e) {
return
}
let transformed
try {
transformed = await transform(ts)
} catch (e) {
process.stdout.write('T')
newline = true
if (verbose) {
console.log('\n' + '='.repeat(80))
console.log(indent(`Original code:\n${ts}`))
console.log(indent(`Transform error:\n${e}`))
newline = false
}
return
}
let actual
try {
actual = evaluate(transformed)
} catch (e) {
actual = e + ''
}
if (actual !== expected) {
process.stdout.write(actual.indexOf('SyntaxError') >= 0 ? 'S' : 'X')
newline = true
if (verbose) {
console.log('\n' + '='.repeat(80))
console.log(indent(`Original code:\n${ts}`))
console.log(indent(`Transformed code:\n${transformed}`))
console.log(indent(`Expected output:\n${expected}`))
console.log(indent(`Actual output:\n${actual}`))
newline = false
}
} else {
process.stdout.write('-')
newline = true
}
})
if (newline) process.stdout.write('\n')
}
function concurrentMap(items, batch, callback) {
return new Promise((resolve, reject) => {
let index = 0
let pending = 0
let next = () => {
if (index === items.length && pending === 0) {
resolve()
} else if (index < items.length) {
let item = items[index++]
pending++
callback(item).then(() => {
pending--
next()
}, e => {
items.length = 0
reject(e)
})
}
}
for (let i = 0; i < batch; i++)next()
})
}
async function main() {
let es = require('./esbuild').installForTests()
let esbuild = async (x) => (await es.transform(x, { target: 'es6', loader: 'ts' })).code.trim()
console.log(`
Options:
--verbose = Print details for failures
Legend:
- = The test passed
X = The test failed
T = The transform function itself failed
S = The generated code has a syntax error`)
let tests = [
// Bindings
ConstDeclaration,
LetDeclaration,
VarDeclaration,
TryCatchBinding,
FunctionStatementArguments,
FunctionExpressionArguments,
ArrowFunctionArguments,
ObjectMethodArguments,
ClassStatementMethodArguments,
ClassExpressionMethodArguments,
ForLoopConst,
ForLoopLet,
ForLoopVar,
ForOfLoopConst,
ForOfLoopLet,
ForOfLoopVar,
// Destructuring
AssignmentOperator,
ForLoop,
ForOfLoop,
// TypeScript-specific
NamespaceExport,
]
let transforms = [
esbuild,
]
let testCases = generateTestCases(100)
for (let transform of transforms) {
console.log()
for (let test of tests) {
await verify(test, transform, testCases)
}
}
}
main().catch(e => setTimeout(() => { throw e }))