Skip to content

Commit

Permalink
Verify computed property dependencies
Browse files Browse the repository at this point in the history
Improves the validator to fail if someone forgets to declare
dependent properties for computed state:

```
export default {
  computed: {
    bar: () => { return new Date().getTime(); }
  }
};
```
  • Loading branch information
nikku committed Dec 4, 2016
1 parent 5770048 commit 65cdead
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion compiler/validate/js/propValidators/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ export default function computed ( validator, prop ) {
return;
}

computation.value.params.forEach( param => {
const params = computation.value.params;

if ( params.length === 0 ) {
validator.error( `A computed value must depend on at least one property`, computation.value.start );
return;
}

params.forEach( param => {
const valid = param.type === 'Identifier' || param.type === 'AssignmentPattern' && param.left.type === 'Identifier';

if ( !valid ) {
Expand Down
8 changes: 8 additions & 0 deletions test/validator/computed-values/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "A computed value must depend on at least one property",
"pos": 49,
"loc": {
"line": 4,
"column": 8
}
}]
7 changes: 7 additions & 0 deletions test/validator/computed-values/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export default {
computed: {
foo: () => {}
}
};
</script>

0 comments on commit 65cdead

Please sign in to comment.