From 67fa013604c4fb74f69cc57910b0defe458e0292 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Mon, 17 Apr 2017 17:43:11 -0400 Subject: [PATCH] implement block.alias, so consecutive each-blocks dont get i, i_1, i_2... --- src/generators/Generator.js | 11 +++++------ src/generators/dom/Block.js | 9 +++++++++ src/generators/dom/visitors/EachBlock.js | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/generators/Generator.js b/src/generators/Generator.js index b1b4154b1d..e16d43da04 100644 --- a/src/generators/Generator.js +++ b/src/generators/Generator.js @@ -40,7 +40,7 @@ export default class Generator { // allow compiler to deconflict user's `import { get } from 'whatever'` and // Svelte's builtin `import { get, ... } from 'svelte/shared.js'`; this.importedNames = new Set(); - this._aliases = new Map(); + this.aliases = new Map(); this._usedNames = new Set( [ name ] ); } @@ -54,12 +54,11 @@ export default class Generator { } alias ( name ) { - if ( this._aliases.has( name ) ) { - return this._aliases.get( name ); + if ( !this.aliases.has( name ) ) { + this.aliases.set( name, this.getUniqueName( name ) ); } - const alias = this.getUniqueName( name ); - this._aliases.set( name, alias ); - return alias; + + return this.aliases.get( name ); } contextualise ( block, expression, context, isEventHandler ) { diff --git a/src/generators/dom/Block.js b/src/generators/dom/Block.js index 093fe214e2..e14d8591f4 100644 --- a/src/generators/dom/Block.js +++ b/src/generators/dom/Block.js @@ -29,6 +29,7 @@ export default class Block { destroy: new CodeBuilder() }; + this.aliases = new Map(); this.getUniqueName = this.generator.getUniqueNameMaker( options.params ); // 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 ) { return new Block( Object.assign( {}, this, options, { parent: this } ) ); } diff --git a/src/generators/dom/visitors/EachBlock.js b/src/generators/dom/visitors/EachBlock.js index e5ed5d8678..0454c4e6ba 100644 --- a/src/generators/dom/visitors/EachBlock.js +++ b/src/generators/dom/visitors/EachBlock.js @@ -7,7 +7,7 @@ export default function visitEachBlock ( generator, block, state, node ) { const create_each_block = node._block.name; const each_block_value = node._block.listName; const iterations = block.getUniqueName( `${each_block}_iterations` ); - const i = block.getUniqueName( `i` ); + const i = block.alias( `i` ); const params = block.params.join( ', ' ); const anchor = block.getUniqueName( `${each_block}_anchor` );