Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
  • Jul 26 14:51
    screetBloom opened #719
  • Jul 17 02:52
    zhizhunbao995 edited #718
  • Jul 16 06:30
    zhizhunbao995 opened #718
  • Jul 13 16:23
    conartist6 commented #712
  • Jun 25 08:58
    N1kto commented #316
  • Jun 15 02:16
    tjx666 commented #717
  • Jun 15 00:06
    eventualbuddha commented #717
  • Jun 14 11:33
    tjx666 edited #717
  • Jun 14 11:07
    tjx666 edited #717
  • Jun 14 11:06
    tjx666 opened #717
  • Jun 12 17:48
    Daniel15 synchronize #716
  • Jun 12 00:53
    Daniel15 commented #559
  • Jun 12 00:53
    Daniel15 opened #716
  • Jun 11 07:33
    bromagosa commented #370
  • Jun 09 15:14
    conartist6 edited #712
  • Jun 07 17:47
    conartist6 synchronize #712
  • Jun 07 17:39
    conartist6 commented #623
  • Jun 07 17:35
    conartist6 commented #706
  • Jun 07 17:34
    conartist6 commented #706
  • Jun 07 17:30
    conartist6 closed #714
Saveliy Baranov
Can this lib preserve comments when pretty printing?
Mitermayer Reis
I am working on a project that would update/adds new comments to the AST to be printed.
  • What is the recommended way to do that ?
  • Can I achieve this using recast/lib/comments?
Ben Newman
@mitermayer In an AST produced by recast.parse, comments are exposed as a .comments property on nodes
each comment object has boolean .leading and .trailing properties, indicating the comment's relationship to the node
you can create new comment objects with require("recast")"comment text") or .line("comment text")
most nodes in the AST won't have a .comments array attached to them, because they don't have any comments
so you might have to create the array if you're adding new comments for the first time
and of course any modifications to existing comments will be picked up by recast.print (only the comment will change unless you modify the AST in other ways)
Ben Newman
the recast/lib/comments module probably won't be helpful to you, as that logic is mostly about attaching comments to nodes during recast.parse and printing them in recast.print
Mitermayer Reis
Thanks @benjamn for the detailed response.
Van Nguyen
Hi im trying to use jscodeshift to write a code mod and part of what I am trying to do is insert a const moduleName = require('moduleName') at the top of the file. Can anyone point me to an example mod or documentation explaining how to create a require declaration node and insert it at the top of a file like writing a normal require like in CommonJS?
Ben Newman
Something like this?
const ast = recast.parse(source);
const b =;
  b.variableDeclaration("const", [
that's how you would do it with recast alone
jscodeshift may have a better way
Mitermayer Reis
Hi @benjamn I have just created an issue where recast is failling to print AST generated from flow parser (benjamn/recast#422). Also added a reference on how prettier is fixing that case on its printer
Mitermayer Reis
sorry one more flow incompatiblity (benjamn/recast#423) looks like when giving recast a flow parsed AST it is failling to attach comments to the correct location
Gianluca Guarini
Hi @benjamn is there a way to replace the global scope? For example foo + bar.baz should become +
of course the scope should be overridden only for the identifiers located into the global context
Gianluca Guarini
Ok i have found a simple solution but it would be nice if that would be directly into the core
Vitaliy Stoliarov
How can I use recast on source code with dynamic import?
Matthew Cheok
Is there good documentation on any APIs available for finding nodes/traversing the AST?
Kay McCormick
Wow the last message was in 2018?
Is there a way to replace all instance of the variable with its value based on the scope?
Hi, I want to get function name and parameters from ast. Please tell me what is right way ,and how i can do
is there any good documentation for AST