From 65b84e707e118b802ad41ca164debd15f4915a95 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Mon, 14 Sep 2020 20:08:51 -0400 Subject: [PATCH] Cherry-pick PR #40273 into release-4.0 (#40303) Component commits: 2c0e01aa94 Fix misplaced comma in auto imported specifier Co-authored-by: Andrew Branch --- src/services/codefixes/importFixes.ts | 2 +- .../importNameCodeFix_trailingComma.ts | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/importNameCodeFix_trailingComma.ts 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`]);