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

Commit 61ea4af

Browse files
authored
fix regression with de-structuring and default parameters (#149)
1 parent 3d8d00d commit 61ea4af

6 files changed

Lines changed: 50 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
**Note**: Gaps between patch versions are faulty/broken releases.
1313
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.
1414

15+
# v0.3.23
16+
17+
- **Bug Fix**
18+
- fix regression with de-structuring and default parameters, fix #148 (@gcanti)
19+
1520
# v0.3.22
1621

1722
- **Bug Fix**

lib/index.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ exports.default = function (_ref) {
2424

2525
var extendTemplate = expression('\n function extendId(types, name) {\n const isAny = (type) => {\n if (type === tcombId.Any) {\n return true;\n }\n if (tcombId.isType(type) && type.meta.kind === \'maybe\') {\n return isAny(type.meta.type)\n }\n return false;\n }\n return tcombId.interface.extend(types.filter(type => !isAny(type)), name)\n }\n ');
2626

27+
var argumentsTemplate = expression('arguments[index] !== undefined ? arguments[index] : defaults');
28+
2729
//
2830
// combinators
2931
//
@@ -367,12 +369,28 @@ exports.default = function (_ref) {
367369
return node;
368370
}
369371

370-
function getParam(isArrow, param, i) {
372+
function getParamId(isArrow, param, i, defaults) {
373+
if (t.isObjectPattern(param)) {
374+
if (isArrow) {
375+
return stripDefaults(param);
376+
}
377+
if (typeof defaults !== 'undefined') {
378+
return argumentsTemplate({ index: t.identifier(i), defaults: defaults });
379+
}
380+
return t.memberExpression(t.identifier('arguments'), t.identifier(i), true);
381+
}
382+
if (t.isRestElement(param)) {
383+
return param.argument;
384+
}
385+
return param;
386+
}
387+
388+
function getParam(isArrow, param, i, defaults) {
371389
if (t.isAssignmentPattern(param) && param.left.typeAnnotation) {
372-
return getParam(isArrow, param.left, i);
390+
return getParam(isArrow, param.left, i, param.right);
373391
}
374392
if (param.typeAnnotation) {
375-
var id = t.isObjectPattern(param) ? isArrow ? stripDefaults(param) : t.memberExpression(t.identifier('arguments'), t.identifier(i), true) : t.isRestElement(param) ? param.argument : param;
393+
var id = getParamId(isArrow, param, i, defaults);
376394

377395
return {
378396
id: id,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-tcomb",
3-
"version": "0.3.22",
3+
"version": "0.3.23",
44
"description": "Babel plugin for static and runtime type checking using Flow and tcomb",
55
"main": "lib/index.js",
66
"files": [

src/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ export default function ({ types: t, template }) {
9494
}
9595
`)
9696

97+
const argumentsTemplate = expression(`arguments[index] !== undefined ? arguments[index] : defaults`)
98+
9799
//
98100
// combinators
99101
//
@@ -462,17 +464,28 @@ export default function ({ types: t, template }) {
462464
return node
463465
}
464466

465-
function getParam(isArrow, param, i) {
467+
function getParamId(isArrow, param, i, defaults) {
468+
if (t.isObjectPattern(param)) {
469+
if (isArrow) {
470+
return stripDefaults(param)
471+
}
472+
if (typeof defaults !== 'undefined') {
473+
return argumentsTemplate({ index: t.identifier(i), defaults })
474+
}
475+
return t.memberExpression(t.identifier('arguments'), t.identifier(i), true)
476+
}
477+
if (t.isRestElement(param)) {
478+
return param.argument
479+
}
480+
return param
481+
}
482+
483+
function getParam(isArrow, param, i, defaults) {
466484
if (t.isAssignmentPattern(param) && param.left.typeAnnotation) {
467-
return getParam(isArrow, param.left, i)
485+
return getParam(isArrow, param.left, i, param.right)
468486
}
469487
if (param.typeAnnotation) {
470-
const id = t.isObjectPattern(param) ?
471-
isArrow ?
472-
stripDefaults(param) :
473-
t.memberExpression(t.identifier('arguments'), t.identifier(i), true) :
474-
t.isRestElement(param) ?
475-
param.argument : param
488+
const id = getParamId(isArrow, param, i, defaults)
476489

477490
return {
478491
id,

test/fixtures/destructuring/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function bar({ a } = {}) {
2121
}
2222

2323
function baz({ x: { y = "ex" } } = {}) {
24-
_assert(arguments[0], _t.interface({
24+
_assert(arguments[0] !== undefined ? arguments[0] : {}, _t.interface({
2525
x: _t.interface({
2626
y: _t.maybe(_t.String)
2727
})

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ describe('emit asserts for: ', () => {
212212
if ((caseName in skipTests)) {
213213
return
214214
}
215-
if (!(caseName in { 'destructuring-with-arrows': 1 })) {
215+
if (!(caseName in { 'destructuring': 1 })) {
216216
// return
217217
}
218218
it(`should ${caseName.split('-').join(' ')}`, () => {

0 commit comments

Comments
 (0)