Skip to content

Commit

Permalink
allow each blocks to introduce reserved words as contexts (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 28, 2017
1 parent a861043 commit 98d9cdb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/generators/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export default class Generator {
// noop
}

else if ( contexts[ name ] ) {
else if ( name in contexts ) {
const context = contexts[ name ];
if ( context !== name ) {
// this is true for 'reserved' names like `root` and `component`
code.overwrite( node.start, node.start + name.length, context, true );
}

dependencies.push( ...contextDependencies[ name ] );
if ( !~usedContexts.indexOf( name ) ) usedContexts.push( name );
}
Expand Down
2 changes: 1 addition & 1 deletion src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export default function dom ( parsed, source, options, names ) {
contexts: {},
indexes: {},

params: 'root',
params: [ 'root' ],
indexNames: {},
listNames: {},

Expand Down
17 changes: 15 additions & 2 deletions src/generators/dom/visitors/EachBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import CodeBuilder from '../../../utils/CodeBuilder.js';
import deindent from '../../../utils/deindent.js';
import getBuilders from '../utils/getBuilders.js';

const reserved = {
component: true,
root: true
};

export default {
enter ( generator, node ) {
const name = generator.getUniqueName( `eachBlock` );
Expand Down Expand Up @@ -172,16 +177,24 @@ export default {
const listNames = Object.assign( {}, generator.current.listNames );
listNames[ node.context ] = listName;

// ensure that contexts like `root` or `component` don't blow up the whole show
let context = node.context;
let c = 1;

while ( context in reserved || ~generator.current.params.indexOf( context ) ) {
context = `${node.context}$${c++}`;
}

const contexts = Object.assign( {}, generator.current.contexts );
contexts[ node.context ] = true;
contexts[ node.context ] = context;

const indexes = Object.assign( {}, generator.current.indexes );
if ( node.index ) indexes[ indexName ] = node.context;

const contextDependencies = Object.assign( {}, generator.current.contextDependencies );
contextDependencies[ node.context ] = dependencies;

const blockParams = generator.current.params + `, ${listName}, ${node.context}, ${indexName}`;
const blockParams = generator.current.params.concat( listName, context, indexName );

generator.push({
name: renderer,
Expand Down
2 changes: 1 addition & 1 deletion src/generators/server-side-rendering/visitors/EachBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
// TODO should this be the generator's job? It's duplicated between
// here and the equivalent DOM compiler visitor
const contexts = Object.assign( {}, generator.current.contexts );
contexts[ node.context ] = true;
contexts[ node.context ] = node.context;

const indexes = Object.assign( {}, generator.current.indexes );
if ( node.index ) indexes[ node.index ] = node.context;
Expand Down

0 comments on commit 98d9cdb

Please sign in to comment.