From 573afbc44f2ee4592660ec53b0bfff2dce3226f6 Mon Sep 17 00:00:00 2001 From: memothelemo Date: Wed, 1 Jan 2025 19:14:56 +0800 Subject: [PATCH] Fix invalid emit of escaped new line character in strings --- .../nodes/expressions/renderStringLiteral.ts | 4 +++- .../fields/renderInterpolatedStringPart.ts | 3 ++- .../util/stripBackslashInEscapedNewLines.ts | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/LuauRenderer/util/stripBackslashInEscapedNewLines.ts diff --git a/src/LuauRenderer/nodes/expressions/renderStringLiteral.ts b/src/LuauRenderer/nodes/expressions/renderStringLiteral.ts index 18d86ea..0d900bd 100644 --- a/src/LuauRenderer/nodes/expressions/renderStringLiteral.ts +++ b/src/LuauRenderer/nodes/expressions/renderStringLiteral.ts @@ -1,6 +1,7 @@ import luau from "LuauAST"; import { RenderState } from "LuauRenderer"; import { getSafeBracketEquals } from "LuauRenderer/util/getSafeBracketEquals"; +import { stripBackslashInEscapedNewLines } from "LuauRenderer/util/stripBackslashInEscapedNewLines"; function needsBracketSpacing(node: luau.StringLiteral) { const parent = node.parent; @@ -32,6 +33,7 @@ export function renderStringLiteral(state: RenderState, node: luau.StringLiteral } else { const eqStr = getSafeBracketEquals(node.value); const spacing = needsBracketSpacing(node) ? " " : ""; - return `${spacing}[${eqStr}[${node.value}]${eqStr}]${spacing}`; + const value = stripBackslashInEscapedNewLines(node.value); + return `${spacing}[${eqStr}[${value}]${eqStr}]${spacing}`; } } diff --git a/src/LuauRenderer/nodes/fields/renderInterpolatedStringPart.ts b/src/LuauRenderer/nodes/fields/renderInterpolatedStringPart.ts index f246820..fdc4fd9 100644 --- a/src/LuauRenderer/nodes/fields/renderInterpolatedStringPart.ts +++ b/src/LuauRenderer/nodes/fields/renderInterpolatedStringPart.ts @@ -1,9 +1,10 @@ import luau from "LuauAST"; import { RenderState } from "LuauRenderer"; +import { stripBackslashInEscapedNewLines } from "LuauRenderer/util/stripBackslashInEscapedNewLines"; export function renderInterpolatedStringPart(state: RenderState, node: luau.InterpolatedStringPart) { return ( - node.text + stripBackslashInEscapedNewLines(node.text) // escape braces, but do not touch braces within unicode escape codes .replace(/(\\u{[a-fA-F0-9]+})|([{}])/g, (_, unicodeEscape, brace) => unicodeEscape ?? "\\" + brace) // escape newlines, captures a CR with optionally an LF after it or just an LF on its own diff --git a/src/LuauRenderer/util/stripBackslashInEscapedNewLines.ts b/src/LuauRenderer/util/stripBackslashInEscapedNewLines.ts new file mode 100644 index 0000000..5e80a72 --- /dev/null +++ b/src/LuauRenderer/util/stripBackslashInEscapedNewLines.ts @@ -0,0 +1,18 @@ +/** + * Strips off any excess backslash if it is escaped with `\n` + * + * **From**: + * ```ts + * "Hello\ + * World!" + * ``` + * + * **To**: + * ```lua + * [[Hello + * World!]] + * ``` + */ +export function stripBackslashInEscapedNewLines(value: string) { + return value.replace(/\\(\r\n?|\n)/g, "$1"); +}