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

Fix extension removal bug in Code_path.main_module_name #512

Merged
merged 3 commits into from
Jul 17, 2024
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ details.

### Other changes

- Fix a bug where `Code_path.main_module_name` would not properly remove
extensions from the filename and therefore return an invalid module name.
(#512, @NathanReb)

- Add `-unused-type-warnings` flag to the driver to allow users to disable
only the generation of warning 34 silencing structure items when using
`[@@deriving ...]` on type declarations. (#511, @mbarbin, @NathanReb)
Expand Down
7 changes: 6 additions & 1 deletion src/code_path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ type t = {
in_expr : bool;
}

let remove_all_extensions basename =
match String.split_on_char ~sep:'.' basename with
| [] -> assert false (* split_on_char never returns the empty list *)
| name :: _ -> name

let top_level ~file_path =
let main_module_name =
file_path |> Stdlib.Filename.basename |> Stdlib.Filename.remove_extension
file_path |> Stdlib.Filename.basename |> remove_all_extensions
|> String.capitalize_ascii
in
{
Expand Down
11 changes: 11 additions & 0 deletions test/code_path/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ let _ =
- : string =
"(code_path(main_module_name Test)(submodule_path())(enclosing_module Test)(enclosing_value())(value())(fully_qualified_path Test))"
|}]


let _ =
(* The main module name should properly remove all extensions *)
let code_path =
Code_path.top_level ~file_path:"some_dir/module_name.cppo.ml"
in
Code_path.main_module_name code_path
[%%expect{|
- : string = "Module_name"
|}]
Loading