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:
parent
4db1c132b7
commit
4a7a550502
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;`,
|
||||
|
||||
|
|
|
@ -9,9 +9,8 @@
|
|||
verify.moveToNewFile({
|
||||
newFileContents: {
|
||||
"/a.ts":
|
||||
`import { x } from './x';
|
||||
|
||||
import 'unrelated';
|
||||
`import 'unrelated';
|
||||
import { x } from './x';
|
||||
|
||||
x;`,
|
||||
|
||||
|
|
Loading…
Reference in a new issue