Commit 07101c36 by kzc Committed by Marijn Haverbeke

fix for-loop init variables with UpdateExpresssions between iterations (#150)

parent 941f4d28
Pipeline #5385122 passed with stage
in 3 minutes 43 seconds
......@@ -8,6 +8,12 @@ export default class UpdateExpression extends Node {
if ( declaration && declaration.kind === 'const' ) {
throw new CompileError( this, `${this.argument.name} is read-only` );
}
// special case – https://gitlab.com/Rich-Harris/buble/issues/150
const statement = declaration && declaration.node.ancestor( 3 );
if ( statement && statement.type === 'ForStatement' && statement.body.contains( this ) ) {
statement.reassigned[ this.argument.name ] = true;
}
}
super.initialise( transforms );
......
......@@ -207,6 +207,59 @@ module.exports = [
},
{
description: 'loop variables with UpdateExpresssions between iterations (#150)',
input: `
var fns = [];
for ( let i = 0, j = 3; i < 10; i += 1 ) {
fns.push(function () { return i; });
++i;
j--;
}`,
output: `
var fns = [];
var loop = function ( i$1, j$1 ) {
fns.push(function () { return i$1; });
++i$1;
j$1--;
i = i$1;
j = j$1;
};
for ( var i = 0, j = 3; i < 10; i += 1 ) loop( i, j );`
},
{
description: 'loop variables with UpdateExpresssions between iterations, with conflict (#150)',
input: `
var i = 'conflicting';
var fns = [];
for ( let i = 0; i < 10; i += 1 ) {
fns.push(function () { return i; });
i++;
}`,
output: `
var i = 'conflicting';
var fns = [];
var loop = function ( i$2 ) {
fns.push(function () { return i$2; });
i$2++;
i$1 = i$2;
};
for ( var i$1 = 0; i$1 < 10; i$1 += 1 ) loop( i$1 );`
},
{
description: 'handles break and continue inside block-scoped loops (#12)',
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