Skip to content

use snake case in generated identifiers (#419) #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/generators/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export default class Generator {

getUniqueName ( name ) {
let alias = name;
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ); alias = `${name}$${i++}` );
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ); alias = `${name}_${i++}` );
this._usedNames.add( alias );
return alias;
}
Expand All @@ -251,7 +251,7 @@ export default class Generator {
const localUsedNames = new Set( params );
return name => {
let alias = name;
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ) || localUsedNames.has( alias ); alias = `${name}$${i++}` );
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ) || localUsedNames.has( alias ); alias = `${name}_${i++}` );
localUsedNames.add( alias );
return alias;
};
Expand Down Expand Up @@ -296,7 +296,7 @@ export default class Generator {
} else {
const { declarations } = annotateWithScopes( js );
let template = 'template';
for ( let i = 1; declarations.has( template ); template = `template$${i++}` );
for ( let i = 1; declarations.has( template ); template = `template_${i++}` );

this.code.overwrite( defaultExport.start, defaultExport.declaration.start, `var ${template} = ` );

Expand Down
20 changes: 11 additions & 9 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DomGenerator extends Generator {
} else {
properties.addBlock( deindent`
update: function ( changed, ${fragment.params.join( ', ' )} ) {
var __tmp;
${fragment.tmp ? `var ${fragment.tmp};` : ''}

${fragment.builders.update}
},
Expand Down Expand Up @@ -119,8 +119,9 @@ class DomGenerator extends Generator {
}
}

generateBlock ( node, name ) {
generateBlock ( node, name, type ) {
this.push({
type,
name,
target: 'target',
localElementDepth: 0,
Expand Down Expand Up @@ -203,7 +204,8 @@ export default function dom ( parsed, source, options ) {
const component = getUniqueName( 'component' );

generator.push({
name: generator.alias( 'renderMainFragment' ),
type: 'block',
name: generator.alias( 'render_main_fragment' ),
namespace,
target: 'target',
localElementDepth: 0,
Expand Down Expand Up @@ -277,13 +279,13 @@ export default function dom ( parsed, source, options ) {

if ( generator.css && options.css !== false ) {
builders.main.addBlock( deindent`
var ${generator.alias( 'addedCss' )} = false;
function ${generator.alias( 'addCss' )} () {
var ${generator.alias( 'added_css' )} = false;
function ${generator.alias( 'add_css' )} () {
var style = ${generator.helper( 'createElement' )}( 'style' );
style.textContent = ${JSON.stringify( generator.css )};
${generator.helper( 'appendNode' )}( style, document.head );

${generator.alias( 'addedCss' )} = true;
${generator.alias( 'added_css' )} = true;
}
` );
}
Expand All @@ -294,7 +296,7 @@ export default function dom ( parsed, source, options ) {
builders.init.addLine( `this._torndown = false;` );

if ( parsed.css && options.css !== false ) {
builders.init.addLine( `if ( !${generator.alias( 'addedCss' )} ) ${generator.alias( 'addCss' )}();` );
builders.init.addLine( `if ( !${generator.alias( 'added_css' )} ) ${generator.alias( 'add_css' )}();` );
}

if ( generator.hasComponents ) {
Expand All @@ -304,15 +306,15 @@ export default function dom ( parsed, source, options ) {
if ( generator.hasComplexBindings ) {
builders.init.addBlock( deindent`
this._bindings = [];
this._fragment = ${generator.alias( 'renderMainFragment' )}( this._state, this );
this._fragment = ${generator.alias( 'render_main_fragment' )}( this._state, this );
if ( options.target ) this._fragment.mount( options.target, null );
while ( this._bindings.length ) this._bindings.pop()();
` );

builders._set.addLine( `while ( this._bindings.length ) this._bindings.pop()();` );
} else {
builders.init.addBlock( deindent`
this._fragment = ${generator.alias( 'renderMainFragment' )}( this._state, this );
this._fragment = ${generator.alias( 'render_main_fragment' )}( this._state, this );
if ( options.target ) this._fragment.mount( options.target, null );
` );
}
Expand Down
4 changes: 4 additions & 0 deletions src/generators/dom/utils/findBlock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function findBlock ( fragment ) {
while ( fragment.type !== 'block' ) fragment = fragment.parent;
return fragment;
}
11 changes: 6 additions & 5 deletions src/generators/dom/visitors/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ export default {

// Component has children, put them in a separate {{yield}} block
if ( hasChildren ) {
const yieldName = generator.getUniqueName( `render${name}YieldFragment` );
const yieldName = generator.getUniqueName( `render_${name}_yield_fragment` );
const params = current.params.join( ', ' );

generator.generateBlock( node, yieldName );
generator.generateBlock( node, yieldName, 'block' );

const yieldFragment = current.getUniqueName( `${name}_yieldFragment` );
const yieldFragment = current.getUniqueName( `${name}_yield_fragment` );

current.builders.init.addLine(
`var ${yieldFragment} = ${yieldName}( ${params}, ${current.component} );`
Expand All @@ -88,11 +88,11 @@ export default {
const initialProps = local.staticAttributes
.concat( local.dynamicAttributes )
.map( attribute => `${attribute.name}: ${attribute.value}` );
const initialData = current.getUniqueName( `${name}_initialData` );
const initialData = current.getUniqueName( `${name}_initial_data` );

if ( initialProps.length ) {
statements.push( deindent`
var ${name}_initialData = {
var ${initialData} = {
${initialProps.join( ',\n' )}
};
` );
Expand Down Expand Up @@ -151,6 +151,7 @@ export default {
if ( !local.update.isEmpty() ) current.builders.update.addBlock( local.update );

generator.push({
type: 'component',
namespace: local.namespace,
target: name,
parent: current,
Expand Down
9 changes: 5 additions & 4 deletions src/generators/dom/visitors/EachBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import getBuilders from '../utils/getBuilders.js';

export default {
enter ( generator, node ) {
const name = generator.getUniqueName( `eachBlock` );
const renderer = generator.getUniqueName( `renderEachBlock` );
const name = generator.getUniqueName( `each_block` );
const renderer = generator.getUniqueName( `render_each_block` );
const elseName = generator.getUniqueName( `${name}_else` );
const renderElse = generator.getUniqueName( `${renderer}_else` );
const i = generator.current.getUniqueName( `i` );
Expand Down Expand Up @@ -168,11 +168,11 @@ export default {
}

if ( node.else ) {
generator.generateBlock( node.else, renderElse );
generator.generateBlock( node.else, renderElse, 'block' );
}

const indexNames = new Map( generator.current.indexNames );
const indexName = node.index || generator.current.getUniqueName( `${node.context}__index` );
const indexName = node.index || generator.current.getUniqueName( `${node.context}_index` );
indexNames.set( node.context, indexName );

const listNames = new Map( generator.current.listNames );
Expand All @@ -193,6 +193,7 @@ export default {
const getUniqueName = generator.getUniqueNameMaker( blockParams );

generator.push({
type: 'block',
name: renderer,
target: 'target',
expression: node.expression,
Expand Down
1 change: 1 addition & 0 deletions src/generators/dom/visitors/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default {
generator.createMountStatement( name );

generator.push({
type: 'element',
namespace: local.namespace,
target: name,
parent: generator.current,
Expand Down
14 changes: 7 additions & 7 deletions src/generators/dom/visitors/IfBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function getConditionsAndBlocks ( generator, node, _name, i = 0 ) {
block: name
}];

generator.generateBlock( node, name );
generator.generateBlock( node, name, 'block' );

if ( node.else && node.else.children.length === 1 &&
node.else.children[0].type === 'IfBlock' ) {
Expand All @@ -24,7 +24,7 @@ function getConditionsAndBlocks ( generator, node, _name, i = 0 ) {
});

if ( node.else ) {
generator.generateBlock( node.else, name );
generator.generateBlock( node.else, name, 'block' );
}
}
return conditionsAndBlocks;
Expand All @@ -33,13 +33,13 @@ function getConditionsAndBlocks ( generator, node, _name, i = 0 ) {
export default {
enter ( generator, node ) {
const params = generator.current.params.join( ', ' );
const name = generator.getUniqueName( `ifBlock` );
const getBlock = generator.current.getUniqueName( `getBlock` );
const currentBlock = generator.current.getUniqueName( `currentBlock` );
const _currentBlock = generator.current.getUniqueName( `_currentBlock` );
const name = generator.getUniqueName( `if_block` );
const getBlock = generator.current.getUniqueName( `get_block` );
const currentBlock = generator.current.getUniqueName( `current_block` );
const _currentBlock = generator.current.getUniqueName( `_current_block` );

const isToplevel = generator.current.localElementDepth === 0;
const conditionsAndBlocks = getConditionsAndBlocks( generator, node, generator.getUniqueName( `renderIfBlock` ) );
const conditionsAndBlocks = getConditionsAndBlocks( generator, node, generator.getUniqueName( `render_if_block` ) );

const anchor = `${name}_anchor`;
generator.createAnchor( anchor );
Expand Down
8 changes: 6 additions & 2 deletions src/generators/dom/visitors/MustacheTag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deindent from '../../../utils/deindent.js';
import findBlock from '../utils/findBlock.js';

export default {
enter ( generator, node ) {
Expand All @@ -9,9 +10,12 @@ export default {
generator.current.builders.init.addLine( `var last_${name} = ${snippet};` );
generator.addElement( name, `${generator.helper( 'createText' )}( last_${name} )`, true );

const fragment = findBlock( generator.current );
if ( !fragment.tmp ) fragment.tmp = fragment.getUniqueName( 'tmp' );

generator.current.builders.update.addBlock( deindent`
if ( ( __tmp = ${snippet} ) !== last_${name} ) {
${name}.data = last_${name} = __tmp;
if ( ( ${fragment.tmp} = ${snippet} ) !== last_${name} ) {
${name}.data = last_${name} = ${fragment.tmp};
}
` );
}
Expand Down
8 changes: 6 additions & 2 deletions src/generators/dom/visitors/RawMustacheTag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deindent from '../../../utils/deindent.js';
import findBlock from '../utils/findBlock.js';

export default {
enter ( generator, node ) {
Expand Down Expand Up @@ -26,9 +27,12 @@ export default {
generator.current.builders.init.addLine( mountStatement );
}

const fragment = findBlock( generator.current );
if ( !fragment.tmp ) fragment.tmp = fragment.getUniqueName( 'tmp' );

generator.current.builders.update.addBlock( deindent`
if ( ( __tmp = ${snippet} ) !== last_${name} ) {
last_${name} = __tmp;
if ( ( ${fragment.tmp} = ${snippet} ) !== last_${name} ) {
last_${name} = ${fragment.tmp};
${detachStatement}
${mountStatement}
}
Expand Down
10 changes: 7 additions & 3 deletions src/generators/dom/visitors/attributes/addElementAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import addElementBinding from './addElementBinding';
import deindent from '../../../../utils/deindent.js';
import flattenReference from '../../../../utils/flattenReference.js';
import getStaticAttributeValue from './binding/getStaticAttributeValue.js';
import findBlock from '../../utils/findBlock.js';

export default function addElementAttributes ( generator, node, local ) {
node.attributes.forEach( attribute => {
Expand Down Expand Up @@ -105,9 +106,12 @@ export default function addElementAttributes ( generator, node, local ) {
}

local.init.addLine( updater );
const fragment = findBlock( generator.current );
if ( !fragment.tmp ) fragment.tmp = fragment.getUniqueName( 'tmp' );

local.update.addBlock( deindent`
if ( ( __tmp = ${snippet} ) !== ${last} ) {
${last} = __tmp;
if ( ( ${fragment.tmp} = ${snippet} ) !== ${last} ) {
${last} = ${fragment.tmp};
${updater}
}
` );
Expand Down Expand Up @@ -177,7 +181,7 @@ export default function addElementAttributes ( generator, node, local ) {
return `var ${listName} = this.__svelte.${listName}, ${indexName} = this.__svelte.${indexName}, ${name} = ${listName}[${indexName}]`;
});

const handlerName = generator.current.getUniqueName( `${name}Handler` );
const handlerName = generator.current.getUniqueName( `${name}_handler` );
const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`;

if ( generator.events.has( name ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function createBinding ( generator, node, attribute, current, loc
if ( !~local.allUsedContexts.indexOf( context ) ) local.allUsedContexts.push( context );
});

const handler = current.getUniqueName( `${local.name}ChangeHandler` );
const handler = current.getUniqueName( `${local.name}_change_handler` );

const isMultipleSelect = node.name === 'select' && node.attributes.find( attr => attr.name.toLowerCase() === 'multiple' ); // TODO use getStaticAttributeValue
const type = getStaticAttributeValue( node, 'type' );
Expand Down
2 changes: 1 addition & 1 deletion test/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe( 'generate', () => {
// check that no ES2015+ syntax slipped in
if ( !config.allowES2015 ) {
try {
const startIndex = code.indexOf( 'function renderMainFragment' ); // may change!
const startIndex = code.indexOf( 'function render_main_fragment' ); // may change!
const es5 = spaces( startIndex ) + code.slice( startIndex ).replace( /export default .+/, '' );
acorn.parse( es5, { ecmaVersion: 5 });
} catch ( err ) {
Expand Down