Skip to content

Presence of underscore prefixed item in sorted import list causes auto-import to place new import at the end #51616

Closed
@jakebailey

Description

@jakebailey

After #51565 and #51579, I noticed that auto-import still sometimes puts new things on the bottom. It turns out that it seems to only happen in files that import __String. Given this test:

/// <reference path="fourslash.ts" />

// @Filename: /a.ts
////export interface HasBar { bar: number }
////export function hasBar(x: unknown): x is HasBar { return x && typeof x.bar === "number" }
////export function foo() {}
////export type __String = string;

// @Filename: /b.ts
////import { __String, HasBar, hasBar } from "./a";
////f/**/;

verify.completions({
    marker: "",
    includes: {
        name: "foo",
        source: "/a",
        sourceDisplay: "./a",
        text: "function foo(): void",
        kind: "function",
        kindModifiers: "export",
        hasAction: true,
        sortText: completion.SortText.AutoImportSuggestions
    },
    preferences: { includeCompletionsForModuleExports: true },
});
verify.applyCodeActionFromCompletion("", {
    name: "foo",
    source: "/a",
    description: `Update import from "./a"`,
    newFileContent: `import { __String, foo, HasBar, hasBar } from "./a";
f;`,
});

The import gets placed at the end:

Expected:
import { __String, foo, HasBar, hasBar } from "./a";
f;

Actual:
import { __String, HasBar, hasBar, foo } from "./a";
f;

Remove the import of __String, and foo is placed in the correct place.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions