mardi 3 décembre 2019

Is there any library to parse a JavaScript source code and return all the import/export statements?

I am writing a script where I need to parse a JavaScript source file and get all kind of information out of it so that I can programatically generate a new JavaScript source file.

The library should have Api's to get all export statements so that I can get all the exports with name.

Example: consider below js file

export function getUserSubscriptions(user, subscriptions) {}
export { default as selectionsPresenterDefaultProps } from './selectionsPresenter.defaultProps';
export default class DayViewTest extends React.Component {}
export function bestPositionOf(positions) {}
export class Team {}
export default class AddEditAdminUser extends React.Component {}
.
.
.
.

The library api should accept the file and should have a mechanism to return all exports. In the above example , the output would be

getUserSubscriptions
selectionsPresenterDefaultProps 
DayViewTest 
bestPositionOf
Team
AddEditAdminUser 

I did think of writing a script where I could use regex to find a pattern in file and then parse it, but since there are many variations it has a lot of work to do and hence I am in a need of finding a library.

I did go through some libraries which would convert the code to AST(Abstract Syntax Tree) but was not very helpful.

Can anyone suggest me a solution over here?





Aucun commentaire:

Enregistrer un commentaire