Skip to content
Closed
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
2 changes: 1 addition & 1 deletion analysis/src/completion_front_end.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ let rec expr_to_context_path_inner ~(in_jsx_context : bool)
(e : Parsetree.expression) =
match e.pexp_desc with
| Pexp_braces {expr} -> expr_to_context_path_inner ~in_jsx_context expr
| Pexp_constant (Pconst_string _ | Pconst_json _ | Pconst_raw_source _) ->
| Pexp_constant (Pconst_string _ | Pconst_raw_source _) ->
Some Completable.CPString
| Pexp_template _ -> Some Completable.CPString
| Pexp_tagged_template {tag} -> (
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/document_symbol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let get_symbols ~source ~kind_file =
| Pexp_fun _ -> Lsp.Types.SymbolKind.Function
| Pexp_braces {expr} -> expr_kind expr
| Pexp_constraint (e, _) -> expr_kind e
| Pexp_constant (Pconst_string _ | Pconst_json _ | Pconst_raw_source _) ->
| Pexp_constant (Pconst_string _ | Pconst_raw_source _) ->
Lsp.Types.SymbolKind.String
| Pexp_template _ -> Lsp.Types.SymbolKind.String
| Pexp_constant (Pconst_float _ | Pconst_integer _) ->
Expand Down
1 change: 0 additions & 1 deletion analysis/src/dump_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ let print_constant const =
^ ", semantic="
^ String_literal.string_semantic payload
^ ")"
| Pconst_json source -> "Pconst_json(" ^ source ^ ")"
| Pconst_raw_source source -> "Pconst_raw_source(" ^ source ^ ")"
| Pconst_float (s, _) -> "Pconst_float(" ^ s ^ ")"

Expand Down
59 changes: 32 additions & 27 deletions analysis/src/signature_help.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,38 @@ let extract_parameters ~signature ~type_str_for_parser ~label_prefix_len =
Psig_value {pval_type = {ptyp_desc = Ptyp_arrow {params = args}}};
};
] ->
List.map
(fun (arg : Parsetree.arg) ->
let start_loc =
(* For a labeled argument the label precedes the type. *)
match arg.lbl with
| Asttypes.Labelled {loc} | Optional {loc} -> loc |> Loc.start
| Nolabel -> arg.typ.ptyp_loc |> Loc.start
in
let start_offset =
start_loc |> Pos.position_to_offset type_str_for_parser |> Option.get
in
let end_offset =
arg.typ.ptyp_loc |> Loc.end_
|> Pos.position_to_offset type_str_for_parser
|> Option.get
in
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
let end_offset =
match arg.lbl with
| Asttypes.Optional _ -> end_offset + 2
| _ -> end_offset
in
( arg.lbl,
(* Remove the label prefix offset here, since we're not
showing that to the end user. *)
start_offset - label_prefix_len,
end_offset - label_prefix_len ))
List.filter_map
(function
| Parsetree.Parg_fixed _ -> None
| Parsetree.Parg_type {lbl; typ; _} ->
let start_loc =
(* For a labeled argument the label precedes the type. *)
match lbl with
| Asttypes.Labelled {loc} | Optional {loc} -> loc |> Loc.start
| Nolabel -> typ.ptyp_loc |> Loc.start
in
let start_offset =
start_loc
|> Pos.position_to_offset type_str_for_parser
|> Option.get
in
let end_offset =
typ.ptyp_loc |> Loc.end_
|> Pos.position_to_offset type_str_for_parser
|> Option.get
in
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
let end_offset =
match lbl with
| Asttypes.Optional _ -> end_offset + 2
| _ -> end_offset
in
Some
( lbl,
(* Remove the label prefix offset here, since we're not
showing that to the end user. *)
start_offset - label_prefix_len,
end_offset - label_prefix_len ))
args
| _ -> []

Expand Down
1 change: 1 addition & 0 deletions changes/raw-external-arguments.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +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
7 changes: 3 additions & 4 deletions compiler/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,9 @@ and expression_desc =
optimizations. Both are required because preserving backquoted
spelling is an output design goal. Tagged-template segments use
[Tagged_template] instead because their escapes need not be valid. *)
| Json_literal of string
(** Validated JavaScript literal source from a supported external
[json`...`] payload. For [json`{"ok": true}`], the string contains
[{"ok": true}] as JavaScript source, not as a decoded ReScript
| Fixed_literal of string
(** Validated JavaScript literal source supplied by a fixed external
argument. The string is JavaScript source, not a decoded ReScript
string. *)
| Raw_js_code of Js_raw_info.t
(** JavaScript source originating from [raw], [ffi], or [re]. For example,
Expand Down
4 changes: 2 additions & 2 deletions compiler/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
| Undefined _ | Null | Bool _ | Var _ -> true
| Fun _ -> true
| Number _ -> true (* Can be refined later *)
| Json_literal _ -> true
| Fixed_literal _ -> true
| Static_index (obj, (_name : string), (_pos : int32 option)) ->
no_side_effect obj
| Array_index (a, b) -> no_side_effect a && no_side_effect b
Expand Down Expand Up @@ -263,7 +263,7 @@ let rec eq_expression ({expression_desc = x0} : J.expression)
eq_expression_list ls0 ls1 && flag0 = flag1 && info0 = info1
| _ -> false)
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
| Js_bnot _ | In _ | Cond _ | New _ | Fun _ | Json_literal _ | Raw_js_code _
| Js_bnot _ | In _ | Cond _ | New _ | Fun _ | Fixed_literal _ | Raw_js_code _
| Array _ | Caml_block_tag _ | Object _ | Tagged_template _ | Jsx _
| Interpolated_template _ | Await _ | Record_rest _ ->
false
Expand Down
6 changes: 3 additions & 3 deletions compiler/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ let rec exp_need_paren ?(arrow = false) (e : J.expression) =
| Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ ) )
| Object _ ->
true
| Json_literal _ -> true
| Fixed_literal _ -> true
| Raw_js_code {code_info = Stmt _}
| Length _ | Call _ | Caml_block_tag _ | Seq _ | Static_index _ | Cond _
| Bin _ | Is_null_or_undefined _ | Array_index _ | String_append _ | Var _
Expand Down Expand Up @@ -701,7 +701,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
| Template_literal segment ->
P.string f ("`" ^ String_literal.template_source segment ^ "`");
cxt
| Json_literal source ->
| Fixed_literal source ->
P.string f source;
cxt
| Raw_js_code {code = s; code_info = info} -> (
Expand Down Expand Up @@ -1243,7 +1243,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
| Some s -> P.string f s
| None -> ());
cxt
| Str _ | Template_literal _ | Json_literal _ -> cxt
| Str _ | Template_literal _ | Fixed_literal _ -> cxt
| _ ->
let cxt =
(if exp_need_paren e then P.paren_group f 1 else P.group f 0) (fun _ ->
Expand Down
6 changes: 3 additions & 3 deletions compiler/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type t = J.expression
*)
let rec remove_pure_sub_exp (x : t) : t option =
match x.expression_desc with
| Var _ | Str _ | Template_literal _ | Json_literal _ | Number _ ->
| Var _ | Str _ | Template_literal _ | Fixed_literal _ | Number _ ->
None (* Can be refined later *)
| Array_index (a, b) ->
if is_pure_sub_exp a && is_pure_sub_exp b then None else Some x
Expand Down Expand Up @@ -209,8 +209,8 @@ let str ?comment txt : t =
let template_literal ?comment segment : t =
{expression_desc = Template_literal segment; comment; source_loc = None}

let json_literal ?comment source : t =
{expression_desc = Json_literal source; comment; source_loc = None}
let fixed_literal ?comment source : t =
{expression_desc = Fixed_literal source; comment; source_loc = None}

let raw_js_code ?comment info s : t =
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ val str : ?comment:string -> string -> t

val template_literal : ?comment:string -> Asttypes.template_segment -> t

val json_literal : ?comment:string -> string -> t
val fixed_literal : ?comment:string -> string -> t

val record_rest : ?comment:string -> J.record_rest_field list -> t -> t

Expand Down
2 changes: 1 addition & 1 deletion compiler/core/js_record_fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ let expression_desc : 'a. ('a, expression_desc) fn =
st
| Str _ -> st
| Template_literal _ -> st
| Json_literal _ -> st
| Fixed_literal _ -> st
| Raw_js_code _ -> st
| Array _x0 -> list _self.expression _self st _x0
| Optional_block (_x0, _x1) ->
Expand Down
2 changes: 1 addition & 1 deletion compiler/core/js_record_iter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ let expression_desc : expression_desc fn =
_self.block _self body
| Str _ -> ()
| Template_literal _ -> ()
| Json_literal _ -> ()
| Fixed_literal _ -> ()
| Raw_js_code _ -> ()
| Array _x0 -> list _self.expression _self _x0
| Optional_block (_x0, _x1) -> _self.expression _self _x0
Expand Down
2 changes: 1 addition & 1 deletion compiler/core/js_record_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ let expression_desc : expression_desc fn =
Fun {fun_ with params; body}
| Str _ as v -> v
| Template_literal _ as v -> v
| Json_literal _ as v -> v
| Fixed_literal _ as v -> v
| Raw_js_code _ as v -> v
| Array _x0 ->
let _x0 = list _self.expression _self _x0 in
Expand Down
4 changes: 1 addition & 3 deletions compiler/core/lam_compile_const.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,4 @@ and translate (x : Lambda.structured_constant) : J.expression =

let translate_arg_cst (cst : External_arg_spec.cst) =
match cst with
| Arg_int_lit i -> E.int (Int32.of_int i)
| Arg_string_lit s -> E.str s
| Arg_json_lit s -> E.json_literal s
| Arg_fixed_lit source -> E.fixed_literal source
35 changes: 8 additions & 27 deletions compiler/frontend/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,36 +165,17 @@ let as_int (attrs : t) =
| None -> Bs_syntaxerr.err loc Expect_int_literal
| Some v -> v)

type as_const_payload = Int of int | Str of string | Json of string
type as_const_payload = Fixed of string

let as_const (attrs : t) =
single_as attrs ~decode:(fun ~loc payload ->
match Ast_payload.is_single_int payload with
| Some v -> Int v
| None -> (
match Ast_payload.semantic_string_of_payload payload with
| Some s -> Str s
| None -> (
match Ast_payload.unwrap_payload_expression payload with
| PStr
[
{
pstr_desc =
Pstr_eval
({pexp_desc = Pexp_constant (Pconst_json s); pexp_loc}, _);
};
] -> (
(* Check that it is a valid object literal. *)
match
Classify_function.classify
~check:
(pexp_loc, Bs_flow_ast_utils.flow_deli_offset (Some "json"))
s
with
| Js_literal _ -> Json s
| _ ->
Location.raise_errorf ~loc:pexp_loc "an object literal expected")
| _ -> Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal)))
match Ast_payload.fixed_source_of_payload payload with
| Some source when Classify_function.is_fixed_literal source ->
Fixed source
| Some _ ->
Location.raise_errorf ~loc
"The fixed-value @as payload must be a JavaScript literal"
| None -> Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal)

let locg = Location.none

Expand Down
2 changes: 1 addition & 1 deletion compiler/frontend/ast_attributes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ val has_unwrap_attr : t -> bool

val as_int : t -> int option

type as_const_payload = Int of int | Str of string | Json of string
type as_const_payload = Fixed of string
val as_const : t -> as_const_payload option

val process_derive_type : t -> derive_attr * t
Expand Down
1 change: 0 additions & 1 deletion compiler/frontend/ast_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ let process_directives str =
|> List.iter (fun (item : Parsetree.structure_item) ->
match item.pstr_desc with
| Pstr_attribute ({txt = "directive"}, payload) -> (
Ast_payload.reject_json_literal_payload payload;
match Ast_payload.semantic_string_of_payload payload with
| Some d ->
(Js_config.current ()).directives :=
Expand Down
8 changes: 5 additions & 3 deletions compiler/frontend/ast_derive_projector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ let init () =
Ext_list.map label_declarations (fun {pld_name; pld_type} ->
Ast_comb.single_non_rec_val ?attrs:gentype_attrs pld_name
(Ast_helper.Typ.arrow
[{attrs = []; lbl = Nolabel; typ = core_type}]
[
Parg_type {attrs = []; lbl = Nolabel; typ = core_type};
]
pld_type
(*arity will alwys be 1 since these are single param functions*)))
| Ptype_variant constructor_declarations ->
Expand Down Expand Up @@ -160,8 +162,8 @@ let init () =
{loc; txt = Ext_string.uncapitalize_ascii con_name}
(match
Ext_list.map pcd_args (fun x ->
({attrs = []; lbl = Nolabel; typ = x}
: Parsetree.arg))
Parsetree.Parg_type
{attrs = []; lbl = Nolabel; typ = x})
with
| [] ->
(* zero-argument constructor: the accessor is a value *)
Expand Down
1 change: 0 additions & 1 deletion compiler/frontend/ast_exp_extension.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ let handle_extension e (_self : Ast_mapper.mapper)
(({txt; loc}, payload) : Parsetree.extension) =
match txt with
| "todo" ->
Ast_payload.reject_json_literal_payload payload;
let todo_message =
match Ast_payload.semantic_string_of_payload payload with
| Some s -> Some s
Expand Down
19 changes: 14 additions & 5 deletions compiler/frontend/ast_exp_handle_external.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let handle_debugger loc (payload : Ast_payload.t) =
Ast_external_mk.local_external_apply loc ~pval_prim:(Prim_name "%debugger")
~pval_type:
(Ast_helper.Typ.arrow
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
[Parg_type {attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
(Ast_literal.type_unit ()))
[Ast_literal.val_unit ~loc ()]
| _ ->
Expand All @@ -49,7 +49,10 @@ let handle_raw loc payload =
~pval_prim:(Prim_name "#raw_expr")
~pval_type:
(Ast_helper.Typ.arrow
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
[
Parg_type
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
]
(Ast_helper.Typ.any ()))
[exp];
pexp_attributes =
Expand Down Expand Up @@ -79,7 +82,7 @@ let handle_ffi ~loc ~payload =
let effective_arity = if arity = 0 then 1 else arity in
let args =
Ext_list.init effective_arity (fun _ ->
({attrs = []; lbl = Nolabel; typ = any} : Parsetree.arg))
Parsetree.Parg_type {attrs = []; lbl = Nolabel; typ = any})
in
Ast_helper.Typ.arrow ~loc args any
in
Expand All @@ -95,7 +98,10 @@ let handle_ffi ~loc ~payload =
~pval_prim:(Prim_name "#raw_expr")
~pval_type:
(Ast_helper.Typ.arrow
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
[
Parg_type
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
]
(Ast_helper.Typ.any ()))
[exp];
pexp_attributes =
Expand All @@ -115,7 +121,10 @@ let handle_raw_structure loc payload =
~pval_prim:(Prim_name "#raw_stmt")
~pval_type:
(Ast_helper.Typ.arrow
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
[
Parg_type
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
]
(Ast_helper.Typ.any ()))
[exp];
}
Expand Down
Loading
Loading