handle this.get special case in helpers

pull/473/head
Rich Harris 8 years ago
parent fa65f7af60
commit cc722f8f7a

@ -24,6 +24,12 @@ export default function helpers ( validator, prop ) {
} }
else if ( lexicalDepth === 0 ) { else if ( lexicalDepth === 0 ) {
// handle special case that's caused some people confusion — using `this.get(...)` instead of passing argument
// TODO do the same thing for computed values?
if ( node.type === 'CallExpression' && node.callee.type === 'MemberExpression' && node.callee.object.type === 'ThisExpression' && node.callee.property.name === 'get' && !node.callee.property.computed ) {
validator.error( `Cannot use this.get(...) — it must be passed into the helper function as an argument`, node.start );
}
if ( node.type === 'ThisExpression' ) { if ( node.type === 'ThisExpression' ) {
validator.error( `Helpers should be pure functions — they do not have access to the component instance and cannot use 'this'. Did you mean to put this in 'methods'?`, node.start ); validator.error( `Helpers should be pure functions — they do not have access to the component instance and cannot use 'this'. Did you mean to put this in 'methods'?`, node.start );
} }

@ -1,8 +1,8 @@
[{ [{
"message": "Helpers should be pure functions — they do not have access to the component instance and cannot use 'this'. Did you mean to put this in 'methods'?", "message": "Helpers should be pure functions — they do not have access to the component instance and cannot use 'this'. Did you mean to put this in 'methods'?",
"pos": 74, "pos": 95,
"loc": { "loc": {
"line": 7, "line": 7,
"column": 11 "column": 4
} }
}] }]

@ -1,10 +1,10 @@
{{foo()}} <button on:click='foo()'>foo</button>
<script> <script>
export default { export default {
helpers: { helpers: {
foo () { foo () {
return this.get( 'bar' ); this.set({ foo: true });
} }
} }
} }

@ -0,0 +1,8 @@
[{
"message": "Cannot use this.get(...) — it must be passed into the helper function as an argument",
"pos": 74,
"loc": {
"line": 7,
"column": 11
}
}]

@ -0,0 +1,11 @@
{{foo()}}
<script>
export default {
helpers: {
foo () {
return this.get( 'bar' );
}
}
}
</script>
Loading…
Cancel
Save