Commit 8eda7aec by kzc Committed by Marijn Haverbeke

use alias for this in right side of nested for-of and for-in loop declarations (#142)

parent 07101c36
Pipeline #5385166 passed with stage
in 2 minutes 47 seconds
......@@ -8,11 +8,9 @@ export default class ThisExpression extends Node {
const arrowFunction = this.findNearest( 'ArrowFunctionExpression' );
const loop = this.findNearest( loopStatement );
if ( arrowFunction && arrowFunction.depth > lexicalBoundary.depth ) {
this.alias = lexicalBoundary.getThisAlias();
}
if ( loop && loop.body.contains( this ) && loop.depth > lexicalBoundary.depth ) {
if ( ( arrowFunction && arrowFunction.depth > lexicalBoundary.depth )
|| ( loop && loop.body.contains( this ) && loop.depth > lexicalBoundary.depth )
|| ( loop && loop.right && loop.right.contains( this ) ) ) {
this.alias = lexicalBoundary.getThisAlias();
}
}
......
......@@ -94,7 +94,9 @@ module.exports = [
}`,
output: `
for (var i = 0, list = this.keys; i < list.length; i += 1) {
var this$1 = this;
for (var i = 0, list = this$1.keys; i < list.length; i += 1) {
var key = list[i];
console.log(key);
......
......@@ -659,5 +659,98 @@ module.exports = [
f(k, r++, s++, t++, x++, y++, z++)
}
`,
}
},
{
description: 'use alias for this in right side of nested for-in loop declaration (#142)',
input: `
let arr = [];
class Foo {
constructor () {
this.foo = { a: 1, b: 2 };
}
do() {
for ( let move = 0; move < 5; ++move ) {
for ( let id in this.foo )
arr.push( id );
( () => { arr.push( move ); } )( move );
}
console.log( arr.join( ' ' ) );
}
}
new Foo().do();
`,
output: `
var arr = [];
var Foo = function Foo () {
this.foo = { a: 1, b: 2 };
};
Foo.prototype.do = function do$1 () {
var this$1 = this;
var loop = function ( move ) {
for ( var id in this$1.foo )
{ arr.push( id ); }
( function () { arr.push( move ); } )( move );
};
for ( var move = 0; move < 5; ++move ) loop( move );
console.log( arr.join( ' ' ) );
};
new Foo().do();
`,
},
{
description: 'use alias for this in right side of nested for-of loop declaration (#142)',
options: { transforms: { dangerousForOf: true } },
input: `
let arr = [];
class Foo {
constructor () {
this.foo = [ 9, 7 ];
}
do() {
for ( let move = 0; move < 5; ++move ) {
for ( let id of this.foo )
arr.push( id );
( () => { arr.push( move ); } )( move );
}
console.log( arr.join( ' ' ) );
}
}
new Foo().do();
`,
output: `
var arr = [];
var Foo = function Foo () {
this.foo = [ 9, 7 ];
};
Foo.prototype.do = function do$1 () {
var this$1 = this;
var loop = function ( move ) {
for ( var i = 0, list = this$1.foo; i < list.length; i += 1 )
{
var id = list[i];
arr.push( id );
}
( function () { arr.push( move ); } )( move );
};
for ( var move = 0; move < 5; ++move ) loop( move );
console.log( arr.join( ' ' ) );
};
new Foo().do();
`,
},
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment