[BACKEND] Simplify script quote generation

This commit is contained in:
Jesse Bannon 2026-01-03 11:55:35 -08:00
parent b2056bec5d
commit 9745f6130d
2 changed files with 39 additions and 2 deletions

View file

@ -113,7 +113,20 @@ class ScriptUtils:
if isinstance(arg, String):
if arg.native == "":
return "" if top_level else "''"
return arg.native if top_level else f"'''{arg.native}'''"
contains_single_quote = "'" in arg.native
contains_double_quote = '"' in arg.native
if not contains_single_quote and not contains_double_quote:
quote = '"'
elif not contains_single_quote and contains_double_quote:
quote = "'"
elif contains_single_quote and not contains_double_quote:
quote = '"'
else:
quote = "'''"
return arg.native if top_level else f"{quote}{arg.native}{quote}"
if isinstance(arg, Integer):
out = f"%int({arg.native})"

View file

@ -18,6 +18,9 @@ class TestScriptUtils:
"string": "value",
"quotes": "has '' and \"\"",
"triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''",
"has-double-quotes": 'i got "some double quotes" in here',
"has-single-quotes": "i got 'some single quotes' in here",
"has-both-quotes": "i got 'both quotes\" in here",
"int": 1,
"bool": True,
"list": [1, 2, 3],
@ -60,7 +63,15 @@ class TestScriptUtils:
def test_to_syntax_tree(self):
out = ScriptUtils.to_native_script(
{"{var_a}": "{var_b}", "static_a": "string with {var_c} in it"}
{
"{var_a}": "{var_b}",
"static_a": "string with {var_c} in it",
"quotes": "has '' and \"\"",
"triple-single-quote": "right here! '''''''''''''''''''''''''''''' ack '''''''",
"has-double-quotes": 'i got "some double quotes" in here',
"has-single-quotes": "i got 'some single quotes' in here",
"has-both-quotes": "i got 'both quotes\" in here",
}
)
assert parse(out) == SyntaxTree(
ast=[
@ -75,6 +86,19 @@ class TestScriptUtils:
BuiltInFunction(name="string", args=[String(value=" in it")]),
],
),
String(value="quotes"): String(value="has '' and \"\""),
String(value="triple-single-quote"): String(
value="right here! '''''''''''''''''''''''''''''' ack '''''''"
),
String(value="has-double-quotes"): String(
value='i got "some double quotes" in here'
),
String(value="has-single-quotes"): String(
value="i got 'some single quotes' in here"
),
String(value="has-both-quotes"): String(
value="i got 'both quotes\" in here"
),
}
)
]