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

Bug: [consistent-type-imports] Attempts to fix the wrong import statement #7209

Closed
4 tasks done
OpportunityLiu opened this issue Jul 12, 2023 · 4 comments · Fixed by #8308
Closed
4 tasks done

Bug: [consistent-type-imports] Attempts to fix the wrong import statement #7209

OpportunityLiu opened this issue Jul 12, 2023 · 4 comments · Fixed by #8308
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working

Comments

@OpportunityLiu
Copy link

OpportunityLiu commented Jul 12, 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.

Issue Description

@typescript-eslint/consistent-type-imports went wrong with side effect import and type import.

$ /workspaces/consistent-type-imports-repro/node_modules/.bin/eslint baz.ts

Oops! Something went wrong! :(

ESLint: 8.44.0

Error: Non-null Assertion Failed: Expected to find a } for the ImportDeclaration.
Occurred while linting /workspaces/consistent-type-imports-repro/baz.ts:1
Rule: "@typescript-eslint/consistent-type-imports"
    at Object.nullThrows (/workspaces/consistent-type-imports-repro/node_modules/@typescript-eslint/utils/dist/eslint-utils/nullThrows.js:23:15)
    at fixInsertNamedSpecifiersInNamedSpecifierList (/workspaces/consistent-type-imports-repro/node_modules/@typescript-eslint/eslint-plugin/dist/rules/consistent-type-imports.js:406:44)
    at fixToValueImportDeclaration (/workspaces/consistent-type-imports-repro/node_modules/@typescript-eslint/eslint-plugin/dist/rules/consistent-type-imports.js:614:55)
    at fixToValueImportDeclaration.next (<anonymous>)
    at Object.fix (/workspaces/consistent-type-imports-repro/node_modules/@typescript-eslint/eslint-plugin/dist/rules/consistent-type-imports.js:262:56)
    at fix.next (<anonymous>)
    at Function.from (<anonymous>)
    at normalizeFixes (/workspaces/consistent-type-imports-repro/node_modules/eslint/lib/linter/report-translator.js:182:33)
    at /workspaces/consistent-type-imports-repro/node_modules/eslint/lib/linter/report-translator.js:349:49
    at Object.report (/workspaces/consistent-type-imports-repro/node_modules/eslint/lib/linter/linter.js:1021:41)
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Reproduction Repository Link

https://github.com/OpportunityLiu/consistent-type-imports-repro

Repro Steps

  1. clone the repo
  2. yarn install
  3. yarn eslint baz.ts

Versions

package version
@typescript-eslint/eslint-plugin 6.0.0
@typescript-eslint/parser 6.0.0
@typescript-eslint/scope-manager 6.0.0
@typescript-eslint/typescript-estree 6.0.0
@typescript-eslint/type-utils 6.0.0
@typescript-eslint/utils 6.0.0
TypeScript 5.1.6
ESLint 8.44.0
node 18.16.1
@OpportunityLiu OpportunityLiu added bug Something isn't working triage Waiting for maintainers to take a look labels Jul 12, 2023
@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Jul 12, 2023
@JoshuaKGoldberg
Copy link
Member

Fun! And thanks for using 6.0.0 - though we should note this also reproduces on v5. Going back through version histories:

Version Works?
5.53
5.52
5.51
5.50

@rubiesonthesky
Copy link
Contributor

Is this little bit same as what @bradzacher commented to another issue #7214 (comment):

Realistically you shouldn't be using both verbatimModuleSyntax and consistent-type-imports though.

I tried to check if there are any tests cases with verbatimModuleSyntax but I couldn't find any in code base. Maybe it would be good in future to add some tests that would check how rules behave with it?

@Josh-Cena
Copy link
Member

Josh-Cena commented Dec 17, 2023

For people's information: this rule in general doesn't handle duplicate imports very well. In this code:

import "./foo-bar.js";
import type { Foo, Bar } from "./foo-bar.js";

export class Baz {
  @console.log bar!: Bar;
}

It wants to fix the second import, but it locates the first import statement instead, which has no } between import and "./foo-bar.js" indeed. We need to do a lot of tweaking to how sourceImports works; I don't know why it only registers typeOnlyNamedImport, valueOnlyNamedImport, and valueImport once, as if you can't have multiple import statements from the same source...

@Josh-Cena Josh-Cena changed the title Bug: [@typescript-eslint/consistent-type-imports] Error: Non-null Assertion Failed: Expected to find a } for the ImportDeclaration. Bug: [consistent-type-imports] Attempts to fix the wrong import statemeent Dec 17, 2023
@Josh-Cena Josh-Cena changed the title Bug: [consistent-type-imports] Attempts to fix the wrong import statemeent Bug: [consistent-type-imports] Attempts to fix the wrong import statement Dec 17, 2023
@JoshuaKGoldberg
Copy link
Member

Re verbatimModuleSyntax: #8335 handles that can of worms. 🪱

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working
Projects
None yet
4 participants