-
Notifications
You must be signed in to change notification settings - Fork 415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept correction files produced by ppx_driver #415
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
open Import | ||
open Jbuild | ||
|
||
module A = Action | ||
module Pset = Path.Set | ||
|
||
module Dir_with_jbuild = struct | ||
|
@@ -901,6 +902,24 @@ module PP = struct | |
mli) in | ||
{ m with impl ; intf } | ||
|
||
let uses_ppx_driver ~pps = | ||
match Option.map ~f:Pp.to_string (List.last pps) with | ||
| Some "ppx_driver.runner" -> true | ||
| Some _ | None -> false | ||
|
||
let promote_correction ~uses_ppx_driver fn build = | ||
if not uses_ppx_driver then | ||
build | ||
else | ||
Build.progn | ||
[ build | ||
; Build.return | ||
(A.promote_if | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really worth introducing a module alias just for this one use? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just that super_context defines an Action module as well, so we get a name clash |
||
[{ src = Path.extend_basename fn ~suffix:".ppx-corrected" | ||
; dst = Path.drop_build_context fn | ||
}]) | ||
] | ||
|
||
let lint_module sctx ~(source : Module.t) ~(ast : Module.t) ~dir | ||
~dep_kind ~lint ~lib_name ~scope = | ||
let alias = Alias.lint ~dir in | ||
|
@@ -909,7 +928,8 @@ module PP = struct | |
(Alias.add_build (aliases sctx) alias build | ||
~stamp:(List [ Atom "lint" | ||
; Sexp.To_sexp.(option string) lib_name | ||
; Atom fn])) | ||
; Atom fn | ||
])) | ||
in | ||
match Preprocess_map.find source.name lint with | ||
| No_preprocessing -> () | ||
|
@@ -938,15 +958,19 @@ module PP = struct | |
; Dep src_path | ||
] | ||
in | ||
let uses_ppx_driver = uses_ppx_driver ~pps in | ||
let args = | ||
(* This hack is needed until -null is standard: | ||
https://github.com/ocaml-ppx/ocaml-migrate-parsetree/issues/35 *) | ||
match Option.map ~f:Pp.to_string (List.last pps) with | ||
| Some "ppx_driver.runner" -> args @ [A "-null"] | ||
| Some _ | None -> args | ||
if uses_ppx_driver then | ||
args @ [ A "-null"; A "-diff-cmd"; A "-" ] | ||
else | ||
args | ||
in | ||
add_alias src.name | ||
(Build.run ~context:sctx.context (Ok ppx_exe) args) | ||
(promote_correction ~uses_ppx_driver | ||
(Option.value_exn (Module.file ~dir source kind)) | ||
(Build.run ~context:sctx.context (Ok ppx_exe) args)) | ||
) | ||
|
||
(* Generate rules to build the .pp files and return a new module map where all filenames | ||
|
@@ -959,51 +983,54 @@ module PP = struct | |
in | ||
let lint_module = lint_module sctx ~dir ~dep_kind ~lint ~lib_name ~scope in | ||
String_map.map modules ~f:(fun (m : Module.t) -> | ||
match Preprocess_map.find m.name preprocess with | ||
| No_preprocessing -> | ||
let ast = setup_reason_rules sctx ~dir m in | ||
lint_module ~ast ~source:m; | ||
ast | ||
| Action action -> | ||
let ast = | ||
pped_module m ~dir ~f:(fun _kind src dst -> | ||
add_rule sctx | ||
(preprocessor_deps | ||
>>> | ||
Build.path src | ||
>>^ (fun _ -> [src]) | ||
>>> | ||
Action.run sctx | ||
(Redirect | ||
(Stdout, | ||
target_var, | ||
Chdir (root_var, | ||
action))) | ||
~dir | ||
~dep_kind | ||
~targets:(Static [dst]) | ||
~scope)) | ||
|> setup_reason_rules sctx ~dir in | ||
lint_module ~ast ~source:m; | ||
ast | ||
| Pps { pps; flags } -> | ||
let ppx_exe = get_ppx_driver sctx pps ~dir ~dep_kind in | ||
let ast = setup_reason_rules sctx ~dir m in | ||
lint_module ~ast ~source:m; | ||
pped_module ast ~dir ~f:(fun kind src dst -> | ||
match Preprocess_map.find m.name preprocess with | ||
| No_preprocessing -> | ||
let ast = setup_reason_rules sctx ~dir m in | ||
lint_module ~ast ~source:m; | ||
ast | ||
| Action action -> | ||
let ast = | ||
pped_module m ~dir ~f:(fun _kind src dst -> | ||
add_rule sctx | ||
(preprocessor_deps | ||
>>> | ||
Build.run ~context:sctx.context | ||
(Ok ppx_exe) | ||
[ As flags | ||
; A "--dump-ast" | ||
; As (cookie_library_name lib_name) | ||
; A "-o"; Target dst | ||
; Ml_kind.ppx_driver_flag kind; Dep src | ||
])) | ||
Build.path src | ||
>>^ (fun _ -> [src]) | ||
>>> | ||
Action.run sctx | ||
(Redirect | ||
(Stdout, | ||
target_var, | ||
Chdir (root_var, | ||
action))) | ||
~dir | ||
~dep_kind | ||
~targets:(Static [dst]) | ||
~scope)) | ||
|> setup_reason_rules sctx ~dir in | ||
lint_module ~ast ~source:m; | ||
ast | ||
| Pps { pps; flags } -> | ||
let ppx_exe = get_ppx_driver sctx pps ~dir ~dep_kind in | ||
let ast = setup_reason_rules sctx ~dir m in | ||
lint_module ~ast ~source:m; | ||
let uses_ppx_driver = uses_ppx_driver ~pps in | ||
pped_module ast ~dir ~f:(fun kind src dst -> | ||
add_rule sctx | ||
(promote_correction ~uses_ppx_driver | ||
(Option.value_exn (Module.file m ~dir kind)) | ||
(preprocessor_deps | ||
>>> | ||
Build.run ~context:sctx.context | ||
(Ok ppx_exe) | ||
[ As flags | ||
; A "--dump-ast" | ||
; As (cookie_library_name lib_name) | ||
; As (if uses_ppx_driver then ["-diff-cmd"; "-"] else []) | ||
; A "-o"; Target dst | ||
; Ml_kind.ppx_driver_flag kind; Dep src | ||
]))) | ||
) | ||
|
||
end | ||
|
||
let expand_and_eval_set t ~scope ~dir set ~standard = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit: I find else clauses in
if (not ...)
confusing and preferif not x then foo else bar
to turn toif x then bar else foo
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I find if/match expressions easier to read when the simple choice is first. That's why I wrote it this way