Skip to content
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

prefer-use-template-ref: should check only root-level variables #2607

Open
2 tasks done
Thomasan1999 opened this issue Nov 14, 2024 · 2 comments · May be fixed by #2612
Open
2 tasks done

prefer-use-template-ref: should check only root-level variables #2607

Thomasan1999 opened this issue Nov 14, 2024 · 2 comments · May be fixed by #2612

Comments

@Thomasan1999
Copy link
Contributor

Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have read the FAQ and my problem is not listed.

Tell us about your environment

  • ESLint version: 9.14.0
  • eslint-plugin-vue version: 9.31.0
  • Vue version: 3.5.12
  • Node version: 20.5.1
  • Operating System: Windows 11 64-bit

Please show your full configuration:

import pluginVue from 'eslint-plugin-vue';

export default [
    ...pluginVue.configs['flat/essential'],
    {
        files: ['**/*.vue'],
        rules: {
            'vue/prefer-use-template-ref': 'error',
        },
    },
];

What did you do?

<template>
    <div>
        <ul>
            <li ref="firstListItem">Morning</li>
            <li ref="second">Afternoon</li>
            <li>Evening</li>
        </ul>
    </div>
</template>

<script setup>
    import { ref } from 'vue';

    function getFirstListItemElement() {
        const firstListItem = ref();

        const a = () => {
            const second = ref();
        };
    }
</script>

What did you expect to happen?
No errors, only root-level variables should be considered, the nested variables are ignored by Vue when assigning template refs (in onMounted I get null). I had noticed this when trying to implement fix option for this rule. I wanted to create an issue to properly discuss whether this is an expected behavior.

What actually happened?

15:31  error    Replace 'ref' with 'useTemplateRef'                                           vue/prefer-use-template-ref
18:28  error    Replace 'ref' with 'useTemplateRef'                                           vue/prefer-use-template-ref

Repository to reproduce this issue

@Thomasan1999 Thomasan1999 changed the title prefer-use-template-ref: should check only root-level variables prefer-use-template-ref: should check only root-level variables Nov 14, 2024
@FloEdelmann
Copy link
Member

Yes,refs inside functions should definitely be ignored. I think we should only consider refs defined in the root scope of <script setup> or the setup() function.

@Thomasan1999
Copy link
Contributor Author

@FloEdelmann , thank you for the answer, will create a PR with bugfix soon :)

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

Successfully merging a pull request may close this issue.

2 participants