useVueComputedPropertyReturn (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/useVueComputedPropertyReturn - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Inspired from
vue/return-in-computed-property
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useVueComputedPropertyReturn": "error" } } }}Description
Section titled “Description”Require computed properties in Vue components to return a value.
The value of a computed property is whatever its getter function returns. When the
getter can finish without returning a value, the computed property is undefined,
which is almost always a mistake, such as a forgotten return keyword or a missing
else branch.
This rule checks the getters of properties in the computed option, and the getters
passed to computed() at the top level of <script setup>. A return statement
without a value is reported too, because it also makes the computed property
undefined.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<script>export default { computed: { fullName() { this.firstName + " " + this.lastName; }, },};</script>code-block.vue:4:5 lint/nursery/useVueComputedPropertyReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The computed property fullName doesn't always return a value.
2 │ export default {
3 │ computed: {
> 4 │ fullName() {
│ ^^^^^^^^^^^^
> 5 │ this.firstName + " " + this.lastName;
> 6 │ },
│ ^
7 │ },
8 │ };
ℹ Vue uses the value the getter returns as the value of the computed property. When the getter ends without a return statement, the computed property is undefined.
ℹ Make sure that every path through the getter ends with a return statement that returns a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script>export default { computed: { label() { if (this.count > 0) { return `${this.count} items`; } }, },};</script>code-block.vue:4:5 lint/nursery/useVueComputedPropertyReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The computed property label doesn't always return a value.
2 │ export default {
3 │ computed: {
> 4 │ label() {
│ ^^^^^^^^^
> 5 │ if (this.count > 0) {
> 6 │ return `${this.count} items`;
> 7 │ }
> 8 │ },
│ ^
9 │ },
10 │ };
ℹ Vue uses the value the getter returns as the value of the computed property. When the getter ends without a return statement, the computed property is undefined.
ℹ Make sure that every path through the getter ends with a return statement that returns a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script setup>import { computed, ref } from "vue";
const count = ref(0);const double = computed(() => { count.value * 2;});</script>code-block.vue:5:25 lint/nursery/useVueComputedPropertyReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The computed property double doesn't always return a value.
4 │ const count = ref(0);
> 5 │ const double = computed(() => {
│ ^^^^^^^
> 6 │ count.value * 2;
> 7 │ });
│ ^
8 │ </script>
9 │
ℹ Vue uses the value the getter returns as the value of the computed property. When the getter ends without a return statement, the computed property is undefined.
ℹ Make sure that every path through the getter ends with a return statement that returns a value.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script setup>import { computed, ref } from "vue";
const count = ref(0);const double = computed(() => { if (count.value > 0) { return; } return count.value * 2;});</script>code-block.vue:7:5 lint/nursery/useVueComputedPropertyReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This return statement in the computed property double doesn't return a value.
5 │ const double = computed(() => {
6 │ if (count.value > 0) {
> 7 │ return;
│ ^^^^^^^
8 │ }
9 │ return count.value * 2;
ℹ A return statement without a value makes the computed property undefined.
ℹ Return the value of the computed property from this statement.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
<script>export default { computed: { fullName() { return this.firstName + " " + this.lastName; }, label: { get() { return this.count > 0 ? `${this.count} items` : "empty"; }, set(value) { this.count = Number.parseInt(value); }, }, },};</script><script setup>import { computed, ref } from "vue";
const count = ref(0);const double = computed(() => count.value * 2);const label = computed(() => { if (count.value > 0) { return `${count.value} items`; } return "empty";});</script>See Also
Section titled “See Also”- To require
getaccessors of classes and objects to return a value, seeuseGetterReturn.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.