Created
November 27, 2018 12:23
-
-
Save emillon/342ceae249b6d9c60cda605414aec238 to your computer and use it in GitHub Desktop.
2018-11-26 Reason Language Server codemod example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); | |
| _ => () | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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