Skip to content

rule: no accessing computed properties in data #1470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
privatenumber opened this issue Apr 1, 2021 · 1 comment · Fixed by #1653
Closed

rule: no accessing computed properties in data #1470

privatenumber opened this issue Apr 1, 2021 · 1 comment · Fixed by #1653

Comments

@privatenumber
Copy link
Contributor

Please describe what the rule should do:

Should warn when accessing a computed property from the data() function, as computed properties are not initialized at that point and returns undefined.

Vue 2 demo: https://jsfiddle.net/hirokiosame/dunkzqL0/7/
Vue 3 demo: https://jsfiddle.net/hirokiosame/4qu8yxfo/2/

What category should the rule belong to?

[ ] Enforces code style (layout)
[x] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule should warn about:

<script>
export default {
    computed: {
        defaultValue() {
            return 123;
        }
    },
    data() {
        return {
            // computed not initialized yet, so returns undefined
            someValue: this.defaultValue,
        };
    },
};
</script>

Additional context

@ota-meshi
Copy link
Member

Thank you for proposed rule!
I like this rule you have proposed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants