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

Commit 4d6870a

Browse files
Eric MORANDtunnckoCore
authored andcommitted
feat(result): values of default params to be always strings (#121)
Changes in [Result object](https://github.com/tunnckoCore/parse-function#result)! **Possible breaking change, if you depend on values of `result.defaults`!** Now `result.defaults` is key/value pairs as before, but the value is always of type `string` or `undefined`! Casting of values is delegated to the end user which before was not consistent and was actual value of the default parameter. Example **(before)**: ```js const result = app.parse('(a = 123) => {}') console.log(result.defaults) // => { a: 123 } ``` Example **(after)**: ```js const result = app.parse('(a = 123) => {}') // notice that `123` now is string! console.log(result.defaults) // => { a: '123' } ``` fixes #120, related #110
1 parent 05018b6 commit 4d6870a

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/lib/plugins/params.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@ export default (app) => (node, result) => {
3434
result.args.push(name)
3535

3636
if (param.right && param.right.type === 'SequenceExpression') {
37-
let value
3837
let lastExpression = param.right.expressions.pop()
3938

40-
if (lastExpression.type === 'NullLiteral') {
41-
value = null
42-
} else {
43-
value = lastExpression.value
44-
}
45-
46-
result.defaults[name] = value
39+
result.defaults[name] = result.value.slice(
40+
lastExpression.start,
41+
lastExpression.end
42+
)
4743
} else {
4844
result.defaults[name] = param.right
4945
? result.value.slice(param.right.start, param.right.end)

test/index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ const actuals = {
3131
'function () {}',
3232
'function (a = (true, false)) {}',
3333
'function (a = (true, null)) {}',
34+
'function (a = (true, "bar")) {}',
3435
'function (a, b = (i++, true)) {}',
36+
'function (a = 1) {}',
3537
],
3638
named: [
3739
'function namedFn (a = {foo: "ba)r", baz: 123}, cb, ...restArgs) {return a * 3}',
@@ -41,7 +43,9 @@ const actuals = {
4143
'function namedFn () {}',
4244
'function namedFn(a = (true, false)) {}',
4345
'function namedFn(a = (true, null)) {}',
46+
'function namedFn(a = (true, "bar")) {}',
4447
'function namedFn(a, b = (i++, true)) {}',
48+
'function namedFn(a = 1) {}',
4549
],
4650
generators: [
4751
'function * namedFn (a = {foo: "ba)r", baz: 123}, cb, ...restArgs) {return a * 3}',
@@ -51,7 +55,9 @@ const actuals = {
5155
'function * namedFn () {}',
5256
'function * namedFn(a = (true, false)) {}',
5357
'function * namedFn(a = (true, null)) {}',
58+
'function * namedFn(a = (true, "bar")) {}',
5459
'function * namedFn(a, b = (i++, true)) {}',
60+
'function * namedFn(a = 1) {}',
5561
],
5662
arrows: [
5763
'(a = {foo: "ba)r", baz: 123}, cb, ...restArgs) => {return a * 3}',
@@ -61,7 +67,9 @@ const actuals = {
6167
'() => {}',
6268
'(a = (true, false)) => {}',
6369
'(a = (true, null)) => {}',
70+
'(a = (true, "bar")) => {}',
6471
'(a, b = (i++, true)) => {}',
72+
'(a = 1) => {}',
6573
'(a) => a * 3 * a',
6674
'd => d * 355 * d',
6775
'e => {return e + 5235 / e}',
@@ -126,21 +134,35 @@ const regulars = [
126134
params: 'a',
127135
args: ['a'],
128136
body: '',
129-
defaults: {a: false},
137+
defaults: { a: 'false' },
130138
},
131139
{
132140
name: null,
133141
params: 'a',
134142
args: ['a'],
135143
body: '',
136-
defaults: {a: null},
144+
defaults: { a: 'null' },
145+
},
146+
{
147+
name: null,
148+
params: 'a',
149+
args: ['a'],
150+
body: '',
151+
defaults: { a: '"bar"' },
137152
},
138153
{
139154
name: null,
140155
params: 'a, b',
141156
args: ['a', 'b'],
142157
body: '',
143-
defaults: {a: undefined, b: true},
158+
defaults: { a: undefined, b: 'true' },
159+
},
160+
{
161+
name: null,
162+
params: 'a',
163+
args: ['a'],
164+
body: '',
165+
defaults: { a: '1' },
144166
},
145167
]
146168

0 commit comments

Comments
 (0)