@@ -122,6 +122,11 @@ let semantic_string semantic =
122122let source_string ~loc source =
123123 Pt. Pconst_string {source; semantic = decode_js_string ~loc source}
124124
125+ let template_source_from0 = function
126+ | source , Some ("js" | "*j" ) -> source
127+ | semantic , _ ->
128+ String_literal. encode_js_template (normalize_ppx_semantic_string semantic)
129+
125130let map_constant ~loc = function
126131 | Pconst_integer (s , suffix ) -> Pt. Pconst_integer (s, suffix)
127132 | Pconst_char semantic ->
@@ -600,9 +605,10 @@ module E = struct
600605 let inner = sub.expr sub {e with pexp_attributes = inner_attrs0} in
601606 await ~loc ~attrs: (sub.attributes sub await_attrs0) inner
602607 | Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
603- | Pexp_constant (Pconst_string (source, Some " js " ))
604- when has_template_attr attrs ->
608+ | Pexp_constant (Pconst_string (text, delimiter ))
609+ when has_template_attr attrs && delimiter <> Some " json " ->
605610 let attrs = remove_template_attr attrs in
611+ let source = template_source_from0 (text, delimiter) in
606612 template ~loc ~attrs [{txt = source; loc}] []
607613 | Pexp_constant x ->
608614 let template = has_template_attr attrs in
@@ -751,26 +757,31 @@ module E = struct
751757 " `json` literals do not support interpolation"
752758 in
753759 let rec collect sources values = function
760+ | [{pexp_desc = Pexp_constant (Pconst_string (_, Some " json" ))}] ->
761+ reject_json_interpolation ()
754762 | [
755763 {
756- pexp_desc = Pexp_constant (Pconst_string (txt, Some " js " ));
764+ pexp_desc = Pexp_constant (Pconst_string (text, delimiter ));
757765 pexp_loc;
758766 };
759767 ] ->
768+ let txt = template_source_from0 (text, delimiter) in
760769 Some
761770 ( List. rev
762771 ({Location. txt; loc = sub.location sub pexp_loc} :: sources),
763772 List. rev values )
764- | [{pexp_desc = Pexp_constant (Pconst_string (_, Some " json" ))}] ->
773+ | {pexp_desc = Pexp_constant (Pconst_string (_, Some " json" ))}
774+ :: _value :: _rest ->
765775 reject_json_interpolation ()
766- | {pexp_desc = Pexp_constant (Pconst_string (txt, Some " js" )); pexp_loc}
776+ | {
777+ pexp_desc = Pexp_constant (Pconst_string (text, delimiter));
778+ pexp_loc;
779+ }
767780 :: value :: rest ->
781+ let txt = template_source_from0 (text, delimiter) in
768782 collect
769783 ({Location. txt; loc = sub.location sub pexp_loc} :: sources)
770784 (value :: values) rest
771- | {pexp_desc = Pexp_constant (Pconst_string (_, Some " json" ))}
772- :: _value :: _rest ->
773- reject_json_interpolation ()
774785 | _ -> None
775786 in
776787 begin match collect [] [] parts with
0 commit comments