diff --git a/src/validate/js/propValidators/helpers.js b/src/validate/js/propValidators/helpers.js index 8248445d44..7e27e31134 100644 --- a/src/validate/js/propValidators/helpers.js +++ b/src/validate/js/propValidators/helpers.js @@ -24,6 +24,12 @@ export default function helpers ( validator, prop ) { } 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' ) { 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 ); } diff --git a/test/validator/samples/helper-purity-check-no-this/errors.json b/test/validator/samples/helper-purity-check-no-this/errors.json index 9de3d7465d..87c71a4034 100644 --- a/test/validator/samples/helper-purity-check-no-this/errors.json +++ b/test/validator/samples/helper-purity-check-no-this/errors.json @@ -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'?", - "pos": 74, + "pos": 95, "loc": { "line": 7, - "column": 11 + "column": 4 } }] \ No newline at end of file diff --git a/test/validator/samples/helper-purity-check-no-this/input.html b/test/validator/samples/helper-purity-check-no-this/input.html index e950152de8..795821b446 100644 --- a/test/validator/samples/helper-purity-check-no-this/input.html +++ b/test/validator/samples/helper-purity-check-no-this/input.html @@ -1,10 +1,10 @@ -{{foo()}} + \ No newline at end of file