Hi! Hope everyone is doing well over there in Google-land!
So, I'm trying to figure out how to automatically refactor some code from using lodash's get function, to using idx. In case you're not familiar, here are some links
https://lodash.com/docs/4.17.11#gethttps://www.npmjs.com/package/idxbut basically _.get(foo, 'bar') should become idx(foo, _ => _.bar)
Since idx is from facebook, it might work better to do this with Facebook's tools such as
https://github.com/facebook/jscodeshift. But jscodeshift seemed a little confusing, so I thought I'd try RefasterJS. If I remember correctly, it's written for Google style code, where everything is goog.require()d instead of require()d as is common in nodejs code. But I tried doing this anyway, just to see if it would even kinda-sorta work:
const _ = require('lodash');
const idx = require('idx');
/**
* Matches any direct assignment to window.location.href.
* @param {*} receiever
* @param {string} prop
*/
function before_idx(receiever, prop) {
_.get(receiver, prop);
}
/**
* Matches any direct assignment to window.location.href.
* @param {*} receiever
* @param {string} prop
*/
function after_idx(receiever, prop) {
idx(receiver, _ => _[prop]);
}
and ... I managed to crash the parser 🙃
$ java -jar ~/bin/refasterjs.jar
$ java -jar ~/bin/refasterjs.jar --inputs=src --refasterjs_template refaster/idx.js
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1967)
at com.google.javascript.jscomp.parsing.IRFactory.normalizeRegex(IRFactory.java:2547)
at com.google.javascript.jscomp.parsing.IRFactory.access$2900(IRFactory.java:156)
at com.google.javascript.jscomp.parsing.IRFactory$TransformDispatcher.processRegExpLiteral(IRFactory.java:1566)
at com.google.javascript.jscomp.parsing.IRFactory$TransformDispatcher.processLiteralExpression(IRFactory.java:2336)
at com.google.javascript.jscomp.parsing.IRFactory$TransformDispatcher.process(IRFactory.java:2423)
perhaps because our code has Flow-style type annotations and therefore isn't technically syntactically valid JavaScript. Or perhaps because some of our files start with #!'s. But in any case, do you know if anyone has used RefasterJS successfully on nodejs style code and/or code with Flow type annotations (or TypeScript, which is more or less the same thing)? Or is this likely to just not work at all?
Thanks!
Tyler