Skip to content

Commit

Permalink
Merge pull request #600 from sveltejs/gh-575
Browse files Browse the repository at this point in the history
always use helpers if referenced, not just for CallExpressions, and warn on context clashes
  • Loading branch information
Rich-Harris authored May 28, 2017
2 parents ecc9a93 + 75ea527 commit 1db0d46
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ export default class Generator {
const { name } = flattenReference( node );
if ( scope.has( name ) ) return;

if ( parent && parent.type === 'CallExpression' && node === parent.callee && helpers.has( name ) ) {
code.prependRight( node.start, `${self.alias( 'template' )}.helpers.` );
}

else if ( name === 'event' && isEventHandler ) {
if ( name === 'event' && isEventHandler ) {
// noop
}

Expand All @@ -135,6 +131,10 @@ export default class Generator {
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 ) ) {
const context = indexes.get( name );
if ( !~usedContexts.indexOf( context ) ) usedContexts.push( context );
Expand Down
11 changes: 11 additions & 0 deletions src/validate/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ export default function validateHtml ( validator: Validator, html: Node ) {
elementDepth += 1;

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 ) {
Expand Down
3 changes: 3 additions & 0 deletions test/runtime/samples/helpers-not-call-expression/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: '<p>1,4,9</p>'
};
15 changes: 15 additions & 0 deletions test/runtime/samples/helpers-not-call-expression/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p>{{numbers.map(square)}}</p>

<script>
export default {
data () {
return {
numbers: [ 1, 2, 3 ]
};
},

helpers: {
square: num => num * num
}
};
</script>
17 changes: 17 additions & 0 deletions test/validator/samples/helper-clash-context/input.html
Original file line number Diff line number Diff line change
@@ -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>
8 changes: 8 additions & 0 deletions test/validator/samples/helper-clash-context/warnings.json
Original file line number Diff line number Diff line change
@@ -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
}]

0 comments on commit 1db0d46

Please sign in to comment.