Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interpolated string rendering issues #483

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/LuauRenderer/nodes/fields/renderInterpolatedStringPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import luau from "LuauAST";
import { RenderState } from "LuauRenderer";

export function renderInterpolatedStringPart(state: RenderState, node: luau.InterpolatedStringPart) {
// braces and newlines are escaped to be valid in luau
// () captures result, [] to search for any of: {}
// $1 fills in capture from original search
return node.text.replace(/([{}])/g, "\\$1").replace(/\n/g, "\\\n");
// escape braces and newlines, but do not touch braces within unicode escape codes
return node.text
.replace(
/(\\u\{[0-9A-Fa-f]+\})|([{}])/g,
Copy link
Contributor

@Dionysusnu Dionysusnu Jun 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex might be cleaner with a negative lookbehind instead of capturing the Unicode sequences to like-for-like replace them. Was it a conscious decision not to use one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to find a way to do this that was readable. Open to suggestions.

(_, unicodeEscape: string | undefined, brace: string | undefined) => unicodeEscape ?? "\\" + brace,
)
.replace(/(\r\n?|\n)/g, "\\$1");
osyrisrblx marked this conversation as resolved.
Show resolved Hide resolved
}
Loading