microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.7k stars 12.45k forks source link

Organize imports preferences don't seem to work #59942

Open mjbvz opened 1 month ago

mjbvz commented 1 month ago

🔎 Search Terms

🕗 Version & Regression Information

5.6, not a regression

⏯ Playground Link

No response

💻 Code

  1. In VS Code set:

    "typescript.preferences.organizeImports": {
    "caseFirst": "upper"
    },
  2. Open a TS file:

    import './a';
    import './A';
  3. Run organize imports

🙁 Actual behavior

No change

🙂 Expected behavior

Should sort imports so upper case comes first?

I confirmed that we send along "organizeImportsCaseFirst": "upper" in the configure request for the file

None of the other organize imports settings seems to work either

Additional information about the issue

No response

grallbring commented 5 days ago

Can confirm, using the typeOrder setting also doesn't seem to work.

"typescript.preferences.organizeImports": {
    "typeOrder": "first"
}

Using this setting, type-only imports are not sorted to be first.

jakebailey commented 3 days ago

When was this option added? I thought this had to be like: https://github.com/microsoft/TypeScript/blob/460be92510abf07b7fb87e1d2a6649823805e195/.vscode/settings.template.json#L25-L38

iisaduan commented 3 days ago

@jakebailey The way they are entered into VScode's typescript settings (aka the non-unstable version) is different. They were added to VSCode over the summer (I think August?).

@grallbring In the meantime while I am investigating why the vscode preferences don't work/fixing this issue, specifying the settings under unstable (as mentioned in Jake's comment) should work, and I believe the unstable settings should override any of the non-working settings.

grallbring commented 3 days ago

@iisaduan These unstable settings do not seem to have any effect whatsoever.

"typescript.unstable": {
    "typeOrder": "first"
}

Using this setting, type-only imports are mixed with the other imports like before.

VS Code also gives me this hint when using the unstable setting: Image

I'm using version 1.94.2.

iisaduan commented 3 days ago

@grallbring The input for the unstable are under different names, since they are directly passed to our preferences object. For type order first, this is the setting you should include. You'll also need a tsserver version of >5.4.

"typescript.unstable": { 
     "organizeImportsTypeOrder": "first" 
}

(Perhaps I can look into making the vscode names the same. IIRC the reason we didn't do this in the first place was because of how vscode groups settings, but maybe this is not a necessity for organize imports)

grallbring commented 3 days ago

@iisaduan I see. I've adjusted the setting:

"typescript.unstable": {
    "organizeImportsTypeOrder": "first"
}

However, the issue remains. Is the tsserver version the same as the typescript version my project uses? I'm using "typescript": "^5.6.3",.

Just to make sure - the setting should make it so that the type import is appearing first in this list, correct?

import DefaultLayout from "@/layouts/DefaultLayout";
import IndexPage from "@/pages/IndexPage";
import { MantineProvider } from "@mantine/core";
import "@mantine/core/styles.css";
import type { Router } from "@remix-run/router"; // <-- this one should be first when saving the file, right?
import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
iisaduan commented 3 days ago

@grallbring It's supposed to apply to only named imports:

import { type Router, type T, A, b, y, Z } from "@remix-run/router"; 

The order of declarations are sorted by module specifier, and if there are multiple declarations with different specifiers, then we tie-break by type of import declaration . IIRC, this ordering is to keep compatibility with ESlint and other formatters.

iisaduan commented 3 days ago

@grallbring The tsserver version, by default, is the one that your project uses, but it is possible to specify a different version using the vscode setting "typescript.tsdk" https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript

grallbring commented 3 days ago

@iisaduan Understood, then I misunderstood the purpose of the setting. Thanks for the detailed explanation!