Skip to content

Commit 8c46867

Browse files
author
Andy Hanson
committed
Compute movedSymbols completely before using, and support export import
1 parent 2bb0760 commit 8c46867

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/services/refactors/moveToNewFile.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ namespace ts.refactor {
325325
forEachTopLevelDeclaration(statement, decl => {
326326
movedSymbols.add(Debug.assertDefined(isExpressionStatement(decl) ? checker.getSymbolAtLocation(decl.expression.left) : decl.symbol));
327327
});
328-
328+
}
329+
for (const statement of toMove) {
329330
forEachReference(statement, checker, symbol => {
330331
if (!symbol.declarations) return;
331332
for (const decl of symbol.declarations) {
@@ -460,7 +461,15 @@ namespace ts.refactor {
460461
}
461462

462463
type TopLevelExpressionStatement = ExpressionStatement & { expression: BinaryExpression & { left: PropertyAccessExpression } }; // 'exports.x = ...'
463-
type NonVariableTopLevelDeclaration = FunctionDeclaration | ClassDeclaration | EnumDeclaration | TypeAliasDeclaration | InterfaceDeclaration | ModuleDeclaration | TopLevelExpressionStatement;
464+
type NonVariableTopLevelDeclaration =
465+
| FunctionDeclaration
466+
| ClassDeclaration
467+
| EnumDeclaration
468+
| TypeAliasDeclaration
469+
| InterfaceDeclaration
470+
| ModuleDeclaration
471+
| TopLevelExpressionStatement
472+
| ImportEqualsDeclaration;
464473
type TopLevelDeclarationStatement = NonVariableTopLevelDeclaration | VariableStatement;
465474
interface TopLevelVariableDeclaration extends VariableDeclaration { parent: VariableDeclarationList & { parent: VariableStatement; }; }
466475
type TopLevelDeclaration = NonVariableTopLevelDeclaration | TopLevelVariableDeclaration;
@@ -481,6 +490,7 @@ namespace ts.refactor {
481490
case SyntaxKind.EnumDeclaration:
482491
case SyntaxKind.TypeAliasDeclaration:
483492
case SyntaxKind.InterfaceDeclaration:
493+
case SyntaxKind.ImportEqualsDeclaration:
484494
return true;
485495
default:
486496
return false;
@@ -495,7 +505,8 @@ namespace ts.refactor {
495505
case SyntaxKind.EnumDeclaration:
496506
case SyntaxKind.TypeAliasDeclaration:
497507
case SyntaxKind.InterfaceDeclaration:
498-
return cb(statement as FunctionDeclaration | ClassDeclaration | EnumDeclaration | ModuleDeclaration | TypeAliasDeclaration | InterfaceDeclaration);
508+
case SyntaxKind.ImportEqualsDeclaration:
509+
return cb(statement as FunctionDeclaration | ClassDeclaration | EnumDeclaration | ModuleDeclaration | TypeAliasDeclaration | InterfaceDeclaration | ImportEqualsDeclaration);
499510

500511
case SyntaxKind.VariableStatement:
501512
return forEach((statement as VariableStatement).declarationList.declarations as ReadonlyArray<TopLevelVariableDeclaration>, cb);
@@ -557,6 +568,8 @@ namespace ts.refactor {
557568
return updateTypeAliasDeclaration(d, d.decorators, modifiers, d.name, d.typeParameters, d.type);
558569
case SyntaxKind.InterfaceDeclaration:
559570
return updateInterfaceDeclaration(d, d.decorators, modifiers, d.name, d.typeParameters, d.heritageClauses, d.members);
571+
case SyntaxKind.ImportEqualsDeclaration:
572+
return updateImportEqualsDeclaration(d, d.decorators, modifiers, d.name, d.moduleReference);
560573
case SyntaxKind.ExpressionStatement:
561574
return Debug.fail(); // Shouldn't try to add 'export' keyword to `exports.x = ...`
562575
default:
@@ -577,6 +590,7 @@ namespace ts.refactor {
577590
case SyntaxKind.EnumDeclaration:
578591
case SyntaxKind.TypeAliasDeclaration:
579592
case SyntaxKind.InterfaceDeclaration:
593+
case SyntaxKind.ImportEqualsDeclaration:
580594
return undefined;
581595
case SyntaxKind.ExpressionStatement:
582596
return Debug.fail(); // Shouldn't try to add 'export' keyword to `exports.x = ...`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////namespace N { export const x = 0; }
5+
////[|import M = N;
6+
////export import O = N;|]
7+
////M;
8+
9+
verify.moveToNewFile({
10+
newFileContents: {
11+
"/a.ts":
12+
`import { M } from "./M";
13+
14+
export namespace N { export const x = 0; }
15+
M;`,
16+
17+
"/M.ts":
18+
`import { N } from "./a";
19+
export import M = N;
20+
export import O = N;`,
21+
},
22+
});

0 commit comments

Comments
 (0)