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

ESLint is not working for auto-imported ref and computed #2678

Closed
shuuji3 opened this issue Mar 11, 2024 · 5 comments · Fixed by #2685
Closed

ESLint is not working for auto-imported ref and computed #2678

shuuji3 opened this issue Mar 11, 2024 · 5 comments · Fixed by #2685

Comments

@shuuji3
Copy link
Member

shuuji3 commented Mar 11, 2024

Elk's ESLint config has rules for ref and computed such as vue/return-in-computed-property and vue/no-ref-as-operand. These rules can prevent errors like missing .value.

But currently eslint-plugin-vue doesn't support them for auto-imported ref and computed yet (ref. vuejs/eslint-plugin-vue#1969).

As a workaround, we can temporarily insert the import statement like import { ref, computed, watch, watchEffect, shallowRef } from 'vue' to every *.vue and *.ts files with the following temporary config adjustment.

diff --git a/eslint.config.js b/eslint.config.js
index e02aae2e..3e19a2fc 100644
--- a/eslint.config.js
+++ b/eslint.config.js
@@ -5,6 +5,7 @@ export default await antfu(
   {
     unocss: false,
     vue: {
+      files: ['**/*.vue', '**/*.ts'],
       overrides: {
         'vue/no-restricted-syntax': ['error', {
           selector: 'VElement[name=\'a\']',
@@ -25,6 +26,13 @@ export default await antfu(
     rules: {
       // TODO: migrate all process reference to `import.meta.env` and remove this rule
       'node/prefer-global/process': 'off',
+      'sort-imports': 'off',
+      'import/order': 'off',
+      'unused-imports/no-unused-imports': 'off',
+      'import/newline-after-import': 'off',
+      'import/no-duplicates': 'off',
+      'ts/no-redeclare': 'off',
     },
   },
   // Sort local files

(Note: **/*.ts was needed to enable vue-related rules other than *.vue file like composables/masto/search.ts. And the bottom ones from sort-imports to ts/no-redeclare is just to ignore temporary import statements.)

To fix this issue, we'll need to wait for the upstream fix.

@shuuji3 shuuji3 added c: bug Something isn't working s: pending triage Pending Triage labels Mar 11, 2024
Copy link

stackblitz bot commented Mar 11, 2024

Solve in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@shuuji3
Copy link
Member Author

shuuji3 commented Mar 11, 2024

Example shell script to insert import statements (this is for fish shell):

for i in (find . -name '*.ts')
       sed -i "1 i\import { ref, computed, watch, watchEffect, shallowRef } from 'vue'" $i
   end
for i in (find . -name '*.vue')
       sed -E -i "s/(<script setup.+)/\1\nimport { ref, computed, watch, watchEffect, shallowRef } from 'vue'/" $i
   end

@userquin
Copy link
Member

/cc @antfu

@patak-dev
Copy link
Member

Ah, good catch. I think it would be good if auto imports supports this rule somehow, it feels important for Vue projects in general. Let's wait to see if @antfu has some ideas.

@antfu
Copy link
Member

antfu commented Mar 11, 2024

PR's here vuejs/eslint-plugin-vue#2422

@userquin userquin added bug: upstream and removed s: pending triage Pending Triage c: bug Something isn't working labels Mar 12, 2024
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.

4 participants