Skip to content

Commit 82234cf

Browse files
author
Andy Hanson
committed
moveToNewFile: Reuse code from importFixes for inserting import
1 parent c437404 commit 82234cf

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
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 = createLiteral(moduleSpecifierWithoutQuotes, shouldUseSingleQuote(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
@@ -120,7 +120,7 @@ namespace ts.refactor {
120120
const useEs6ModuleSyntax = !!oldFile.externalModuleIndicator;
121121
const importsFromNewFile = createOldFileImportsFromNewFile(usage.oldFileImportsFromNewFile, newModuleName, useEs6ModuleSyntax, preferences);
122122
if (importsFromNewFile) {
123-
changes.insertNodeBefore(oldFile, oldFile.statements[0], importsFromNewFile, /*blankLineBetween*/ true);
123+
insertImport(changes, oldFile, importsFromNewFile);
124124
}
125125

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

src/services/utilities.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,16 @@ namespace ts {
13631363
return textSpanContainsPosition(span, node.getStart(file)) &&
13641364
node.getEnd() <= textSpanEnd(span);
13651365
}
1366+
1367+
export function insertImport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importDecl: Statement): void {
1368+
const lastImportDeclaration = findLast(sourceFile.statements, isAnyImportSyntax);
1369+
if (lastImportDeclaration) {
1370+
changes.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
1371+
}
1372+
else {
1373+
changes.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
1374+
}
1375+
}
13661376
}
13671377

13681378
// Display-part writer helpers

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

0 commit comments

Comments
 (0)