Commit cbc17ad5 by Adrian Heine Committed by Marijn Haverbeke

Fix multiple compiled, returning loops

parent 16aa650c
Pipeline #5380008 passed with stage
in 2 minutes 52 seconds
......@@ -68,7 +68,7 @@ export default class LoopStatement extends Node {
let insert = `{\n${i1}var ${returned} = ${loop}(${argString});\n`;
if ( this.canBreak ) insert += `\n${i1}if ( ${returned} === 'break' ) break;`;
if ( this.canReturn ) insert += `\n${i1}if ( ${returned} ) return returned.v;`;
if ( this.canReturn ) insert += `\n${i1}if ( ${returned} ) return ${returned}.v;`;
insert += `\n${i0}}`;
code.insertRight( this.body.end, insert );
......
......@@ -389,6 +389,60 @@ module.exports = [
},
{
description: 'supports two compiled loops in one function',
input: `
function foo ( x ) {
for ( let i = 0; i < x; i += 1 ) {
setTimeout( () => {
console.log( i );
});
if ( x > 5 ) return;
}
for ( let i = 0; i < x; i += 1 ) {
setTimeout( () => {
console.log( i );
});
if ( x > 5 ) return;
}
}`,
output: `
function foo ( x ) {
var loop = function ( i ) {
setTimeout( function () {
console.log( i );
});
if ( x > 5 ) { return {}; }
};
for ( var i = 0; i < x; i += 1 ) {
var returned = loop( i );
if ( returned ) return returned.v;
}
var loop$1 = function ( i ) {
setTimeout( function () {
console.log( i );
});
if ( x > 5 ) { return {}; }
};
for ( var i$1 = 0; i$1 < x; i$1 += 1 ) {
var returned$1 = loop$1( i$1 );
if ( returned$1 ) return returned$1.v;
}
}`
},
{
description: 'destructures variable declaration in for loop head',
input: `
......
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 to comment