using RefasterJS on code written for node

39 prikaza
Preskoči na prvu nepročitanu poruku

tbrei...@hustle.life

nepročitano,
8. stu 2018. 13:29:5308. 11. 2018.
u Closure Compiler Discuss
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#get
https://www.npmjs.com/package/idx

but 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

Ben Lickly

nepročitano,
12. stu 2018. 16:05:2812. 11. 2018.
u closure-comp...@googlegroups.com
By flow-style type annotations, do you mean the ones inside comments or out?

vs.

--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/closure-compiler-discuss/bf1301f7-3bca-44e5-824b-8817c7857898%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tyler Breisacher

nepročitano,
13. stu 2018. 12:44:0713. 11. 2018.
u closure-comp...@googlegroups.com
The second one. Maybe I should have said TypeScript style.

John Lenz

nepročitano,
13. stu 2018. 14:37:3513. 11. 2018.
u closure-compiler
What version are you using, the stack trace doesn't appear to match the current code?

On Thu, Nov 8, 2018 at 10:29 AM <tbrei...@hustle.life> wrote:
--

Tyler Breisacher

nepročitano,
13. stu 2018. 14:44:0113. 11. 2018.
u closure-comp...@googlegroups.com
I just grabbed the zip from http://dl.google.com/closure-compiler/refasterjs/refasterjs-latest.zip , I'm not sure what version that is or if it is actually the latest version


For more options, visit https://groups.google.com/d/optout.


--
Odgovori svima
Odgovori autoru
Proslijedi
0 novih poruka