Skip to content

Instantly share code, notes, and snippets.

@emillon
Created November 27, 2018 12:23
Show Gist options
  • Save emillon/342ceae249b6d9c60cda605414aec238 to your computer and use it in GitHub Desktop.
Save emillon/342ceae249b6d9c60cda605414aec238 to your computer and use it in GitHub Desktop.
2018-11-26 Reason Language Server codemod example
open Parsetree;
open Codemod.Helpers;
let replaceErrors = (ctx, expr) =>
expr
->mapExpr((mapper, expr) => {
switch (expr.pexp_desc) {
| Pexp_construct({txt: Longident.Lident("Student")} as lid, Some({pexp_desc: Pexp_tuple([name, age])})) =>
let loc = Location.none
Some([%expr Student({name: [%e name], age: [%e age]})])
| _ => None
};
});
let modify = (ctx, structure) =>
{
structure->strExpr((mapper, expr) =>
expr->mapFnExpr((mapper, args, body) => {
/* Format.printf("%a\n%!", Pprintast.expression, body);*/
switch (ctx->getExprType(body)) {
| Reference(Public({moduleName: "More-Tryit", modulePath: _, name: "person"} as s), _) =>
Format.printf("\n\x1b[33m%s \x1b[34m%s\x1b[0m\n%!", s.moduleName, s.name);
Some((args, replaceErrors(ctx, body)))
| _ =>
None
};
})
->Some
);
};
switch (Sys.argv) {
| [|_, root|] =>
print_endline("Running on project: " ++ root);
Codemod.run(
root,
(path, moduleName) => Filename.extension(path) == ".re",
modify
);
| _ => ()
};
open Result;
type person =
| Student(string, int)
| Teacher
| Admin;
let s1 = Student("Alexa", 0);
/*
original code:
let nonCollapsible = something => Student("wow", 9999);
*/
let nonCollapsible = something => Student({name: "wow", age: 9999});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment