From ce01152fe733b49f1bbe7707823357468dae59cf Mon Sep 17 00:00:00 2001 From: Rimuy Date: Fri, 3 Jun 2022 14:03:32 -0300 Subject: [PATCH 01/26] Add line wrapping --- package-lock.json | 1 + package.json | 1 + src/LuauRenderer/RenderState.ts | 29 +++++++++++++++- .../indexable/renderCallExpression.ts | 17 +++++++++- .../renderComputedIndexExpression.ts | 9 ++++- .../indexable/renderMethodCallExpression.ts | 17 +++++++++- .../renderPropertyAccessExpression.ts | 6 ++++ .../nodes/expressions/renderArray.ts | 23 +++++++++++-- .../expressions/renderFunctionExpression.ts | 34 ++++++++++++++++--- .../nodes/statements/renderAssignment.ts | 11 ++++++ .../nodes/statements/renderBreakStatement.ts | 2 +- .../nodes/statements/renderCallStatement.ts | 2 +- .../statements/renderContinueStatement.ts | 2 +- .../nodes/statements/renderDoStatement.ts | 4 +-- .../nodes/statements/renderForStatement.ts | 3 +- .../statements/renderFunctionDeclaration.ts | 20 +++++++++-- .../nodes/statements/renderIfStatement.ts | 4 +-- .../statements/renderMethodDeclaration.ts | 20 +++++++++-- .../statements/renderNumericForStatement.ts | 10 +++--- .../nodes/statements/renderRepeatStatement.ts | 2 +- .../statements/renderVariableDeclaration.ts | 11 ++++-- .../nodes/statements/renderWhileStatement.ts | 2 +- src/LuauRenderer/util/format.ts | 13 +++++++ src/LuauRenderer/util/renderArguments.ts | 3 +- src/LuauRenderer/util/renderParameters.ts | 3 +- 25 files changed, 215 insertions(+), 34 deletions(-) create mode 100644 src/LuauRenderer/util/format.ts diff --git a/package-lock.json b/package-lock.json index 3968c49..138edad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "@roblox-ts/luau-ast", "version": "1.0.1", "license": "MIT", "devDependencies": { diff --git a/package.json b/package.json index 83274b0..08e5a0e 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.1", "description": "Luau AST + Renderer for roblox-ts", "main": "out/LuauAST/index.js", + "types": "out/LuauAST/index.d.ts", "repository": { "type": "git", "url": "https://github.com/roblox-ts/luau-ast.git" diff --git a/src/LuauRenderer/RenderState.ts b/src/LuauRenderer/RenderState.ts index 5cc0cce..03c640b 100644 --- a/src/LuauRenderer/RenderState.ts +++ b/src/LuauRenderer/RenderState.ts @@ -3,9 +3,19 @@ import { assert } from "LuauAST/util/assert"; import { getEnding } from "LuauRenderer/util/getEnding"; import { getOrSetDefault } from "LuauRenderer/util/getOrSetDefault"; +const PRINT_WIDTH = 70; const INDENT_CHARACTER = "\t"; const INDENT_CHARACTER_LENGTH = INDENT_CHARACTER.length; +const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ + luau.SyntaxKind.CallExpression, + luau.SyntaxKind.MethodCallExpression, + luau.SyntaxKind.Array, + luau.SyntaxKind.Map, + luau.SyntaxKind.Set, + luau.SyntaxKind.MixedTable, +]); + /** * Represents the state of a rendering process. */ @@ -25,7 +35,7 @@ export class RenderState { * Pops an indent from the current indent level. */ private popIndent() { - this.indent = this.indent.substr(INDENT_CHARACTER_LENGTH); + this.indent = this.indent.substring(INDENT_CHARACTER_LENGTH); } private tempIdFallback = 0; @@ -104,4 +114,21 @@ export class RenderState { this.popIndent(); return result; } + + /** + * Checks to see if `text` can be formatted. + * + * If a `expressionKind` value is passed, it will also check to see if the right-hand side expression can be formated. + * @param text The text. + * @param expressionKind The kind of the right-hand side expression. + * @param skipCheck If set to true, the result will be always formattable. + */ + public isFormattable(text: string, expressionKind?: keyof luau.ExpressionByKind, skipCheck = false) { + if (skipCheck) return true; + const isLongStr = text.length > PRINT_WIDTH; + if (expressionKind !== undefined) { + return isLongStr && !NON_FORMATTABLE_RHS_EXPRESSIONS.has(expressionKind); + } + return isLongStr; + } } diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts index f2d15b0..9e15e8b 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts @@ -3,5 +3,20 @@ import { render, RenderState } from "LuauRenderer"; import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderCallExpression(state: RenderState, node: luau.CallExpression) { - return `${render(state, node.expression)}(${renderArguments(state, node.args)})`; + const expStr = render(state, node.expression); + const argStrs = renderArguments(state, node.args).join(""); + + if (state.isFormattable(`${expStr}(${argStrs})`)) { + let result = ""; + result += state.newline(`${expStr}(`); + result += state.block(() => + renderArguments(state, node.args) + .map(a => state.line(a)) + .join(""), + ); + result += state.indented(")"); + + return result; + } + return `${expStr}(${argStrs})`; } diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts index 884e538..cc402fc 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts @@ -4,7 +4,14 @@ import { render, RenderState } from "LuauRenderer"; export function renderComputedIndexExpression(state: RenderState, node: luau.ComputedIndexExpression) { const expStr = render(state, node.expression); if (luau.isStringLiteral(node.index) && luau.isValidIdentifier(node.index.value)) { - return `${expStr}.${node.index.value}`; + const nameStr = node.index.value; + if (state.isFormattable(`${expStr}.${nameStr}`)) { + let result = ""; + result += state.newline(expStr); + result += state.block(() => state.indented(`.${nameStr}`)); + return result; + } + return `${expStr}.${nameStr}`; } else { const indexStr = render(state, node.index); return `${expStr}[${indexStr}]`; diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts index ff35b41..fb88715 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts @@ -5,5 +5,20 @@ import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderMethodCallExpression(state: RenderState, node: luau.MethodCallExpression) { assert(luau.isValidIdentifier(node.name)); - return `${render(state, node.expression)}:${node.name}(${renderArguments(state, node.args)})`; + const objStr = render(state, node.expression); + const argStr = renderArguments(state, node.args).join(""); + + if (state.isFormattable(`${objStr}:${node.name}(${argStr})`)) { + let result = ""; + result += state.newline(`${objStr}:${node.name}(`); + result += state.block(() => + renderArguments(state, node.args) + .map(a => state.line(a)) + .join(""), + ); + result += state.indented(")"); + return result; + } + + return `${objStr}:${node.name}(${argStr})`; } diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts index 1fd1d23..d4cb089 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts @@ -5,6 +5,12 @@ export function renderPropertyAccessExpression(state: RenderState, node: luau.Pr const expStr = render(state, node.expression); const nameStr = node.name; if (luau.isValidIdentifier(nameStr)) { + if (state.isFormattable(`${expStr}.${nameStr}`)) { + let result = ""; + result += state.newline(expStr); + result += state.block(() => state.indented(`.${nameStr}`)); + return result; + } return `${expStr}.${nameStr}`; } else { return `${expStr}["${nameStr}"]`; diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index 5bdc230..f258d6a 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -1,11 +1,30 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; +import { format } from "LuauRenderer/util/format"; export function renderArray(state: RenderState, node: luau.Array) { if (luau.list.isEmpty(node.members)) { return "{}"; } - const membersStr = luau.list.mapToArray(node.members, member => render(state, member)).join(", "); - return `{ ${membersStr} }`; + const getMembers = () => luau.list.mapToArray(node.members, member => render(state, member)).map(format(state)); + const arrStr = getMembers().join(""); + const hasFunctionExpression = luau.list.some( + node.members, + m => luau.isFunctionExpression(m) && !luau.list.isEmpty(m.statements), + ); + + if (state.isFormattable(`{ ${arrStr} }`, undefined, hasFunctionExpression)) { + let result = ""; + result += state.newline("{"); + result += state.block(() => + getMembers() + .map(m => state.line(m)) + .join(""), + ); + result += state.indented("}"); + return result; + } + + return `{ ${arrStr} }`; } diff --git a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts index 298b78e..4d93824 100644 --- a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts +++ b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts @@ -4,13 +4,39 @@ import { renderParameters } from "LuauRenderer/util/renderParameters"; import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderFunctionExpression(state: RenderState, node: luau.FunctionExpression) { + const paramStrs = renderParameters(state, node); + const isFormattable = state.isFormattable(`function(${paramStrs.join("")})`); + + let result = ""; + if (luau.list.isEmpty(node.statements)) { - return `function(${renderParameters(state, node)}) end`; + if (isFormattable) { + result += state.newline("function("); + result += state.block(() => + renderParameters(state, node) + .map(p => state.line(p)) + .join(""), + ); + result += state.indented(") end"); + return result; + } + + return `function(${paramStrs.join("")}) end`; + } + + if (isFormattable) { + result += state.newline("function("); + result += state.block(() => + renderParameters(state, node) + .map(p => state.line(p)) + .join(""), + ); + result += state.line(")"); + } else { + result += `function(${paramStrs.join("")}${state.newline(")")}`; } - let result = ""; - result += state.newline(`function(${renderParameters(state, node)})`); result += state.block(() => renderStatements(state, node.statements)); - result += state.indented(`end`); + result += state.indented("end"); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderAssignment.ts b/src/LuauRenderer/nodes/statements/renderAssignment.ts index 0f63240..9013ffe 100644 --- a/src/LuauRenderer/nodes/statements/renderAssignment.ts +++ b/src/LuauRenderer/nodes/statements/renderAssignment.ts @@ -11,5 +11,16 @@ export function renderAssignment(state: RenderState, node: luau.Assignment) { leftStr = render(state, node.left); } const rightStr = render(state, node.right); + + if (state.isFormattable(rightStr, node.right.kind)) { + let result = ""; + result += state.line(`${leftStr} ${node.operator}`); + result += state.block(() => + state.isFormattable(leftStr) + ? state.block(() => state.line(render(state, node.right), node)) + : state.line(render(state, node.right), node), + ); + return result; + } return state.line(`${leftStr} ${node.operator} ${rightStr}`, node); } diff --git a/src/LuauRenderer/nodes/statements/renderBreakStatement.ts b/src/LuauRenderer/nodes/statements/renderBreakStatement.ts index 0290262..dea7705 100644 --- a/src/LuauRenderer/nodes/statements/renderBreakStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderBreakStatement.ts @@ -1,5 +1,5 @@ import { RenderState } from "LuauRenderer"; export function renderBreakStatement(state: RenderState) { - return state.line(`break`); + return state.line("break"); } diff --git a/src/LuauRenderer/nodes/statements/renderCallStatement.ts b/src/LuauRenderer/nodes/statements/renderCallStatement.ts index 3e18f13..b1875d6 100644 --- a/src/LuauRenderer/nodes/statements/renderCallStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderCallStatement.ts @@ -2,5 +2,5 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; export function renderCallStatement(state: RenderState, node: luau.CallStatement) { - return state.line(`${render(state, node.expression)}`, node); + return state.line(render(state, node.expression), node); } diff --git a/src/LuauRenderer/nodes/statements/renderContinueStatement.ts b/src/LuauRenderer/nodes/statements/renderContinueStatement.ts index 272ee77..1db50d8 100644 --- a/src/LuauRenderer/nodes/statements/renderContinueStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderContinueStatement.ts @@ -1,5 +1,5 @@ import { RenderState } from "LuauRenderer"; export function renderContinueStatement(state: RenderState) { - return state.line(`continue`); + return state.line("continue"); } diff --git a/src/LuauRenderer/nodes/statements/renderDoStatement.ts b/src/LuauRenderer/nodes/statements/renderDoStatement.ts index a87ca96..ff9c6d3 100644 --- a/src/LuauRenderer/nodes/statements/renderDoStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderDoStatement.ts @@ -4,8 +4,8 @@ import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderDoStatement(state: RenderState, node: luau.DoStatement) { let result = ""; - result += state.line(`do`); + result += state.line("do"); result += state.block(() => renderStatements(state, node.statements)); - result += state.line(`end`); + result += state.line("end"); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderForStatement.ts b/src/LuauRenderer/nodes/statements/renderForStatement.ts index 8b3e94e..5c09132 100644 --- a/src/LuauRenderer/nodes/statements/renderForStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderForStatement.ts @@ -9,7 +9,6 @@ export function renderForStatement(state: RenderState, node: luau.ForStatement) let result = ""; result += state.line(`for ${idsStr} in ${expStr} do`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line(`end`); - + result += state.line("end"); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts index 9bfc9cf..6041da5 100644 --- a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts @@ -9,11 +9,25 @@ export function renderFunctionDeclaration(state: RenderState, node: luau.Functio assert(luau.isAnyIdentifier(node.name), "local function cannot be a property"); } const nameStr = render(state, node.name); - const paramStr = renderParameters(state, node); + const fnStart = `${node.localize ? "local " : ""}function ${nameStr}(`; + const paramStrs = renderParameters(state, node); let result = ""; - result += state.line(`${node.localize ? "local " : ""}function ${nameStr}(${paramStr})`); + + if (state.isFormattable(`${fnStart}${paramStrs.join("")})`)) { + result += state.line(fnStart); + result += state.block(() => + renderParameters(state, node) + .map(p => state.line(p)) + .join(""), + ); + result += state.line(")"); + } else { + result += state.line(`${fnStart}${paramStrs.join("")})`); + } + result += state.block(() => renderStatements(state, node.statements)); - result += state.line(`end`); + result += state.line("end"); + return result; } diff --git a/src/LuauRenderer/nodes/statements/renderIfStatement.ts b/src/LuauRenderer/nodes/statements/renderIfStatement.ts index dc538cf..26bebfc 100644 --- a/src/LuauRenderer/nodes/statements/renderIfStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderIfStatement.ts @@ -17,12 +17,12 @@ export function renderIfStatement(state: RenderState, node: luau.IfStatement) { } if (currentElseBody && luau.list.isNonEmpty(currentElseBody)) { - result += state.line(`else`); + result += state.line("else"); const statements = currentElseBody; result += state.block(() => renderStatements(state, statements)); } - result += state.line(`end`); + result += state.line("end"); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts index 715a7c4..153e477 100644 --- a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts @@ -4,9 +4,25 @@ import { renderParameters } from "LuauRenderer/util/renderParameters"; import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderMethodDeclaration(state: RenderState, node: luau.MethodDeclaration) { + const objStr = render(state, node.expression); + const paramStrs = renderParameters(state, node); + let result = ""; - result += state.line(`function ${render(state, node.expression)}:${node.name}(${renderParameters(state, node)})`); + + if (state.isFormattable(paramStrs.join(""))) { + result += state.line(`function ${objStr}:${node.name}(`); + result += state.block(() => + renderParameters(state, node) + .map(p => state.line(p)) + .join(""), + ); + result += state.line(")"); + } else { + result += state.line(`function ${objStr}:${node.name}(${paramStrs.join("")})`); + } + result += state.block(() => renderStatements(state, node.statements)); - result += state.line(`end`); + result += state.line("end"); + return result; } diff --git a/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts b/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts index 7fd3e50..fe3d4f9 100644 --- a/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts @@ -8,14 +8,16 @@ export function renderNumericForStatement(state: RenderState, node: luau.Numeric const endStr = render(state, node.end); let predicateStr = `${startStr}, ${endStr}`; - if (node.step) { - predicateStr += `, ${render(state, node.step)}`; + + // step of 1 can be omitted + if (node.step && (!luau.isNumberLiteral(node.step) || Number(node.step.value) !== 1)) { + const stepStr = render(state, node.step); + predicateStr += `, ${stepStr}`; } let result = ""; result += state.line(`for ${idStr} = ${predicateStr} do`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line(`end`); - + result += state.line("end"); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts b/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts index c9326f7..6028eb7 100644 --- a/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts @@ -4,7 +4,7 @@ import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderRepeatStatement(state: RenderState, node: luau.RepeatStatement) { let result = ""; - result += state.line(`repeat`); + result += state.line("repeat"); result += state.block(() => renderStatements(state, node.statements)); result += state.line(`until ${render(state, node.condition)}`); return result; diff --git a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts index 9576cf1..8c45123 100644 --- a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts @@ -10,8 +10,15 @@ export function renderVariableDeclaration(state: RenderState, node: luau.Variabl } else { leftStr = render(state, node.left); } - if (node.right) { - const rightStr = render(state, node.right); + const rightNode = node.right; + if (rightNode) { + const rightStr = render(state, rightNode); + if (state.isFormattable(`${leftStr} = ${rightStr}`, rightNode.kind)) { + let result = ""; + result += state.line(`local ${leftStr} =`); + result += state.block(() => state.line(render(state, rightNode), node)); + return result; + } return state.line(`local ${leftStr} = ${rightStr}`, node); } else { return state.line(`local ${leftStr}`, node); diff --git a/src/LuauRenderer/nodes/statements/renderWhileStatement.ts b/src/LuauRenderer/nodes/statements/renderWhileStatement.ts index 384fe1b..122e261 100644 --- a/src/LuauRenderer/nodes/statements/renderWhileStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderWhileStatement.ts @@ -6,6 +6,6 @@ export function renderWhileStatement(state: RenderState, node: luau.WhileStateme let result = ""; result += state.line(`while ${render(state, node.condition)} do`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line(`end`); + result += state.line("end"); return result; } diff --git a/src/LuauRenderer/util/format.ts b/src/LuauRenderer/util/format.ts new file mode 100644 index 0000000..29f414e --- /dev/null +++ b/src/LuauRenderer/util/format.ts @@ -0,0 +1,13 @@ +import { RenderState } from "LuauAST"; + +/** + * Utility function that takes a list of string values + * and removes the space at the final of each string if the result is formattable. + * @param state The render state. + */ +export function format(state: RenderState) { + return (value: string, index: number, list: ReadonlyArray) => + list.length > 1 && index < list.length - 1 + ? `${value},${state.isFormattable(list.join(", ")) ? "" : " "}` + : value; +} diff --git a/src/LuauRenderer/util/renderArguments.ts b/src/LuauRenderer/util/renderArguments.ts index 3d9d98e..c3e707f 100644 --- a/src/LuauRenderer/util/renderArguments.ts +++ b/src/LuauRenderer/util/renderArguments.ts @@ -1,7 +1,8 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; +import { format } from "LuauRenderer/util/format"; /** Renders the given list of expressions into a string separated by commas */ export function renderArguments(state: RenderState, expressions: luau.List) { - return luau.list.mapToArray(expressions, v => render(state, v)).join(", "); + return luau.list.mapToArray(expressions, v => render(state, v)).map(format(state)); } diff --git a/src/LuauRenderer/util/renderParameters.ts b/src/LuauRenderer/util/renderParameters.ts index d730884..b6a6414 100644 --- a/src/LuauRenderer/util/renderParameters.ts +++ b/src/LuauRenderer/util/renderParameters.ts @@ -1,5 +1,6 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; +import { format } from "LuauRenderer/util/format"; /** * Renders the given list of identifiers inside of `node` into a string sepearted by commas @@ -11,5 +12,5 @@ export function renderParameters(state: RenderState, node: luau.HasParameters) { if (node.hasDotDotDot) { paramStrs.push("..."); } - return paramStrs.join(", "); + return paramStrs.map(format(state)); } From 788f109fe53a7918916410e4fd694446b94af4b0 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Fri, 3 Jun 2022 14:12:59 -0300 Subject: [PATCH 02/26] Rename short parameters --- .../nodes/expressions/indexable/renderCallExpression.ts | 2 +- .../nodes/expressions/indexable/renderMethodCallExpression.ts | 2 +- src/LuauRenderer/nodes/expressions/renderArray.ts | 4 ++-- .../nodes/expressions/renderFunctionExpression.ts | 2 +- .../nodes/statements/renderFunctionDeclaration.ts | 2 +- src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts index 9e15e8b..2c90590 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts @@ -11,7 +11,7 @@ export function renderCallExpression(state: RenderState, node: luau.CallExpressi result += state.newline(`${expStr}(`); result += state.block(() => renderArguments(state, node.args) - .map(a => state.line(a)) + .map(arg => state.line(arg)) .join(""), ); result += state.indented(")"); diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts index fb88715..77a853b 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts @@ -13,7 +13,7 @@ export function renderMethodCallExpression(state: RenderState, node: luau.Method result += state.newline(`${objStr}:${node.name}(`); result += state.block(() => renderArguments(state, node.args) - .map(a => state.line(a)) + .map(arg => state.line(arg)) .join(""), ); result += state.indented(")"); diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index f258d6a..993edce 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -11,7 +11,7 @@ export function renderArray(state: RenderState, node: luau.Array) { const arrStr = getMembers().join(""); const hasFunctionExpression = luau.list.some( node.members, - m => luau.isFunctionExpression(m) && !luau.list.isEmpty(m.statements), + member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), ); if (state.isFormattable(`{ ${arrStr} }`, undefined, hasFunctionExpression)) { @@ -19,7 +19,7 @@ export function renderArray(state: RenderState, node: luau.Array) { result += state.newline("{"); result += state.block(() => getMembers() - .map(m => state.line(m)) + .map(value => state.line(value)) .join(""), ); result += state.indented("}"); diff --git a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts index 4d93824..3a5a5d1 100644 --- a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts +++ b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts @@ -14,7 +14,7 @@ export function renderFunctionExpression(state: RenderState, node: luau.Function result += state.newline("function("); result += state.block(() => renderParameters(state, node) - .map(p => state.line(p)) + .map(param => state.line(param)) .join(""), ); result += state.indented(") end"); diff --git a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts index 6041da5..701bf41 100644 --- a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts @@ -18,7 +18,7 @@ export function renderFunctionDeclaration(state: RenderState, node: luau.Functio result += state.line(fnStart); result += state.block(() => renderParameters(state, node) - .map(p => state.line(p)) + .map(param => state.line(param)) .join(""), ); result += state.line(")"); diff --git a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts index 153e477..7b30a0b 100644 --- a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts @@ -13,7 +13,7 @@ export function renderMethodDeclaration(state: RenderState, node: luau.MethodDec result += state.line(`function ${objStr}:${node.name}(`); result += state.block(() => renderParameters(state, node) - .map(p => state.line(p)) + .map(param => state.line(param)) .join(""), ); result += state.line(")"); From ac640e143bd1a2153f6bff0dcbb13c1b610f8c99 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Fri, 3 Jun 2022 15:51:01 -0300 Subject: [PATCH 03/26] Eliminate some duplicated values --- .../expressions/indexable/renderCallExpression.ts | 7 ++++--- .../indexable/renderComputedIndexExpression.ts | 5 +++-- .../indexable/renderMethodCallExpression.ts | 7 ++++--- .../indexable/renderPropertyAccessExpression.ts | 5 +++-- src/LuauRenderer/nodes/expressions/renderArray.ts | 5 +++-- .../nodes/expressions/renderFunctionExpression.ts | 8 ++++---- .../nodes/statements/renderFunctionDeclaration.ts | 10 +++++----- .../nodes/statements/renderMethodDeclaration.ts | 6 +++--- .../nodes/statements/renderVariableDeclaration.ts | 6 +++--- 9 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts index 2c90590..6a57f57 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts @@ -4,9 +4,10 @@ import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderCallExpression(state: RenderState, node: luau.CallExpression) { const expStr = render(state, node.expression); - const argStrs = renderArguments(state, node.args).join(""); + const argsStr = renderArguments(state, node.args).join(""); + const formatStr = `${expStr}(${argsStr})`; - if (state.isFormattable(`${expStr}(${argStrs})`)) { + if (state.isFormattable(formatStr)) { let result = ""; result += state.newline(`${expStr}(`); result += state.block(() => @@ -18,5 +19,5 @@ export function renderCallExpression(state: RenderState, node: luau.CallExpressi return result; } - return `${expStr}(${argStrs})`; + return formatStr; } diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts index cc402fc..e27cf07 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts @@ -5,13 +5,14 @@ export function renderComputedIndexExpression(state: RenderState, node: luau.Com const expStr = render(state, node.expression); if (luau.isStringLiteral(node.index) && luau.isValidIdentifier(node.index.value)) { const nameStr = node.index.value; - if (state.isFormattable(`${expStr}.${nameStr}`)) { + const formatStr = `${expStr}.${nameStr}`; + if (state.isFormattable(formatStr)) { let result = ""; result += state.newline(expStr); result += state.block(() => state.indented(`.${nameStr}`)); return result; } - return `${expStr}.${nameStr}`; + return formatStr; } else { const indexStr = render(state, node.index); return `${expStr}[${indexStr}]`; diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts index 77a853b..8828767 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts @@ -6,9 +6,10 @@ import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderMethodCallExpression(state: RenderState, node: luau.MethodCallExpression) { assert(luau.isValidIdentifier(node.name)); const objStr = render(state, node.expression); - const argStr = renderArguments(state, node.args).join(""); + const argsStr = renderArguments(state, node.args).join(""); + const formatStr = `${objStr}:${node.name}(${argsStr})`; - if (state.isFormattable(`${objStr}:${node.name}(${argStr})`)) { + if (state.isFormattable(formatStr)) { let result = ""; result += state.newline(`${objStr}:${node.name}(`); result += state.block(() => @@ -20,5 +21,5 @@ export function renderMethodCallExpression(state: RenderState, node: luau.Method return result; } - return `${objStr}:${node.name}(${argStr})`; + return formatStr; } diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts index d4cb089..de9e228 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts @@ -5,13 +5,14 @@ export function renderPropertyAccessExpression(state: RenderState, node: luau.Pr const expStr = render(state, node.expression); const nameStr = node.name; if (luau.isValidIdentifier(nameStr)) { - if (state.isFormattable(`${expStr}.${nameStr}`)) { + const formatStr = `${expStr}.${nameStr}`; + if (state.isFormattable(formatStr)) { let result = ""; result += state.newline(expStr); result += state.block(() => state.indented(`.${nameStr}`)); return result; } - return `${expStr}.${nameStr}`; + return formatStr; } else { return `${expStr}["${nameStr}"]`; } diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index 993edce..b7456c2 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -9,12 +9,13 @@ export function renderArray(state: RenderState, node: luau.Array) { const getMembers = () => luau.list.mapToArray(node.members, member => render(state, member)).map(format(state)); const arrStr = getMembers().join(""); + const formatStr = `{ ${arrStr} }`; const hasFunctionExpression = luau.list.some( node.members, member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), ); - if (state.isFormattable(`{ ${arrStr} }`, undefined, hasFunctionExpression)) { + if (state.isFormattable(formatStr, undefined, hasFunctionExpression)) { let result = ""; result += state.newline("{"); result += state.block(() => @@ -26,5 +27,5 @@ export function renderArray(state: RenderState, node: luau.Array) { return result; } - return `{ ${arrStr} }`; + return formatStr; } diff --git a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts index 3a5a5d1..0c633f4 100644 --- a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts +++ b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts @@ -4,8 +4,8 @@ import { renderParameters } from "LuauRenderer/util/renderParameters"; import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderFunctionExpression(state: RenderState, node: luau.FunctionExpression) { - const paramStrs = renderParameters(state, node); - const isFormattable = state.isFormattable(`function(${paramStrs.join("")})`); + const paramStr = renderParameters(state, node).join(""); + const isFormattable = state.isFormattable(`function(${paramStr})`); let result = ""; @@ -21,7 +21,7 @@ export function renderFunctionExpression(state: RenderState, node: luau.Function return result; } - return `function(${paramStrs.join("")}) end`; + return `function(${paramStr}) end`; } if (isFormattable) { @@ -33,7 +33,7 @@ export function renderFunctionExpression(state: RenderState, node: luau.Function ); result += state.line(")"); } else { - result += `function(${paramStrs.join("")}${state.newline(")")}`; + result += `function(${paramStr}${state.newline(")")}`; } result += state.block(() => renderStatements(state, node.statements)); diff --git a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts index 701bf41..428a3e5 100644 --- a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts @@ -9,13 +9,13 @@ export function renderFunctionDeclaration(state: RenderState, node: luau.Functio assert(luau.isAnyIdentifier(node.name), "local function cannot be a property"); } const nameStr = render(state, node.name); - const fnStart = `${node.localize ? "local " : ""}function ${nameStr}(`; - const paramStrs = renderParameters(state, node); + const startStr = `${node.localize ? "local " : ""}function ${nameStr}(`; + const formatStr = `${startStr}${renderParameters(state, node).join("")})`; let result = ""; - if (state.isFormattable(`${fnStart}${paramStrs.join("")})`)) { - result += state.line(fnStart); + if (state.isFormattable(formatStr)) { + result += state.line(startStr); result += state.block(() => renderParameters(state, node) .map(param => state.line(param)) @@ -23,7 +23,7 @@ export function renderFunctionDeclaration(state: RenderState, node: luau.Functio ); result += state.line(")"); } else { - result += state.line(`${fnStart}${paramStrs.join("")})`); + result += state.line(formatStr); } result += state.block(() => renderStatements(state, node.statements)); diff --git a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts index 7b30a0b..de0cf1a 100644 --- a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts @@ -5,11 +5,11 @@ import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderMethodDeclaration(state: RenderState, node: luau.MethodDeclaration) { const objStr = render(state, node.expression); - const paramStrs = renderParameters(state, node); + const startStr = `function ${objStr}:${node.name}(${renderParameters(state, node).join("")})`; let result = ""; - if (state.isFormattable(paramStrs.join(""))) { + if (state.isFormattable(startStr)) { result += state.line(`function ${objStr}:${node.name}(`); result += state.block(() => renderParameters(state, node) @@ -18,7 +18,7 @@ export function renderMethodDeclaration(state: RenderState, node: luau.MethodDec ); result += state.line(")"); } else { - result += state.line(`function ${objStr}:${node.name}(${paramStrs.join("")})`); + result += state.line(startStr); } result += state.block(() => renderStatements(state, node.statements)); diff --git a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts index 8c45123..e9de0c6 100644 --- a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts @@ -12,14 +12,14 @@ export function renderVariableDeclaration(state: RenderState, node: luau.Variabl } const rightNode = node.right; if (rightNode) { - const rightStr = render(state, rightNode); - if (state.isFormattable(`${leftStr} = ${rightStr}`, rightNode.kind)) { + const formatStr = `${leftStr} = ${render(state, rightNode)}`; + if (state.isFormattable(formatStr, rightNode.kind)) { let result = ""; result += state.line(`local ${leftStr} =`); result += state.block(() => state.line(render(state, rightNode), node)); return result; } - return state.line(`local ${leftStr} = ${rightStr}`, node); + return state.line(formatStr, node); } else { return state.line(`local ${leftStr}`, node); } From 07e924d01bdeedd5329989bc6d8ae897c9e2c9d8 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 10:39:01 -0300 Subject: [PATCH 04/26] Fix variable declaration --- src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts index e9de0c6..8b01626 100644 --- a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts @@ -12,7 +12,7 @@ export function renderVariableDeclaration(state: RenderState, node: luau.Variabl } const rightNode = node.right; if (rightNode) { - const formatStr = `${leftStr} = ${render(state, rightNode)}`; + const formatStr = `local ${leftStr} = ${render(state, rightNode)}`; if (state.isFormattable(formatStr, rightNode.kind)) { let result = ""; result += state.line(`local ${leftStr} =`); From ea9d1c1a022d91c7361b320c2b6a0c95c11f54ee Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 10:42:55 -0300 Subject: [PATCH 05/26] Update renderFunctionExpression.ts --- .../nodes/expressions/renderFunctionExpression.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts index 0c633f4..7ee4dfa 100644 --- a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts +++ b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts @@ -4,8 +4,9 @@ import { renderParameters } from "LuauRenderer/util/renderParameters"; import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderFunctionExpression(state: RenderState, node: luau.FunctionExpression) { - const paramStr = renderParameters(state, node).join(""); - const isFormattable = state.isFormattable(`function(${paramStr})`); + const paramsStr = renderParameters(state, node).join(""); + const startStr = `function(${paramsStr})`; + const isFormattable = state.isFormattable(startStr); let result = ""; @@ -21,7 +22,7 @@ export function renderFunctionExpression(state: RenderState, node: luau.Function return result; } - return `function(${paramStr}) end`; + return `${startStr} end`; } if (isFormattable) { @@ -33,7 +34,7 @@ export function renderFunctionExpression(state: RenderState, node: luau.Function ); result += state.line(")"); } else { - result += `function(${paramStr}${state.newline(")")}`; + result += `function(${paramsStr}${state.newline(")")}`; } result += state.block(() => renderStatements(state, node.statements)); From 51e5527c815187c1ac21555e909db966d5f9a75f Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 11:41:13 -0300 Subject: [PATCH 06/26] Reduce render calls duplication --- .../expressions/renderFunctionExpression.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts index 7ee4dfa..7cc508b 100644 --- a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts +++ b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts @@ -7,31 +7,26 @@ export function renderFunctionExpression(state: RenderState, node: luau.Function const paramsStr = renderParameters(state, node).join(""); const startStr = `function(${paramsStr})`; const isFormattable = state.isFormattable(startStr); + const mapParameters = () => + renderParameters(state, node) + .map(param => state.line(param)) + .join(""); let result = ""; if (luau.list.isEmpty(node.statements)) { if (isFormattable) { result += state.newline("function("); - result += state.block(() => - renderParameters(state, node) - .map(param => state.line(param)) - .join(""), - ); + result += state.block(mapParameters); result += state.indented(") end"); return result; } - return `${startStr} end`; } if (isFormattable) { result += state.newline("function("); - result += state.block(() => - renderParameters(state, node) - .map(p => state.line(p)) - .join(""), - ); + result += state.block(mapParameters); result += state.line(")"); } else { result += `function(${paramsStr}${state.newline(")")}`; From f8de1415dd9373906dd3ad67e70e40d1079b60f8 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 12:03:29 -0300 Subject: [PATCH 07/26] Remove redundant variables --- .../nodes/expressions/indexable/renderCallExpression.ts | 4 +--- .../nodes/expressions/indexable/renderMethodCallExpression.ts | 3 +-- src/LuauRenderer/nodes/expressions/renderArray.ts | 3 +-- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts index 6a57f57..bf05a75 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts @@ -4,8 +4,7 @@ import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderCallExpression(state: RenderState, node: luau.CallExpression) { const expStr = render(state, node.expression); - const argsStr = renderArguments(state, node.args).join(""); - const formatStr = `${expStr}(${argsStr})`; + const formatStr = `${expStr}(${renderArguments(state, node.args).join("")})`; if (state.isFormattable(formatStr)) { let result = ""; @@ -16,7 +15,6 @@ export function renderCallExpression(state: RenderState, node: luau.CallExpressi .join(""), ); result += state.indented(")"); - return result; } return formatStr; diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts index 8828767..3417a08 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts @@ -6,8 +6,7 @@ import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderMethodCallExpression(state: RenderState, node: luau.MethodCallExpression) { assert(luau.isValidIdentifier(node.name)); const objStr = render(state, node.expression); - const argsStr = renderArguments(state, node.args).join(""); - const formatStr = `${objStr}:${node.name}(${argsStr})`; + const formatStr = `${objStr}:${node.name}(${renderArguments(state, node.args).join("")})`; if (state.isFormattable(formatStr)) { let result = ""; diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index b7456c2..d96fa63 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -8,8 +8,7 @@ export function renderArray(state: RenderState, node: luau.Array) { } const getMembers = () => luau.list.mapToArray(node.members, member => render(state, member)).map(format(state)); - const arrStr = getMembers().join(""); - const formatStr = `{ ${arrStr} }`; + const formatStr = `{ ${getMembers().join("")} }`; const hasFunctionExpression = luau.list.some( node.members, member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), From 11df90de23d06c0a3b51fc1348bc4f6a07842cc5 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 18:50:14 -0300 Subject: [PATCH 08/26] Improve format handling for functions and arrays --- .../indexable/renderCallExpression.ts | 17 +------ .../indexable/renderMethodCallExpression.ts | 18 +------- .../nodes/expressions/renderArray.ts | 46 ++++++++++--------- .../expressions/renderFunctionExpression.ts | 29 ++---------- .../nodes/statements/renderAssignment.ts | 1 - .../statements/renderFunctionDeclaration.ts | 18 +------- .../statements/renderMethodDeclaration.ts | 19 +------- src/LuauRenderer/util/format.ts | 13 ------ src/LuauRenderer/util/formatFunction.ts | 19 ++++++++ src/LuauRenderer/util/renderArguments.ts | 6 +-- src/LuauRenderer/util/renderParameters.ts | 6 +-- 11 files changed, 62 insertions(+), 130 deletions(-) delete mode 100644 src/LuauRenderer/util/format.ts create mode 100644 src/LuauRenderer/util/formatFunction.ts diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts index bf05a75..9d58a94 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderCallExpression.ts @@ -3,19 +3,6 @@ import { render, RenderState } from "LuauRenderer"; import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderCallExpression(state: RenderState, node: luau.CallExpression) { - const expStr = render(state, node.expression); - const formatStr = `${expStr}(${renderArguments(state, node.args).join("")})`; - - if (state.isFormattable(formatStr)) { - let result = ""; - result += state.newline(`${expStr}(`); - result += state.block(() => - renderArguments(state, node.args) - .map(arg => state.line(arg)) - .join(""), - ); - result += state.indented(")"); - return result; - } - return formatStr; + const nameStr = render(state, node.expression); + return `${nameStr}(${renderArguments(state, node.args, nameStr)})`; } diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts index 3417a08..c1b23e7 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderMethodCallExpression.ts @@ -5,20 +5,6 @@ import { renderArguments } from "LuauRenderer/util/renderArguments"; export function renderMethodCallExpression(state: RenderState, node: luau.MethodCallExpression) { assert(luau.isValidIdentifier(node.name)); - const objStr = render(state, node.expression); - const formatStr = `${objStr}:${node.name}(${renderArguments(state, node.args).join("")})`; - - if (state.isFormattable(formatStr)) { - let result = ""; - result += state.newline(`${objStr}:${node.name}(`); - result += state.block(() => - renderArguments(state, node.args) - .map(arg => state.line(arg)) - .join(""), - ); - result += state.indented(")"); - return result; - } - - return formatStr; + const nameStr = `${render(state, node.expression)}:${node.name}`; + return `${nameStr}(${renderArguments(state, node.args, nameStr)})`; } diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index d96fa63..b7325a8 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -1,30 +1,34 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; -import { format } from "LuauRenderer/util/format"; -export function renderArray(state: RenderState, node: luau.Array) { - if (luau.list.isEmpty(node.members)) { - return "{}"; - } +function formatArray(state: RenderState, renderMembers: () => ReadonlyArray, hasFunctionExpression: boolean) { + const members = renderMembers(); + const arrayStr = `{ ${members.join(", ")} }`; - const getMembers = () => luau.list.mapToArray(node.members, member => render(state, member)).map(format(state)); - const formatStr = `{ ${getMembers().join("")} }`; - const hasFunctionExpression = luau.list.some( - node.members, - member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), - ); - - if (state.isFormattable(formatStr, undefined, hasFunctionExpression)) { - let result = ""; - result += state.newline("{"); - result += state.block(() => - getMembers() - .map(value => state.line(value)) - .join(""), - ); + if (state.isFormattable(arrayStr, undefined, hasFunctionExpression)) { + let result = "{\n"; + state.block(() => { + renderMembers().forEach( + (member, i) => (result += state.line(`${member}${i < members.length - 1 ? "," : ""}`)), + ); + }); result += state.indented("}"); return result; } - return formatStr; + return arrayStr; +} + +export function renderArray(state: RenderState, node: luau.Array) { + if (luau.list.isEmpty(node.members)) { + return "{}"; + } + return formatArray( + state, + () => luau.list.mapToArray(node.members, member => render(state, member)), + luau.list.some( + node.members, + member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), + ), + ); } diff --git a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts index 7cc508b..9945f56 100644 --- a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts +++ b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts @@ -4,34 +4,13 @@ import { renderParameters } from "LuauRenderer/util/renderParameters"; import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderFunctionExpression(state: RenderState, node: luau.FunctionExpression) { - const paramsStr = renderParameters(state, node).join(""); - const startStr = `function(${paramsStr})`; - const isFormattable = state.isFormattable(startStr); - const mapParameters = () => - renderParameters(state, node) - .map(param => state.line(param)) - .join(""); - - let result = ""; - + const nameStr = "function"; if (luau.list.isEmpty(node.statements)) { - if (isFormattable) { - result += state.newline("function("); - result += state.block(mapParameters); - result += state.indented(") end"); - return result; - } - return `${startStr} end`; - } - - if (isFormattable) { - result += state.newline("function("); - result += state.block(mapParameters); - result += state.line(")"); - } else { - result += `function(${paramsStr}${state.newline(")")}`; + return `${nameStr}(${renderParameters(state, node, nameStr)}) end`; } + let result = ""; + result += state.newline(`${nameStr}(${renderParameters(state, node, nameStr)})`); result += state.block(() => renderStatements(state, node.statements)); result += state.indented("end"); return result; diff --git a/src/LuauRenderer/nodes/statements/renderAssignment.ts b/src/LuauRenderer/nodes/statements/renderAssignment.ts index 9013ffe..e769300 100644 --- a/src/LuauRenderer/nodes/statements/renderAssignment.ts +++ b/src/LuauRenderer/nodes/statements/renderAssignment.ts @@ -11,7 +11,6 @@ export function renderAssignment(state: RenderState, node: luau.Assignment) { leftStr = render(state, node.left); } const rightStr = render(state, node.right); - if (state.isFormattable(rightStr, node.right.kind)) { let result = ""; result += state.line(`${leftStr} ${node.operator}`); diff --git a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts index 428a3e5..0095ef3 100644 --- a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts @@ -9,25 +9,11 @@ export function renderFunctionDeclaration(state: RenderState, node: luau.Functio assert(luau.isAnyIdentifier(node.name), "local function cannot be a property"); } const nameStr = render(state, node.name); - const startStr = `${node.localize ? "local " : ""}function ${nameStr}(`; - const formatStr = `${startStr}${renderParameters(state, node).join("")})`; + const paramStr = renderParameters(state, node, nameStr); let result = ""; - - if (state.isFormattable(formatStr)) { - result += state.line(startStr); - result += state.block(() => - renderParameters(state, node) - .map(param => state.line(param)) - .join(""), - ); - result += state.line(")"); - } else { - result += state.line(formatStr); - } - + result += state.line(`${node.localize ? "local " : ""}function ${nameStr}(${paramStr})`); result += state.block(() => renderStatements(state, node.statements)); result += state.line("end"); - return result; } diff --git a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts index de0cf1a..9926ae0 100644 --- a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts @@ -4,25 +4,10 @@ import { renderParameters } from "LuauRenderer/util/renderParameters"; import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderMethodDeclaration(state: RenderState, node: luau.MethodDeclaration) { - const objStr = render(state, node.expression); - const startStr = `function ${objStr}:${node.name}(${renderParameters(state, node).join("")})`; - + const nameStr = render(state, node.expression); let result = ""; - - if (state.isFormattable(startStr)) { - result += state.line(`function ${objStr}:${node.name}(`); - result += state.block(() => - renderParameters(state, node) - .map(param => state.line(param)) - .join(""), - ); - result += state.line(")"); - } else { - result += state.line(startStr); - } - + result += state.line(`function ${nameStr}:${node.name}(${renderParameters(state, node, nameStr)})`); result += state.block(() => renderStatements(state, node.statements)); result += state.line("end"); - return result; } diff --git a/src/LuauRenderer/util/format.ts b/src/LuauRenderer/util/format.ts deleted file mode 100644 index 29f414e..0000000 --- a/src/LuauRenderer/util/format.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { RenderState } from "LuauAST"; - -/** - * Utility function that takes a list of string values - * and removes the space at the final of each string if the result is formattable. - * @param state The render state. - */ -export function format(state: RenderState) { - return (value: string, index: number, list: ReadonlyArray) => - list.length > 1 && index < list.length - 1 - ? `${value},${state.isFormattable(list.join(", ")) ? "" : " "}` - : value; -} diff --git a/src/LuauRenderer/util/formatFunction.ts b/src/LuauRenderer/util/formatFunction.ts new file mode 100644 index 0000000..fcce6c7 --- /dev/null +++ b/src/LuauRenderer/util/formatFunction.ts @@ -0,0 +1,19 @@ +import { RenderState } from "LuauAST"; + +/** + * Utility function that takes a list of string values + * and removes the space at the final of each string if the result is formattable. + * @param state The render state. + */ +export function formatFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { + const list = renderList(); + if (state.isFormattable(`${name ?? ""}(${renderList().join(", ")})`)) { + let result = "\n"; + state.block(() => { + renderList().forEach((v, i) => (result += state.line(`${v}${i < list.length - 1 ? "," : ""}`))); + }); + result += state.indented(""); + return result; + } + return list.join(", "); +} diff --git a/src/LuauRenderer/util/renderArguments.ts b/src/LuauRenderer/util/renderArguments.ts index c3e707f..1be5f6b 100644 --- a/src/LuauRenderer/util/renderArguments.ts +++ b/src/LuauRenderer/util/renderArguments.ts @@ -1,8 +1,8 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; -import { format } from "LuauRenderer/util/format"; +import { formatFunction } from "LuauRenderer/util/formatFunction"; /** Renders the given list of expressions into a string separated by commas */ -export function renderArguments(state: RenderState, expressions: luau.List) { - return luau.list.mapToArray(expressions, v => render(state, v)).map(format(state)); +export function renderArguments(state: RenderState, expressions: luau.List, name: string) { + return formatFunction(state, () => luau.list.mapToArray(expressions, v => render(state, v)), name); } diff --git a/src/LuauRenderer/util/renderParameters.ts b/src/LuauRenderer/util/renderParameters.ts index b6a6414..b8075f1 100644 --- a/src/LuauRenderer/util/renderParameters.ts +++ b/src/LuauRenderer/util/renderParameters.ts @@ -1,16 +1,16 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; -import { format } from "LuauRenderer/util/format"; +import { formatFunction } from "LuauRenderer/util/formatFunction"; /** * Renders the given list of identifiers inside of `node` into a string sepearted by commas * * Adds `...` onto the end if node.hasDotDotDot is true */ -export function renderParameters(state: RenderState, node: luau.HasParameters) { +export function renderParameters(state: RenderState, node: luau.HasParameters, name?: string) { const paramStrs = luau.list.mapToArray(node.parameters, param => render(state, param)); if (node.hasDotDotDot) { paramStrs.push("..."); } - return paramStrs.map(format(state)); + return formatFunction(state, () => paramStrs, name); } From c46e3cb74d5f61cc2832b53386908d1294bcdd6a Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 18:54:35 -0300 Subject: [PATCH 09/26] Add JSDoc parameters --- src/LuauRenderer/util/formatFunction.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/LuauRenderer/util/formatFunction.ts b/src/LuauRenderer/util/formatFunction.ts index fcce6c7..f87c736 100644 --- a/src/LuauRenderer/util/formatFunction.ts +++ b/src/LuauRenderer/util/formatFunction.ts @@ -4,6 +4,8 @@ import { RenderState } from "LuauAST"; * Utility function that takes a list of string values * and removes the space at the final of each string if the result is formattable. * @param state The render state. + * @param renderList The renderer callback. + * @param name The name of the function. */ export function formatFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { const list = renderList(); From 1af441fcf67b9337fb622289ae645a3daa1b84f6 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 18:54:59 -0300 Subject: [PATCH 10/26] Fix doc typo in renderParameters.ts --- src/LuauRenderer/util/renderParameters.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuauRenderer/util/renderParameters.ts b/src/LuauRenderer/util/renderParameters.ts index b8075f1..3ad17e0 100644 --- a/src/LuauRenderer/util/renderParameters.ts +++ b/src/LuauRenderer/util/renderParameters.ts @@ -3,7 +3,7 @@ import { render, RenderState } from "LuauRenderer"; import { formatFunction } from "LuauRenderer/util/formatFunction"; /** - * Renders the given list of identifiers inside of `node` into a string sepearted by commas + * Renders the given list of identifiers inside of `node` into a string separated by commas * * Adds `...` onto the end if node.hasDotDotDot is true */ From 07f7bf10521b7910841ca34e908e1426def94f38 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sat, 4 Jun 2022 19:01:28 -0300 Subject: [PATCH 11/26] Rename formatFunction -> renderFunction --- src/LuauRenderer/util/renderArguments.ts | 4 ++-- .../util/{formatFunction.ts => renderFunction.ts} | 2 +- src/LuauRenderer/util/renderParameters.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/LuauRenderer/util/{formatFunction.ts => renderFunction.ts} (91%) diff --git a/src/LuauRenderer/util/renderArguments.ts b/src/LuauRenderer/util/renderArguments.ts index 1be5f6b..e48370f 100644 --- a/src/LuauRenderer/util/renderArguments.ts +++ b/src/LuauRenderer/util/renderArguments.ts @@ -1,8 +1,8 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; -import { formatFunction } from "LuauRenderer/util/formatFunction"; +import { renderFunction } from "LuauRenderer/util/renderFunction"; /** Renders the given list of expressions into a string separated by commas */ export function renderArguments(state: RenderState, expressions: luau.List, name: string) { - return formatFunction(state, () => luau.list.mapToArray(expressions, v => render(state, v)), name); + return renderFunction(state, () => luau.list.mapToArray(expressions, v => render(state, v)), name); } diff --git a/src/LuauRenderer/util/formatFunction.ts b/src/LuauRenderer/util/renderFunction.ts similarity index 91% rename from src/LuauRenderer/util/formatFunction.ts rename to src/LuauRenderer/util/renderFunction.ts index f87c736..5ec2e07 100644 --- a/src/LuauRenderer/util/formatFunction.ts +++ b/src/LuauRenderer/util/renderFunction.ts @@ -7,7 +7,7 @@ import { RenderState } from "LuauAST"; * @param renderList The renderer callback. * @param name The name of the function. */ -export function formatFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { +export function renderFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { const list = renderList(); if (state.isFormattable(`${name ?? ""}(${renderList().join(", ")})`)) { let result = "\n"; diff --git a/src/LuauRenderer/util/renderParameters.ts b/src/LuauRenderer/util/renderParameters.ts index 3ad17e0..bf8c38d 100644 --- a/src/LuauRenderer/util/renderParameters.ts +++ b/src/LuauRenderer/util/renderParameters.ts @@ -1,6 +1,6 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; -import { formatFunction } from "LuauRenderer/util/formatFunction"; +import { renderFunction } from "LuauRenderer/util/renderFunction"; /** * Renders the given list of identifiers inside of `node` into a string separated by commas @@ -12,5 +12,5 @@ export function renderParameters(state: RenderState, node: luau.HasParameters, n if (node.hasDotDotDot) { paramStrs.push("..."); } - return formatFunction(state, () => paramStrs, name); + return renderFunction(state, () => paramStrs, name); } From b98067f77e45e5d5467d532da47edbf9d5865fd2 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 12:14:24 -0300 Subject: [PATCH 12/26] Update renderArray.ts --- .../nodes/expressions/renderArray.ts | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index b7325a8..a8e5450 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -1,11 +1,20 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; -function formatArray(state: RenderState, renderMembers: () => ReadonlyArray, hasFunctionExpression: boolean) { +export function renderArray(state: RenderState, node: luau.Array) { + if (luau.list.isEmpty(node.members)) { + return "{}"; + } + const renderMembers = () => luau.list.mapToArray(node.members, member => render(state, member)); const members = renderMembers(); const arrayStr = `{ ${members.join(", ")} }`; + // should format if a member is a non-empty function expression + const skipFormatCheck = luau.list.some( + node.members, + member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), + ); - if (state.isFormattable(arrayStr, undefined, hasFunctionExpression)) { + if (state.isFormattable(arrayStr, undefined, skipFormatCheck)) { let result = "{\n"; state.block(() => { renderMembers().forEach( @@ -18,17 +27,3 @@ function formatArray(state: RenderState, renderMembers: () => ReadonlyArray luau.list.mapToArray(node.members, member => render(state, member)), - luau.list.some( - node.members, - member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), - ), - ); -} From da07037890f735588a73b50a15b0d3cde35da5a6 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 13:10:57 -0300 Subject: [PATCH 13/26] Add renderRightHandSide.ts --- src/LuauRenderer/RenderState.ts | 21 ++-------------- .../nodes/expressions/renderArray.ts | 2 +- .../nodes/statements/renderAssignment.ts | 14 ++--------- .../statements/renderVariableDeclaration.ts | 13 +++------- src/LuauRenderer/util/renderRightHandSide.ts | 24 +++++++++++++++++++ 5 files changed, 32 insertions(+), 42 deletions(-) create mode 100644 src/LuauRenderer/util/renderRightHandSide.ts diff --git a/src/LuauRenderer/RenderState.ts b/src/LuauRenderer/RenderState.ts index 03c640b..b571b00 100644 --- a/src/LuauRenderer/RenderState.ts +++ b/src/LuauRenderer/RenderState.ts @@ -7,15 +7,6 @@ const PRINT_WIDTH = 70; const INDENT_CHARACTER = "\t"; const INDENT_CHARACTER_LENGTH = INDENT_CHARACTER.length; -const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ - luau.SyntaxKind.CallExpression, - luau.SyntaxKind.MethodCallExpression, - luau.SyntaxKind.Array, - luau.SyntaxKind.Map, - luau.SyntaxKind.Set, - luau.SyntaxKind.MixedTable, -]); - /** * Represents the state of a rendering process. */ @@ -117,18 +108,10 @@ export class RenderState { /** * Checks to see if `text` can be formatted. - * - * If a `expressionKind` value is passed, it will also check to see if the right-hand side expression can be formated. * @param text The text. - * @param expressionKind The kind of the right-hand side expression. * @param skipCheck If set to true, the result will be always formattable. */ - public isFormattable(text: string, expressionKind?: keyof luau.ExpressionByKind, skipCheck = false) { - if (skipCheck) return true; - const isLongStr = text.length > PRINT_WIDTH; - if (expressionKind !== undefined) { - return isLongStr && !NON_FORMATTABLE_RHS_EXPRESSIONS.has(expressionKind); - } - return isLongStr; + public isFormattable(text: string, skipCheck = false) { + return skipCheck || text.length > PRINT_WIDTH; } } diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index a8e5450..65c865e 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -14,7 +14,7 @@ export function renderArray(state: RenderState, node: luau.Array) { member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), ); - if (state.isFormattable(arrayStr, undefined, skipFormatCheck)) { + if (state.isFormattable(arrayStr, skipFormatCheck)) { let result = "{\n"; state.block(() => { renderMembers().forEach( diff --git a/src/LuauRenderer/nodes/statements/renderAssignment.ts b/src/LuauRenderer/nodes/statements/renderAssignment.ts index e769300..93245ff 100644 --- a/src/LuauRenderer/nodes/statements/renderAssignment.ts +++ b/src/LuauRenderer/nodes/statements/renderAssignment.ts @@ -1,6 +1,7 @@ import luau from "LuauAST"; import { assert } from "LuauAST/util/assert"; import { render, RenderState } from "LuauRenderer"; +import { renderRightHandSide } from "LuauRenderer/util/renderRightHandSide"; export function renderAssignment(state: RenderState, node: luau.Assignment) { let leftStr: string; @@ -10,16 +11,5 @@ export function renderAssignment(state: RenderState, node: luau.Assignment) { } else { leftStr = render(state, node.left); } - const rightStr = render(state, node.right); - if (state.isFormattable(rightStr, node.right.kind)) { - let result = ""; - result += state.line(`${leftStr} ${node.operator}`); - result += state.block(() => - state.isFormattable(leftStr) - ? state.block(() => state.line(render(state, node.right), node)) - : state.line(render(state, node.right), node), - ); - return result; - } - return state.line(`${leftStr} ${node.operator} ${rightStr}`, node); + return state.line(renderRightHandSide(state, `${leftStr} ${node.operator} `, node.right), node); } diff --git a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts index 8b01626..e1471ef 100644 --- a/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderVariableDeclaration.ts @@ -1,6 +1,7 @@ import luau from "LuauAST"; import { assert } from "LuauAST/util/assert"; import { render, RenderState } from "LuauRenderer"; +import { renderRightHandSide } from "LuauRenderer/util/renderRightHandSide"; export function renderVariableDeclaration(state: RenderState, node: luau.VariableDeclaration) { let leftStr: string; @@ -10,16 +11,8 @@ export function renderVariableDeclaration(state: RenderState, node: luau.Variabl } else { leftStr = render(state, node.left); } - const rightNode = node.right; - if (rightNode) { - const formatStr = `local ${leftStr} = ${render(state, rightNode)}`; - if (state.isFormattable(formatStr, rightNode.kind)) { - let result = ""; - result += state.line(`local ${leftStr} =`); - result += state.block(() => state.line(render(state, rightNode), node)); - return result; - } - return state.line(formatStr, node); + if (node.right) { + return state.line(renderRightHandSide(state, `local ${leftStr} = `, node.right), node); } else { return state.line(`local ${leftStr}`, node); } diff --git a/src/LuauRenderer/util/renderRightHandSide.ts b/src/LuauRenderer/util/renderRightHandSide.ts new file mode 100644 index 0000000..52fd392 --- /dev/null +++ b/src/LuauRenderer/util/renderRightHandSide.ts @@ -0,0 +1,24 @@ +import luau, { render, RenderState } from "LuauAST"; + +const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ + luau.SyntaxKind.CallExpression, + luau.SyntaxKind.MethodCallExpression, + luau.SyntaxKind.Array, + luau.SyntaxKind.Map, + luau.SyntaxKind.Set, + luau.SyntaxKind.MixedTable, +]); + +/** Renders statements with left and right-hand side values and formats them if necessary. */ +export function renderRightHandSide(state: RenderState, lhsStr: string, rhsExpr: luau.Expression) { + const rightStr = render(state, rhsExpr); + const formatStr = `${lhsStr}${rightStr}`; + if ( + state.isFormattable(formatStr) && + state.isFormattable(rightStr) && + !NON_FORMATTABLE_RHS_EXPRESSIONS.has(rhsExpr.kind) + ) { + return `${state.newline(lhsStr)}${state.block(() => state.indented(render(state, rhsExpr)))}`; + } + return formatStr; +} From 9575278dc28490cbe36548017f2333419c57e2e4 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 13:12:31 -0300 Subject: [PATCH 14/26] Update renderFunction.ts --- src/LuauRenderer/util/renderFunction.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/LuauRenderer/util/renderFunction.ts b/src/LuauRenderer/util/renderFunction.ts index 5ec2e07..d1a3fc3 100644 --- a/src/LuauRenderer/util/renderFunction.ts +++ b/src/LuauRenderer/util/renderFunction.ts @@ -1,11 +1,10 @@ import { RenderState } from "LuauAST"; /** + * Renders function parameters and call arguments. + * * Utility function that takes a list of string values * and removes the space at the final of each string if the result is formattable. - * @param state The render state. - * @param renderList The renderer callback. - * @param name The name of the function. */ export function renderFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { const list = renderList(); From ea962ac1ad37f353fdd632fed9a0e9c56ab811a3 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 13:32:53 -0300 Subject: [PATCH 15/26] Simplify indexable expressions --- .../indexable/renderComputedIndexExpression.ts | 11 ++--------- .../indexable/renderPropertyAccessExpression.ts | 5 +---- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts index e27cf07..550d3e6 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts @@ -1,18 +1,11 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; +import { renderRightHandSide } from "LuauRenderer/util/renderRightHandSide"; export function renderComputedIndexExpression(state: RenderState, node: luau.ComputedIndexExpression) { const expStr = render(state, node.expression); if (luau.isStringLiteral(node.index) && luau.isValidIdentifier(node.index.value)) { - const nameStr = node.index.value; - const formatStr = `${expStr}.${nameStr}`; - if (state.isFormattable(formatStr)) { - let result = ""; - result += state.newline(expStr); - result += state.block(() => state.indented(`.${nameStr}`)); - return result; - } - return formatStr; + return renderRightHandSide(state, `${expStr}.`, node.index); } else { const indexStr = render(state, node.index); return `${expStr}[${indexStr}]`; diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts index de9e228..7d2ef49 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts @@ -7,10 +7,7 @@ export function renderPropertyAccessExpression(state: RenderState, node: luau.Pr if (luau.isValidIdentifier(nameStr)) { const formatStr = `${expStr}.${nameStr}`; if (state.isFormattable(formatStr)) { - let result = ""; - result += state.newline(expStr); - result += state.block(() => state.indented(`.${nameStr}`)); - return result; + return `${state.newline(expStr)}${state.block(() => state.indented(`.${nameStr}`))}`; } return formatStr; } else { From 19052c06ca432d2311f05c61a9f3347eba8f01f5 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 14:52:58 -0300 Subject: [PATCH 16/26] Add RenderState.list() --- src/LuauRenderer/RenderState.ts | 12 ++++++++++++ src/LuauRenderer/nodes/expressions/renderArray.ts | 9 +-------- src/LuauRenderer/util/renderFunction.ts | 7 +------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/LuauRenderer/RenderState.ts b/src/LuauRenderer/RenderState.ts index b571b00..ca00828 100644 --- a/src/LuauRenderer/RenderState.ts +++ b/src/LuauRenderer/RenderState.ts @@ -106,6 +106,18 @@ export class RenderState { return result; } + /** + * Returns a rendered list of lines. + * @param callback The renderer callback. + */ + public list(callback: () => ReadonlyArray) { + let result = ""; + this.block(() => { + callback().forEach((v, i, list) => (result += this.line(`${v}${i < list.length - 1 ? "," : ""}`))); + }); + return result; + } + /** * Checks to see if `text` can be formatted. * @param text The text. diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index 65c865e..7636090 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -15,14 +15,7 @@ export function renderArray(state: RenderState, node: luau.Array) { ); if (state.isFormattable(arrayStr, skipFormatCheck)) { - let result = "{\n"; - state.block(() => { - renderMembers().forEach( - (member, i) => (result += state.line(`${member}${i < members.length - 1 ? "," : ""}`)), - ); - }); - result += state.indented("}"); - return result; + return `${state.newline("{")}${state.list(renderMembers)}${state.indented("}")}`; } return arrayStr; diff --git a/src/LuauRenderer/util/renderFunction.ts b/src/LuauRenderer/util/renderFunction.ts index d1a3fc3..2b11160 100644 --- a/src/LuauRenderer/util/renderFunction.ts +++ b/src/LuauRenderer/util/renderFunction.ts @@ -9,12 +9,7 @@ import { RenderState } from "LuauAST"; export function renderFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { const list = renderList(); if (state.isFormattable(`${name ?? ""}(${renderList().join(", ")})`)) { - let result = "\n"; - state.block(() => { - renderList().forEach((v, i) => (result += state.line(`${v}${i < list.length - 1 ? "," : ""}`))); - }); - result += state.indented(""); - return result; + return `\n${state.list(renderList)}${state.indented("")}`; } return list.join(", "); } From 9bb9e5337c29cd8a09c0566254b478b80b0a18d7 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 15:03:14 -0300 Subject: [PATCH 17/26] Update renderFunction.ts JSDoc info --- src/LuauRenderer/util/renderFunction.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/LuauRenderer/util/renderFunction.ts b/src/LuauRenderer/util/renderFunction.ts index 2b11160..912d747 100644 --- a/src/LuauRenderer/util/renderFunction.ts +++ b/src/LuauRenderer/util/renderFunction.ts @@ -3,8 +3,7 @@ import { RenderState } from "LuauAST"; /** * Renders function parameters and call arguments. * - * Utility function that takes a list of string values - * and removes the space at the final of each string if the result is formattable. + * Takes a list of string values and removes the space at the final of each string if the result is formattable. */ export function renderFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { const list = renderList(); From 9ec84d5da9561462a7f98d851bee37ed8b1de3a0 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 15:07:04 -0300 Subject: [PATCH 18/26] Improve renderFunction.ts --- src/LuauRenderer/util/renderFunction.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/LuauRenderer/util/renderFunction.ts b/src/LuauRenderer/util/renderFunction.ts index 912d747..c330f75 100644 --- a/src/LuauRenderer/util/renderFunction.ts +++ b/src/LuauRenderer/util/renderFunction.ts @@ -6,9 +6,9 @@ import { RenderState } from "LuauAST"; * Takes a list of string values and removes the space at the final of each string if the result is formattable. */ export function renderFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { - const list = renderList(); - if (state.isFormattable(`${name ?? ""}(${renderList().join(", ")})`)) { + const formatStr = renderList().join(", "); + if (state.isFormattable(`${name ?? ""}(${formatStr})`)) { return `\n${state.list(renderList)}${state.indented("")}`; } - return list.join(", "); + return formatStr; } From ee97a72ebbcafe61b3b0908335e2c9c226ba9511 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 15:13:15 -0300 Subject: [PATCH 19/26] Update renderComputedIndexExpression.ts --- .../indexable/renderComputedIndexExpression.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts index 550d3e6..5c969c8 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts @@ -1,11 +1,15 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; -import { renderRightHandSide } from "LuauRenderer/util/renderRightHandSide"; export function renderComputedIndexExpression(state: RenderState, node: luau.ComputedIndexExpression) { const expStr = render(state, node.expression); if (luau.isStringLiteral(node.index) && luau.isValidIdentifier(node.index.value)) { - return renderRightHandSide(state, `${expStr}.`, node.index); + const nameStr = node.index.value; + const formatStr = `${expStr}.${nameStr}`; + if (state.isFormattable(formatStr)) { + return `${state.newline(expStr)}${state.block(() => state.indented(`.${nameStr}`))}`; + } + return formatStr; } else { const indexStr = render(state, node.index); return `${expStr}[${indexStr}]`; From 3e7473932191d018a16e5c7418540c1a63199c73 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 15:15:52 -0300 Subject: [PATCH 20/26] Remove redundant generic in renderRightHandSide.ts --- src/LuauRenderer/util/renderRightHandSide.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuauRenderer/util/renderRightHandSide.ts b/src/LuauRenderer/util/renderRightHandSide.ts index 52fd392..15cc9d8 100644 --- a/src/LuauRenderer/util/renderRightHandSide.ts +++ b/src/LuauRenderer/util/renderRightHandSide.ts @@ -1,6 +1,6 @@ import luau, { render, RenderState } from "LuauAST"; -const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ +const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ luau.SyntaxKind.CallExpression, luau.SyntaxKind.MethodCallExpression, luau.SyntaxKind.Array, From 557599b116a338331bc71c58c7364f865a09d2c7 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 20:10:02 -0300 Subject: [PATCH 21/26] Increase printWidth to 100 --- src/LuauRenderer/RenderState.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuauRenderer/RenderState.ts b/src/LuauRenderer/RenderState.ts index ca00828..7fe4167 100644 --- a/src/LuauRenderer/RenderState.ts +++ b/src/LuauRenderer/RenderState.ts @@ -3,7 +3,7 @@ import { assert } from "LuauAST/util/assert"; import { getEnding } from "LuauRenderer/util/getEnding"; import { getOrSetDefault } from "LuauRenderer/util/getOrSetDefault"; -const PRINT_WIDTH = 70; +const PRINT_WIDTH = 100; const INDENT_CHARACTER = "\t"; const INDENT_CHARACTER_LENGTH = INDENT_CHARACTER.length; From 555ed715be217dc5424abf82396c8f6e5aedf427 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 21:23:48 -0300 Subject: [PATCH 22/26] Remove redundant skipCheck paramater in RenderState.isFormattable --- src/LuauRenderer/RenderState.ts | 5 ++--- src/LuauRenderer/nodes/expressions/renderArray.ts | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/LuauRenderer/RenderState.ts b/src/LuauRenderer/RenderState.ts index 7fe4167..d7ab184 100644 --- a/src/LuauRenderer/RenderState.ts +++ b/src/LuauRenderer/RenderState.ts @@ -121,9 +121,8 @@ export class RenderState { /** * Checks to see if `text` can be formatted. * @param text The text. - * @param skipCheck If set to true, the result will be always formattable. */ - public isFormattable(text: string, skipCheck = false) { - return skipCheck || text.length > PRINT_WIDTH; + public isFormattable(text: string) { + return text.length > PRINT_WIDTH; } } diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index 7636090..724c152 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -14,7 +14,7 @@ export function renderArray(state: RenderState, node: luau.Array) { member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), ); - if (state.isFormattable(arrayStr, skipFormatCheck)) { + if (skipFormatCheck || state.isFormattable(arrayStr)) { return `${state.newline("{")}${state.list(renderMembers)}${state.indented("}")}`; } From e7f3b7caa65167bdcc300df6ff7ed05f33363595 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Sun, 5 Jun 2022 21:49:40 -0300 Subject: [PATCH 23/26] Remove package*.json updates --- package-lock.json | 1 - package.json | 1 - 2 files changed, 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 138edad..3968c49 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,6 @@ "requires": true, "packages": { "": { - "name": "@roblox-ts/luau-ast", "version": "1.0.1", "license": "MIT", "devDependencies": { diff --git a/package.json b/package.json index 08e5a0e..83274b0 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "version": "1.0.1", "description": "Luau AST + Renderer for roblox-ts", "main": "out/LuauAST/index.js", - "types": "out/LuauAST/index.d.ts", "repository": { "type": "git", "url": "https://github.com/roblox-ts/luau-ast.git" From 2faf92f4d1d3424aebeaa57b917c561b55ae1d29 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Mon, 6 Jun 2022 00:29:26 -0300 Subject: [PATCH 24/26] Move isFormattable out of RenderState --- src/LuauRenderer/RenderState.ts | 9 --------- .../indexable/renderComputedIndexExpression.ts | 3 ++- .../indexable/renderPropertyAccessExpression.ts | 3 ++- src/LuauRenderer/nodes/expressions/renderArray.ts | 3 ++- src/LuauRenderer/util/isFormattable.ts | 9 +++++++++ src/LuauRenderer/util/renderFunction.ts | 3 ++- src/LuauRenderer/util/renderRightHandSide.ts | 7 ++----- 7 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 src/LuauRenderer/util/isFormattable.ts diff --git a/src/LuauRenderer/RenderState.ts b/src/LuauRenderer/RenderState.ts index d7ab184..5176bce 100644 --- a/src/LuauRenderer/RenderState.ts +++ b/src/LuauRenderer/RenderState.ts @@ -3,7 +3,6 @@ import { assert } from "LuauAST/util/assert"; import { getEnding } from "LuauRenderer/util/getEnding"; import { getOrSetDefault } from "LuauRenderer/util/getOrSetDefault"; -const PRINT_WIDTH = 100; const INDENT_CHARACTER = "\t"; const INDENT_CHARACTER_LENGTH = INDENT_CHARACTER.length; @@ -117,12 +116,4 @@ export class RenderState { }); return result; } - - /** - * Checks to see if `text` can be formatted. - * @param text The text. - */ - public isFormattable(text: string) { - return text.length > PRINT_WIDTH; - } } diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts index 5c969c8..b8bcb90 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderComputedIndexExpression.ts @@ -1,12 +1,13 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; +import { isFormattable } from "LuauRenderer/util/isFormattable"; export function renderComputedIndexExpression(state: RenderState, node: luau.ComputedIndexExpression) { const expStr = render(state, node.expression); if (luau.isStringLiteral(node.index) && luau.isValidIdentifier(node.index.value)) { const nameStr = node.index.value; const formatStr = `${expStr}.${nameStr}`; - if (state.isFormattable(formatStr)) { + if (isFormattable(formatStr)) { return `${state.newline(expStr)}${state.block(() => state.indented(`.${nameStr}`))}`; } return formatStr; diff --git a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts index 7d2ef49..c526491 100644 --- a/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts +++ b/src/LuauRenderer/nodes/expressions/indexable/renderPropertyAccessExpression.ts @@ -1,12 +1,13 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; +import { isFormattable } from "LuauRenderer/util/isFormattable"; export function renderPropertyAccessExpression(state: RenderState, node: luau.PropertyAccessExpression) { const expStr = render(state, node.expression); const nameStr = node.name; if (luau.isValidIdentifier(nameStr)) { const formatStr = `${expStr}.${nameStr}`; - if (state.isFormattable(formatStr)) { + if (isFormattable(formatStr)) { return `${state.newline(expStr)}${state.block(() => state.indented(`.${nameStr}`))}`; } return formatStr; diff --git a/src/LuauRenderer/nodes/expressions/renderArray.ts b/src/LuauRenderer/nodes/expressions/renderArray.ts index 724c152..49977f9 100644 --- a/src/LuauRenderer/nodes/expressions/renderArray.ts +++ b/src/LuauRenderer/nodes/expressions/renderArray.ts @@ -1,5 +1,6 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; +import { isFormattable } from "LuauRenderer/util/isFormattable"; export function renderArray(state: RenderState, node: luau.Array) { if (luau.list.isEmpty(node.members)) { @@ -14,7 +15,7 @@ export function renderArray(state: RenderState, node: luau.Array) { member => luau.isFunctionExpression(member) && !luau.list.isEmpty(member.statements), ); - if (skipFormatCheck || state.isFormattable(arrayStr)) { + if (skipFormatCheck || isFormattable(arrayStr)) { return `${state.newline("{")}${state.list(renderMembers)}${state.indented("}")}`; } diff --git a/src/LuauRenderer/util/isFormattable.ts b/src/LuauRenderer/util/isFormattable.ts new file mode 100644 index 0000000..596f9bb --- /dev/null +++ b/src/LuauRenderer/util/isFormattable.ts @@ -0,0 +1,9 @@ +const PRINT_WIDTH = 100; + +/** + * Checks to see if `text` can be formatted. + * @param text The text. + */ +export function isFormattable(text: string) { + return text.length > PRINT_WIDTH; +} diff --git a/src/LuauRenderer/util/renderFunction.ts b/src/LuauRenderer/util/renderFunction.ts index c330f75..df58ad5 100644 --- a/src/LuauRenderer/util/renderFunction.ts +++ b/src/LuauRenderer/util/renderFunction.ts @@ -1,4 +1,5 @@ import { RenderState } from "LuauAST"; +import { isFormattable } from "LuauRenderer/util/isFormattable"; /** * Renders function parameters and call arguments. @@ -7,7 +8,7 @@ import { RenderState } from "LuauAST"; */ export function renderFunction(state: RenderState, renderList: () => ReadonlyArray, name?: string) { const formatStr = renderList().join(", "); - if (state.isFormattable(`${name ?? ""}(${formatStr})`)) { + if (isFormattable(`${name ?? ""}(${formatStr})`)) { return `\n${state.list(renderList)}${state.indented("")}`; } return formatStr; diff --git a/src/LuauRenderer/util/renderRightHandSide.ts b/src/LuauRenderer/util/renderRightHandSide.ts index 15cc9d8..b4d444e 100644 --- a/src/LuauRenderer/util/renderRightHandSide.ts +++ b/src/LuauRenderer/util/renderRightHandSide.ts @@ -1,4 +1,5 @@ import luau, { render, RenderState } from "LuauAST"; +import { isFormattable } from "LuauRenderer/util/isFormattable"; const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ luau.SyntaxKind.CallExpression, @@ -13,11 +14,7 @@ const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ export function renderRightHandSide(state: RenderState, lhsStr: string, rhsExpr: luau.Expression) { const rightStr = render(state, rhsExpr); const formatStr = `${lhsStr}${rightStr}`; - if ( - state.isFormattable(formatStr) && - state.isFormattable(rightStr) && - !NON_FORMATTABLE_RHS_EXPRESSIONS.has(rhsExpr.kind) - ) { + if (isFormattable(formatStr) && isFormattable(rightStr) && !NON_FORMATTABLE_RHS_EXPRESSIONS.has(rhsExpr.kind)) { return `${state.newline(lhsStr)}${state.block(() => state.indented(render(state, rhsExpr)))}`; } return formatStr; From 4fc50947d33fdf4829ebc065e77a472671570c1e Mon Sep 17 00:00:00 2001 From: Rimuy Date: Mon, 6 Jun 2022 00:49:40 -0300 Subject: [PATCH 25/26] Revert string literals change --- .../nodes/expressions/renderFunctionExpression.ts | 2 +- src/LuauRenderer/nodes/statements/renderBreakStatement.ts | 2 +- src/LuauRenderer/nodes/statements/renderCallStatement.ts | 2 +- src/LuauRenderer/nodes/statements/renderContinueStatement.ts | 2 +- src/LuauRenderer/nodes/statements/renderDoStatement.ts | 4 ++-- src/LuauRenderer/nodes/statements/renderForStatement.ts | 2 +- .../nodes/statements/renderFunctionDeclaration.ts | 2 +- src/LuauRenderer/nodes/statements/renderIfStatement.ts | 4 ++-- src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts | 2 +- .../nodes/statements/renderNumericForStatement.ts | 2 +- src/LuauRenderer/nodes/statements/renderRepeatStatement.ts | 2 +- src/LuauRenderer/nodes/statements/renderWhileStatement.ts | 2 +- 12 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts index 9945f56..ae265fb 100644 --- a/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts +++ b/src/LuauRenderer/nodes/expressions/renderFunctionExpression.ts @@ -12,6 +12,6 @@ export function renderFunctionExpression(state: RenderState, node: luau.Function let result = ""; result += state.newline(`${nameStr}(${renderParameters(state, node, nameStr)})`); result += state.block(() => renderStatements(state, node.statements)); - result += state.indented("end"); + result += state.indented(`end`); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderBreakStatement.ts b/src/LuauRenderer/nodes/statements/renderBreakStatement.ts index dea7705..0290262 100644 --- a/src/LuauRenderer/nodes/statements/renderBreakStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderBreakStatement.ts @@ -1,5 +1,5 @@ import { RenderState } from "LuauRenderer"; export function renderBreakStatement(state: RenderState) { - return state.line("break"); + return state.line(`break`); } diff --git a/src/LuauRenderer/nodes/statements/renderCallStatement.ts b/src/LuauRenderer/nodes/statements/renderCallStatement.ts index b1875d6..3e18f13 100644 --- a/src/LuauRenderer/nodes/statements/renderCallStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderCallStatement.ts @@ -2,5 +2,5 @@ import luau from "LuauAST"; import { render, RenderState } from "LuauRenderer"; export function renderCallStatement(state: RenderState, node: luau.CallStatement) { - return state.line(render(state, node.expression), node); + return state.line(`${render(state, node.expression)}`, node); } diff --git a/src/LuauRenderer/nodes/statements/renderContinueStatement.ts b/src/LuauRenderer/nodes/statements/renderContinueStatement.ts index 1db50d8..272ee77 100644 --- a/src/LuauRenderer/nodes/statements/renderContinueStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderContinueStatement.ts @@ -1,5 +1,5 @@ import { RenderState } from "LuauRenderer"; export function renderContinueStatement(state: RenderState) { - return state.line("continue"); + return state.line(`continue`); } diff --git a/src/LuauRenderer/nodes/statements/renderDoStatement.ts b/src/LuauRenderer/nodes/statements/renderDoStatement.ts index ff9c6d3..a87ca96 100644 --- a/src/LuauRenderer/nodes/statements/renderDoStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderDoStatement.ts @@ -4,8 +4,8 @@ import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderDoStatement(state: RenderState, node: luau.DoStatement) { let result = ""; - result += state.line("do"); + result += state.line(`do`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line("end"); + result += state.line(`end`); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderForStatement.ts b/src/LuauRenderer/nodes/statements/renderForStatement.ts index 5c09132..69c744b 100644 --- a/src/LuauRenderer/nodes/statements/renderForStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderForStatement.ts @@ -9,6 +9,6 @@ export function renderForStatement(state: RenderState, node: luau.ForStatement) let result = ""; result += state.line(`for ${idsStr} in ${expStr} do`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line("end"); + result += state.line(`end`); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts index 0095ef3..fed7ee3 100644 --- a/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderFunctionDeclaration.ts @@ -14,6 +14,6 @@ export function renderFunctionDeclaration(state: RenderState, node: luau.Functio let result = ""; result += state.line(`${node.localize ? "local " : ""}function ${nameStr}(${paramStr})`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line("end"); + result += state.line(`end`); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderIfStatement.ts b/src/LuauRenderer/nodes/statements/renderIfStatement.ts index 26bebfc..dc538cf 100644 --- a/src/LuauRenderer/nodes/statements/renderIfStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderIfStatement.ts @@ -17,12 +17,12 @@ export function renderIfStatement(state: RenderState, node: luau.IfStatement) { } if (currentElseBody && luau.list.isNonEmpty(currentElseBody)) { - result += state.line("else"); + result += state.line(`else`); const statements = currentElseBody; result += state.block(() => renderStatements(state, statements)); } - result += state.line("end"); + result += state.line(`end`); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts index 9926ae0..06cca4f 100644 --- a/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts +++ b/src/LuauRenderer/nodes/statements/renderMethodDeclaration.ts @@ -8,6 +8,6 @@ export function renderMethodDeclaration(state: RenderState, node: luau.MethodDec let result = ""; result += state.line(`function ${nameStr}:${node.name}(${renderParameters(state, node, nameStr)})`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line("end"); + result += state.line(`end`); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts b/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts index fe3d4f9..26de80c 100644 --- a/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderNumericForStatement.ts @@ -18,6 +18,6 @@ export function renderNumericForStatement(state: RenderState, node: luau.Numeric let result = ""; result += state.line(`for ${idStr} = ${predicateStr} do`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line("end"); + result += state.line(`end`); return result; } diff --git a/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts b/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts index 6028eb7..c9326f7 100644 --- a/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderRepeatStatement.ts @@ -4,7 +4,7 @@ import { renderStatements } from "LuauRenderer/util/renderStatements"; export function renderRepeatStatement(state: RenderState, node: luau.RepeatStatement) { let result = ""; - result += state.line("repeat"); + result += state.line(`repeat`); result += state.block(() => renderStatements(state, node.statements)); result += state.line(`until ${render(state, node.condition)}`); return result; diff --git a/src/LuauRenderer/nodes/statements/renderWhileStatement.ts b/src/LuauRenderer/nodes/statements/renderWhileStatement.ts index 122e261..384fe1b 100644 --- a/src/LuauRenderer/nodes/statements/renderWhileStatement.ts +++ b/src/LuauRenderer/nodes/statements/renderWhileStatement.ts @@ -6,6 +6,6 @@ export function renderWhileStatement(state: RenderState, node: luau.WhileStateme let result = ""; result += state.line(`while ${render(state, node.condition)} do`); result += state.block(() => renderStatements(state, node.statements)); - result += state.line("end"); + result += state.line(`end`); return result; } From feb3e66ae7bbe2f39795c874f5415fe3f5a03149 Mon Sep 17 00:00:00 2001 From: Rimuy Date: Mon, 6 Jun 2022 01:08:25 -0300 Subject: [PATCH 26/26] Update renderRightHandSide.ts --- src/LuauRenderer/util/renderRightHandSide.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/LuauRenderer/util/renderRightHandSide.ts b/src/LuauRenderer/util/renderRightHandSide.ts index b4d444e..98daafe 100644 --- a/src/LuauRenderer/util/renderRightHandSide.ts +++ b/src/LuauRenderer/util/renderRightHandSide.ts @@ -12,10 +12,14 @@ const NON_FORMATTABLE_RHS_EXPRESSIONS = new Set([ /** Renders statements with left and right-hand side values and formats them if necessary. */ export function renderRightHandSide(state: RenderState, lhsStr: string, rhsExpr: luau.Expression) { - const rightStr = render(state, rhsExpr); - const formatStr = `${lhsStr}${rightStr}`; - if (isFormattable(formatStr) && isFormattable(rightStr) && !NON_FORMATTABLE_RHS_EXPRESSIONS.has(rhsExpr.kind)) { - return `${state.newline(lhsStr)}${state.block(() => state.indented(render(state, rhsExpr)))}`; + const rhsStr = render(state, rhsExpr); + const isRhsFormattable = isFormattable(rhsStr); + const formatStr = `${lhsStr}${rhsStr}`; + const renderRhs = () => state.indented(render(state, rhsExpr)); + if (isFormattable(formatStr) && isRhsFormattable && !NON_FORMATTABLE_RHS_EXPRESSIONS.has(rhsExpr.kind)) { + return `${state.newline(lhsStr)}${ + isRhsFormattable ? state.block(() => state.block(renderRhs)) : state.block(renderRhs) + }`; } return formatStr; }