Skip to content

Commit 088c358

Browse files
cknittclaude
andcommitted
Add provisional %raw syntax for fixed external arguments
`%raw("{}")` writes a fixed external argument as a JavaScript literal. The `@as(value) _` form is deprecated and formats as `%raw(...)`; `json` literals become ordinary tagged templates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NP47qMfjMbjUTKZDsLthDo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 19a5a20 commit 088c358

98 files changed

Lines changed: 1048 additions & 372 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎analysis/src/completion_front_end.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ let rec expr_to_context_path_inner ~(in_jsx_context : bool)
210210
(e : Parsetree.expression) =
211211
match e.pexp_desc with
212212
| Pexp_braces {expr} -> expr_to_context_path_inner ~in_jsx_context expr
213-
| Pexp_constant (Pconst_string _ | Pconst_json _ | Pconst_raw_source _) ->
213+
| Pexp_constant (Pconst_string _ | Pconst_raw_source _) ->
214214
Some Completable.CPString
215215
| Pexp_template _ -> Some Completable.CPString
216216
| Pexp_tagged_template {tag} -> (

‎analysis/src/document_symbol.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let get_symbols ~source ~kind_file =
2020
| Pexp_fun _ -> Lsp.Types.SymbolKind.Function
2121
| Pexp_braces {expr} -> expr_kind expr
2222
| Pexp_constraint (e, _) -> expr_kind e
23-
| Pexp_constant (Pconst_string _ | Pconst_json _ | Pconst_raw_source _) ->
23+
| Pexp_constant (Pconst_string _ | Pconst_raw_source _) ->
2424
Lsp.Types.SymbolKind.String
2525
| Pexp_template _ -> Lsp.Types.SymbolKind.String
2626
| Pexp_constant (Pconst_float _ | Pconst_integer _) ->

‎analysis/src/dump_ast.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ let print_constant const =
5151
^ ", semantic="
5252
^ String_literal.string_semantic payload
5353
^ ")"
54-
| Pconst_json source -> "Pconst_json(" ^ source ^ ")"
5554
| Pconst_raw_source source -> "Pconst_raw_source(" ^ source ^ ")"
5655
| Pconst_float (s, _) -> "Pconst_float(" ^ s ^ ")"
5756

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add `%raw(...)` arguments for externals, for example `external assign: (%raw("{}"), t) => t = "Object.assign"`. The string contains the raw JavaScript value supplied by the binding, and the argument is omitted at call sites. The legacy `` @as(json`...`) _ ``, `@as("...") _`, and `@as(1) _` forms now emit a deprecation warning and are formatted as `%raw(...)`. https://github.com/rescript-lang/rescript/pull/8609

‎compiler/frontend/ast_attributes.ml‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,31 @@ let as_const (attrs : t) =
181181
{
182182
pstr_desc =
183183
Pstr_eval
184-
({pexp_desc = Pexp_constant (Pconst_json s); pexp_loc}, _);
184+
( {
185+
pexp_desc =
186+
Pexp_tagged_template
187+
{
188+
tag =
189+
{pexp_desc = Pexp_ident {txt = Lident "json"}};
190+
raw_sources = [{txt = s}];
191+
values = [];
192+
};
193+
pexp_loc;
194+
},
195+
_ );
185196
};
186197
] -> (
187-
(* Check that it is a valid object literal. *)
198+
(* Check that it is exactly one JavaScript literal. *)
188199
match
189200
Classify_function.classify
190201
~check:
191202
(pexp_loc, Bs_flow_ast_utils.flow_deli_offset (Some "json"))
192203
s
193204
with
194-
| Js_literal _ -> Json s
205+
| Js_literal _ when Classify_function.is_fixed_literal s -> Json s
195206
| _ ->
196-
Location.raise_errorf ~loc:pexp_loc "an object literal expected")
207+
Location.raise_errorf ~loc:pexp_loc
208+
"The json payload must be a JavaScript literal")
197209
| _ -> Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal)))
198210

199211
let locg = Location.none

‎compiler/frontend/ast_config.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ let process_directives str =
4747
|> List.iter (fun (item : Parsetree.structure_item) ->
4848
match item.pstr_desc with
4949
| Pstr_attribute ({txt = "directive"}, payload) -> (
50-
Ast_payload.reject_json_literal_payload payload;
5150
match Ast_payload.semantic_string_of_payload payload with
5251
| Some d ->
5352
(Js_config.current ()).directives :=

‎compiler/frontend/ast_exp_extension.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ let handle_extension e (_self : Ast_mapper.mapper)
2727
(({txt; loc}, payload) : Parsetree.extension) =
2828
match txt with
2929
| "todo" ->
30-
Ast_payload.reject_json_literal_payload payload;
3130
let todo_message =
3231
match Ast_payload.semantic_string_of_payload payload with
3332
| Some s -> Some s

‎compiler/frontend/ast_external_process.ml‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
203203
| PStr [] -> prim_name_or_pval_prim
204204
(* It is okay to have [@@val] without payload *)
205205
| _ -> (
206-
Ast_payload.reject_json_literal_payload payload;
207206
match Ast_payload.semantic_string_of_payload payload with
208207
| Some val_name -> {name = val_name; source = Payload}
209208
| None -> Location.raise_errorf ~loc "Invalid payload")
@@ -400,12 +399,25 @@ type response = {
400399
}
401400

402401
(* A fixed argument means what [@as(value) _] means, so decode it with the
403-
same reader. *)
402+
same reader. [%raw] is JavaScript source and must be exactly one literal. *)
404403
let fixed_arg_type
405404
((value, loc) : Parsetree.fixed_value Location.loc * Location.t) =
406-
match Ast_attributes.as_const [Ast_fixed_arg.to_attribute ~loc value] with
407-
| Some cst -> arg_cst_of_as_const cst
408-
| None -> Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal
405+
match value.txt with
406+
| Pfixed_raw source ->
407+
if Classify_function.is_fixed_literal source then
408+
External_arg_spec.Arg_cst (External_arg_spec.cst_json source)
409+
else
410+
Location.raise_errorf ~loc:value.loc
411+
"The %%raw payload must be a JavaScript literal"
412+
| Pfixed_int _ | Pfixed_string _ | Pfixed_template _ | Pfixed_json _ -> (
413+
match Ast_attributes.as_const [Ast_fixed_arg.to_attribute ~loc value] with
414+
| Some cst -> arg_cst_of_as_const cst
415+
| None -> Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal)
416+
417+
(* A fixed argument is always passed, so it has nothing to make optional. *)
418+
let reject_optional_fixed
419+
((_, loc) : Parsetree.fixed_value Location.loc * Location.t) =
420+
Location.raise_errorf ~loc "A %%raw argument cannot be optional"
409421

410422
(* The label and type of an argument. A fixed argument is typed as its [_], so
411423
the checks for [@as(value) _] apply to it unchanged. Its location ends with
@@ -416,7 +428,7 @@ let arg_parts (arg : Parsetree.arg) =
416428
| Parg_fixed {attrs; lbl; value; loc} ->
417429
( lbl,
418430
attrs,
419-
Ast_helper.Typ.any ~loc:(Ast_fixed_arg.underscore_loc loc) (),
431+
Ast_helper.Typ.any ~loc:(Ast_fixed_arg.closing_loc loc) (),
420432
Some (value, loc) )
421433

422434
let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
@@ -529,7 +541,11 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
529541
| Some alias -> alias
530542
| None -> label
531543
in
532-
let obj_arg_type = get_opt_arg_type ~nolabel:false ty in
544+
let obj_arg_type =
545+
match fixed with
546+
| Some fixed -> reject_optional_fixed fixed
547+
| None -> get_opt_arg_type ~nolabel:false ty
548+
in
533549
match obj_arg_type with
534550
| Ignore ->
535551
( External_arg_spec.empty_kind obj_arg_type,
@@ -956,7 +972,11 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
956972
new_arg_types ) =
957973
match arg_label with
958974
| Optional {txt = s} -> (
959-
let arg_type = get_opt_arg_type ~nolabel:false ty in
975+
let arg_type =
976+
match fixed with
977+
| Some fixed -> reject_optional_fixed fixed
978+
| None -> get_opt_arg_type ~nolabel:false ty
979+
in
960980
match arg_type with
961981
| Poly_var _ ->
962982
(* ?x:([`x of int ] [@string]) does not make sense *)

‎compiler/frontend/bs_builtin_ppx.ml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ let pat_mapper (self : mapper) (p : Parsetree.pattern) =
101101
match p.ppat_desc with
102102
| Ppat_constant (Pconst_integer (s, Some 'l')) ->
103103
{p with ppat_desc = Ppat_constant (Pconst_integer (s, None))}
104-
| Ppat_constant (Pconst_json _) ->
105-
Location.raise_errorf ~loc:p.ppat_loc
106-
"Tagged template literals are not supported in patterns"
107104
| _ -> default_pat_mapper self p
108105

109106
(* Unpack requires core_type package for type inference:
@@ -598,9 +595,6 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
598595
let has_inline_property =
599596
Ast_attributes.has_inline_payload pvb_attributes
600597
in
601-
Option.iter
602-
(fun (_, payload) -> Ast_payload.reject_json_literal_payload payload)
603-
has_inline_property;
604598
match
605599
(has_inline_property, (Ast_payload.unwrap_braces pvb_expr).pexp_desc)
606600
with

‎compiler/frontend/ppx_entry.ml‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,6 @@
2424

2525
let unsafe_mapper = Bs_builtin_ppx.mapper
2626

27-
(* [json] payloads are syntax-level expressions until built-in FFI processing
28-
consumes valid [@as(json`...`)] occurrences. Reject anything left only
29-
after that processing, so generic attributes and ordinary expressions
30-
cannot reinterpret them as strings. *)
31-
let unconsumed_json_iterator =
32-
let default = Ast_iterator.default_iterator in
33-
{
34-
default with
35-
expr =
36-
(fun self expression ->
37-
match expression.pexp_desc with
38-
| Pexp_constant (Pconst_json _) ->
39-
Ast_payload.reject_json_literal ~loc:expression.pexp_loc
40-
| _ -> default.expr self expression);
41-
pat =
42-
(fun self pattern ->
43-
match pattern.ppat_desc with
44-
| Ppat_constant (Pconst_json _) ->
45-
Ast_payload.reject_json_literal ~loc:pattern.ppat_loc
46-
| _ -> default.pat self pattern);
47-
}
48-
4927
let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
5028
Bs_ast_invariant.iter_warnings_on_sigi ast;
5129
Ast_config.process_sig ast;
@@ -61,7 +39,6 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
6139
if !((Js_config.current ()).no_builtin_ppx) then ast
6240
else
6341
let result = unsafe_mapper.signature unsafe_mapper ast in
64-
unconsumed_json_iterator.signature unconsumed_json_iterator result;
6542
(* Keep this check, since the check is not inexpensive*)
6643
Bs_ast_invariant.emit_external_warnings_on_signature result;
6744
result
@@ -81,7 +58,6 @@ let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
8158
if !((Js_config.current ()).no_builtin_ppx) then ast
8259
else
8360
let result = unsafe_mapper.structure unsafe_mapper ast in
84-
unconsumed_json_iterator.structure unconsumed_json_iterator result;
8561
(* Keep this check since it is not inexpensive*)
8662
Bs_ast_invariant.emit_external_warnings_on_structure result;
8763
result

0 commit comments

Comments
 (0)