implement block.alias, so consecutive each-blocks dont get i, i_1, i_2...

pull/491/head
Rich-Harris 8 years ago
parent 9f2bd988a0
commit 67fa013604

@ -40,7 +40,7 @@ export default class Generator {
// allow compiler to deconflict user's `import { get } from 'whatever'` and // allow compiler to deconflict user's `import { get } from 'whatever'` and
// Svelte's builtin `import { get, ... } from 'svelte/shared.js'`; // Svelte's builtin `import { get, ... } from 'svelte/shared.js'`;
this.importedNames = new Set(); this.importedNames = new Set();
this._aliases = new Map(); this.aliases = new Map();
this._usedNames = new Set( [ name ] ); this._usedNames = new Set( [ name ] );
} }
@ -54,12 +54,11 @@ export default class Generator {
} }
alias ( name ) { alias ( name ) {
if ( this._aliases.has( name ) ) { if ( !this.aliases.has( name ) ) {
return this._aliases.get( name ); this.aliases.set( name, this.getUniqueName( name ) );
} }
const alias = this.getUniqueName( name );
this._aliases.set( name, alias ); return this.aliases.get( name );
return alias;
} }
contextualise ( block, expression, context, isEventHandler ) { contextualise ( block, expression, context, isEventHandler ) {

@ -29,6 +29,7 @@ export default class Block {
destroy: new CodeBuilder() destroy: new CodeBuilder()
}; };
this.aliases = new Map();
this.getUniqueName = this.generator.getUniqueNameMaker( options.params ); this.getUniqueName = this.generator.getUniqueNameMaker( options.params );
// unique names // unique names
@ -61,6 +62,14 @@ export default class Block {
} }
} }
alias ( name ) {
if ( !this.aliases.has( name ) ) {
this.aliases.set( name, this.getUniqueName( name ) );
}
return this.aliases.get( name );
}
child ( options ) { child ( options ) {
return new Block( Object.assign( {}, this, options, { parent: this } ) ); return new Block( Object.assign( {}, this, options, { parent: this } ) );
} }

@ -7,7 +7,7 @@ export default function visitEachBlock ( generator, block, state, node ) {
const create_each_block = node._block.name; const create_each_block = node._block.name;
const each_block_value = node._block.listName; const each_block_value = node._block.listName;
const iterations = block.getUniqueName( `${each_block}_iterations` ); const iterations = block.getUniqueName( `${each_block}_iterations` );
const i = block.getUniqueName( `i` ); const i = block.alias( `i` );
const params = block.params.join( ', ' ); const params = block.params.join( ', ' );
const anchor = block.getUniqueName( `${each_block}_anchor` ); const anchor = block.getUniqueName( `${each_block}_anchor` );

Loading…
Cancel
Save