diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 2adcba0a9eec0..291dbdf55144f 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -700,7 +700,7 @@ namespace ts.codefix { } else if (existingSpecifiers?.length) { for (const spec of newSpecifiers) { - changes.insertNodeAtEndOfList(sourceFile, existingSpecifiers, spec); + changes.insertNodeInListAfter(sourceFile, last(existingSpecifiers), spec, existingSpecifiers); } } else { diff --git a/tests/cases/fourslash/importNameCodeFix_trailingComma.ts b/tests/cases/fourslash/importNameCodeFix_trailingComma.ts new file mode 100644 index 0000000000000..2717ff10822fb --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFix_trailingComma.ts @@ -0,0 +1,24 @@ +/// + +// Bug #40219 only happens when existing import specifiers are unsorted. + +// @Filename: index.ts +//// import { +//// T2, +//// T1, +//// } from "./types"; +//// +//// const x: T3/**/ + +// @Filename: types.ts +//// export type T1 = 0; +//// export type T2 = 0; +//// export type T3 = 0; + +verify.importFixAtPosition([`import { + T2, + T1, + T3, +} from "./types"; + +const x: T3`]);