Skip to content

Commit ba63f49

Browse files
author
Andy
authored
moveToNewFile: Don't provide refactor if selection is just imports (microsoft#24336)
1 parent e53e56c commit ba63f49

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/services/refactors/moveToNewFile.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace ts.refactor {
33
const refactorName = "Move to a new file";
44
registerRefactor(refactorName, {
55
getAvailableActions(context): ApplicableRefactorInfo[] | undefined {
6-
if (!context.preferences.allowTextChangesInNewFiles || getFirstAndLastStatementToMove(context) === undefined) return undefined;
6+
if (!context.preferences.allowTextChangesInNewFiles || getStatementsToMove(context) === undefined) return undefined;
77
const description = getLocaleSpecificMessage(Diagnostics.Move_to_a_new_file);
88
return [{ name: refactorName, description, actions: [{ name: refactorName, description }] }];
99
},
@@ -15,7 +15,7 @@ namespace ts.refactor {
1515
}
1616
});
1717

18-
function getFirstAndLastStatementToMove(context: RefactorContext): { readonly first: number, readonly afterLast: number } | undefined {
18+
function getRangeToMove(context: RefactorContext): ReadonlyArray<Statement> | undefined {
1919
const { file } = context;
2020
const range = createTextRangeFromSpan(getRefactorContextSpan(context));
2121
const { statements } = file;
@@ -25,7 +25,7 @@ namespace ts.refactor {
2525

2626
const startStatement = statements[startNodeIndex];
2727
if (isNamedDeclaration(startStatement) && startStatement.name && rangeContainsRange(startStatement.name, range)) {
28-
return { first: startNodeIndex, afterLast: startNodeIndex + 1 };
28+
return [statements[startNodeIndex]];
2929
}
3030

3131
// Can't only partially include the start node or be partially into the next node
@@ -34,7 +34,7 @@ namespace ts.refactor {
3434
// Can't be partially into the next node
3535
if (afterEndNodeIndex !== -1 && (afterEndNodeIndex === 0 || statements[afterEndNodeIndex].getStart(file) < range.end)) return undefined;
3636

37-
return { first: startNodeIndex, afterLast: afterEndNodeIndex === -1 ? statements.length : afterEndNodeIndex };
37+
return statements.slice(startNodeIndex, afterEndNodeIndex === -1 ? statements.length : afterEndNodeIndex);
3838
}
3939

4040
function doChange(oldFile: SourceFile, program: Program, toMove: ToMove, changes: textChanges.ChangeTracker, host: LanguageServiceHost): void {
@@ -63,16 +63,15 @@ namespace ts.refactor {
6363

6464
// Filters imports out of the range of statements to move. Imports will be copied to the new file anyway, and may still be needed in the old file.
6565
function getStatementsToMove(context: RefactorContext): ToMove | undefined {
66-
const { statements } = context.file;
67-
const { first, afterLast } = getFirstAndLastStatementToMove(context)!;
66+
const rangeToMove = getRangeToMove(context);
67+
if (rangeToMove === undefined) return undefined;
6868
const all: Statement[] = [];
6969
const ranges: StatementRange[] = [];
70-
const rangeToMove = statements.slice(first, afterLast);
7170
getRangesWhere(rangeToMove, s => !isPureImport(s), (start, afterEnd) => {
7271
for (let i = start; i < afterEnd; i++) all.push(rangeToMove[i]);
7372
ranges.push({ first: rangeToMove[start], last: rangeToMove[afterEnd - 1] });
7473
});
75-
return { all, ranges };
74+
return all.length === 0 ? undefined : { all, ranges };
7675
}
7776

7877
function isPureImport(node: Node): boolean {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export {};
5+
////[|import foo from "./foo";|]
6+
7+
// No refactor available if every statement in the range is an import, since we don't move those.
8+
9+
verify.noMoveToNewFile();

0 commit comments

Comments
 (0)