From ed396777126988cb25cfa9d718fa293ec923a14b Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sat, 11 Mar 2023 10:19:08 +0100 Subject: [PATCH] Deprecate unsafe ``` j`$(a)$(b)` ``` interpolation --- CHANGELOG.md | 1 + .../expected/jinterp.res.expected | 9 +++ .../super_errors/fixtures/jinterp.res | 3 + jscomp/frontend/ast_utf8_string_interp.ml | 6 +- jscomp/stdlib-406/arg.ml | 4 +- jscomp/stdlib-406/printexc.ml | 60 +++++++++---------- lib/4.06.1/unstable/all_ounit_tests.ml | 6 +- lib/4.06.1/unstable/js_compiler.ml | 6 +- lib/4.06.1/unstable/js_playground_compiler.ml | 6 +- lib/4.06.1/whole_compiler.ml | 6 +- 10 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 jscomp/build_tests/super_errors/expected/jinterp.res.expected create mode 100644 jscomp/build_tests/super_errors/fixtures/jinterp.res diff --git a/CHANGELOG.md b/CHANGELOG.md index 30b5bad143..fb0d3204e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - Better error message for extension point https://github.com/rescript-lang/rescript-compiler/pull/6057 - Improve format check help https://github.com/rescript-lang/rescript-compiler/pull/6056 +- Deprecate unsafe ``` j`$(a)$(b)` ``` interpolation: use string templates ``` `${a}${b}` ``` instead https://github.com/rescript-lang/rescript-compiler/pull/6067 # 10.1.3 diff --git a/jscomp/build_tests/super_errors/expected/jinterp.res.expected b/jscomp/build_tests/super_errors/expected/jinterp.res.expected new file mode 100644 index 0000000000..b484e68eec --- /dev/null +++ b/jscomp/build_tests/super_errors/expected/jinterp.res.expected @@ -0,0 +1,9 @@ + + Warning number 3 + /.../fixtures/jinterp.res:3:10-21 + + 1 │ + 2 │ let a = 11 + 3 │ let b = j`number $(a)` + + deprecated: The unsafe j`$(a)$(b)` interpolation is deprecated, use string template `${a}${b}` instead. \ No newline at end of file diff --git a/jscomp/build_tests/super_errors/fixtures/jinterp.res b/jscomp/build_tests/super_errors/fixtures/jinterp.res new file mode 100644 index 0000000000..cd6608feed --- /dev/null +++ b/jscomp/build_tests/super_errors/fixtures/jinterp.res @@ -0,0 +1,3 @@ + +let a = 11 +let b = j`number $(a)` \ No newline at end of file diff --git a/jscomp/frontend/ast_utf8_string_interp.ml b/jscomp/frontend/ast_utf8_string_interp.ml index e16404da46..fdbb5c7500 100644 --- a/jscomp/frontend/ast_utf8_string_interp.ml +++ b/jscomp/frontend/ast_utf8_string_interp.ml @@ -409,7 +409,11 @@ let transform_exp (e : Parsetree.expression) s delim : Parsetree.expression = e with pexp_desc = Pexp_constant (Pconst_string (js_str, Delim.escaped)); } - | J -> transform_interp e.pexp_loc s + | J -> + Location.prerr_warning e.pexp_loc + (Warnings.Deprecated + ("The unsafe j`$(a)$(b)` interpolation is deprecated, use string template `${a}${b}` instead.", e.pexp_loc, e.pexp_loc)); + transform_interp e.pexp_loc s | Unrecognized -> e diff --git a/jscomp/stdlib-406/arg.ml b/jscomp/stdlib-406/arg.ml index f2137c00ac..213411021c 100644 --- a/jscomp/stdlib-406/arg.ml +++ b/jscomp/stdlib-406/arg.ml @@ -81,9 +81,9 @@ let print_spec buf (key, spec, doc) = match spec with | Symbol (l, _) -> let sym = make_symlist "{" "|" "}" l in - Buffer.add_string buf {j| $(key) $(sym)$(doc)\n|j} + Buffer.add_string buf (((((" " ^ key) ^ " ") ^ sym) ^ doc) ^ "\n") | _ -> - Buffer.add_string buf {j| $(key) $(doc)\n|j} + Buffer.add_string buf ((((" " ^ key) ^ " ") ^ doc) ^ "\n") diff --git a/jscomp/stdlib-406/printexc.ml b/jscomp/stdlib-406/printexc.ml index 0db4641fa3..0dd6bf5dc6 100644 --- a/jscomp/stdlib-406/printexc.ml +++ b/jscomp/stdlib-406/printexc.ml @@ -12,16 +12,21 @@ (* special exception on linking described in the file LICENSE. *) (* *) (**************************************************************************) -[@@@bs.config { flags = [|"-bs-no-cross-module-opt" |]}] - +[@@@bs.config { flags = [| "-bs-no-cross-module-opt" |] }] let printers = ref [] -let locfmt s (linum : int) (start : int) (finish : int) msg = - {j|File "$(s)", line $(linum), characters $(start)-$(finish): $(msg)|j} - +let locfmt s (linum : int) (start : int) (finish : int) msg = + (((((((("File \"" ^ s) ^ "\", line ") ^ __unsafe_cast linum) ^ ", characters ") + ^ __unsafe_cast start) + ^ "-") + ^ __unsafe_cast finish) + ^ ": ") + ^ msg -let fields : exn -> string = [%raw{|function(x){ +let fields : exn -> string = + [%raw + {|function(x){ var s = "" var index = 1 while ("_"+index in x){ @@ -33,47 +38,38 @@ let fields : exn -> string = [%raw{|function(x){ } return "(" + s + ")" } -|}] - - - - +|}] external exn_slot_name : exn -> string = "?exn_slot_name" -let to_string x = +let to_string x = let rec conv = function - | hd :: tl -> - (match try hd x with _ -> None with - | Some s -> s - | None -> conv tl) - | [] -> + | hd :: tl -> ( + match try hd x with _ -> None with Some s -> s | None -> conv tl) + | [] -> ( match x with - | Match_failure(file, line, char) -> - locfmt file line char (char+5) "Pattern matching failed" - | Assert_failure(file, line, char) -> - locfmt file line char (char+6) "Assertion failed" - | Undefined_recursive_module(file, line, char) -> - locfmt file line char (char+6) "Undefined recursive module" + | Match_failure (file, line, char) -> + locfmt file line char (char + 5) "Pattern matching failed" + | Assert_failure (file, line, char) -> + locfmt file line char (char + 6) "Assertion failed" + | Undefined_recursive_module (file, line, char) -> + locfmt file line char (char + 6) "Undefined recursive module" | _ -> - let constructor = - exn_slot_name x in - constructor ^ fields x in + let constructor = exn_slot_name x in + constructor ^ fields x) + in conv !printers let print fct arg = - try - fct arg + try fct arg with x -> Js.log ("Uncaught exception: " ^ to_string x); raise x let catch fct arg = - try - fct arg + try fct arg with x -> Js.log ("Uncaught exception: " ^ to_string x); exit 2 -let register_printer fn = - printers := fn :: !printers +let register_printer fn = printers := fn :: !printers diff --git a/lib/4.06.1/unstable/all_ounit_tests.ml b/lib/4.06.1/unstable/all_ounit_tests.ml index 5eb71ebc44..376a960d1e 100644 --- a/lib/4.06.1/unstable/all_ounit_tests.ml +++ b/lib/4.06.1/unstable/all_ounit_tests.ml @@ -52179,7 +52179,11 @@ let transform_exp (e : Parsetree.expression) s delim : Parsetree.expression = e with pexp_desc = Pexp_constant (Pconst_string (js_str, Delim.escaped)); } - | J -> transform_interp e.pexp_loc s + | J -> + Location.prerr_warning e.pexp_loc + (Warnings.Deprecated + ("The unsafe j`$(a)$(b)` interpolation is deprecated, use string template `${a}${b}` instead.", e.pexp_loc, e.pexp_loc)); + transform_interp e.pexp_loc s | Unrecognized -> e diff --git a/lib/4.06.1/unstable/js_compiler.ml b/lib/4.06.1/unstable/js_compiler.ml index 5cb5ba7c06..bedd27dc03 100644 --- a/lib/4.06.1/unstable/js_compiler.ml +++ b/lib/4.06.1/unstable/js_compiler.ml @@ -85152,7 +85152,11 @@ let transform_exp (e : Parsetree.expression) s delim : Parsetree.expression = e with pexp_desc = Pexp_constant (Pconst_string (js_str, Delim.escaped)); } - | J -> transform_interp e.pexp_loc s + | J -> + Location.prerr_warning e.pexp_loc + (Warnings.Deprecated + ("The unsafe j`$(a)$(b)` interpolation is deprecated, use string template `${a}${b}` instead.", e.pexp_loc, e.pexp_loc)); + transform_interp e.pexp_loc s | Unrecognized -> e diff --git a/lib/4.06.1/unstable/js_playground_compiler.ml b/lib/4.06.1/unstable/js_playground_compiler.ml index eaee3a54d5..5d885cdfd7 100644 --- a/lib/4.06.1/unstable/js_playground_compiler.ml +++ b/lib/4.06.1/unstable/js_playground_compiler.ml @@ -85152,7 +85152,11 @@ let transform_exp (e : Parsetree.expression) s delim : Parsetree.expression = e with pexp_desc = Pexp_constant (Pconst_string (js_str, Delim.escaped)); } - | J -> transform_interp e.pexp_loc s + | J -> + Location.prerr_warning e.pexp_loc + (Warnings.Deprecated + ("The unsafe j`$(a)$(b)` interpolation is deprecated, use string template `${a}${b}` instead.", e.pexp_loc, e.pexp_loc)); + transform_interp e.pexp_loc s | Unrecognized -> e diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index 8f29965e81..eafe131291 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -179572,7 +179572,11 @@ let transform_exp (e : Parsetree.expression) s delim : Parsetree.expression = e with pexp_desc = Pexp_constant (Pconst_string (js_str, Delim.escaped)); } - | J -> transform_interp e.pexp_loc s + | J -> + Location.prerr_warning e.pexp_loc + (Warnings.Deprecated + ("The unsafe j`$(a)$(b)` interpolation is deprecated, use string template `${a}${b}` instead.", e.pexp_loc, e.pexp_loc)); + transform_interp e.pexp_loc s | Unrecognized -> e