Skip to content

Commit 7b1620b

Browse files
TypeScript Bota-tarasyuk
TypeScript Bot
andauthored
🤖 Pick PR #58811 (fix(58801): "Move to file" on globa...) into release-5.5 (#58923)
Co-authored-by: Oleksandr T <[email protected]>
1 parent 5367ae1 commit 7b1620b

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

Diff for: ‎src/services/refactors/moveToFile.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
891891
const unusedImportsFromOldFile = new Set<Symbol>();
892892
for (const statement of toMove) {
893893
forEachReference(statement, checker, (symbol, isValidTypeOnlyUseSite) => {
894-
if (!symbol.declarations) {
894+
if (!symbol.declarations || isGlobalType(checker, symbol)) {
895895
return;
896896
}
897897
if (existingTargetLocals.has(skipAlias(symbol, checker))) {
@@ -952,6 +952,10 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
952952
}
953953
}
954954

955+
function isGlobalType(checker: TypeChecker, symbol: Symbol) {
956+
return !!checker.resolveName(symbol.name, /*location*/ undefined, SymbolFlags.Type, /*excludeGlobals*/ false);
957+
}
958+
955959
function makeUniqueFilename(proposedFilename: string, extension: string, inDirectory: string, host: LanguageServiceHost): string {
956960
let newFilename = proposedFilename;
957961
for (let i = 1;; i++) {

Diff for: ‎tests/cases/fourslash/moveToNewFile_global2.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////interface String {
5+
//// reverse(): string;
6+
////}
7+
////
8+
////[|String.prototype.reverse = function (): string {
9+
//// return this.split("").reverse().join("");
10+
////}|]
11+
12+
verify.moveToNewFile({
13+
newFileContents: {
14+
"/a.ts":
15+
`interface String {
16+
reverse(): string;
17+
}
18+
19+
`,
20+
"/newFile.ts":
21+
`String.prototype.reverse = function(): string {
22+
return this.split("").reverse().join("");
23+
};
24+
`,
25+
}
26+
});

Diff for: ‎tests/cases/fourslash/moveToNewFile_global3.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////[|// this file extends the string prototype
5+
////interface String {
6+
//// reverse(): string;
7+
////}
8+
////String.prototype.reverse = function(): string {
9+
//// return this.split("").reverse().join("");
10+
////};|]
11+
12+
verify.moveToNewFile({
13+
newFileContents: {
14+
"/a.ts": "",
15+
"/String.ts":
16+
`// this file extends the string prototype
17+
interface String {
18+
reverse(): string;
19+
}
20+
String.prototype.reverse = function(): string {
21+
return this.split("").reverse().join("");
22+
};
23+
`,
24+
}
25+
});

0 commit comments

Comments
 (0)