This repository has been archived by the owner on Feb 8, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
482 lines (434 loc) · 12.5 KB
/
index.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/**
* @author Charlike Mike Reagent <open.source.charlike@gmail.com>
* @copyright 2016-present @tunnckoCore/team and contributors
* @license MIT
*/
/* eslint-disable max-len */
import test from 'mukla'
import espree from 'espree'
import babylon from 'babylon'
import acornLoose from 'acorn/dist/acorn_loose'
import acorn from 'acorn'
import forIn from 'for-in'
import clone from 'clone-deep'
import parseFunction from '../src/index'
const acornParse = acorn.parse
const espreeParse = espree.parse
const babylonParse = babylon.parse
const acornLooseParse = acornLoose.parse_dammit
const actuals = {
regulars: [
'function (a = {foo: "ba)r", baz: 123}, cb, ...restArgs) {return a * 3}',
'function (b, callback, ...restArgs) {callback(null, b + 3)}',
'function (c) {return c * 3}',
'function (...restArgs) {return 321}',
'function () {}',
'function (a = (true, false)) {}',
'function (a = (true, null)) {}',
'function (a = (true, "bar")) {}',
'function (a, b = (i++, true)) {}',
'function (a = 1) {}',
],
named: [
'function namedFn (a = {foo: "ba)r", baz: 123}, cb, ...restArgs) {return a * 3}',
'function namedFn (b, callback, ...restArgs) {callback(null, b + 3)}',
'function namedFn (c) {return c * 3}',
'function namedFn (...restArgs) {return 321}',
'function namedFn () {}',
'function namedFn(a = (true, false)) {}',
'function namedFn(a = (true, null)) {}',
'function namedFn(a = (true, "bar")) {}',
'function namedFn(a, b = (i++, true)) {}',
'function namedFn(a = 1) {}',
],
generators: [
'function * namedFn (a = {foo: "ba)r", baz: 123}, cb, ...restArgs) {return a * 3}',
'function * namedFn (b, callback, ...restArgs) {callback(null, b + 3)}',
'function * namedFn (c) {return c * 3}',
'function * namedFn (...restArgs) {return 321}',
'function * namedFn () {}',
'function * namedFn(a = (true, false)) {}',
'function * namedFn(a = (true, null)) {}',
'function * namedFn(a = (true, "bar")) {}',
'function * namedFn(a, b = (i++, true)) {}',
'function * namedFn(a = 1) {}',
],
arrows: [
'(a = {foo: "ba)r", baz: 123}, cb, ...restArgs) => {return a * 3}',
'(b, callback, ...restArgs) => {callback(null, b + 3)}',
'(c) => {return c * 3}',
'(...restArgs) => {return 321}',
'() => {}',
'(a = (true, false)) => {}',
'(a = (true, null)) => {}',
'(a = (true, "bar")) => {}',
'(a, b = (i++, true)) => {}',
'(a = 1) => {}',
'(a) => a * 3 * a',
'd => d * 355 * d',
'e => {return e + 5235 / e}',
'(a, b) => a + 3 + b',
'(x, y, ...restArgs) => console.log({ value: x * y })',
],
}
/**
* Merge all into one
* and prepend `async` keyword
* before each function
*/
actuals.asyncs = actuals.regulars
.concat(actuals.named)
.concat(actuals.arrows)
.map((item) => `async ${item}`)
const regulars = [
{
name: null,
params: 'a, cb, restArgs',
args: ['a', 'cb', 'restArgs'],
body: 'return a * 3',
defaults: {
a: '{foo: "ba)r", baz: 123}',
cb: undefined,
restArgs: undefined,
},
},
{
name: null,
params: 'b, callback, restArgs',
args: ['b', 'callback', 'restArgs'],
body: 'callback(null, b + 3)',
defaults: { b: undefined, callback: undefined, restArgs: undefined },
},
{
name: null,
params: 'c',
args: ['c'],
body: 'return c * 3',
defaults: { c: undefined },
},
{
name: null,
params: 'restArgs',
args: ['restArgs'],
body: 'return 321',
defaults: { restArgs: undefined },
},
{
name: null,
params: '',
args: [],
body: '',
defaults: {},
},
{
name: null,
params: 'a',
args: ['a'],
body: '',
defaults: { a: 'false' },
},
{
name: null,
params: 'a',
args: ['a'],
body: '',
defaults: { a: 'null' },
},
{
name: null,
params: 'a',
args: ['a'],
body: '',
defaults: { a: '"bar"' },
},
{
name: null,
params: 'a, b',
args: ['a', 'b'],
body: '',
defaults: { a: undefined, b: 'true' },
},
{
name: null,
params: 'a',
args: ['a'],
body: '',
defaults: { a: '1' },
},
]
/**
* All of the regular functions
* variants plus few more
*/
const arrows = clone(regulars).concat([
{
name: null,
params: 'a',
args: ['a'],
body: 'a * 3 * a',
defaults: { a: undefined },
},
{
name: null,
params: 'd',
args: ['d'],
body: 'd * 355 * d',
defaults: { d: undefined },
},
{
name: null,
params: 'e',
args: ['e'],
body: 'return e + 5235 / e',
defaults: { e: undefined },
},
{
name: null,
params: 'a, b',
args: ['a', 'b'],
body: 'a + 3 + b',
defaults: { a: undefined, b: undefined },
},
{
name: null,
params: 'x, y, restArgs',
args: ['x', 'y', 'restArgs'],
body: 'console.log({ value: x * y })',
defaults: { x: undefined, y: undefined, restArgs: undefined },
},
])
/**
* All of the regulars, but
* with different name
*/
const named = clone(regulars).map((item) => {
item.name = 'namedFn'
return item
})
const expected = {
regulars: regulars,
named: named,
generators: named,
arrows: arrows,
asyncs: regulars.concat(named).concat(arrows),
}
let testsCount = 1
/**
* Factory for DRY, we run all tests
* over two available parsers - one
* is the default `babylon`, second is
* the `acorn.parse` method.
*/
function factory (parserName, parseFn) {
forIn(actuals, (values, key) => {
values.forEach((code, i) => {
const actual = parseFn(code)
const expect = expected[key][i]
const value = actual.value.replace(/^\(\{? ?/, '').replace(/\)$/, '')
test(`#${testsCount++} - ${parserName} - ${value}`, (done) => {
test.strictEqual(actual.isValid, true)
test.strictEqual(actual.name, expect.name)
test.strictEqual(actual.body, expect.body)
test.strictEqual(actual.params, expect.params)
test.deepEqual(actual.args, expect.args)
test.deepEqual(actual.defaults, expect.defaults)
test.ok(actual.value)
done()
})
})
})
test(`#${testsCount++} - ${parserName} - should return object with default values when invalid`, (done) => {
const actual = parseFn(123456)
test.strictEqual(actual.isValid, false)
test.strictEqual(actual.value, '')
test.strictEqual(actual.name, null)
test.strictEqual(actual.body, '')
test.strictEqual(actual.params, '')
test.deepEqual(actual.args, [])
done()
})
test(`#${testsCount++} - ${parserName} - should have '.isValid' and few '.is*'' hidden properties`, (done) => {
const actual = parseFn([1, 2, 3])
test.strictEqual(actual.isValid, false)
test.strictEqual(actual.isArrow, false)
test.strictEqual(actual.isAsync, false)
test.strictEqual(actual.isNamed, false)
test.strictEqual(actual.isAnonymous, false)
test.strictEqual(actual.isGenerator, false)
test.strictEqual(actual.isExpression, false)
done()
})
// bug in v4 and v5
// https://github.com/tunnckoCore/parse-function/issues/3
// test(`#${testsCount++} - ${parserName} - should not fails to get .body when something after close curly`, (done) => {
// const actual = parseFn('function (a) {return a * 2}; var b = 1')
// test.strictEqual(actual.body, 'return a * 2')
// done()
// })
test(`#${testsCount++} - ${parserName} - should work when comment in arguments (see #11)`, (done) => {
const actual = parseFn('function (/* done */) { return 123 }')
test.strictEqual(actual.params, '')
test.strictEqual(actual.body, ' return 123 ')
const res = parseFn('function (foo/* done */, bar) { return 123 }')
test.strictEqual(res.params, 'foo, bar')
test.strictEqual(res.body, ' return 123 ')
done()
})
test(`#${testsCount++} - ${parserName} - should support to parse generator functions`, (done) => {
const actual = parseFn('function * named (abc) { return abc + 123 }')
test.strictEqual(actual.name, 'named')
test.strictEqual(actual.params, 'abc')
test.strictEqual(actual.body, ' return abc + 123 ')
done()
})
test(`#${testsCount++} - ${parserName} - should support to parse async functions (ES2016)`, (done) => {
const actual = parseFn('async function foo (bar) { return bar }')
test.strictEqual(actual.name, 'foo')
test.strictEqual(actual.params, 'bar')
test.strictEqual(actual.body, ' return bar ')
done()
})
test(`#${testsCount++} - ${parserName} - should parse a real function which is passed`, (done) => {
const actual = parseFn(function fooBar (a, bc) {
return a + bc
})
test.strictEqual(actual.name, 'fooBar')
test.strictEqual(actual.params, 'a, bc')
test.strictEqual(actual.body, '\n return a + bc;\n ')
done()
})
test(`#${testsCount++} - ${parserName} - should work for object methods`, (done) => {
const obj = {
foo (a, b, c) {
return 123
},
bar (a) {
return () => a
},
* gen (a) {},
}
const actual = parseFn(obj.foo)
test.strictEqual(actual.name, 'foo')
test.strictEqual(actual.params, 'a, b, c')
test.strictEqual(actual.body, '\n return 123;\n ')
const bar = parseFn(obj.bar)
test.strictEqual(bar.name, 'bar')
test.strictEqual(bar.body, '\n return () => a;\n ')
const gen = parseFn(obj.gen)
test.strictEqual(gen.name, 'gen')
const namedFn = `namedFn (a = {foo: 'ba)r', baz: 123}, cb, ...restArgs) { return a * 3 }`
const named = parseFn(namedFn)
test.strictEqual(named.name, 'namedFn')
test.strictEqual(named.args.length, 3)
test.strictEqual(named.body, ' return a * 3 ')
done()
})
test(`#${testsCount++} - ${parserName} - plugins api`, (done) => {
const fnStr = `() => 123 + a + 44`
const plugin = (app) => (node, result) => {
result.called = true
// you may want to return the `result`,
// but it is the same as not return it
// return result
}
const result = parseFn(fnStr, {}, plugin)
test.strictEqual(result.called, true)
done()
})
test(`#${testsCount++} - ${parserName} - fn named "anonymous" has .name: 'anonymous'`, (done) => {
const result = parseFn('function anonymous () {}')
test.strictEqual(result.name, 'anonymous')
test.strictEqual(result.isAnonymous, false)
done()
})
test(`#${testsCount++} - ${parserName} - real anonymous fn has .name: null`, (done) => {
const actual = parseFn('function () {}')
test.strictEqual(actual.name, null)
test.strictEqual(actual.isAnonymous, true)
done()
})
}
/**
* Actually run all the tests
*/
factory('babylon default', (code, opts, plugin) => {
const app = parseFunction()
if (plugin) app.use(plugin)
return app.parse(code, opts)
})
factory('babylon.parse', (code, opts, plugin) => {
const app = parseFunction({
parse: babylonParse,
ecmaVersion: 2017,
})
if (plugin) app.use(plugin)
return app.parse(code, opts)
})
factory('acorn.parse', (code, opts, plugin) => {
const app = parseFunction({
parse: acornParse,
ecmaVersion: 2017,
})
if (plugin) app.use(plugin)
return app.parse(code, opts)
})
factory('acorn.parse_dammit', (code, opts, plugin) => {
const app = parseFunction()
if (plugin) app.use(plugin)
return app.parse(
code,
Object.assign(
{
parse: acornLooseParse,
ecmaVersion: 2017,
},
opts
)
)
})
factory('espree.parse', (code, opts, plugin) => {
const app = parseFunction({
parse: espreeParse,
ecmaVersion: 8,
})
if (plugin) app.use(plugin)
return app.parse(code, opts)
})
test('should just extend the core API, not the end result', (done) => {
const app = parseFunction()
app.use((inst) => {
app.define(inst, 'hello', (place) => `Hello ${place}!!`)
})
const ret = app.hello('pinky World')
test.strictEqual(ret, 'Hello pinky World!!')
done()
})
test('should call fn returned from plugin only when `parse` is called', (done) => {
const app = parseFunction({
ecmaVersion: 2017,
})
let called = 0
app.use((app) => {
called = 1
return (node, result) => {
called = 2
}
})
test.strictEqual(called, 1)
let res = app.parse('(a, b) => {}')
test.strictEqual(called, 2)
test.strictEqual(res.params, 'a, b')
done()
})
// https://github.com/tunnckoCore/parse-function/issues/61
test('should work with an async arrow function with an `if` statement', (done) => {
const app = parseFunction()
const parsed = app.parse('async (v) => { if (v) {} }')
test.deepEqual(parsed, {
name: null,
body: ' if (v) {} ',
args: ['v'],
params: 'v',
})
done()
})