From e25d4e752c0f7063e9fc9e5d19e4711d28adf872 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 17 Mar 2017 20:11:32 -0400 Subject: [PATCH 1/4] deconflict non-helper functions and variables (#388) --- src/generators/dom/index.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index 406e676e95..5ed7c5b8b2 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -131,12 +131,14 @@ class DomGenerator extends Generator { node.children = []; } - helper ( name ) { + helper ( name, isSharedHelper ) { if ( this.options.dev && `${name}Dev` in shared ) { name = `${name}Dev`; } - this.uses[ name ] = true; + if ( isSharedHelper !== false ) { + this.uses[ name ] = true; + } if ( !( name in this.aliases ) ) { let alias = name; @@ -188,7 +190,7 @@ export default function dom ( parsed, source, options, names ) { } generator.push({ - name: 'renderMainFragment', + name: generator.helper( 'renderMainFragment', false ), namespace, target: 'target', localElementDepth: 0, @@ -238,12 +240,12 @@ export default function dom ( parsed, source, options, names ) { }); builders.main.addBlock( deindent` - function applyComputations ( state, newState, oldState, isInitial ) { + function ${generator.helper( 'applyComputations', false )} ( state, newState, oldState, isInitial ) { ${builder} } ` ); - builders._set.addLine( `applyComputations( this._state, newState, oldState, false )` ); + builders._set.addLine( `${generator.helper( 'applyComputations', false )}( this._state, newState, oldState, false )` ); } // TODO is the `if` necessary? @@ -259,13 +261,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.helper( 'addedCss', false )} = false; + function ${generator.helper( 'addCss', false )} () { var style = ${generator.helper( 'createElement' )}( 'style' ); style.textContent = ${JSON.stringify( processCss( parsed, generator.code ) )}; ${generator.helper( 'appendNode' )}( style, document.head ); - addedCss = true; + ${generator.helper( 'addedCss', false )} = true; } ` ); } @@ -276,7 +278,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.helper( 'addedCss', false )} ) ${generator.helper( 'addCss', false )}();` ); } if ( generator.hasComponents ) { @@ -286,7 +288,7 @@ 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.helper( 'renderMainFragment', false )}( this._state, this ); if ( options.target ) this._fragment.mount( options.target, null ); while ( this._bindings.length ) this._bindings.pop()(); ` ); @@ -294,7 +296,7 @@ export default function dom ( parsed, source, options, names ) { builders._set.addLine( `while ( this._bindings.length ) this._bindings.pop()();` ); } else { builders.init.addBlock( deindent` - this._fragment = renderMainFragment( this._state, this ); + this._fragment = ${generator.helper( 'renderMainFragment', false )}( this._state, this ); if ( options.target ) this._fragment.mount( options.target, null ); ` ); } @@ -327,7 +329,7 @@ export default function dom ( parsed, source, options, names ) { if ( templateProperties.computed ) { constructorBlock.addLine( - `applyComputations( this._state, this._state, {}, true );` + `${generator.helper( 'applyComputations', false )}( this._state, this._state, {}, true );` ); } @@ -419,4 +421,4 @@ export default function dom ( parsed, source, options, names ) { } return generator.generate( builders.main.toString(), options, { name, format } ); -} \ No newline at end of file +} From 331a1a2bb2e5f2c4ff00e82179cce22a880856a4 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 17 Mar 2017 20:28:56 -0400 Subject: [PATCH 2/4] nicer way of generating aliases for non-helper functions --- src/generators/dom/index.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index 5ed7c5b8b2..f3b9d00ae6 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -131,15 +131,17 @@ class DomGenerator extends Generator { node.children = []; } - helper ( name, isSharedHelper ) { + helper ( name ) { if ( this.options.dev && `${name}Dev` in shared ) { name = `${name}Dev`; } - if ( isSharedHelper !== false ) { - this.uses[ name ] = true; - } + this.uses[ name ] = true; + return this.alias( name ); + } + + alias ( name ) { if ( !( name in this.aliases ) ) { let alias = name; let i = 1; @@ -190,7 +192,7 @@ export default function dom ( parsed, source, options, names ) { } generator.push({ - name: generator.helper( 'renderMainFragment', false ), + name: generator.alias( 'renderMainFragment' ), namespace, target: 'target', localElementDepth: 0, @@ -240,12 +242,12 @@ export default function dom ( parsed, source, options, names ) { }); builders.main.addBlock( deindent` - function ${generator.helper( 'applyComputations', false )} ( state, newState, oldState, isInitial ) { + function ${generator.alias( 'applyComputations' )} ( state, newState, oldState, isInitial ) { ${builder} } ` ); - builders._set.addLine( `${generator.helper( 'applyComputations', false )}( this._state, newState, oldState, false )` ); + builders._set.addLine( `${generator.alias( 'applyComputations' )}( this._state, newState, oldState, false )` ); } // TODO is the `if` necessary? @@ -261,13 +263,13 @@ export default function dom ( parsed, source, options, names ) { if ( parsed.css && options.css !== false ) { builders.main.addBlock( deindent` - var ${generator.helper( 'addedCss', false )} = false; - function ${generator.helper( 'addCss', false )} () { + 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 ); - ${generator.helper( 'addedCss', false )} = true; + ${generator.alias( 'addedCss' )} = true; } ` ); } @@ -278,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 ( !${generator.helper( 'addedCss', false )} ) ${generator.helper( 'addCss', false )}();` ); + builders.init.addLine( `if ( !${generator.alias( 'addedCss' )} ) ${generator.alias( 'addCss' )}();` ); } if ( generator.hasComponents ) { @@ -288,7 +290,7 @@ export default function dom ( parsed, source, options, names ) { if ( generator.hasComplexBindings ) { builders.init.addBlock( deindent` this._bindings = []; - this._fragment = ${generator.helper( 'renderMainFragment', false )}( this._state, this ); + this._fragment = ${generator.alas( 'renderMainFragment' )}( this._state, this ); if ( options.target ) this._fragment.mount( options.target, null ); while ( this._bindings.length ) this._bindings.pop()(); ` ); @@ -296,7 +298,7 @@ export default function dom ( parsed, source, options, names ) { builders._set.addLine( `while ( this._bindings.length ) this._bindings.pop()();` ); } else { builders.init.addBlock( deindent` - this._fragment = ${generator.helper( 'renderMainFragment', false )}( this._state, this ); + this._fragment = ${generator.alias( 'renderMainFragment' )}( this._state, this ); if ( options.target ) this._fragment.mount( options.target, null ); ` ); } @@ -329,7 +331,7 @@ export default function dom ( parsed, source, options, names ) { if ( templateProperties.computed ) { constructorBlock.addLine( - `${generator.helper( 'applyComputations', false )}( this._state, this._state, {}, true );` + `${generator.alias( 'applyComputations' )}( this._state, this._state, {}, true );` ); } From 8f6499a9dd65c4b460c44c848dec38223dc012eb Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 17 Mar 2017 20:32:15 -0400 Subject: [PATCH 3/4] alas poor typo --- src/generators/dom/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index f3b9d00ae6..2872a948c1 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -290,7 +290,7 @@ export default function dom ( parsed, source, options, names ) { if ( generator.hasComplexBindings ) { builders.init.addBlock( deindent` this._bindings = []; - this._fragment = ${generator.alas( '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()(); ` ); From dfd73987e348f6659022b6de8e015944c5f9e4cd Mon Sep 17 00:00:00 2001 From: Conduitry Date: Fri, 17 Mar 2017 20:55:10 -0400 Subject: [PATCH 4/4] add unit test --- .../samples/deconflict-non-helpers/_config.js | 8 ++++++++ .../samples/deconflict-non-helpers/main.html | 17 +++++++++++++++++ .../samples/deconflict-non-helpers/module.js | 4 ++++ 3 files changed, 29 insertions(+) create mode 100644 test/generator/samples/deconflict-non-helpers/_config.js create mode 100644 test/generator/samples/deconflict-non-helpers/main.html create mode 100644 test/generator/samples/deconflict-non-helpers/module.js diff --git a/test/generator/samples/deconflict-non-helpers/_config.js b/test/generator/samples/deconflict-non-helpers/_config.js new file mode 100644 index 0000000000..978e4c27e3 --- /dev/null +++ b/test/generator/samples/deconflict-non-helpers/_config.js @@ -0,0 +1,8 @@ +export default { + html: `ABCD`, + + test ( assert, component ) { + assert.equal( component.get( 'compute' ), 'ABCD' ); + component.destroy(); + } +}; diff --git a/test/generator/samples/deconflict-non-helpers/main.html b/test/generator/samples/deconflict-non-helpers/main.html new file mode 100644 index 0000000000..8541b090f9 --- /dev/null +++ b/test/generator/samples/deconflict-non-helpers/main.html @@ -0,0 +1,17 @@ +{{compute}} + + diff --git a/test/generator/samples/deconflict-non-helpers/module.js b/test/generator/samples/deconflict-non-helpers/module.js new file mode 100644 index 0000000000..ff4e71e251 --- /dev/null +++ b/test/generator/samples/deconflict-non-helpers/module.js @@ -0,0 +1,4 @@ +export const addCss = 'a'; +export const addedCss = 'b'; +export const applyComputations = 'c'; +export const renderMainFragment = 'd';