Skip to content

Exclude JsxImport during finding references #53328

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 7 additions & 1 deletion src/services/importTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
forEach,
getAssignmentDeclarationKind,
getFirstIdentifier,
getJSXImplicitImportBase,
getJSXRuntimeImport,
getNameOfAccessExpression,
getSourceFileOfNode,
getSymbolId,
Expand Down Expand Up @@ -63,6 +65,7 @@ import {
NamedImportsOrExports,
NamespaceImport,
Node,
nodeIsSynthesized,
nodeSeenTracker,
Program,
some,
Expand Down Expand Up @@ -455,6 +458,7 @@ export type ModuleReference =
export function findModuleReferences(program: Program, sourceFiles: readonly SourceFile[], searchModuleSymbol: Symbol): ModuleReference[] {
const refs: ModuleReference[] = [];
const checker = program.getTypeChecker();
const compilerOptions = program.getCompilerOptions();
for (const referencingFile of sourceFiles) {
const searchSourceFile = searchModuleSymbol.valueDeclaration;
if (searchSourceFile?.kind === SyntaxKind.SourceFile) {
Expand All @@ -471,9 +475,11 @@ export function findModuleReferences(program: Program, sourceFiles: readonly Sou
}
}

const jsxImport = getJSXRuntimeImport(getJSXImplicitImportBase(compilerOptions, referencingFile), compilerOptions);
forEachImport(referencingFile, (_importDecl, moduleSpecifier) => {
const moduleSymbol = checker.getSymbolAtLocation(moduleSpecifier);
if (moduleSymbol === searchModuleSymbol) {

if (moduleSymbol === searchModuleSymbol && !(moduleSpecifier.text === jsxImport && nodeIsSynthesized(moduleSpecifier))) {
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to specifically check for the jsxImport, any node for which nodeIsSynthesized is true is going to be a hassle for the import tracker, since it has no associated positions to report. Just checking that will also remove any errors related to finding all references on the synthetic tslib import we also add. However, just skipping the result leaves a bit lacking, no? That's why the other PR adds a new reference type and actually records the file as a reference for the import.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have specified jsxImport because we still want to hit the assertion in case a bug is introduced.

refs.push({ kind: "import", literal: moduleSpecifier });
}
});
Expand Down