Skip to content

Commit 046c5ea

Browse files
author
Andy
authored
moveToNewFile: Ignore reference to global (microsoft#24651)
1 parent 779fa98 commit 046c5ea

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/services/refactors/moveToNewFile.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ namespace ts.refactor {
404404
if (isInImport(decl)) {
405405
oldImportsNeededByNewFile.add(symbol);
406406
}
407-
else if (isTopLevelDeclaration(decl) && !movedSymbols.has(symbol)) {
407+
else if (isTopLevelDeclaration(decl) && sourceFileOfTopLevelDeclaration(decl) === oldFile && !movedSymbols.has(symbol)) {
408408
newFileImportsFromOldFile.add(symbol);
409409
}
410410
}
@@ -546,7 +546,11 @@ namespace ts.refactor {
546546
interface TopLevelVariableDeclaration extends VariableDeclaration { parent: VariableDeclarationList & { parent: VariableStatement; }; }
547547
type TopLevelDeclaration = NonVariableTopLevelDeclaration | TopLevelVariableDeclaration;
548548
function isTopLevelDeclaration(node: Node): node is TopLevelDeclaration {
549-
return isNonVariableTopLevelDeclaration(node) || isVariableDeclaration(node) && isSourceFile(node.parent.parent.parent);
549+
return isNonVariableTopLevelDeclaration(node) && isSourceFile(node.parent) || isVariableDeclaration(node) && isSourceFile(node.parent.parent.parent);
550+
}
551+
552+
function sourceFileOfTopLevelDeclaration(node: TopLevelDeclaration): Node {
553+
return isVariableDeclaration(node) ? node.parent.parent.parent : node.parent;
550554
}
551555

552556
function isTopLevelDeclarationStatement(node: Node): node is TopLevelDeclarationStatement {

tests/cases/fourslash/moveToNewFile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////import './foo';
55
////import { a, b, alreadyUnused } from './other';
66
////const p = 0;
7-
////[|const y = p + b;|]
7+
////[|const y: Date = p + b;|]
88
////a; y;
99

1010
verify.moveToNewFile({
@@ -20,7 +20,7 @@ a; y;`,
2020
"/y.ts":
2121
`import { b } from './other';
2222
import { p } from './a';
23-
export const y = p + b;`,
23+
export const y: Date = p + b;`,
2424
},
2525

2626
preferences: {

0 commit comments

Comments
 (0)