Skip to content

Commit

Permalink
Fix invalid emit of escaped new line character in strings
Browse files Browse the repository at this point in the history
  • Loading branch information
memothelemo committed Jan 1, 2025
1 parent a0b2acd commit 573afbc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/LuauRenderer/nodes/expressions/renderStringLiteral.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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}`;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/LuauRenderer/util/stripBackslashInEscapedNewLines.ts
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 573afbc

Please sign in to comment.