Skip to content

🤖 Pick PR #58811 (fix(58801): "Move to file" on globa...) into release-5.5 #58923

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
merged 1 commit into from
Jun 19, 2024
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
6 changes: 5 additions & 1 deletion src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
const unusedImportsFromOldFile = new Set<Symbol>();
for (const statement of toMove) {
forEachReference(statement, checker, (symbol, isValidTypeOnlyUseSite) => {
if (!symbol.declarations) {
if (!symbol.declarations || isGlobalType(checker, symbol)) {
return;
}
if (existingTargetLocals.has(skipAlias(symbol, checker))) {
Expand Down Expand Up @@ -952,6 +952,10 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[],
}
}

function isGlobalType(checker: TypeChecker, symbol: Symbol) {
return !!checker.resolveName(symbol.name, /*location*/ undefined, SymbolFlags.Type, /*excludeGlobals*/ false);
}

function makeUniqueFilename(proposedFilename: string, extension: string, inDirectory: string, host: LanguageServiceHost): string {
let newFilename = proposedFilename;
for (let i = 1;; i++) {
Expand Down
26 changes: 26 additions & 0 deletions tests/cases/fourslash/moveToNewFile_global2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////interface String {
//// reverse(): string;
////}
////
////[|String.prototype.reverse = function (): string {
//// return this.split("").reverse().join("");
////}|]

verify.moveToNewFile({
newFileContents: {
"/a.ts":
`interface String {
reverse(): string;
}

`,
"/newFile.ts":
`String.prototype.reverse = function(): string {
return this.split("").reverse().join("");
};
`,
}
});
25 changes: 25 additions & 0 deletions tests/cases/fourslash/moveToNewFile_global3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////[|// this file extends the string prototype
////interface String {
//// reverse(): string;
////}
////String.prototype.reverse = function(): string {
//// return this.split("").reverse().join("");
////};|]

verify.moveToNewFile({
newFileContents: {
"/a.ts": "",
"/String.ts":
`// this file extends the string prototype
interface String {
reverse(): string;
}
String.prototype.reverse = function(): string {
return this.split("").reverse().join("");
};
`,
}
});