moveToNewFile: Reuse code from importFixes for inserting import (#24957)

* moveToNewFile: Reuse code from importFixes for inserting import

* Fix test failures

* Update API baselines (#24966)
This commit is contained in:
Andy 2018-06-14 15:41:47 -07:00 committed by GitHub
parent 4db1c132b7
commit 4a7a550502
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 14 deletions

View file

@ -194,7 +194,6 @@ namespace ts.codefix {
function getCodeActionForNewImport(context: SymbolContext & { preferences: UserPreferences }, { moduleSpecifier, importKind }: NewImportInfo): CodeFixAction {
const { sourceFile, symbolName, preferences } = context;
const lastImportDeclaration = findLast(sourceFile.statements, isAnyImportSyntax);
const moduleSpecifierWithoutQuotes = stripQuotes(moduleSpecifier);
const quotedModuleSpecifier = makeStringLiteral(moduleSpecifierWithoutQuotes, getQuotePreference(sourceFile, preferences));
@ -210,14 +209,7 @@ namespace ts.codefix {
createIdentifier(symbolName),
createExternalModuleReference(quotedModuleSpecifier));
const changes = ChangeTracker.with(context, changeTracker => {
if (lastImportDeclaration) {
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
}
else {
changeTracker.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
}
});
const changes = ChangeTracker.with(context, t => insertImport(t, sourceFile, importDecl));
// if this file doesn't have any import statements, insert an import statement and then insert a new line
// between the only import statement and user code. Otherwise just insert the statement because chances

View file

@ -121,7 +121,7 @@ namespace ts.refactor {
const quotePreference = getQuotePreference(oldFile, preferences);
const importsFromNewFile = createOldFileImportsFromNewFile(usage.oldFileImportsFromNewFile, newModuleName, useEs6ModuleSyntax, quotePreference);
if (importsFromNewFile) {
changes.insertNodeBefore(oldFile, oldFile.statements[0], importsFromNewFile, /*blankLineBetween*/ true);
insertImport(changes, oldFile, importsFromNewFile);
}
deleteUnusedOldImports(oldFile, toMove.all, changes, usage.unusedImportsFromOldFile, checker);

View file

@ -1379,6 +1379,17 @@ namespace ts {
return textSpanContainsPosition(span, node.getStart(file)) &&
node.getEnd() <= textSpanEnd(span);
}
/* @internal */
export function insertImport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importDecl: Statement): void {
const lastImportDeclaration = findLast(sourceFile.statements, isAnyImportSyntax);
if (lastImportDeclaration) {
changes.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl);
}
else {
changes.insertNodeAtTopOfFile(sourceFile, importDecl, /*blankLineBetween*/ true);
}
}
}
// Display-part writer helpers

View file

@ -10610,6 +10610,7 @@ declare namespace ts {
some(pred: (node: Node) => boolean): boolean;
}
function getParentNodeInSpan(node: Node | undefined, file: SourceFile, span: TextSpan): Node | undefined;
function insertImport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importDecl: Statement): void;
}
declare namespace ts {
function isFirstDeclarationOfSymbolParameter(symbol: Symbol): boolean;

View file

@ -10790,6 +10790,7 @@ declare namespace ts {
some(pred: (node: Node) => boolean): boolean;
}
function getParentNodeInSpan(node: Node | undefined, file: SourceFile, span: TextSpan): Node | undefined;
function insertImport(changes: textChanges.ChangeTracker, sourceFile: SourceFile, importDecl: Statement): void;
}
declare namespace ts {
function isFirstDeclarationOfSymbolParameter(symbol: Symbol): boolean;

View file

@ -1,6 +1,8 @@
/// <reference path='fourslash.ts' />
// @Filename: /a.ts
////// header comment
////
////import './foo';
////import { a, b, alreadyUnused } from './other';
////const p = 0;
@ -10,10 +12,11 @@
verify.moveToNewFile({
newFileContents: {
"/a.ts":
`import { y } from './y';
`// header comment
import './foo';
import { a, alreadyUnused } from './other';
import { y } from './y';
export const p = 0;
a; y;`,

View file

@ -9,9 +9,8 @@
verify.moveToNewFile({
newFileContents: {
"/a.ts":
`import { x } from './x';
import 'unrelated';
`import 'unrelated';
import { x } from './x';
x;`,