fix(58801): "Move to file" on global code unnecessarily imports/exports, generates invalid code (#58811)

This commit is contained in:
Oleksandr T 2024-06-19 03:24:22 +03:00 committed by GitHub
parent 867476e57a
commit e8fca15b14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 56 additions and 1 deletions

View File

@ -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))) {
@ -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++) {

View File

@ -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("");
};
`,
}
});

View File

@ -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("");
};
`,
}
});