sourceFile.getExportedDeclarations() returns duplicate (any number that has been exported) nodes however this is not wrong but I need just unique nodes.
sample.ts
export class X {}
index.ts
export { X } from './sample';
It returns 2 records for X first from sample.ts and second index.ts while both export one thing and one of them is enough.
I have a project and they export anything multiple times so I can not get one export per type.
How to get just unique list of exported nodes? Any unique id per node? Distinct?
If you're only interested in names, map the array to names and then create a Set out of it. This will remove duplicates.
@lazarljubenovic
What name exactly? I have some
Export default functions
Destructuring objects
Or, What about an interface and a class in the same file with same name?
@HamedFathi I don't think I understand the question because when I try this I only get one export as I would expect:
const project = new Project({ useVirtualFileSystem: true });
const sampleSourceFile = project.createSourceFile("/sample.ts", "export class X {}");
const indexSourceFile = project.createSourceFile("/index.ts", "export { X } from './sample';");
const exportedDeclarations = indexSourceFile.getExportedDeclarations();
expect(exportedDeclarations.size).to.equal(1);
expect(exportedDeclarations.get("X")!.length).to.equal(1);
expect(exportedDeclarations.get("X")![0].getText()).to.equal(`export class X {}`);
Could you show a specific example that has duplicates?
I was under impression that OP's running it for both files:
It returns 2 records for X first from
sample.tsand secondindex.tswhile both export one thing and one of them is enough.
I guess this is why a reproduction is always a good idea :smile:
@dsherret
So strange, I will check it again but because of duplicate exports, Even I made Id for each node to find duplicates!!! Here
id: getSha256(node.getFullText() + getSourceFilePath()),
The project I'm handling is big and it's hard to find a good example, but I'm sure I had some duplicate exports.
Maybe, I have done something wrong!!! I should check it again.
For now, I am closing this.
Thank you both.
@HamedFathi yeah, please let me know if you find duplicates! That would most likely indicate a bug and I'd like to fix it.