Skip to content

moveToNewFile: Reuse code from importFixes for inserting import #24957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
3 commits merged into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion tests/cases/fourslash/moveToNewFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////// header comment
////
////import './foo';
////import { a, b, alreadyUnused } from './other';
////const p = 0;
Expand All @@ -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;`,

Expand Down
5 changes: 2 additions & 3 deletions tests/cases/fourslash/moveToNewFile_inferQuoteStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
verify.moveToNewFile({
newFileContents: {
"/a.ts":
`import { x } from './x';

import 'unrelated';
`import 'unrelated';
import { x } from './x';

x;`,

Expand Down