Skip to content

Commit ec3c81f

Browse files
authored
Merge pull request #389 from sveltejs/gh-388-deconflict-non-helper-functions
Deconflict non helper functions
2 parents efce7ac + dfd7398 commit ec3c81f

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

src/generators/dom/index.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ class DomGenerator extends Generator {
138138

139139
this.uses[ name ] = true;
140140

141+
return this.alias( name );
142+
}
143+
144+
alias ( name ) {
141145
if ( !( name in this.aliases ) ) {
142146
let alias = name;
143147
let i = 1;
@@ -188,7 +192,7 @@ export default function dom ( parsed, source, options, names ) {
188192
}
189193

190194
generator.push({
191-
name: 'renderMainFragment',
195+
name: generator.alias( 'renderMainFragment' ),
192196
namespace,
193197
target: 'target',
194198
localElementDepth: 0,
@@ -238,12 +242,12 @@ export default function dom ( parsed, source, options, names ) {
238242
});
239243

240244
builders.main.addBlock( deindent`
241-
function applyComputations ( state, newState, oldState, isInitial ) {
245+
function ${generator.alias( 'applyComputations' )} ( state, newState, oldState, isInitial ) {
242246
${builder}
243247
}
244248
` );
245249

246-
builders._set.addLine( `applyComputations( this._state, newState, oldState, false )` );
250+
builders._set.addLine( `${generator.alias( 'applyComputations' )}( this._state, newState, oldState, false )` );
247251
}
248252

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

260264
if ( parsed.css && options.css !== false ) {
261265
builders.main.addBlock( deindent`
262-
var addedCss = false;
263-
function addCss () {
266+
var ${generator.alias( 'addedCss' )} = false;
267+
function ${generator.alias( 'addCss' )} () {
264268
var style = ${generator.helper( 'createElement' )}( 'style' );
265269
style.textContent = ${JSON.stringify( processCss( parsed, generator.code ) )};
266270
${generator.helper( 'appendNode' )}( style, document.head );
267271
268-
addedCss = true;
272+
${generator.alias( 'addedCss' )} = true;
269273
}
270274
` );
271275
}
@@ -276,7 +280,7 @@ export default function dom ( parsed, source, options, names ) {
276280
builders.init.addLine( `this._torndown = false;` );
277281

278282
if ( parsed.css && options.css !== false ) {
279-
builders.init.addLine( `if ( !addedCss ) addCss();` );
283+
builders.init.addLine( `if ( !${generator.alias( 'addedCss' )} ) ${generator.alias( 'addCss' )}();` );
280284
}
281285

282286
if ( generator.hasComponents ) {
@@ -286,15 +290,15 @@ export default function dom ( parsed, source, options, names ) {
286290
if ( generator.hasComplexBindings ) {
287291
builders.init.addBlock( deindent`
288292
this._bindings = [];
289-
this._fragment = renderMainFragment( this._state, this );
293+
this._fragment = ${generator.alias( 'renderMainFragment' )}( this._state, this );
290294
if ( options.target ) this._fragment.mount( options.target, null );
291295
while ( this._bindings.length ) this._bindings.pop()();
292296
` );
293297

294298
builders._set.addLine( `while ( this._bindings.length ) this._bindings.pop()();` );
295299
} else {
296300
builders.init.addBlock( deindent`
297-
this._fragment = renderMainFragment( this._state, this );
301+
this._fragment = ${generator.alias( 'renderMainFragment' )}( this._state, this );
298302
if ( options.target ) this._fragment.mount( options.target, null );
299303
` );
300304
}
@@ -327,7 +331,7 @@ export default function dom ( parsed, source, options, names ) {
327331

328332
if ( templateProperties.computed ) {
329333
constructorBlock.addLine(
330-
`applyComputations( this._state, this._state, {}, true );`
334+
`${generator.alias( 'applyComputations' )}( this._state, this._state, {}, true );`
331335
);
332336
}
333337

@@ -419,4 +423,4 @@ export default function dom ( parsed, source, options, names ) {
419423
}
420424

421425
return generator.generate( builders.main.toString(), options, { name, format } );
422-
}
426+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
html: `ABCD`,
3+
4+
test ( assert, component ) {
5+
assert.equal( component.get( 'compute' ), 'ABCD' );
6+
component.destroy();
7+
}
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{compute}}
2+
<script>
3+
import { addCss, addedCss, applyComputations, renderMainFragment } from './module.js';
4+
5+
export default {
6+
data() {
7+
return {
8+
value: addCss + addedCss + applyComputations + renderMainFragment
9+
};
10+
},
11+
computed: {
12+
compute: value => value.toUpperCase()
13+
}
14+
};
15+
</script>
16+
<style>
17+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const addCss = 'a';
2+
export const addedCss = 'b';
3+
export const applyComputations = 'c';
4+
export const renderMainFragment = 'd';

0 commit comments

Comments
 (0)