gkz / grasp

JavaScript structural search, replace, and refactor
http://graspjs.com
MIT License
1.28k stars 33 forks source link

Provide an interface for modifying AST structures using grasp replacement syntax #3

Closed benjamn closed 10 years ago

benjamn commented 10 years ago

How much trouble would it be to support an interface like the following?

var ast = require("esprima").parse(code);
ast = require("grasp").replace("{{.r}}+{{.l}}", ast);
process.stdout.write(require("escodegen").generate(ast));

Or, even better:

var transform = require("grasp").compileReplacer("{{.r}}+{{.l}}");
var ast = transform(require("esprima").parse(code));
process.stdout.write(require("escodegen").generate(ast));

This kind of interface would allow multiple transformations to be chained together easily, and would also leave the complexity of reprinting to an external library like https://github.com/Constellation/escodegen or https://github.com/benjamn/recast.

If grasp already supports use cases like this, please feel free to point me there (and close this issue).

gkz commented 10 years ago

Using grasp as a library was not fully developed for version 0.1.0. In an upcoming version, this will be further developed so you don't have to mock fs or process.stdin for input.

Thank you for your use cases, they are useful. Please list any other ones you have as well. It looks like some other people are also looking for this type of thing: http://stackoverflow.com/questions/19878078/string-variable-to-stdin-interface

gkz commented 10 years ago

Grasp 0.2.0 was released today which adds some additional feature when using grasp as a library: http://graspjs.com/docs/lib/ - take a look at the new input option, and the grasp.search and grasp.replace helper functions.