Skip to content
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

Deconflict non helper functions #389

Merged
merged 4 commits into from
Mar 18, 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
26 changes: 15 additions & 11 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class DomGenerator extends Generator {

this.uses[ name ] = true;

return this.alias( name );
}

alias ( name ) {
if ( !( name in this.aliases ) ) {
let alias = name;
let i = 1;
Expand Down Expand Up @@ -188,7 +192,7 @@ export default function dom ( parsed, source, options, names ) {
}

generator.push({
name: 'renderMainFragment',
name: generator.alias( 'renderMainFragment' ),
namespace,
target: 'target',
localElementDepth: 0,
Expand Down Expand Up @@ -238,12 +242,12 @@ export default function dom ( parsed, source, options, names ) {
});

builders.main.addBlock( deindent`
function applyComputations ( state, newState, oldState, isInitial ) {
function ${generator.alias( 'applyComputations' )} ( state, newState, oldState, isInitial ) {
${builder}
}
` );

builders._set.addLine( `applyComputations( this._state, newState, oldState, false )` );
builders._set.addLine( `${generator.alias( 'applyComputations' )}( this._state, newState, oldState, false )` );
}

// TODO is the `if` necessary?
Expand All @@ -259,13 +263,13 @@ export default function dom ( parsed, source, options, names ) {

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

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

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

if ( generator.hasComponents ) {
Expand All @@ -286,15 +290,15 @@ export default function dom ( parsed, source, options, names ) {
if ( generator.hasComplexBindings ) {
builders.init.addBlock( deindent`
this._bindings = [];
this._fragment = renderMainFragment( this._state, this );
this._fragment = ${generator.alias( 'renderMainFragment' )}( 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 = renderMainFragment( this._state, this );
this._fragment = ${generator.alias( 'renderMainFragment' )}( this._state, this );
if ( options.target ) this._fragment.mount( options.target, null );
` );
}
Expand Down Expand Up @@ -327,7 +331,7 @@ export default function dom ( parsed, source, options, names ) {

if ( templateProperties.computed ) {
constructorBlock.addLine(
`applyComputations( this._state, this._state, {}, true );`
`${generator.alias( 'applyComputations' )}( this._state, this._state, {}, true );`
);
}

Expand Down Expand Up @@ -419,4 +423,4 @@ export default function dom ( parsed, source, options, names ) {
}

return generator.generate( builders.main.toString(), options, { name, format } );
}
}
8 changes: 8 additions & 0 deletions test/generator/samples/deconflict-non-helpers/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
html: `ABCD`,

test ( assert, component ) {
assert.equal( component.get( 'compute' ), 'ABCD' );
component.destroy();
}
};
17 changes: 17 additions & 0 deletions test/generator/samples/deconflict-non-helpers/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{compute}}
<script>
import { addCss, addedCss, applyComputations, renderMainFragment } from './module.js';

export default {
data() {
return {
value: addCss + addedCss + applyComputations + renderMainFragment
};
},
computed: {
compute: value => value.toUpperCase()
}
};
</script>
<style>
</style>
4 changes: 4 additions & 0 deletions test/generator/samples/deconflict-non-helpers/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const addCss = 'a';
export const addedCss = 'b';
export const applyComputations = 'c';
export const renderMainFragment = 'd';