dsherret / dsherret/ts-morph

.tsx files do not return referencing SourceFiles

Open
#1,250 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 13.0.2

I *believe* this is a bug, but I'm not 100% sure. I'm getting results from `SourceFile.getReferencingSourceFiles()` as well as `SourceFile.getReferencingNodesInOtherSourceFiles()` when running some of my code through an actual (persisted on disk) TypeScript project, but not when writing unit tests with an in-memory file system.

I'm not able to put this code in a CodeSandbox environment - it keeps freezing, I'm assuming there's some missing API/system access attempt locking up the sandbox UI, but this code sample should be a very simple example of what I'm trying to do.

**To Reproduce**

```ts
import {
Project,
VariableDeclarationKind,
} from "ts-morph";

const project = new Project();
const file1 = project.createSourceFile("file1.tsx");

file1.addVariableStatement({
declarationKind: VariableDeclarationKind.Const,
declarations: [{ name: "example", initializer: "5" }],
});

file1.addExportDeclaration({
namedExports: ["example"],
});

const file2 = project.createSourceFile(
"file2.tsx",
`
console.log("example", example);
`
);

file2.addImportDeclaration({
namedImports: ["example"],
moduleSpecifier: file2.getRelativePathAsModuleSpecifierTo(file1),
});

console.log(file1.getReferencingSourceFiles());
expect(file1.getReferencingSourceFiles()).toContain(file2);
```

**Expected behavior**

`file1.getReferencingSourceFiles()` should return an array containing `file2` since it imports a node from it.

EDIT: After some further testing, I narrowed it down to the files being created with a `.tsx` extension, not `.ts`, and I don't think it's related to being an in-memory filesystem or not. I updated the example to reflect this.

Contributor guide

Open the contributing guide

Research direction

Read CONTRIBUTING.md, then run the provided reproduction using the TypeScript project and compare `.tsx` files with `.ts` files. Trace `SourceFile.getReferencingSourceFiles()` and `SourceFile.getReferencingNodesInOtherSourceFiles()`; done means the `.tsx` case returns `file2` as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.