Skip to content

Bug: [consistent-type-assertions] error when type check is disabled for vue files #8153

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
4 tasks done
kuoruan opened this issue Dec 29, 2023 · 6 comments
Closed
4 tasks done
Labels
bug Something isn't working fix: user error issue was fixed by correcting the configuration / correcting the code package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin vue issues relating to vue support

Comments

@kuoruan
Copy link

kuoruan commented Dec 29, 2023

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://codesandbox.io/p/devbox/delicate-platform-64cd72

Repro Code

error reported when vue files have no `<script lang="ts">` block defined.

npm run lint

ESLint Config

module.exports = {
  root: true,
  extends: [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended-type-checked",
    "plugin:@typescript-eslint/stylistic-type-checked",
  ],
  parserOptions: {
    project: ["./tsconfig.json", "./tsconfig.node.json"],
  },
  overrides: [
    {
      files: ["*.vue"],
      extends: [
        "plugin:@typescript-eslint/disable-type-checked",
        "plugin:vue/vue3-recommended",
      ],
    },
  ],
};

tsconfig

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "strict": true,
    "jsx": "preserve",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["ESNext", "DOM"],
    "skipLibCheck": true,
    "noEmit": true
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
}

Expected Result

No error report

Actual Result

Error: Error while loading rule '@typescript-eslint/consistent-type-assertions': 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.
Occurred while linting /workspaces/workspace/src/App.vue
    at getParserServices (/workspaces/workspace/node_modules/@typescript-eslint/utils/dist/eslint-utils/getParserServices.js:21:15)
    at create (/workspaces/workspace/node_modules/@typescript-eslint/eslint-plugin/dist/rules/consistent-type-assertions.js:90:61)
    at Object.create (/workspaces/workspace/node_modules/@typescript-eslint/utils/dist/eslint-utils/RuleCreator.js:38:20)
    at createRuleListeners (/workspaces/workspace/node_modules/eslint/lib/linter/linter.js:895:21)
    at /workspaces/workspace/node_modules/eslint/lib/linter/linter.js:1066:110
    at Array.forEach (<anonymous>)
    at runRules (/workspaces/workspace/node_modules/eslint/lib/linter/linter.js:1003:34)
    at Linter._verifyWithoutProcessors (/workspaces/workspace/node_modules/eslint/lib/linter/linter.js:1355:31)
    at /workspaces/workspace/node_modules/eslint/lib/linter/linter.js:1913:29
    at Array.map (<anonymous>)

Additional Info

#7723

@kuoruan kuoruan added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Dec 29, 2023
@auvred
Copy link
Member

auvred commented Dec 29, 2023

Could you try to do the following?

module.exports = {
  root: true,
  extends: [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/stylistic",
  ],
  parserOptions: {
    project: ["./tsconfig.json", "./tsconfig.node.json"],
  },
  overrides: [
    {
      files: ["*.vue"],
+     parserOptions: {
+       parser: '@typescript-eslint/parser',
+     },
      extends: [
        "plugin:@typescript-eslint/disable-type-checked",
        "plugin:vue/vue3-recommended",
      ],
    },
  ],
};

Ref: https://typescript-eslint.io/linting/troubleshooting#i-am-running-into-errors-when-parsing-typescript-in-my-vue-files

@kuoruan
Copy link
Author

kuoruan commented Dec 29, 2023

Hi @auvred
Thank you for the information.
consistent-type-assertions works when set parser to @typescript-eslint/parser

In my original repo. I set paser to:

parser: {
    ts: "@typescript-eslint/parser",
    tsx: "@typescript-eslint/parser",
    js: "espree",
    jsx: "espree",
    "<template>": "@typescript-eslint/parser",
 },

It will get the same error.
Maybe it is a issue of vue-eslint-parser?

@kuoruan
Copy link
Author

kuoruan commented Dec 29, 2023

But only consistent-type-assertions shows this error. Every thing works when I disable it.

@auvred
Copy link
Member

auvred commented Dec 29, 2023

It's because consistent-type-assertions requires parser services to be generated

const parserServices = getParserServices(context, true);

@bradzacher bradzacher added the vue issues relating to vue support label Dec 29, 2023
@bradzacher
Copy link
Member

https://typescript-eslint.io/linting/typed-linting

here are our docs on type-aware linting.
consistent-type-assertions is a type-aware rule and thus requires the config mentioned by the docs and by auvred already.

if you're struggling to configure our parser with vue-eslint-parser - consider reaching out to that project as they have a non-standard way of integrating with other parsers.

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2023
@bradzacher bradzacher added fix: user error issue was fixed by correcting the configuration / correcting the code and removed triage Waiting for team members to take a look labels Dec 29, 2023
@kuoruan
Copy link
Author

kuoruan commented Dec 29, 2023

related to vuejs/vue-eslint-parser#104

set parser.js to @typescript-eslint/parser fixed this.

because the parser.js is returned by default
https://github.com/vuejs/vue-eslint-parser/blob/92dd3f306c30b3991a81eaca519286bc97b5f7ba/src/common/parser-options.ts#L97

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working fix: user error issue was fixed by correcting the configuration / correcting the code package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin vue issues relating to vue support
Projects
None yet
Development

No branches or pull requests

3 participants