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

ts/consistent-type-imports "You have used a rule which requires parserServices to be generated." #570

Open
5 of 7 tasks
fregante opened this issue Jul 31, 2024 · 6 comments

Comments

@fregante
Copy link

Describe the bug

I'm having the same as #372 when I try to change this config:

import antfu from '@antfu/eslint-config';

export default antfu({
  rules: {
-   'ts/consistent-type-imports': 'off',
+   'ts/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports' }],
    'ts/consistent-type-definitions': ['error', 'type'],
  },
});

I assume that this is happening because the rule is being configured in the global config instead of in a TS override… but that doesn't explain why the following ts/consistent-type-definitions works fine.

Oops! Something went wrong! :(

ESLint: 9.8.0

Error: Error while loading rule 'ts/consistent-type-imports': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Parser: undefined
Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.
Occurred while linting ./tsconfig.json
    at throwError (./node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/utils/dist/eslint-utils/getParserServices.js:39:11)
    at getParserServices (./node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/utils/dist/eslint-utils/getParserServices.js:20:9)
    at create (./node_modules/@typescript-eslint/eslint-plugin/dist/rules/consistent-type-imports.js:85:68)
    at Object.create (./node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/utils/dist/eslint-utils/RuleCreator.js:31:20)
    at createRuleListeners (./node_modules/eslint/lib/linter/linter.js:977:21)
    at ./node_modules/eslint/lib/linter/linter.js:1108:84
    at Array.forEach (<anonymous>)
    at runRules (./node_modules/eslint/lib/linter/linter.js:1039:34)
    at Linter._verifyWithFlatConfigArrayAndWithoutProcessors (./node_modules/eslint/lib/linter/linter.js:1947:31)
    at Linter._verifyWithFlatConfigArray (./node_modules/eslint/lib/linter/linter.js:2086:21)

Reproduction

Config pasted above

System Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Pro
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 22.3.0 - /usr/local/bin/node
    npm: 10.8.1 - /usr/local/bin/npm

Latest versions:

    "@antfu/eslint-config": "^2.24.0",
    "eslint": "^9.8.0",

Used Package Manager

npm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Contributions

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)
@fregante
Copy link
Author

Moving it to the TypeScript-specific override fixes it:

export default antfu({
  rules: {
    'ts/consistent-type-definitions': ['error', 'type'],

    // Prettier conflicts
    'style/jsx-one-expression-per-line': 'off',
  },
}, {
  files: ['**/*.ts', '**/*.tsx'],
  rules: {
    'ts/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports' }],
  },
});

But this raises a few questions:

  • Why does this rule fail while ts/consistent-type-definitions works well in the main rules object?
  • Can this be fixed/avoided?
  • Is there an easier way to specify the list of TS extensions? ts and tsx are just 2 of many.

@antfu
Copy link
Owner

antfu commented Jul 31, 2024

You can do something like this:

export default antfu({
  typescript: {
    overrides: {
      'ts/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports' }],
    },
  }
})

@joejordan
Copy link

joejordan commented Sep 13, 2024

You can do something like this:

export default antfu({
  typescript: {
    overrides: {
      'ts/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports' }],
    },
  }
})

I was getting the You have used a rule which requires parserServices error as well when attempting to modify ts/ rules in the rules section. Using the typescript key overrides addressed the issue adequately, though if it's fixable, I did spend a fair amount of time trying to figure out why running pnpm run lint was suddenly erroring.

Edit:

I did come across at least one rule that didn't work with this approach, specifically the ts/no-misused-promises rule. I can turn it off completely without issue, but it errors when I customize it as follows:

  typescript: {
    overrides: {
      'ts/consistent-type-definitions': 'off',
      'ts/no-misused-promises': [
        'error',
        {
          'checksVoidReturn': false,
        },
      ],
    },
  },

The above entry still errors with the parserServices error mentioned previously.

@JianJroh
Copy link

JianJroh commented Sep 14, 2024

You can do something like this:你可以这样做:

export default antfu({
  typescript: {
    overrides: {
      'ts/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports' }],
    },
  }
})

I was getting the You have used a rule which requires parserServices error as well when attempting to modify ts/ rules in the rules section. Using the typescript key overrides addressed the issue adequately, though if it's fixable, I did spend a fair amount of time trying to figure out why running pnpm run lint was suddenly erroring.我正在得到 You have used a rule which requires parserServices 尝试修改规则部分中的ts/规则时也会出错。使用typescript键覆盖充分解决了这个问题,尽管如果可以修复,我确实花了相当多的时间试图找出为什么运行pnpm run lint突然出错。

Edit: 编辑:

I did come across at least one rule that didn't work with this approach, specifically the ts/no-misused-promises rule. I can turn it off completely without issue, but it errors when I customize it as follows:我确实遇到了至少一条不适用于这种方法的规则,特别是ts/no-misused-promises规则。我可以毫无问题地完全关闭它,但是当我按如下方式自定义它时会出错:

  typescript: {
    overrides: {
      'ts/consistent-type-definitions': 'off',
      'ts/no-misused-promises': [
        'error',
        {
          'checksVoidReturn': false,
        },
      ],
    },
  },

The above entry still errors with the parserServices error mentioned previously.上面的条目仍然存在前面提到的parserServices错误。

You can try overridesTypeAware.

export default antfu({
  typescript: {
    overridesTypeAware: {
      'ts/no-misused-promises': [
        'error',
        {
          checksVoidReturn: false,
        },
      ],
    },
  },
})

@xfstu
Copy link

xfstu commented Sep 14, 2024

I had a similar problem:

// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu({
  vue: true,
  typescript: {
    tsconfigPath: 'tsconfig.json',
    overridesTypeAware: {
      //This configuration works
      'ts/no-unsafe-return': 'warn'
    }
  },
  rules: {
    //If the rule is configured here, an error is reported: You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for xxx
    //'ts/no-unsafe-return': 'warn'
  }
})

It is a great project that configuration rules only take effect at overridesTypeAware. Expect to be able to override or set rules through :

export default antfu({
  rules:{}
})

in a future release.
Thank you to every open source author.

@throrin19
Copy link

throrin19 commented Oct 1, 2024

It does not works with my config. I set ts/consistent-type-imports in typescript.overrideand it is totally ignored :

/**
 * @typedef {import("@antfu/eslint-config/index.d.ts").} Serverless
 */

import antfu from '@antfu/eslint-config';
import preferArrowFunctions from 'eslint-plugin-prefer-arrow-functions';

export default antfu(
    {
        formatters: {
            css: true,
            html: true,
        },
        ignores: [
            '.vscode/**',
            'dist/**',
            '.dependency-cruiser.js',
        ],
        isInEditor: false,
        plugins: {
            'prefer-arrow-functions': preferArrowFunctions,
        },
        stylistic: {
            indent: 4,
            quotes: 'single',
            semi: true,
        },
        toml: false,
        typescript: {
            overrides: {
                'ts/consistent-type-imports': ['error', {
                    fixStyle: 'separate-type-imports',
                    prefer: 'type-imports',
                }],
            },
        },
        vue: true,
    },
    {
        // Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
        files: ['**/*.json'],
        rules: {
            'jsonc/indent': ['error', 2],
            'jsonc/sort-keys': 'off',
        },
    },
    {
        // Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
        files: ['**/*.vue'],
        rules: {
            'vue/block-order': ['error', { order: ['template', 'script', 'style'] }],
            'vue/component-name-in-template-casing': ['warn', 'kebab-case'],
            'vue/custom-event-name-casing': 'off',
            'vue/html-closing-bracket-newline': ['error', { multiline: 'never' }],
            'vue/html-indent': ['error', 4],
            'vue/no-v-html': 'off',
            'vue/no-v-model-argument': 'off',
            'vue/operator-linebreak': ['error', 'before'],
        },
    },
    {
        files: ['**/*.yml'],
        rules: {
            'yaml/indent': ['error', 2],
        },
    },
    {
        // Without `files`, they are general rules for all files
        rules: {
            'antfu/top-level-function': 'off',
            'jsdoc/check-param-names': ['warn', {
                checkDestructured: false,
                disableMissingParamChecks: true,
            }],
            'no-unused-vars': 'off',
            'perfectionist/sort-classes': 'error',
            'perfectionist/sort-imports': ['error', {
                groups: [
                    'type',
                    'internal-type',
                    ['parent-type', 'sibling-type', 'index-type'],
                    'builtin',
                    'external',
                    'internal',
                    ['parent', 'sibling', 'index'],
                    'object',
                    'unknown',
                ],
                internalPattern: ['@/**'],
                newlinesBetween: 'never',
            }],
            'perfectionist/sort-named-exports': 'error',
            'perfectionist/sort-named-imports': 'error',
            'perfectionist/sort-objects': 'error',
            'prefer-arrow-functions/prefer-arrow-functions': ['error', {
                returnStyle: 'implicit',
            }],
            'style/brace-style': ['error', '1tbs'],
            'style/indent-binary-ops': ['off'],
            'style/semi': ['error', 'always'],
            'ts/no-empty-function': 'error',
            'ts/no-empty-interface': 'error',
            'ts/no-explicit-any': ['error'],
            'ts/no-non-null-assertion': ['warn'],
            'ts/no-unsafe-declaration-merging': 'off',
            'ts/no-unused-vars': ['error', {
                args: 'after-used',
                argsIgnorePattern: '^_',
                caughtErrors: 'all',
                caughtErrorsIgnorePattern: '^_',
                destructuredArrayIgnorePattern: '^_',
                vars: 'all',
                varsIgnorePattern: '^_',
            }],
            'unused-imports/no-unused-imports': ['off'],
            'unused-imports/no-unused-vars': ['off'],
        },
    },
);

And a file with the problem :

import type { AxiosRequestConfig } from 'axios';
import type { OrgIdOrCustomerId } from './common.js';
import type { Model, ModelClass, ModelStructure } from './Model.js';
// normally this part must return an error and fix with separate type Ref and ref. but nothing apear
import { ref, type Ref } from 'vue'; 
import request from '../request.js';

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

6 participants