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

No way to findReferences for exported arrow function #1564

Open
alexgorbatchev opened this issue Aug 16, 2024 · 1 comment
Open

No way to findReferences for exported arrow function #1564

alexgorbatchev opened this issue Aug 16, 2024 · 1 comment

Comments

@alexgorbatchev
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant