Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit d9e6102

Browse files
Eric MORANDtunnckoCore
authored andcommitted
feat(params): add support for list of expressions as default value (#111)
(a = (doSomething(), doSomethingElse(), true)) => {} is a prefectly valid syntax that is used extensively by code instrumenters. When a list of expressions is used as a default value, only the last expression is the actual default value. This commit add support for this syntax. TAG: latest fixes #110
1 parent f96b70d commit d9e6102

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/lib/plugins/params.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,23 @@ export default (app) => (node, result) => {
3232
const name = param.name || defaultArgsName || restArgName
3333

3434
result.args.push(name)
35-
result.defaults[name] = param.right
36-
? result.value.slice(param.right.start, param.right.end)
37-
: undefined
35+
36+
if (param.right && param.right.type === 'SequenceExpression') {
37+
let value
38+
let lastExpression = param.right.expressions.pop()
39+
40+
if (lastExpression.type === 'NullLiteral') {
41+
value = null
42+
} else {
43+
value = lastExpression.value
44+
}
45+
46+
result.defaults[name] = value
47+
} else {
48+
result.defaults[name] = param.right
49+
? result.value.slice(param.right.start, param.right.end)
50+
: undefined
51+
}
3852
})
3953
result.params = result.args.join(', ')
4054

test/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,39 @@ const actuals = {
2929
'function (c) {return c * 3}',
3030
'function (...restArgs) {return 321}',
3131
'function () {}',
32+
'function (a = (true, false)) {}',
33+
'function (a = (true, null)) {}',
34+
'function (a, b = (i++, true)) {}',
3235
],
3336
named: [
3437
'function namedFn (a = {foo: "ba)r", baz: 123}, cb, ...restArgs) {return a * 3}',
3538
'function namedFn (b, callback, ...restArgs) {callback(null, b + 3)}',
3639
'function namedFn (c) {return c * 3}',
3740
'function namedFn (...restArgs) {return 321}',
3841
'function namedFn () {}',
42+
'function namedFn(a = (true, false)) {}',
43+
'function namedFn(a = (true, null)) {}',
44+
'function namedFn(a, b = (i++, true)) {}',
3945
],
4046
generators: [
4147
'function * namedFn (a = {foo: "ba)r", baz: 123}, cb, ...restArgs) {return a * 3}',
4248
'function * namedFn (b, callback, ...restArgs) {callback(null, b + 3)}',
4349
'function * namedFn (c) {return c * 3}',
4450
'function * namedFn (...restArgs) {return 321}',
4551
'function * namedFn () {}',
52+
'function * namedFn(a = (true, false)) {}',
53+
'function * namedFn(a = (true, null)) {}',
54+
'function * namedFn(a, b = (i++, true)) {}',
4655
],
4756
arrows: [
4857
'(a = {foo: "ba)r", baz: 123}, cb, ...restArgs) => {return a * 3}',
4958
'(b, callback, ...restArgs) => {callback(null, b + 3)}',
5059
'(c) => {return c * 3}',
5160
'(...restArgs) => {return 321}',
5261
'() => {}',
62+
'(a = (true, false)) => {}',
63+
'(a = (true, null)) => {}',
64+
'(a, b = (i++, true)) => {}',
5365
'(a) => a * 3 * a',
5466
'd => d * 355 * d',
5567
'e => {return e + 5235 / e}',
@@ -109,6 +121,27 @@ const regulars = [
109121
body: '',
110122
defaults: {},
111123
},
124+
{
125+
name: null,
126+
params: 'a',
127+
args: ['a'],
128+
body: '',
129+
defaults: {a: false},
130+
},
131+
{
132+
name: null,
133+
params: 'a',
134+
args: ['a'],
135+
body: '',
136+
defaults: {a: null},
137+
},
138+
{
139+
name: null,
140+
params: 'a, b',
141+
args: ['a', 'b'],
142+
body: '',
143+
defaults: {a: undefined, b: true},
144+
},
112145
]
113146

114147
/**

0 commit comments

Comments
 (0)