Skip to content

Commit

Permalink
Merge pull request #1724 from nathancahill/fix-sequence-this-context
Browse files Browse the repository at this point in the history
Maintain this context when sequence is callable
  • Loading branch information
lukastaegert committed Nov 17, 2017
2 parents 2e09e21 + a9b842c commit 1d05530
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ast/nodes/SequenceExpression.js
Expand Up @@ -34,6 +34,10 @@ export default class SequenceExpression extends Node {
const last = this.expressions[ this.expressions.length - 1 ];
last.render( code, es );

if ( this.parent.type === 'CallExpression' && last.type === 'MemberExpression' && this.expressions.length > 1 ) {
this.expressions[0].included = true
}

const included = this.expressions.slice( 0, this.expressions.length - 1 ).filter( expression => expression.included );
if ( included.length === 0 ) {
code.remove( this.start, last.start );
Expand Down
5 changes: 5 additions & 0 deletions test/form/samples/sequence-expression/_expected/amd.js
Expand Up @@ -20,4 +20,9 @@ define(function () { 'use strict';
// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

// should maintain this context
var module$1 = {};
module$1.bar = function () { console.log( 'bar' );};
var h = (0, module$1.bar)();

});
5 changes: 5 additions & 0 deletions test/form/samples/sequence-expression/_expected/cjs.js
Expand Up @@ -19,3 +19,8 @@ var e = (foo$1());

// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

// should maintain this context
var module$1 = {};
module$1.bar = function () { console.log( 'bar' );};
var h = (0, module$1.bar)();
5 changes: 5 additions & 0 deletions test/form/samples/sequence-expression/_expected/es.js
Expand Up @@ -17,3 +17,8 @@ var e = (foo$1());

// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

// should maintain this context
var module$1 = {};
module$1.bar = function () { console.log( 'bar' );};
var h = (0, module$1.bar)();
5 changes: 5 additions & 0 deletions test/form/samples/sequence-expression/_expected/iife.js
Expand Up @@ -21,4 +21,9 @@
// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

// should maintain this context
var module$1 = {};
module$1.bar = function () { console.log( 'bar' );};
var h = (0, module$1.bar)();

}());
5 changes: 5 additions & 0 deletions test/form/samples/sequence-expression/_expected/umd.js
Expand Up @@ -24,4 +24,9 @@
// should properly render complex sub-expressions
var g = ((() => {console.log(foo$1());})(), 1);

// should maintain this context
var module$1 = {};
module$1.bar = function () { console.log( 'bar' );};
var h = (0, module$1.bar)();

})));
5 changes: 5 additions & 0 deletions test/form/samples/sequence-expression/main.js
Expand Up @@ -21,3 +21,8 @@ var e = (0, f.foo());

// should properly render complex sub-expressions
var g = ((() => {})(), (() => {console.log(f.foo())})(), 1);

// should maintain this context
var module = {};
module.bar = function () { console.log( 'bar' )};
var h = (0, module.bar)();
3 changes: 3 additions & 0 deletions test/function/samples/sequence-expression/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'do not change context of callable in sequence expressions'
};
5 changes: 5 additions & 0 deletions test/function/samples/sequence-expression/main.js
@@ -0,0 +1,5 @@

// should maintain this context
var module = {};
module.bar = function () { assert.notEqual(this, module); };
var h = (0, module.bar)();

0 comments on commit 1d05530

Please sign in to comment.