News for hackers. Stuff that matters.
Re:Show HN: Auto install npm dependencies as you code by _RPM on Monday August 08, 2016 @01:34PM (#12249447)
The AST? What AST is that? You don't get access to an AST.
Re:Show HN: Auto install npm dependencies as you code by concise_unicorn on Monday August 08, 2016 @01:58PM (#12249625)
Tools like acorn[1] and Esprima[2] can parse the JavaScript source and output an ESTree compliant syntax tree.

Then you can traverse it and modify it like any other AST.

[1] https://github.com/ternjs/acorn [2] http://esprima.org/ [3] https://github.com/estree/estree

Re:Show HN: Auto install npm dependencies as you code by throwanem on Monday August 08, 2016 @01:46PM (#12249538)
The AST you get from any of a wide variety of parsers, of which esprima may still be only the most popular of many.
Re:Show HN: Auto install npm dependencies as you code by Etzos on Monday August 08, 2016 @01:47PM (#12249547)
It has to open and read a .js file already, it can certainly turn that into the representative AST for said file and then use the data from that. It will be slower, but it will also be more accurate and less likely to turn up false positives or miss things.
Re:Show HN: Auto install npm dependencies as you code by _RPM on Monday August 08, 2016 @01:59PM (#12249637)
It has to parse a javascript file, which isn't trivial. The reason they use regular expression is because implementing a javascript parser isn't an easy problem to solve fast, even though the grammar is available.
Re:Show HN: Auto install npm dependencies as you code by Etzos on Monday August 08, 2016 @03:15PM (#12250186)
One certainly does not need to implement a parser, just use one of the many available. As the sibling here pointed out the time it would take to parse and walk the AST is negligible compared to the downloads happening.
Re:Show HN: Auto install npm dependencies as you code by elmigranto on Monday August 08, 2016 @02:15PM (#12249751)
You downloading megabytes over network and performing disk IO with it. What's the problem with 100 milliseconds of file parsing?