From 45d9eb5e3c3c0ca6ac502d6ae0e54b1d137aa836 Mon Sep 17 00:00:00 2001 From: Jesse Trinity Date: Wed, 5 Aug 2020 14:05:30 -0700 Subject: [PATCH] place first import after header --- src/services/textChanges.ts | 4 +-- src/services/utilities.ts | 4 ++- .../importNameCodeFix_HeaderComment.ts | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/importNameCodeFix_HeaderComment.ts diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index 023a34c05663f..41f0797ca8ef9 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -384,8 +384,8 @@ namespace ts.textChanges { } } - public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false): void { - this.insertNodeAt(sourceFile, getAdjustedStartPosition(sourceFile, before, {}), newNode, this.getOptionsForInsertNodeBefore(before, newNode, blankLineBetween)); + public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false, options = {}): void { + this.insertNodeAt(sourceFile, getAdjustedStartPosition(sourceFile, before, options), newNode, this.getOptionsForInsertNodeBefore(before, newNode, blankLineBetween)); } public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index c11c65d076dd6..216586c8f86ad 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1913,7 +1913,9 @@ namespace ts { for (const newImport of sortedNewImports) { const insertionIndex = OrganizeImports.getImportDeclarationInsertionIndex(existingImportStatements, newImport); if (insertionIndex === 0) { - changes.insertNodeBefore(sourceFile, existingImportStatements[0], newImport, /*blankLineBetween*/ false); + changes.insertNodeBefore(sourceFile, existingImportStatements[0], newImport, /*blankLineBetween*/ false, { + leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude, + }); } else { const prevImport = existingImportStatements[insertionIndex - 1]; diff --git a/tests/cases/fourslash/importNameCodeFix_HeaderComment.ts b/tests/cases/fourslash/importNameCodeFix_HeaderComment.ts new file mode 100644 index 0000000000000..d3263217b2faa --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFix_HeaderComment.ts @@ -0,0 +1,26 @@ +/// + +// @Filename: /a.ts +////export const foo = 0; + +// @Filename: /b.ts +////export const bar = 0; + +// @Filename: /c.ts +/////*-------------------- +//// * Copyright Header +//// *--------------------*/ +//// +////import { bar } from "./b"; +////foo; + +goTo.file("/c.ts"); +verify.importFixAtPosition([ +`/*-------------------- + * Copyright Header + *--------------------*/ + +import { foo } from "./a"; +import { bar } from "./b"; +foo;`, +]);