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

Add ESLint Processor to differentiate Vue SFC <script> langs to apply different rules, parsers, etc. #1910

Open
DrJume opened this issue Jun 10, 2022 · 4 comments

Comments

@DrJume
Copy link

DrJume commented Jun 10, 2022

Tell us about your environment

  • ESLint version: 8.17.0
  • eslint-plugin-vue version: 9.1.0
  • Node version: 16.15.1

The problem you want to solve.

It would be awesome to be able to filter .vue with lang="ts" in ESLint overrides.
Currently, it seems to be not possible.

But if there was an ESLint Processor for Vue, it would be possible to filter for the <script> lang (or even <template>?):
https://eslint.org/docs/user-guide/configuring/plugins#specifying-processor.

Your take on the correct solution to problem.

Implement an ESLint Processor for Vue SFCs.
Something like the following could be possible?

.eslintrc.js

{
    plugin: 'vue',
    processor: 'vue/sfc-processor',
    parser: 'vue-eslint-parser', // why not 'vue/parser'?
    overrides: [
        {
            files: ['*.vue'],
            rules: {
                // ...
            }
        },
        {
            files: ['**/*.vue/*.ts'],
            parser: '@typescript-eslint/parser',
            extends: [
                'plugin:@typescript-eslint/recommended',
                'plugin:@typescript-eslint/recommended-requiring-type-checking',
            ]
        }
    ]
}

Additional context

Addresses:

@DrJume
Copy link
Author

DrJume commented Nov 20, 2023

Just wanted to ask, if this issue is still relevant. Will this be implemented in v10 or is it too complex? Is there anything I could help with?

@ota-meshi
Copy link
Member

Using ESLint's processor gives the code block a virtual filename that doesn't exist, making it impossible to provide TypeScript type information. So for now I don't think using a processor will work.

For now, if you want to configure a rule with only lang="ts", I think the only way is to analyze the file yourself.

e.g.

const glob = require('fast-glob');

const scriptTs = 'lang="ts"';
const vueTsFiles = glob
  .sync(['**/*.vue', '!**/node_modules/**'], {cwd: path.resolve()})
  .filter((filePath) => fs.readFileSync(filePath, 'utf8').includes(scriptTs));

module.exports = {
  overrides: [
    ...
    {
      files: vueTsFiles,
      parser: 'vue-eslint-parser',
      parserOptions: {
        parser: '@typescript-eslint/parser',
        ...
      },
      extends: [...],
      rules: {...}
    }
  ]
};

@DrJume
Copy link
Author

DrJume commented Nov 21, 2023

Thank you very much for your thorough answer 💚

@languanghao
Copy link

Using ESLint's processor gives the code block a virtual filename that doesn't exist, making it impossible to provide TypeScript type information. So for now I don't think using a processor will work.

For now, if you want to configure a rule with only lang="ts", I think the only way is to analyze the file yourself.

e.g.

const glob = require('fast-glob');

const scriptTs = 'lang="ts"';
const vueTsFiles = glob
  .sync(['**/*.vue', '!**/node_modules/**'], {cwd: path.resolve()})
  .filter((filePath) => fs.readFileSync(filePath, 'utf8').includes(scriptTs));

module.exports = {
  overrides: [
    ...
    {
      files: vueTsFiles,
      parser: 'vue-eslint-parser',
      parserOptions: {
        parser: '@typescript-eslint/parser',
        ...
      },
      extends: [...],
      rules: {...}
    }
  ]
};

Thank you for your answer, it works!

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

No branches or pull requests

3 participants