gkz / grasp

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

A way to handle commas when joining function arguments #28

Open kirjs opened 10 years ago

kirjs commented 10 years ago

e.g.

>> test.js:
define(["one", "two", "old"], callback);
define(["old"], callback);
define(["old", "three", "four"], callback);

What I tried

grasp -e 'define([_$before,"old",_$after], $callback)' test.js -R "define([{{before | join ','}},'new', {{after | join ','}}], callback)"

Output

define(["one","two",'new', ], callback);
define([,'new', ], callback);
define([,'new', "three","four"], callback);

What I would like to achieve

define(["one","two",'new' ], callback);
define(['new' ], callback);
define(['new', "three","four"], callback);
kirjs commented 10 years ago

If variable could be used in filters one possible solution could look like this:

grasp -e 'define([_$before,"old",_$after], $callback)' test.js
-R define([{{before | concat 'now' | concat after | join ',' }}], callback)"
gkz commented 10 years ago

yes, I think a concat filter is a good idea