mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
Verify computed property dependencies
Improves the validator to fail if someone forgets to declare dependent properties for computed state: ``` export default { computed: { bar: () => { return new Date().getTime(); } } }; ```
This commit is contained in:
parent
5770048855
commit
65cdead991
@ -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 ) {
|
||||
|
8
test/validator/computed-values/errors.json
Normal file
8
test/validator/computed-values/errors.json
Normal file
@ -0,0 +1,8 @@
|
||||
[{
|
||||
"message": "A computed value must depend on at least one property",
|
||||
"pos": 49,
|
||||
"loc": {
|
||||
"line": 4,
|
||||
"column": 8
|
||||
}
|
||||
}]
|
7
test/validator/computed-values/input.html
Normal file
7
test/validator/computed-values/input.html
Normal file
@ -0,0 +1,7 @@
|
||||
<script>
|
||||
export default {
|
||||
computed: {
|
||||
foo: () => {}
|
||||
}
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user