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: Enable consistent-type-imports #1832

Closed
wants to merge 2 commits into from

Conversation

auscompgeek
Copy link
Member

@auscompgeek auscompgeek commented Aug 25, 2023

Revives #872.

It might be a good idea to enable import/no-duplicates first though.

https://typescript-eslint.io/rules/consistent-type-imports/

🥞

Checklist

Copy link
Member

@pokey pokey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had one question

Also, re running deduplicate, do we not want to run deduplicate after this one rather than before, due to interaction with import-js/eslint-plugin-import#334. Not totally sure tbh 🤔

Comment on lines +1 to +2
import type { CheatsheetInfo } from "@cursorless/cheatsheet";
import { CheatsheetPage } from "@cursorless/cheatsheet";
Copy link
Member

@pokey pokey Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to do this in one line

Suggested change
import type { CheatsheetInfo } from "@cursorless/cheatsheet";
import { CheatsheetPage } from "@cursorless/cheatsheet";
import { type CheatsheetInfo, CheatsheetPage } from "@cursorless/cheatsheet";

Is there a way to get it to do that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh looks like there are 4 different lint rules / options related to this question:

I'd be tempted to have one PR that gets us to the configuration we're after in one go, rather than multiple PRs that touch all of our imports, given that I'd imagine they could interact in interesting ways

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@auscompgeek maybe let's aim to discuss this inline type thing at a meet-up? to discuss Plan to discuss at meet-up

Copy link
Member

@pokey pokey Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion, we want to:

  1. enable "@typescript-eslint/consistent-type-imports": "error"
  2. enable --verbatimModuleSyntax TS flag
  3. enable "import/consistent-type-specifier-style": ["error", "prefer-top-level"]
  4. enable "import/no-duplicates": "error" (note we don't need prefer-inline because we don't use inline)

The reasoning is that we want import type to be like its own kind of statement that we know always gets dropped in the output Javascript, and in addition, if we were to have a rule where import {type foo} => import type {foo} only when there are no non-type imports in the statement, it would feel a bit inconsistent that it doesn't happen when there are non-type imports. The proposed rule is simpler: types always get imported in their own statement, and never have side effects

Each step should be its own PR, and they should be in the above order. Note that 1) must come before 2) because without 2), 1) should have no side effects, and then the effects of 2) should become more clear once 1) is in

Note that we don't need no-import-type-side-effects lint rule because 2 and 3 won't allow us to have inline type anywhere

Copy link
Member

@pokey pokey Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to test the following cases:

  • you use something as a type but not runtime and it's currently imported as runtime
  • you use something as a type that is not yet imported
  • you use something as runtime that is currently imported as a type

For each of the above cases, we want to test:

  • Auto-fix
  • Organize imports
  • Auto-import

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still never got a chance to test that matrix, but up until TypeScript 5.3 whether auto-import used import type depends on the tsconfig. TypeScript 5.3 adds an IDE setting to prefer auto-importing with import type https://devblogs.microsoft.com/typescript/announcing-typescript-5-3/#settings-to-prefer-type-auto-imports

The presence of this lint rule will obviously not affect auto-import behaviour since eslint doesn't change the tsconfig. The auto-import behaviour probably isn't too big of an issue anyway since we all have eslint autofix on save, I believe.

@pokey pokey mentioned this pull request Aug 25, 2023
3 tasks
Base automatically changed from auscompgeek/isolated-modules to main September 1, 2023 13:20
@auscompgeek auscompgeek force-pushed the auscompgeek/eslint-type-imports branch from ae4b9bc to 206c9ca Compare September 1, 2023 13:46
@pokey pokey added the to discuss Plan to discuss at meet-up label Sep 15, 2023
@pokey pokey removed the to discuss Plan to discuss at meet-up label Sep 28, 2023
@pokey pokey self-assigned this Jun 20, 2024
@pokey pokey added the 30 mins PRs that are very close and just need a couple quick tweaks to merge; maintainer may take it label Aug 1, 2024
@pokey
Copy link
Member

pokey commented Aug 1, 2024

Note from meet-up:

@pokey pokey removed their assignment Aug 1, 2024
@AndreasArvidsson AndreasArvidsson self-assigned this Aug 1, 2024
@AndreasArvidsson
Copy link
Member

Replaced by #2619

github-merge-queue bot pushed a commit that referenced this pull request Aug 2, 2024
related #2618
revives #1832

vscode quick fix import and auto import appears to be doing the right
thing in regards to type only or runtime imports. Imports organize or
imports fix unfortunately do not convert a runtime import to a type
import if it's only used as a type. We probably need to rely on the
linter to do that part for us.

## Checklist

- [/] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [/] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [/] I have not broken the cheatsheet

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
cursorless-bot pushed a commit to hands-free-vim/cursorless.nvim that referenced this pull request Aug 2, 2024
related #2618
revives cursorless-dev/cursorless#1832

vscode quick fix import and auto import appears to be doing the right
thing in regards to type only or runtime imports. Imports organize or
imports fix unfortunately do not convert a runtime import to a type
import if it's only used as a type. We probably need to rely on the
linter to do that part for us.

## Checklist

- [/] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [/] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [/] I have not broken the cheatsheet

---------

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
@auscompgeek auscompgeek deleted the auscompgeek/eslint-type-imports branch August 5, 2024 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
30 mins PRs that are very close and just need a couple quick tweaks to merge; maintainer may take it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants