ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

No way to `findReferences` for exported arrow function

Open alexgorbatchev opened this issue 1 year ago • 1 comments

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.

alexgorbatchev avatar Aug 16 '24 22:08 alexgorbatchev

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()`
  }
}

alexgorbatchev avatar Aug 16 '24 23:08 alexgorbatchev