don't insert before non-header

This commit is contained in:
Jesse Trinity
2020-08-05 15:34:58 -07:00
parent 45d9eb5e3c
commit d35d719dab
4 changed files with 37 additions and 4 deletions
+1 -1
View File
@@ -384,7 +384,7 @@ namespace ts.textChanges {
}
}
public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false, options = {}): void {
public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false, options: ConfigurableStartEnd = {}): void {
this.insertNodeAt(sourceFile, getAdjustedStartPosition(sourceFile, before, options), newNode, this.getOptionsForInsertNodeBefore(before, newNode, blankLineBetween));
}
+4 -3
View File
@@ -1913,9 +1913,10 @@ namespace ts {
for (const newImport of sortedNewImports) {
const insertionIndex = OrganizeImports.getImportDeclarationInsertionIndex(existingImportStatements, newImport);
if (insertionIndex === 0) {
changes.insertNodeBefore(sourceFile, existingImportStatements[0], newImport, /*blankLineBetween*/ false, {
leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude,
});
// If the first import is top-of-file, insert after the leading comment which is likely the header.
const options = existingImportStatements[0] === sourceFile.statements[0] ?
{ leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude } : {};
changes.insertNodeBefore(sourceFile, existingImportStatements[0], newImport, /*blankLineBetween*/ false, options);
}
else {
const prevImport = existingImportStatements[insertionIndex - 1];
@@ -0,0 +1,32 @@
/// <reference path="fourslash.ts" />
// @Filename: /a.ts
////export const foo = 0;
// @Filename: /b.ts
////export const bar = 0;
// @Filename: /c.ts
/////*--------------------
//// * Copyright Header
//// *--------------------*/
////
////const afterHeader = 1;
////
////// non-header comment
////import { bar } from "./b";
////foo;
goTo.file("/c.ts");
verify.importFixAtPosition([
`/*--------------------
* Copyright Header
*--------------------*/
const afterHeader = 1;
import { foo } from "./a";
// non-header comment
import { bar } from "./b";
foo;`,
]);