Skip to content

Commit bf4d21b

Browse files
committed
Proof of concept: support directives.
Allow to specify directives emitted verbatim at the top of generated js files. See #5840
1 parent 1cd4aad commit bf4d21b

12 files changed

+73
-4
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# 10.1.3
1414

1515
#### :nail_care: Polish
16-
1716
- Add the gap property to jsxDOMStyle https://github.com/rescript-lang/rescript-compiler/pull/5956
1817

1918
#### :bug: Bug Fix
19+
2020
- Fix issue where error messages related to non-existent props were displayed without location information https://github.com/rescript-lang/syntax/pull/730
2121
- Fix issue where uncurried functions were incorrectly converting the type of a prop given as a default value to curried https://github.com/rescript-lang/syntax/pull/731
2222
- Fix issue with nested async functions, where the inner function would be emitted without `async` https://github.com/rescript-lang/rescript-compiler/pull/5984
@@ -28,6 +28,10 @@
2828
- Fix issue with using alias and default value together https://github.com/rescript-lang/syntax/pull/734
2929
- Fix issue in `Js.Promise2` where `then` and `catch` were returning `undefined` https://github.com/rescript-lang/rescript-compiler/pull/5996
3030

31+
#### :rocket: New Feature
32+
33+
- Add experimental suppport for directives. An annotation such as `@@directive("use client;")` emits `use client;` verbatim before imports https://github.com/rescript-lang/rescript-compiler/pull/5999
34+
3135
# 10.1.2
3236

3337
#### :bug: Bug Fix

jscomp/common/js_config.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ type jsx_module = React
3030
type jsx_mode = Classic | Automatic
3131

3232
let no_version_header = ref false
33+
let directives = ref []
3334
let cross_module_inline = ref false
3435
let diagnose = ref false
3536

3637
let get_diagnose () =
3738
!diagnose
3839

39-
# 38 "common/js_config.pp.ml"
40+
# 39 "common/js_config.pp.ml"
4041
|| Sys.getenv_opt "RES_DEBUG_FILE" <> None
4142

42-
# 41 "common/js_config.pp.ml"
43+
# 42 "common/js_config.pp.ml"
4344
(* let (//) = Filename.concat *)
4445

4546
(* let get_packages_info () = !packages_info *)

jscomp/common/js_config.mli

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type jsx_mode = Classic | Automatic
3232
val no_version_header : bool ref
3333
(** set/get header *)
3434

35+
val directives : string list ref
36+
(** directives printed verbatims just after the version header *)
37+
3538
(** return [package_name] and [path]
3639
when in script mode:
3740
*)

jscomp/common/js_config.pp.ml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type jsx_module = React
2929
type jsx_mode = Classic | Automatic
3030

3131
let no_version_header = ref false
32+
let directives = ref []
3233
let cross_module_inline = ref false
3334
let diagnose = ref false
3435

jscomp/core/js_dump_program.ml

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ let pp_deps_program ~(output_prefix : string)
105105
if not !Js_config.no_version_header then (
106106
P.string f Bs_version.header;
107107
P.newline f);
108+
!Js_config.directives |> List.iter (fun prim ->
109+
P.string f prim;
110+
P.newline f);
111+
108112
if deps_program_is_empty program then P.string f empty_explanation
109113
(* This is empty module, it won't be referred anywhere *)
110114
else

jscomp/frontend/bs_builtin_ppx.ml

+4
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
475475
[ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] );
476476
})
477477
| Pstr_attribute ({ txt = "bs.config" | "config" }, _) -> str
478+
| Pstr_attribute ({ txt = "directive" },
479+
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
480+
Js_config.directives := d :: !Js_config.directives;
481+
str
478482
| _ -> default_mapper.structure_item self str
479483

480484
let local_module_name =

jscomp/test/build.ninja

+2-1
Large diffs are not rendered by default.

jscomp/test/directives.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
first directive;
2+
second directive;
3+
'use strict';
4+
5+
var Belt_Array = require("../../lib/js/belt_Array.js");
6+
7+
var a = Belt_Array.forEach;
8+
9+
exports.a = a;
10+
/* No side effect */

jscomp/test/directives.res

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@@directive("first directive;")
2+
@@directive("second directive;")
3+
4+
let a = Belt.Array.forEach
5+

lib/4.06.1/unstable/js_compiler.ml

+12
Original file line numberDiff line numberDiff line change
@@ -17380,6 +17380,9 @@ type jsx_mode = Classic | Automatic
1738017380
val no_version_header : bool ref
1738117381
(** set/get header *)
1738217382

17383+
val directives : string list ref
17384+
(** directives printed verbatims just after the version header *)
17385+
1738317386
(** return [package_name] and [path]
1738417387
when in script mode:
1738517388
*)
@@ -17494,6 +17497,7 @@ type jsx_module = React
1749417497
type jsx_mode = Classic | Automatic
1749517498

1749617499
let no_version_header = ref false
17500+
let directives = ref []
1749717501
let cross_module_inline = ref false
1749817502
let diagnose = ref false
1749917503

@@ -89287,6 +89291,10 @@ let pp_deps_program ~(output_prefix : string)
8928789291
if not !Js_config.no_version_header then (
8928889292
P.string f Bs_version.header;
8928989293
P.newline f);
89294+
!Js_config.directives |> List.iter (fun prim ->
89295+
P.string f prim;
89296+
P.newline f);
89297+
8929089298
if deps_program_is_empty program then P.string f empty_explanation
8929189299
(* This is empty module, it won't be referred anywhere *)
8929289300
else
@@ -273344,6 +273352,10 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
273344273352
[ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] );
273345273353
})
273346273354
| Pstr_attribute ({ txt = "bs.config" | "config" }, _) -> str
273355+
| Pstr_attribute ({ txt = "directive" },
273356+
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
273357+
Js_config.directives := d :: !Js_config.directives;
273358+
str
273347273359
| _ -> default_mapper.structure_item self str
273348273360

273349273361
let local_module_name =

lib/4.06.1/unstable/js_playground_compiler.ml

+12
Original file line numberDiff line numberDiff line change
@@ -17380,6 +17380,9 @@ type jsx_mode = Classic | Automatic
1738017380
val no_version_header : bool ref
1738117381
(** set/get header *)
1738217382

17383+
val directives : string list ref
17384+
(** directives printed verbatims just after the version header *)
17385+
1738317386
(** return [package_name] and [path]
1738417387
when in script mode:
1738517388
*)
@@ -17494,6 +17497,7 @@ type jsx_module = React
1749417497
type jsx_mode = Classic | Automatic
1749517498

1749617499
let no_version_header = ref false
17500+
let directives = ref []
1749717501
let cross_module_inline = ref false
1749817502
let diagnose = ref false
1749917503

@@ -89287,6 +89291,10 @@ let pp_deps_program ~(output_prefix : string)
8928789291
if not !Js_config.no_version_header then (
8928889292
P.string f Bs_version.header;
8928989293
P.newline f);
89294+
!Js_config.directives |> List.iter (fun prim ->
89295+
P.string f prim;
89296+
P.newline f);
89297+
8929089298
if deps_program_is_empty program then P.string f empty_explanation
8929189299
(* This is empty module, it won't be referred anywhere *)
8929289300
else
@@ -273344,6 +273352,10 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
273344273352
[ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] );
273345273353
})
273346273354
| Pstr_attribute ({ txt = "bs.config" | "config" }, _) -> str
273355+
| Pstr_attribute ({ txt = "directive" },
273356+
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
273357+
Js_config.directives := d :: !Js_config.directives;
273358+
str
273347273359
| _ -> default_mapper.structure_item self str
273348273360

273349273361
let local_module_name =

lib/4.06.1/whole_compiler.ml

+12
Original file line numberDiff line numberDiff line change
@@ -180346,6 +180346,9 @@ type jsx_mode = Classic | Automatic
180346180346
val no_version_header : bool ref
180347180347
(** set/get header *)
180348180348

180349+
val directives : string list ref
180350+
(** directives printed verbatims just after the version header *)
180351+
180349180352
(** return [package_name] and [path]
180350180353
when in script mode:
180351180354
*)
@@ -180460,6 +180463,7 @@ type jsx_module = React
180460180463
type jsx_mode = Classic | Automatic
180461180464

180462180465
let no_version_header = ref false
180466+
let directives = ref []
180463180467
let cross_module_inline = ref false
180464180468
let diagnose = ref false
180465180469

@@ -258786,6 +258790,10 @@ let pp_deps_program ~(output_prefix : string)
258786258790
if not !Js_config.no_version_header then (
258787258791
P.string f Bs_version.header;
258788258792
P.newline f);
258793+
!Js_config.directives |> List.iter (fun prim ->
258794+
P.string f prim;
258795+
P.newline f);
258796+
258789258797
if deps_program_is_empty program then P.string f empty_explanation
258790258798
(* This is empty module, it won't be referred anywhere *)
258791258799
else
@@ -283731,6 +283739,10 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
283731283739
[ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] );
283732283740
})
283733283741
| Pstr_attribute ({ txt = "bs.config" | "config" }, _) -> str
283742+
| Pstr_attribute ({ txt = "directive" },
283743+
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
283744+
Js_config.directives := d :: !Js_config.directives;
283745+
str
283734283746
| _ -> default_mapper.structure_item self str
283735283747

283736283748
let local_module_name =

0 commit comments

Comments
 (0)