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

How to get named export node (more easily)

Open webpro opened this issue 3 years ago • 0 comments

First of all, great project. Had fun using it!

Maybe someone knows a better way to do this, I found it a bit inconvenient for something I would expect to be easier.

Let's take this input file:

export { ExportA, ExportB } from './DepA';
export { ExportC } from './DepB';
export type { TypeA, TypeB } from './types';

Now I wanted to get the node of ExportC so I can find it's references in other source files:

const namedExports = sourceFile.getExportDeclarations().map(declaration => declaration.getNamedExports());
const exportSpecifier = namedExports.flat().find(namedExport => namedExport.getName() === 'ExportC');
const node = exportSpecifier?.getNameNode();
const references = node?.findReferencesAsNodes();

It works as intended, yet I think this is slightly tedious, maybe there's a better way?

webpro avatar Sep 19 '22 16:09 webpro