by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 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
  • Jun 07 17:30
    conartist6 commented #714
  • Jun 07 17:27
    conartist6 commented #715
  • Jun 07 17:22
    conartist6 opened #715
  • Jun 06 19:05
    conartist6 commented #572
Brendan Annable
@BrendanAnnable
maybe taking the sourceFileName from the options given to recast.parse?
Brendan Annable
@BrendanAnnable
My use case that I have no way of tracking the filename when performing AST transformations (which may include filename changes). I could track it with external state, but that would be sucky. Especially when there is a perfect File node which could store it directly within the AST itself.
Brendan Annable
@BrendanAnnable
Oh man, it was a 2 line change to ast-types and a 1 line change to recast and works perfectly for my use case. I'll create the PRs, hopefully there isn't an issue.
Brendan Annable
@BrendanAnnable
PRs in benjamn/ast-types#200 benjamn/recast#354
Saveliy Baranov
@concubicycle
Hey
Can this lib preserve comments when pretty printing?
Mitermayer Reis
@mitermayer
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
@benjamn
@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").types.builders.block("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
@benjamn
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
@mitermayer
Thanks @benjamn for the detailed response.
Van Nguyen
@vanmichael
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
@benjamn
Something like this?
const ast = recast.parse(source);
const b = recast.types.builders;
ast.program.body.unshift(
  b.variableDeclaration("const", [
    b.variableDeclarator(
      b.identifier("moduleName"),
      b.callExpression(
        b.identifier("require"),
        [b.literal("moduleName")]
      )
    )
  ])
);
console.log(recast.print(ast).code);
@vanmichael
that's how you would do it with recast alone
jscodeshift may have a better way
Mitermayer Reis
@mitermayer
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
@mitermayer
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
@GianlucaGuarini
Hi @benjamn is there a way to replace the global scope? For example foo + bar.baz should become scope.foo + scope.bar.baz
of course the scope should be overridden only for the identifiers located into the global context
Gianluca Guarini
@GianlucaGuarini
Ok i have found a simple solution https://repl.it/repls/OnerlookedModestFtpclient but it would be nice if that would be directly into the core
Vitaliy Stoliarov
@Ni55aN
How can I use recast on source code with dynamic import?
Matthew Cheok
@matthewcheok
Is there good documentation on any APIs available for finding nodes/traversing the AST?
Kay McCormick
@kaymccormick
Wow the last message was in 2018?
venpa
@venpa
Is there a way to replace all instance of the variable with its value based on the scope?