Skip to content

Commit

Permalink
Handle member expressions in array patterns (#2760)
Browse files Browse the repository at this point in the history
* Debug array patterns

* Handle member expressions in patterns (resolves #2750)
  • Loading branch information
lukastaegert committed Mar 20, 2019
1 parent 26d2b31 commit 8166eb0
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ast/nodes/ExportDefaultDeclaration.ts
Expand Up @@ -44,6 +44,7 @@ export default class ExportDefaultDeclaration extends NodeBase {
scope: ModuleScope;
type: NodeType.tExportDefaultDeclaration;
variable: ExportDefaultVariable;

private declarationName: string;

include(includeAllChildrenRecursively: boolean) {
Expand Down
5 changes: 4 additions & 1 deletion src/ast/nodes/MemberExpression.ts
Expand Up @@ -24,6 +24,7 @@ import Identifier from './Identifier';
import Literal from './Literal';
import * as NodeType from './NodeType';
import { ExpressionNode, Node, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';

function getResolvablePropertyKey(memberExpression: MemberExpression): string | null {
return memberExpression.computed
Expand Down Expand Up @@ -72,7 +73,7 @@ export function isMemberExpression(node: Node): node is MemberExpression {
return node.type === NodeType.MemberExpression;
}

export default class MemberExpression extends NodeBase implements DeoptimizableEntity {
export default class MemberExpression extends NodeBase implements DeoptimizableEntity, PatternNode {
computed: boolean;
object: ExpressionNode;
property: ExpressionNode;
Expand All @@ -84,6 +85,8 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
private expressionsToBeDeoptimized: DeoptimizableEntity[];
private replacement: string | null;

addExportedVariables(): void {}

bind() {
if (this.bound) return;
this.bound = true;
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/pattern-member-expressions/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles member expressions in patterns (#2750)'
};
23 changes: 23 additions & 0 deletions test/form/samples/pattern-member-expressions/_expected/amd.js
@@ -0,0 +1,23 @@
define(function () { 'use strict';

const array = [false];
const obj1 = {value: false};
const obj2 = {value: false};

([array[0]] = [true]);
({a: obj1.value} = {a: true});
({[globalVar1]: obj2[globalVar2]} = {[globalVar3]: true});

if (array[0]) {
console.log('retained');
}

if (obj1.value) {
console.log('retained');
}

if (obj2.value) {
console.log('retained');
}

});
21 changes: 21 additions & 0 deletions test/form/samples/pattern-member-expressions/_expected/cjs.js
@@ -0,0 +1,21 @@
'use strict';

const array = [false];
const obj1 = {value: false};
const obj2 = {value: false};

([array[0]] = [true]);
({a: obj1.value} = {a: true});
({[globalVar1]: obj2[globalVar2]} = {[globalVar3]: true});

if (array[0]) {
console.log('retained');
}

if (obj1.value) {
console.log('retained');
}

if (obj2.value) {
console.log('retained');
}
19 changes: 19 additions & 0 deletions test/form/samples/pattern-member-expressions/_expected/es.js
@@ -0,0 +1,19 @@
const array = [false];
const obj1 = {value: false};
const obj2 = {value: false};

([array[0]] = [true]);
({a: obj1.value} = {a: true});
({[globalVar1]: obj2[globalVar2]} = {[globalVar3]: true});

if (array[0]) {
console.log('retained');
}

if (obj1.value) {
console.log('retained');
}

if (obj2.value) {
console.log('retained');
}
24 changes: 24 additions & 0 deletions test/form/samples/pattern-member-expressions/_expected/iife.js
@@ -0,0 +1,24 @@
(function () {
'use strict';

const array = [false];
const obj1 = {value: false};
const obj2 = {value: false};

([array[0]] = [true]);
({a: obj1.value} = {a: true});
({[globalVar1]: obj2[globalVar2]} = {[globalVar3]: true});

if (array[0]) {
console.log('retained');
}

if (obj1.value) {
console.log('retained');
}

if (obj2.value) {
console.log('retained');
}

}());
28 changes: 28 additions & 0 deletions test/form/samples/pattern-member-expressions/_expected/system.js
@@ -0,0 +1,28 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

const array = [false];
const obj1 = {value: false};
const obj2 = {value: false};

([array[0]] = [true]);
({a: obj1.value} = {a: true});
({[globalVar1]: obj2[globalVar2]} = {[globalVar3]: true});

if (array[0]) {
console.log('retained');
}

if (obj1.value) {
console.log('retained');
}

if (obj2.value) {
console.log('retained');
}

}
};
});
26 changes: 26 additions & 0 deletions test/form/samples/pattern-member-expressions/_expected/umd.js
@@ -0,0 +1,26 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(function () { 'use strict';

const array = [false];
const obj1 = {value: false};
const obj2 = {value: false};

([array[0]] = [true]);
({a: obj1.value} = {a: true});
({[globalVar1]: obj2[globalVar2]} = {[globalVar3]: true});

if (array[0]) {
console.log('retained');
}

if (obj1.value) {
console.log('retained');
}

if (obj2.value) {
console.log('retained');
}

}));
19 changes: 19 additions & 0 deletions test/form/samples/pattern-member-expressions/main.js
@@ -0,0 +1,19 @@
const array = [false];
const obj1 = {value: false};
const obj2 = {value: false};

([array[0]] = [true]);
({a: obj1.value} = {a: true});
({[globalVar1]: obj2[globalVar2]} = {[globalVar3]: true});

if (array[0]) {
console.log('retained');
}

if (obj1.value) {
console.log('retained');
}

if (obj2.value) {
console.log('retained');
}

0 comments on commit 8166eb0

Please sign in to comment.