From e282c440ddde23c46abb84ec7185115a4258d884 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 18 Apr 2023 01:56:29 -0300 Subject: [PATCH 1/6] initial tests --- jscomp/frontend/ast_config.ml | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/jscomp/frontend/ast_config.ml b/jscomp/frontend/ast_config.ml index 8b0917292e..6108d65784 100644 --- a/jscomp/frontend/ast_config.ml +++ b/jscomp/frontend/ast_config.ml @@ -46,17 +46,24 @@ let process_directives str = str |> List.iter (fun (item : Parsetree.structure_item) -> match item.pstr_desc with - | Pstr_attribute - ( {txt = "directive"}, - PStr - [ - { - pstr_desc = - Pstr_eval - ({pexp_desc = Pexp_constant (Pconst_string (d, _))}, _); - }; - ] ) -> - Js_config.directives := !Js_config.directives @ [d] + | Pstr_attribute ({txt = "directive"}, payload) -> ( + match payload with + | PStr structure -> ( + match structure with + | [item] -> ( + match item.pstr_desc with + | Pstr_eval + ({pexp_desc = Pexp_constant (Pconst_string (d, _))}, _) -> + Js_config.directives := !Js_config.directives @ [d] + | _ -> + Location.raise_errorf ~loc:item.pstr_loc + "@@@directive argument expect an string") + | _ -> + Location.raise_errorf ~loc:item.pstr_loc + "@@@directive attribute expect an argument") + | _ -> + Location.raise_errorf ~loc:item.pstr_loc + "@@@directive attribute can only be applied to a string") | Pstr_attribute ({txt = "uncurried"}, _) -> Config.uncurried := Uncurried | _ -> ()) From 1e644bc8656c07f28d4c24c976c08fb106233cc7 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 18 Apr 2023 03:22:13 -0300 Subject: [PATCH 2/6] fix typo --- jscomp/frontend/ast_config.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jscomp/frontend/ast_config.ml b/jscomp/frontend/ast_config.ml index 6108d65784..569be866a5 100644 --- a/jscomp/frontend/ast_config.ml +++ b/jscomp/frontend/ast_config.ml @@ -57,7 +57,7 @@ let process_directives str = Js_config.directives := !Js_config.directives @ [d] | _ -> Location.raise_errorf ~loc:item.pstr_loc - "@@@directive argument expect an string") + "@@@directive argument expect a string") | _ -> Location.raise_errorf ~loc:item.pstr_loc "@@@directive attribute expect an argument") From d668cd964fbe6631bef98ee40422c19f84abc555 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 18 Apr 2023 03:46:09 -0300 Subject: [PATCH 3/6] refactor --- jscomp/frontend/ast_config.ml | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/jscomp/frontend/ast_config.ml b/jscomp/frontend/ast_config.ml index 569be866a5..9f7eb7e311 100644 --- a/jscomp/frontend/ast_config.ml +++ b/jscomp/frontend/ast_config.ml @@ -47,23 +47,9 @@ let process_directives str = |> List.iter (fun (item : Parsetree.structure_item) -> match item.pstr_desc with | Pstr_attribute ({txt = "directive"}, payload) -> ( - match payload with - | PStr structure -> ( - match structure with - | [item] -> ( - match item.pstr_desc with - | Pstr_eval - ({pexp_desc = Pexp_constant (Pconst_string (d, _))}, _) -> - Js_config.directives := !Js_config.directives @ [d] - | _ -> - Location.raise_errorf ~loc:item.pstr_loc - "@@@directive argument expect a string") - | _ -> - Location.raise_errorf ~loc:item.pstr_loc - "@@@directive attribute expect an argument") - | _ -> - Location.raise_errorf ~loc:item.pstr_loc - "@@@directive attribute can only be applied to a string") + match Ast_payload.is_single_string payload with + | Some (d, _) -> Js_config.directives := !Js_config.directives @ [d] + | None -> Bs_syntaxerr.err item.pstr_loc Expect_string_literal) | Pstr_attribute ({txt = "uncurried"}, _) -> Config.uncurried := Uncurried | _ -> ()) From 076c7fd9c8c92b2222d4dc99992c24e220977fb8 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 18 Apr 2023 04:14:59 -0300 Subject: [PATCH 4/6] add test --- .../super_errors/expected/directive_attr.res.expected | 9 +++++++++ .../build_tests/super_errors/fixtures/directive_attr.res | 1 + 2 files changed, 10 insertions(+) create mode 100644 jscomp/build_tests/super_errors/expected/directive_attr.res.expected create mode 100644 jscomp/build_tests/super_errors/fixtures/directive_attr.res diff --git a/jscomp/build_tests/super_errors/expected/directive_attr.res.expected b/jscomp/build_tests/super_errors/expected/directive_attr.res.expected new file mode 100644 index 0000000000..710f63458d --- /dev/null +++ b/jscomp/build_tests/super_errors/expected/directive_attr.res.expected @@ -0,0 +1,9 @@ + + We've found a bug for you! + /.../fixtures/directive_attr.res:1:1-11 + + 1 │ @@directive + 2 │ + 3 │ @@directive(2) + + expect string literal \ No newline at end of file diff --git a/jscomp/build_tests/super_errors/fixtures/directive_attr.res b/jscomp/build_tests/super_errors/fixtures/directive_attr.res new file mode 100644 index 0000000000..ec80ff9c0d --- /dev/null +++ b/jscomp/build_tests/super_errors/fixtures/directive_attr.res @@ -0,0 +1 @@ +@@directive From db874e4888a549fd8a7fdbca02bf6b056df13d23 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 18 Apr 2023 04:16:19 -0300 Subject: [PATCH 5/6] update tests --- .../super_errors/expected/directive_attr.res.expected | 1 - 1 file changed, 1 deletion(-) diff --git a/jscomp/build_tests/super_errors/expected/directive_attr.res.expected b/jscomp/build_tests/super_errors/expected/directive_attr.res.expected index 710f63458d..ee4b5d89e1 100644 --- a/jscomp/build_tests/super_errors/expected/directive_attr.res.expected +++ b/jscomp/build_tests/super_errors/expected/directive_attr.res.expected @@ -4,6 +4,5 @@ 1 │ @@directive 2 │ - 3 │ @@directive(2) expect string literal \ No newline at end of file From adb87ac8b4d5332497b20a1ecb916f1589f8f368 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 18 Apr 2023 04:17:22 -0300 Subject: [PATCH 6/6] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 602c95d206..faedd3378d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ #### :nail_care: Polish - Update list of reserved JS keywords. https://github.com/rescript-lang/rescript-compiler/pull/6167 +- Add error message to `@@directive`. https://github.com/rescript-lang/rescript-compiler/pull/6174 # 11.0.0-alpha.3