always use helpers if referenced, not just for CallExpressions, and warn on context clashes (#575)

pull/600/head
Rich Harris 8 years ago
parent 4f56b6553c
commit 75ea52754d

@ -117,11 +117,7 @@ export default class Generator {
const { name } = flattenReference( node ); const { name } = flattenReference( node );
if ( scope.has( name ) ) return; if ( scope.has( name ) ) return;
if ( parent && parent.type === 'CallExpression' && node === parent.callee && helpers.has( name ) ) { if ( name === 'event' && isEventHandler ) {
code.prependRight( node.start, `${self.alias( 'template' )}.helpers.` );
}
else if ( name === 'event' && isEventHandler ) {
// noop // noop
} }
@ -135,6 +131,10 @@ export default class Generator {
if ( !~usedContexts.indexOf( name ) ) usedContexts.push( name ); if ( !~usedContexts.indexOf( name ) ) usedContexts.push( name );
} }
else if ( helpers.has( name ) ) {
code.prependRight( node.start, `${self.alias( 'template' )}.helpers.` );
}
else if ( indexes.has( name ) ) { else if ( indexes.has( name ) ) {
const context = indexes.get( name ); const context = indexes.get( name );
if ( !~usedContexts.indexOf( context ) ) usedContexts.push( context ); if ( !~usedContexts.indexOf( context ) ) usedContexts.push( context );

@ -26,6 +26,17 @@ export default function validateHtml ( validator: Validator, html: Node ) {
elementDepth += 1; elementDepth += 1;
validateElement( validator, node ); validateElement( validator, node );
} else if ( node.type === 'EachBlock' ) {
if ( validator.helpers.has( node.context ) ) {
let c = node.expression.end;
// find start of context
while ( /\s/.test( validator.source[c] ) ) c += 1;
c += 2;
while ( /\s/.test( validator.source[c] ) ) c += 1;
validator.warn( `Context clashes with a helper. Rename one or the other to eliminate any ambiguity`, c );
}
} }
if ( node.children ) { if ( node.children ) {

@ -0,0 +1,3 @@
export default {
html: '<p>1,4,9</p>'
};

@ -0,0 +1,15 @@
<p>{{numbers.map(square)}}</p>
<script>
export default {
data () {
return {
numbers: [ 1, 2, 3 ]
};
},
helpers: {
square: num => num * num
}
};
</script>

@ -0,0 +1,17 @@
{{#each things as thing}}
{{thing}}
{{/each}}
<script>
export default {
data () {
return { things: [ 'a', 'b', 'c' ] };
},
helpers: {
thing: function ( x ) {
return x;
}
}
};
</script>

@ -0,0 +1,8 @@
[{
"message": "Context clashes with a helper. Rename one or the other to eliminate any ambiguity",
"loc": {
"line": 1,
"column": 18
},
"pos": 18
}]
Loading…
Cancel
Save