Skip to content

Commit e3301f7

Browse files
jessetrinitytypescript-bot
authored andcommitted
Cherry-pick PR #39924 into release-4.0
Component commits: 45d9eb5 place first import after header d35d719 don't insert before non-header
1 parent 65b84e7 commit e3301f7

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

src/services/textChanges.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ namespace ts.textChanges {
384384
}
385385
}
386386

387-
public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false): void {
388-
this.insertNodeAt(sourceFile, getAdjustedStartPosition(sourceFile, before, {}), newNode, this.getOptionsForInsertNodeBefore(before, newNode, blankLineBetween));
387+
public insertNodeBefore(sourceFile: SourceFile, before: Node, newNode: Node, blankLineBetween = false, options: ConfigurableStartEnd = {}): void {
388+
this.insertNodeAt(sourceFile, getAdjustedStartPosition(sourceFile, before, options), newNode, this.getOptionsForInsertNodeBefore(before, newNode, blankLineBetween));
389389
}
390390

391391
public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {

src/services/utilities.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,10 @@ namespace ts {
19131913
for (const newImport of sortedNewImports) {
19141914
const insertionIndex = OrganizeImports.getImportDeclarationInsertionIndex(existingImportStatements, newImport);
19151915
if (insertionIndex === 0) {
1916-
changes.insertNodeBefore(sourceFile, existingImportStatements[0], newImport, /*blankLineBetween*/ false);
1916+
// If the first import is top-of-file, insert after the leading comment which is likely the header.
1917+
const options = existingImportStatements[0] === sourceFile.statements[0] ?
1918+
{ leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude } : {};
1919+
changes.insertNodeBefore(sourceFile, existingImportStatements[0], newImport, /*blankLineBetween*/ false, options);
19171920
}
19181921
else {
19191922
const prevImport = existingImportStatements[insertionIndex - 1];
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
////export const foo = 0;
5+
6+
// @Filename: /b.ts
7+
////export const bar = 0;
8+
9+
// @Filename: /c.ts
10+
/////*--------------------
11+
//// * Copyright Header
12+
//// *--------------------*/
13+
////
14+
////import { bar } from "./b";
15+
////foo;
16+
17+
goTo.file("/c.ts");
18+
verify.importFixAtPosition([
19+
`/*--------------------
20+
* Copyright Header
21+
*--------------------*/
22+
23+
import { foo } from "./a";
24+
import { bar } from "./b";
25+
foo;`,
26+
]);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.ts
4+
////export const foo = 0;
5+
6+
// @Filename: /b.ts
7+
////export const bar = 0;
8+
9+
// @Filename: /c.ts
10+
/////*--------------------
11+
//// * Copyright Header
12+
//// *--------------------*/
13+
////
14+
////const afterHeader = 1;
15+
////
16+
////// non-header comment
17+
////import { bar } from "./b";
18+
////foo;
19+
20+
goTo.file("/c.ts");
21+
verify.importFixAtPosition([
22+
`/*--------------------
23+
* Copyright Header
24+
*--------------------*/
25+
26+
const afterHeader = 1;
27+
28+
import { foo } from "./a";
29+
// non-header comment
30+
import { bar } from "./b";
31+
foo;`,
32+
]);

0 commit comments

Comments
 (0)