From 65cdead99145232591d079a4305dee127d8eec44 Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Sun, 4 Dec 2016 21:18:13 +0100 Subject: [PATCH] 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(); } } }; ``` --- compiler/validate/js/propValidators/computed.js | 9 ++++++++- test/validator/computed-values/errors.json | 8 ++++++++ test/validator/computed-values/input.html | 7 +++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/validator/computed-values/errors.json create mode 100644 test/validator/computed-values/input.html diff --git a/compiler/validate/js/propValidators/computed.js b/compiler/validate/js/propValidators/computed.js index 102ce73333..00b10af00d 100644 --- a/compiler/validate/js/propValidators/computed.js +++ b/compiler/validate/js/propValidators/computed.js @@ -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 ) { diff --git a/test/validator/computed-values/errors.json b/test/validator/computed-values/errors.json new file mode 100644 index 0000000000..3f4994e790 --- /dev/null +++ b/test/validator/computed-values/errors.json @@ -0,0 +1,8 @@ +[{ + "message": "A computed value must depend on at least one property", + "pos": 49, + "loc": { + "line": 4, + "column": 8 + } +}] \ No newline at end of file diff --git a/test/validator/computed-values/input.html b/test/validator/computed-values/input.html new file mode 100644 index 0000000000..92cb29d732 --- /dev/null +++ b/test/validator/computed-values/input.html @@ -0,0 +1,7 @@ +