Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Aug 16 06:45
    theajack opened #620
  • Aug 16 05:42
    skeggse commented #589
  • Aug 15 01:10
    greenkeeper[bot] commented #602
  • Aug 15 01:10

    greenkeeper[bot] on @types

    chore(package): update @types/n… chore(package): update lockfile… (compare)

  • Aug 13 17:14
    mheiber opened #619
  • Aug 13 15:54
    mheiber commented #616
  • Aug 13 03:09
    greenkeeper[bot] commented #600
  • Aug 13 03:09

    greenkeeper[bot] on flow-parser-0.105.2

    chore(package): update flow-par… chore(package): update lockfile… (compare)

  • Aug 11 14:55
    zxbodya synchronize #615
  • Aug 11 14:45

    benjamn on v0.18.2

    (compare)

  • Aug 11 14:45

    benjamn on master

    Bump npm version to 0.18.2 in p… (compare)

  • Aug 11 14:43

    benjamn on master

    Update package-lock.json. (compare)

  • Aug 11 14:39
    zxbodya synchronize #615
  • Aug 11 14:33

    greenkeeper[bot] on reify-0.20.7

    (compare)

  • Aug 11 14:33

    greenkeeper[bot] on reify-0.20.11

    (compare)

  • Aug 11 14:33
    steelbrain commented #613
  • Aug 11 14:33

    greenkeeper[bot] on reify-0.20.9

    (compare)

  • Aug 11 14:33

    greenkeeper[bot] on reify-0.20.8

    (compare)

  • Aug 11 14:33

    greenkeeper[bot] on reify-0.20.10

    (compare)

  • Aug 11 14:33
    benjamn closed #610
Brendan Annable
@BrendanAnnable
@benjamn So that parser only parses the typing information, so I doubt it would have .loc information. i.e. if you have a comment e.g. /** @type {Array<SomeFancyType>} */ it won't actually parse the entire comment, just the Array<SomeFancyType> part.
I only briefly looked into it though
jsdoc have their own "regex parser" infront of it https://github.com/jsdoc3/jsdoc/blob/master/lib/jsdoc/doclet.js#L110
Matheus Fernandes
@matheuss
Hi guys! I'm reading a file that have this array:
plugins: [],
With recast, I'm adding to elements and then saving the file. Turns out that recast does this:
plugins: ["hyperpanic", "hypercwd"],
Any change that I could print this?
plugins: [
   "hyperpanic",
   "hypercwd"
],
Brendan Annable
@BrendanAnnable
Recast doesn't have the ability to print according to a style guide.
There was a github issue requesting such a feature.
Matheus Fernandes
@matheuss
😔
Thanks!
Ari Porad
@ariporad

Hi All, quick question: Is there a way to use scope to eliminate unused variables? For example, if I have this:

function () {
  var x = 5;
  doFoo(123);
};

I want to convert it to:

function () {
  // Notice: No `x`
  doFoo(123);
}

Thanks!

Brendan Annable
@BrendanAnnable
@ariporad Maybe collect two pieces of information in a single traversal and analze after. e.g. Collect each declared Identifier and its associated scope, and also collect all used variables and their associated scope (using scope.lookup) and then compare the two for disparities.
Ari Porad
@ariporad
Ok, thanks!
Brendan Annable
@BrendanAnnable
@benjamn Is there any plans for scope.js to support defaulted function params? It currently treats them as global variables.
(ast-types)
Lucas Bento
@lucasbento
Do I have to enable something for recast to parse async/await?
I'm not finding anything about it on the repository
Ben Newman
@benjamn
@BrendanAnnable that's definitely an oversight/bug, and I'd be happy to review a pull request!
@lucasbento try recast.parse(source, { parser: <any object with a .parse method> })
Lucas Bento
@lucasbento
@benjamn: thank you, I fixed it by using babylon parser
sorry I forgot to mention that here
Ben Newman
@benjamn
np, glad you figured it out
Lucas Bento
@lucasbento
@benjamn: ;)
Brendan Annable
@BrendanAnnable
@benjamn looks like the File type does not a filename? It is somehow possible to add an optional filename to it?
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?