Skip to content

Commit 4a7a550

Browse files
author
Andy
authored
moveToNewFile: Reuse code from importFixes for inserting import (#24957)
* moveToNewFile: Reuse code from importFixes for inserting import * Fix test failures * Update API baselines (#24966)
1 parent 4db1c13 commit 4a7a550

File tree

7 files changed

+21
-14
lines changed

7 files changed

+21
-14
lines changed

src/services/codefixes/importFixes.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ namespace ts.codefix {
194194

195195
function getCodeActionForNewImport(context: SymbolContext & { preferences: UserPreferences }, { moduleSpecifier, importKind }: NewImportInfo): CodeFixAction {
196196
const { sourceFile, symbolName, preferences } = context;
197-
const lastImportDeclaration = findLast(sourceFile.statements, isAnyImportSyntax);
198197

199198
const moduleSpecifierWithoutQuotes = stripQuotes(moduleSpecifier);
200199
const quotedModuleSpecifier = makeStringLiteral(moduleSpecifierWithoutQuotes, getQuotePreference(sourceFile, preferences));
@@ -210,14 +209,7 @@ namespace ts.codefix {
210209
createIdentifier(symbolName),
211210
createExternalModuleReference(quotedModuleSpecifier));
212211

213-
const changes = ChangeTracker.with(context, changeTracker => {
214-
if (lastImportDeclaration) {
215-
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
216-
}
217-
else {
218-
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
219-
}
220-
});
212+
const changes = ChangeTracker.with(context, t => insertImport(t, sourceFile, importDecl));
221213

222214
// if this file doesn't have any import statements, insert an import statement and then insert a new line
223215
// between the only import statement and user code. Otherwise just insert the statement because chances

src/services/refactors/moveToNewFile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ namespace ts.refactor {
121121
const quotePreference = getQuotePreference(oldFile, preferences);
122122
const importsFromNewFile = createOldFileImportsFromNewFile(usage.oldFileImportsFromNewFile, newModuleName, useEs6ModuleSyntax, quotePreference);
123123
if (importsFromNewFile) {
124-
changes.insertNodeBefore(oldFile, oldFile.statements[0], importsFromNewFile, /*blankLineBetween*/ true);
124+
insertImport(changes, oldFile, importsFromNewFile);
125125
}
126126

127127
deleteUnusedOldImports(oldFile, toMove.all, changes, usage.unusedImportsFromOldFile, checker);

src/services/utilities.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,17 @@ namespace ts {
13791379
return textSpanContainsPosition(span, node.getStart(file)) &&
13801380
node.getEnd() <= textSpanEnd(span);
13811381
}
1382+
1383+
/* @internal */
1384+
export function insertImport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importDecl: Statement): void {
1385+
const lastImportDeclaration = findLast(sourceFile.statements, isAnyImportSyntax);
1386+
if (lastImportDeclaration) {
1387+
changes.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
1388+
}
1389+
else {
1390+
changes.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
1391+
}
1392+
}
13821393
}
13831394

13841395
// Display-part writer helpers

tests/baselines/reference/api/tsserverlibrary.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10610,6 +10610,7 @@ declare namespace ts {
1061010610
some(pred: (node: Node) => boolean): boolean;
1061110611
}
1061210612
function getParentNodeInSpan(node: Node | undefined, file: SourceFile, span: TextSpan): Node | undefined;
10613+
function insertImport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importDecl: Statement): void;
1061310614
}
1061410615
declare namespace ts {
1061510616
function isFirstDeclarationOfSymbolParameter(symbol: Symbol): boolean;

tests/baselines/reference/api/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10790,6 +10790,7 @@ declare namespace ts {
1079010790
some(pred: (node: Node) => boolean): boolean;
1079110791
}
1079210792
function getParentNodeInSpan(node: Node | undefined, file: SourceFile, span: TextSpan): Node | undefined;
10793+
function insertImport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importDecl: Statement): void;
1079310794
}
1079410795
declare namespace ts {
1079510796
function isFirstDeclarationOfSymbolParameter(symbol: Symbol): boolean;

tests/cases/fourslash/moveToNewFile.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

33
// @Filename: /a.ts
4+
////// header comment
5+
////
46
////import './foo';
57
////import { a, b, alreadyUnused } from './other';
68
////const p = 0;
@@ -10,10 +12,11 @@
1012
verify.moveToNewFile({
1113
newFileContents: {
1214
"/a.ts":
13-
`import { y } from './y';
15+
`// header comment
1416
1517
import './foo';
1618
import { a, alreadyUnused } from './other';
19+
import { y } from './y';
1720
export const p = 0;
1821
a; y;`,
1922

tests/cases/fourslash/moveToNewFile_inferQuoteStyle.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
verify.moveToNewFile({
1010
newFileContents: {
1111
"/a.ts":
12-
`import { x } from './x';
13-
14-
import 'unrelated';
12+
`import 'unrelated';
13+
import { x } from './x';
1514
1615
x;`,
1716

0 commit comments

Comments
 (0)