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 top level extensions #2693

Merged
merged 19 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.9 (unreleased)

- Fix missing patterns around contraint pattern (a pattern with a type annotation).
- Fix top level extension printing

## 3.8.2

Expand Down
2 changes: 2 additions & 0 deletions formatTest/unit_tests/expected_output/extensions.re
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ let () = {
something_else();
};

let f = [%bs.raw x => x];

[%bs.raw x => x];

let work = () => {
Expand Down
2 changes: 2 additions & 0 deletions formatTest/unit_tests/input/extensions.re
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ let () = {
something_else();
};

let f = [%bs.raw x => x];

[%bs.raw x => x];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we also accept this form? I think it should keep being tested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed back the test case, we do support them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still removed. did you forget to push?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added all of the cases in the cram tests. Let me add them in the formatTest as well


let work = () => {
Expand Down
31 changes: 22 additions & 9 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7576,29 +7576,42 @@ let printer = object(self:'self)
| Pmod_constraint _
| Pmod_structure _ -> self#simple_module_expr x


method structure structureItems =
(* We don't have any way to know if an extension is placed at the top level by the parsetree
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we supported extensions at the toplevel with %, and that %%bs.raw was specifically a bucklescript thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a BuckleScript thing, but we support both cases. Would be a nice idea to unify all of these cases with Melange. %bs.raw -> bs.raw_js_expr and %%bs.raw -> bs.raw_js or something like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get what needs to be unified?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could avoid having % and %% at the same time and support only %

while there's a difference syntactically (% for structure_items/expressons and %% for top_level).
This small fn detects this particular case (structure > structure_item > extension > value) and
prints with double % *)
let structure_item item =
match item.pstr_desc with
| Pstr_extension ((extension, PStr [item]), attrs) ->
begin match item.pstr_desc with
(* In case of a value, the extension gets inlined `let%private a = 1` *)
| Pstr_value (rf, vb_list) -> self#bindings ~extension (rf, vb_list)
| _ -> self#attach_std_item_attrs attrs (self#payload "%%" extension (PStr [item]))
end
| _ -> self#structure_item item
in
match structureItems with
| [] -> atom ""
| first::_ as structureItems ->
| first :: _ as structureItems ->
let last = match (List.rev structureItems) with | last::_ -> last | [] -> assert false in
let loc_start = first.pstr_loc.loc_start in
let loc_end = last.pstr_loc.loc_end in
let items =
groupAndPrint
~xf:self#structure_item
~xf:structure_item
~getLoc:(fun x -> x.pstr_loc)
~comments:self#comments
structureItems
in
source_map ~loc:{loc_start; loc_end; loc_ghost = false}
(makeList
~postSpace:true
~break:Always_rec
~indent:0
~inline:(true, false)
~sep:(SepFinal (";", ";"))
items)
~postSpace:true
~break:Always_rec
~indent:0
~inline:(true, false)
~sep:(SepFinal (";", ";"))
items)

(*
How do modules become parsed?
Expand Down
21 changes: 17 additions & 4 deletions test/extensions.t/input.re
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,29 @@ let () = {
something_else();
};

[%bs.raw x => x];

let work = () => {
open Syntax;
let%bind name = x;
name;
};

/** header */
[%raw "console.log(42)"];
/* Extensions can have % or %% at the top-level */

[%bs.raw x => x];

[%%bs.raw x => x];

[%%randomExtension "with string payload"];

[%%randomExtension { with_obj: 33 }];

[%randomExtension { with_obj: 33 }];

/** with a comment on top */
[%%raw "console.log(42)"];

/* extensions live under expresions with only one % */
let f = [%bs.raw x => x];

/* https://github.com/facebook/reason/issues/2032 */
let predicate =
Expand Down
21 changes: 17 additions & 4 deletions test/extensions.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,29 @@ Format extensions
something_else();
};

[%bs.raw x => x];

let work = () => {
open Syntax;
let%bind name = x;
name;
};

/** header */
[%raw "console.log(42)"];
/* Extensions can have % or %% at the top-level */

[%bs.raw x => x];

[%%bs.raw x => x];

[%%randomExtension "with string payload"];

[%%randomExtension {with_obj: 33}];

[%randomExtension {with_obj: 33}];

/** with a comment on top */
[%%raw "console.log(42)"];

/* extensions live under expresions with only one % */
let f = [%bs.raw x => x];

/* https://github.com/facebook/reason/issues/2032 */
let predicate =
Expand Down