[ENG-3870] rx.call_script with f-string var produces incorrect code

Avoid casting javascript code with embedded Var as LiteralStringVar

There are two cases that need to be handled:

1. The javascript code contains Vars with VarData, these can only be evaluated
   in the component context, since they may use hooks. Vars with VarData cannot be
   used from the backend. In this case, we cast the given code as a raw js
   expression and include the extracted VarData.

2. The javascript code has no VarData. In this case, we pass the code as the
   raw js expression and cast to a python str to get a js literal string to eval.
This commit is contained in:
Masen Furer 2024-10-01 16:49:47 -07:00
parent 565df06508
commit 0bf6addf44
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95

View File

@ -839,6 +839,16 @@ def call_script(
), ),
), ),
} }
if isinstance(javascript_code, str):
# When there is VarData, include it and eval the JS code inline on the client.
javascript_code, original_code = (
LiteralVar.create(javascript_code),
javascript_code,
)
if javascript_code._get_all_var_data() is None:
# Without VarData, cast to string and eval the code in the event loop.
javascript_code = str(Var(_js_expr=original_code))
return server_side( return server_side(
"_call_script", "_call_script",
get_fn_signature(call_script), get_fn_signature(call_script),