4 ms·
> js = jsfmt.rewrite(js, "_.each(a, b) -> a.forEach(b)"); Of course _.each handles objects (not just arrays) and returns its input (where Array#forEach returns
by masklinn 13y ago
> js = jsfmt.rewrite(js, "_.each(a, b) -> a.forEach(b)");
Of course _.each handles objects (not just arrays) and returns its input (where Array#forEach returns undefined), and there does not seem to be any type-based conditions (or otherwise) so you've got a souped-up sed, not something you can add to any sort of automated workflow.
- placeybordeaux 13y agoAs far as I understand go fmt works on the AST, if your reformatter doesn't work on the AST then you will eventually have problems. Also go fmt doesn't ever change your code, thats what go fix is for.
- moonlighter 13y agogofmt -r applies rewrite rules, and -s also performs source transformations; both change the code.
- NateDad 13y agogofmt without flags does not change your code. Yes, you can ask it to change your code if you want it to, but by default, it won't.
- masklinn 13y ago> As far as I understand go fmt works on the AST, if your reformatter doesn't work on the AST then you will eventually have problems. JsFmt also works on the AST. Working on the AST does not mean your rewrite rules can be bulletproof, let alone are.
- nilkn 13y agoFrom the readme: > Javascript formatters exist but most (all?) work on just strings, not the AST. Using Esprima under the hood we have access to the full AST and can do useful things like intelligent find and replace as in gofmt.
- jimfleming 13y agoYou're correct it doesn't have type support which would make it vastly more useful. I honestly wasn't aware `forEach` returned anything when I wrote the example. The other example finding component dependencies is a little more realistic.
- jkrems 13y agoAlso native `forEach` tends to be slower and not fully supported in all environments, so that rule seems to be pretty dangerous in general.