Skip to content
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

Fixing self imports for typeReference nodes #61107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,9 @@ export function getExistingLocals(sourceFile: SourceFile, statements: readonly S
if (symbol.valueDeclaration && getSourceFileOfNode(symbol.valueDeclaration).path === sourceFile.path) {
existingLocals.add(symbol);
}
else if (!symbol.valueDeclaration && symbol.parent?.valueDeclaration && getSourceFileOfNode(symbol.parent.valueDeclaration).path === sourceFile.path) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this is right. I'm not sure we ever should have been using valueDeclaration at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you think using valueDeclaration is wrong? Also, another way I had thought of was to do something like this
if (symbol.declarations && some(symbol.declarations, d => getSourceFileOfNode(d).path === sourceFile.path)) { existingLocals.add(symbol); }
but this again would go into another loop and check for each declaration which might probably end up taking even more time. Another way could be to do something like findAllReferences. I'm not exactly clear on what happens there but possible some way of comparing text to see which symbols already exist in a file. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valueDeclaration only includes value-meaning declarations, and the failing test case here makes it clear why that’s not sufficient. I think looking at symbol.declarations is correct.

existingLocals.add(symbol);
}
});
}
return existingLocals;
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/fourslash/moveToFile_noSelfImports3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

//@Filename: /b.ts
////import { ISomething } from './a';
////
////export function [|func|](something: ISomething) {
////}

// @Filename: /a.ts
////export interface ISomething {
////}

verify.moveToFile({
newFileContents: {
"/a.ts": `export interface ISomething {
}
export function func(something: ISomething) {
}
`,

"/b.ts": `
`,
},
interactiveRefactorArguments: { targetFile: "/a.ts" }
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/moveToFile_noSelfImports4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />

//@Filename: /b.ts
////export type BaseTest = string;

// @Filename: /a.ts
////import { BaseTest } from "./b";
////
////export type [|Test|] = BaseTest;

verify.moveToFile({
newFileContents: {
"/b.ts": `export type BaseTest = string;
export type Test = BaseTest;
`,

"/a.ts": `
`,
},
interactiveRefactorArguments: { targetFile: "/b.ts" }
});