From c8d7fd171f654ab9460977f87c2b771ac686c457 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Thu, 28 Jul 2022 17:45:02 -0700 Subject: [PATCH] Escape interpolated strings better --- Ast/src/StringUtils.cpp | 10 +++++++++- tests/Transpiler.test.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Ast/src/StringUtils.cpp b/Ast/src/StringUtils.cpp index 0dc3f3f5..a0e6bf65 100644 --- a/Ast/src/StringUtils.cpp +++ b/Ast/src/StringUtils.cpp @@ -237,7 +237,7 @@ std::string escape(std::string_view s) for (uint8_t c : s) { - if (c >= ' ' && c != '\\' && c != '\'' && c != '\"') + if (c >= ' ' && c != '\\' && c != '\'' && c != '\"' && c != '`' && c != '{') r += c; else { @@ -272,6 +272,14 @@ std::string escape(std::string_view s) case '\"': r += '\"'; break; + // INTERP CODE REVIEW: This is going to apply it to all escaped strings, not just + // interpolated ones. Is that acceptable, or should this be split into two functions/a toggle? + case '`': + r += '`'; + break; + case '{': + r += '{'; + break; case '\\': r += '\\'; break; diff --git a/tests/Transpiler.test.cpp b/tests/Transpiler.test.cpp index d6eac999..e79bc9b7 100644 --- a/tests/Transpiler.test.cpp +++ b/tests/Transpiler.test.cpp @@ -688,4 +688,13 @@ TEST_CASE_FIXTURE(Fixture, "transpile_string_interp") CHECK_EQ(code, transpile(code, {}, true).code); } +TEST_CASE_FIXTURE(Fixture, "transpile_string_literal_escape") +{ + ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; + + std::string code = R"( local _ = ` bracket = \{, backtick = \` = {'ok'} ` )"; + + CHECK_EQ(code, transpile(code, {}, true).code); +} + TEST_SUITE_END();