-
Notifications
You must be signed in to change notification settings - Fork 471
Stop mangling tagged templates and backquoted strings #7841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -275,35 +275,69 @@ module Delim = struct | |||||||||
| None -> Some External_arg_spec.DNone | ||||||||||
| Some "json" -> Some DNoQuotes | ||||||||||
| Some "*j" -> Some DStarJ | ||||||||||
| Some "bq" -> Some DBackQuotes | ||||||||||
| _ -> None | ||||||||||
|
||||||||||
type interpolation = | ||||||||||
| Js (* string interpolation *) | ||||||||||
| BackQuotes (* string interpolation *) | ||||||||||
| Js (* simple double quoted string *) | ||||||||||
| Unrecognized (* no interpolation: delimiter not recognized *) | ||||||||||
let parse_unprocessed = function | ||||||||||
| "js" -> Js | ||||||||||
let parse_unprocessed is_template = function | ||||||||||
| "js" -> if is_template then BackQuotes else Js | ||||||||||
| _ -> Unrecognized | ||||||||||
|
||||||||||
let escaped_j_delimiter = "*j" (* not user level syntax allowed *) | ||||||||||
let escaped_back_quote_delimiter = "bq" | ||||||||||
let some_escaped_back_quote_delimiter = Some "bq" | ||||||||||
let unescaped_js_delimiter = "js" | ||||||||||
let escaped = Some escaped_j_delimiter | ||||||||||
let some_escaped_j_delimiter = Some escaped_j_delimiter | ||||||||||
end | ||||||||||
|
||||||||||
let transform_exp (e : Parsetree.expression) s delim : Parsetree.expression = | ||||||||||
match Delim.parse_unprocessed delim with | ||||||||||
let is_template = | ||||||||||
Ext_list.exists e.pexp_attributes (fun ({txt}, _) -> | ||||||||||
match txt with | ||||||||||
| "res.template" | "res.taggedTemplate" -> true | ||||||||||
| _ -> false) | ||||||||||
in | ||||||||||
match Delim.parse_unprocessed is_template delim with | ||||||||||
| Js -> | ||||||||||
let js_str = Ast_utf8_string.transform e.pexp_loc s in | ||||||||||
{e with pexp_desc = Pexp_constant (Pconst_string (js_str, Delim.escaped))} | ||||||||||
{ | ||||||||||
e with | ||||||||||
pexp_desc = | ||||||||||
Pexp_constant (Pconst_string (js_str, Delim.some_escaped_j_delimiter)); | ||||||||||
} | ||||||||||
| BackQuotes -> | ||||||||||
{ | ||||||||||
e with | ||||||||||
pexp_desc = | ||||||||||
Pexp_constant | ||||||||||
(Pconst_string (s, Delim.some_escaped_back_quote_delimiter)); | ||||||||||
} | ||||||||||
| Unrecognized -> e | ||||||||||
|
||||||||||
let transform_pat (p : Parsetree.pattern) s delim : Parsetree.pattern = | ||||||||||
match Delim.parse_unprocessed delim with | ||||||||||
match Delim.parse_unprocessed false delim with | ||||||||||
| Js -> | ||||||||||
let js_str = Ast_utf8_string.transform p.ppat_loc s in | ||||||||||
{p with ppat_desc = Ppat_constant (Pconst_string (js_str, Delim.escaped))} | ||||||||||
{ | ||||||||||
p with | ||||||||||
ppat_desc = | ||||||||||
Ppat_constant (Pconst_string (js_str, Delim.some_escaped_j_delimiter)); | ||||||||||
} | ||||||||||
| BackQuotes -> | ||||||||||
{ | ||||||||||
p with | ||||||||||
ppat_desc = | ||||||||||
Ppat_constant | ||||||||||
(Pconst_string (s, Delim.some_escaped_back_quote_delimiter)); | ||||||||||
} | ||||||||||
| Unrecognized -> p | ||||||||||
|
||||||||||
let is_unicode_string opt = Ext_string.equal opt Delim.escaped_j_delimiter | ||||||||||
let is_unicode_string opt = | ||||||||||
Ext_string.equal opt Delim.escaped_j_delimiter | ||||||||||
|| Ext_string.equal opt Delim.escaped_back_quote_delimiter | ||||||||||
|
||||||||||
let is_unescaped s = Ext_string.equal s Delim.unescaped_js_delimiter | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong suggestion |
||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The template detection logic is duplicated in the
transform_exp
function but not intransform_pat
. This inconsistency could lead to different behavior between expressions and patterns. Consider extracting this logic into a helper function or ensuring consistent template detection across both functions.Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is because you can't have string interpolation in pattern matching.