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

Add InterpolatedString #419

Merged
merged 8 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/LuauAST/impl/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum SyntaxKind {
BinaryExpression,
UnaryExpression,
IfExpression,
InterpolatedString,
Array,
Map,
Set,
Expand Down
1 change: 1 addition & 0 deletions src/LuauAST/impl/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const isFunctionExpression = makeGuard(luau.SyntaxKind.FunctionExpression
export const isBinaryExpression = makeGuard(luau.SyntaxKind.BinaryExpression);
export const isUnaryExpression = makeGuard(luau.SyntaxKind.UnaryExpression);
export const isIfExpression = makeGuard(luau.SyntaxKind.IfExpression);
export const isInterpolatedString = makeGuard(luau.SyntaxKind.InterpolatedString);
export const isArray = makeGuard(luau.SyntaxKind.Array);
export const isMap = makeGuard(luau.SyntaxKind.Map);
export const isSet = makeGuard(luau.SyntaxKind.Set);
Expand Down
1 change: 1 addition & 0 deletions src/LuauAST/types/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ExpressionByKind extends IndexableExpressionByKind {
[luau.SyntaxKind.BinaryExpression]: luau.BinaryExpression;
[luau.SyntaxKind.UnaryExpression]: luau.UnaryExpression;
[luau.SyntaxKind.IfExpression]: luau.IfExpression;
[luau.SyntaxKind.InterpolatedString]: luau.InterpolatedString;
[luau.SyntaxKind.Array]: luau.Array;
[luau.SyntaxKind.Map]: luau.Map;
[luau.SyntaxKind.Set]: luau.Set;
Expand Down
4 changes: 4 additions & 0 deletions src/LuauAST/types/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export interface IfExpression extends luau.BaseExpression<luau.SyntaxKind.IfExpr
alternative: luau.Expression;
}

export interface InterpolatedString extends luau.BaseExpression<luau.SyntaxKind.InterpolatedString> {
segments: luau.List<luau.Expression>;
rimuy marked this conversation as resolved.
Show resolved Hide resolved
}

export interface Array extends luau.BaseExpression<luau.SyntaxKind.Array> {
members: luau.List<luau.Expression>;
}
Expand Down
27 changes: 27 additions & 0 deletions src/LuauRenderer/nodes/expressions/renderInterpolatedString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import luau from "LuauAST";
import { render, RenderState } from "LuauRenderer";

const STRING_LITERAL_EDGE = '"'.length;

export function renderInterpolatedString(state: RenderState, node: luau.InterpolatedString) {
let result = "";
luau.list.forEach(node.segments, expression => {
let expressionStr = render(state, expression);
if (luau.isStringLiteral(expression)) {
rimuy marked this conversation as resolved.
Show resolved Hide resolved
// braces and newlines are escaped to be valid in luau
result += expressionStr
.slice(STRING_LITERAL_EDGE, -STRING_LITERAL_EDGE)
.replace(/([{}])/g, "\\$1")
rimuy marked this conversation as resolved.
Show resolved Hide resolved
.replace(/\n/g, "\\\n");
return;
}

// `{{}}` is invalid, so we wrap it in parenthesis
if (luau.isTable(expression)) {
expressionStr = `(${expressionStr})`;
}

result += `{${expressionStr}}`;
});
return `\`${result}\``;
rimuy marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion src/LuauRenderer/nodes/expressions/renderStringLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function needsBracketSpacing(node: luau.StringLiteral) {

export function renderStringLiteral(state: RenderState, node: luau.StringLiteral) {
const isMultiline = node.value.includes("\n");
if (!isMultiline && !node.value.includes('"')) {
if ((node.parent && luau.isInterpolatedString(node.parent)) || (!isMultiline && !node.value.includes('"'))) {
rimuy marked this conversation as resolved.
Show resolved Hide resolved
return `"${node.value}"`;
} else if (!isMultiline && !node.value.includes("'")) {
return `'${node.value}'`;
Expand Down
2 changes: 2 additions & 0 deletions src/LuauRenderer/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { renderArray } from "LuauRenderer/nodes/expressions/renderArray";
import { renderBinaryExpression } from "LuauRenderer/nodes/expressions/renderBinaryExpression";
import { renderFunctionExpression } from "LuauRenderer/nodes/expressions/renderFunctionExpression";
import { renderIfExpression } from "LuauRenderer/nodes/expressions/renderIfExpression";
import { renderInterpolatedString } from "LuauRenderer/nodes/expressions/renderInterpolatedString";
import { renderMap } from "LuauRenderer/nodes/expressions/renderMap";
import { renderMixedTable } from "LuauRenderer/nodes/expressions/renderMixedTable";
import { renderNumberLiteral } from "LuauRenderer/nodes/expressions/renderNumberLiteral";
Expand Down Expand Up @@ -64,6 +65,7 @@ const KIND_TO_RENDERER = identity<{ [K in luau.SyntaxKind]: Renderer<K> }>({
[luau.SyntaxKind.BinaryExpression]: renderBinaryExpression,
[luau.SyntaxKind.UnaryExpression]: renderUnaryExpression,
[luau.SyntaxKind.IfExpression]: renderIfExpression,
[luau.SyntaxKind.InterpolatedString]: renderInterpolatedString,
[luau.SyntaxKind.Array]: renderArray,
[luau.SyntaxKind.Map]: renderMap,
[luau.SyntaxKind.Set]: renderSet,
Expand Down
1 change: 1 addition & 0 deletions src/LuauRenderer/util/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const KIND_TO_VISITOR = identity<{ [K in luau.SyntaxKind]: VisitStrategy<K> }>({
visitNode(node.expression, visitor);
visitNode(node.alternative, visitor);
},
[luau.SyntaxKind.InterpolatedString]: (node, visitor) => visitList(node.segments, visitor),
[luau.SyntaxKind.Array]: (node, visitor) => visitList(node.members, visitor),
[luau.SyntaxKind.Map]: (node, visitor) => visitList(node.fields, visitor),
[luau.SyntaxKind.Set]: (node, visitor) => visitList(node.members, visitor),
Expand Down