ts-morph
ts-morph copied to clipboard
No way to `findReferences` for exported arrow function
Describe the bug
Version: 23.0.0
No way to find references for exported arrow function.
To Reproduce
import { Project, SyntaxKind } from 'ts-morph';
const project = new Project();
const sourceFile = project.createSourceFile('test.ts', `export default () => {}`);
for (const [name, declarations] of sourceFile.getExportedDeclarations()) {
if (name === 'default') {
declarations.forEach(defaultDeclaration => {
if (defaultDeclaration.isKind(SyntaxKind.ArrowFunction)) {
const exportAssignment = defaultDeclaration.getParentIfKind(SyntaxKind.ExportAssignment);
// defaultDeclaration.findReferences() - not available
if (exportAssignment) {
// exportAssignment.findReferences() - not available
}
}
});
}
}
Expected behavior
I think either ExportAssignment or ArrowFunction should have findReferences() available.
I found a workaround, but it's not ideal :)
if (defaultDeclaration.isKind(SyntaxKind.ArrowFunction)) {
const exportAssignment = defaultDeclaration.getParentIfKind(SyntaxKind.ExportAssignment);
if (exportAssignment) {
exportAssignment.replaceWithText(`export const Foo = ${defaultDeclaration.getText()}`);
file.addExportAssignment({ expression: 'Foo', isExportEquals: false });
// here file.getExportedDeclarations() will have `default: VariableDeclaration` which has `.findReferences()`
}
}