Skip to content
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

Better ignore doc comment #2456

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/Normalize_std_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,50 @@ let make_mapper conf ~ignore_doc_comments =
let typ = {typ with ptyp_loc_stack= []} in
Ast_mapper.default_mapper.typ m typ
in
let structure =
if ignore_doc_comments then fun (m : Ast_mapper.mapper) l ->
List.filter l ~f:(function
| {pstr_desc= Pstr_attribute a; _} -> not (is_doc a)
| _ -> true )
|> Ast_mapper.default_mapper.structure m
else Ast_mapper.default_mapper.structure
in
let signature =
if ignore_doc_comments then fun (m : Ast_mapper.mapper) l ->
List.filter l ~f:(function
| {psig_desc= Psig_attribute a; _} -> not (is_doc a)
| _ -> true )
|> Ast_mapper.default_mapper.signature m
else Ast_mapper.default_mapper.signature
in
let class_structure =
if ignore_doc_comments then fun (m : Ast_mapper.mapper) x ->
let pcstr_fields =
List.filter x.pcstr_fields ~f:(function
| {pcf_desc= Pcf_attribute a; _} -> not (is_doc a)
| _ -> true )
in
Ast_mapper.default_mapper.class_structure m {x with pcstr_fields}
else Ast_mapper.default_mapper.class_structure
in
let class_signature =
if ignore_doc_comments then fun (m : Ast_mapper.mapper) x ->
let pcsig_fields =
List.filter x.pcsig_fields ~f:(function
| {pctf_desc= Pctf_attribute a; _} -> not (is_doc a)
| _ -> true )
in
Ast_mapper.default_mapper.class_signature m {x with pcsig_fields}
else Ast_mapper.default_mapper.class_signature
in
{ Ast_mapper.default_mapper with
location
; attribute
; attributes
; structure
; signature
; class_signature
; class_structure
; expr
; pat
; typ }
Expand Down
7 changes: 6 additions & 1 deletion lib/Std_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ let map (type a) (x : a t) (m : Ast_mapper.mapper) : a -> a =
match x with
| Structure -> m.structure m
| Signature -> m.signature m
| Use_file -> List.map ~f:(m.toplevel_phrase m)
| Use_file ->
List.filter_map ~f:(fun x ->
match m.toplevel_phrase m x with
| Ptop_def [] -> None
| Ptop_def _ as x -> Some x
| Ptop_dir _ as x -> Some x )
| Core_type -> m.typ m
| Module_type -> m.module_type m
| Expression -> m.expr m
Expand Down
18 changes: 18 additions & 0 deletions test/passing/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5401,6 +5401,24 @@
(package ocamlformat)
(action (diff tests/verbose1.ml.err verbose1.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(package ocamlformat)
(action
(with-stdout-to w50.ml.stdout
(with-stderr-to w50.ml.stderr
(run %{bin:ocamlformat} --margin-check --no-comment-check -q --max-iters=3 %{dep:tests/w50.ml})))))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/w50.ml.ref w50.ml.stdout)))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/w50.ml.err w50.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(enabled_if (<> %{os_type} Win32))
Expand Down
20 changes: 20 additions & 0 deletions test/passing/tests/w50.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(* When using [--no-comment-check] (to format code despite warning 50),
We should not complain if doc-comments start appearing in the AST.
*)

module type T = sig
val test_raises_some_exc : ('a -> 'b) -> 'a -> bool;;
(** AAAA *)

val test_raises_this_exc : exn -> ('a -> 'b) -> 'a -> bool;;
(** BBBB *)
end

module T = struct

let test_raises_some_exc = 2;;
(** CCCC *)

let test_raises_this_exc = 3;;
(** DDDD *)
end
1 change: 1 addition & 0 deletions test/passing/tests/w50.ml.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--no-comment-check -q --max-iters=3
20 changes: 20 additions & 0 deletions test/passing/tests/w50.ml.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(* When using [--no-comment-check] (to format code despite warning 50), We
should not complain if doc-comments start appearing in the AST. *)

module type T = sig
val test_raises_some_exc : ('a -> 'b) -> 'a -> bool

(** AAAA *)

val test_raises_this_exc : exn -> ('a -> 'b) -> 'a -> bool
(** BBBB *)
end

module T = struct
let test_raises_some_exc = 2

(** CCCC *)

(** DDDD *)
let test_raises_this_exc = 3
end
Loading