Using jscodeshift, Is there a way to extract a node path from a collection if there is only one node path?

323 Views Asked by At

I'm just getting started with writing CodeMods with jscodeshift. I noticed that the find method seems to always return a Collection. If there is only one node path in the collection is there an easy way to extract that node path?

For example, I can easily get the default export, of which there will only ever be one:

j(file.source).find(j.ExportDefaultDeclaration)

However, this returns a collection, which I then need to call forEach on to process the one and only node.

Is there a better way?

1

There are 1 best solutions below

0
On

It looks like .get(0) will get the first Node Path in a Collection, so

j(file.source).find(j.ExportDefaultDeclaration).get(0);

would return the Node Path in question.