jaredly / example-reason-codemod

3 stars 0 forks source link

Detect a function of a given module? #1

Open jchavarri opened 5 years ago

jchavarri commented 5 years ago

Thanks for creating these codemod utils and example! 🙂 Really cool.

I'm exploring if I could use this codemod for the migrations of BuckleScript Js modules proposed in https://github.com/BuckleScript/bucklescript/issues/2975. I wonder how I could approach the detection of usages of some specific functions in Js.X modules:

If I use syntactic analysis (e.g. use Pexp_ident to match against the function names) it might not be effective in all cases, for example the function can be called with Js.Array.map or just map if the Js.Array module has been opened before.

There is getExprType which is awesome but it just works at the type level, so I wouldn't know which function has been called, only the type of the expression.

Is there an approach that I'm missing to do this kind of detection in an accurate way?

jchavarri commented 5 years ago

So, I've found out that I should be using Pexp_apply in the expression mapper, as that's the type of expression that the parser will use for something like Js.Array.map(), which is what this BuckleScript codemod should track.

I am trying to use References.locForLocations, as SharedTypes.Loc.t seems like the main type to operate with. The main issue I'm having is that all the calls to References.locForLocations return a Typed(___, NotFound) value, and the relevant info to know where those types are defined seems to be precisely in the typed value of Typed(flexibleType, typed) of Loc.t. So I can't find a way to get the meaningful data about which module is the one that declares the expression of the applied function.

I've tried with References.definitionForPos but it also returns None (not only for the applied function case, but for any expression).

Something that might be related: the locations in expr.pexp_loc passed to locForLocations seem to be missing the file name pos_fname.

@jaredly Do you know if I am looking in the right direction? What should I do to know which module has the definition of a given function application expression?