From 150fceabcbd4d73dbcc202db57bc7928115d38ef Mon Sep 17 00:00:00 2001 From: verytactical <186486509+verytactical@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:55:33 +0400 Subject: [PATCH] refactor: prettier gone wild --- src/prettyPrinter.ts | 176 +++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 106 deletions(-) diff --git a/src/prettyPrinter.ts b/src/prettyPrinter.ts index 9de5e31ba..03a0a929e 100644 --- a/src/prettyPrinter.ts +++ b/src/prettyPrinter.ts @@ -29,13 +29,10 @@ export const ppAstMapType = ({ export const ppAstBouncedMessageType = ({ messageType, -}: A.AstBouncedMessageType): string => { - return `bounced<${ppAstTypeId(messageType)}>`; -}; +}: A.AstBouncedMessageType): string => `bounced<${ppAstTypeId(messageType)}>`; -export const ppAstOptionalType = ({ typeArg }: A.AstOptionalType): string => { - return `${ppAstType(typeArg)}?`; -}; +export const ppAstOptionalType = ({ typeArg }: A.AstOptionalType): string => + `${ppAstType(typeArg)}?`; export const ppAstType = makeVisitor()({ type_id: ppAstTypeId, @@ -94,53 +91,45 @@ export const ppAstStructFieldInit = ( ): string => `${ppAstId(param.field)}: ${ppAstExpression(param.initializer)}`; export const ppExprArgs: ExprPrinter = - (args) => (currPrecedence) => { - return args.map((arg) => ppAstExpression(arg, currPrecedence)).join(", "); - }; + (args) => (currPrecedence) => + args.map((arg) => ppAstExpression(arg, currPrecedence)).join(", "); type ExprPrinter = (expr: T) => (currPrecedence: number) => string; export const ppAstOpBinary: ExprPrinter = ({ left, op, right }) => - (currPrecedence) => { - return `${ppAstExpression(left, currPrecedence)} ${op} ${ppAstExpression(right, currPrecedence)}`; - }; + (currPrecedence) => + `${ppAstExpression(left, currPrecedence)} ${op} ${ppAstExpression(right, currPrecedence)}`; export const ppAstOpUnary: ExprPrinter = ({ op, operand }) => - (currPrecedence) => { - return `${op}${ppAstExpression(operand, currPrecedence)}`; - }; + (currPrecedence) => + `${op}${ppAstExpression(operand, currPrecedence)}`; export const ppAstFieldAccess: ExprPrinter = ({ aggregate, field }) => - (currPrecedence) => { - return `${ppAstExpression(aggregate, currPrecedence)}.${ppAstId(field)}`; - }; + (currPrecedence) => + `${ppAstExpression(aggregate, currPrecedence)}.${ppAstId(field)}`; export const ppAstMethodCall: ExprPrinter = ({ self, method, args }) => - (currPrecedence) => { - return `${ppAstExpression(self, currPrecedence)}.${ppAstId(method)}(${ppExprArgs(args)(currPrecedence)})`; - }; + (currPrecedence) => + `${ppAstExpression(self, currPrecedence)}.${ppAstId(method)}(${ppExprArgs(args)(currPrecedence)})`; export const ppAstStaticCall: ExprPrinter = ({ function: func, args }) => - (currPrecedence) => { - return `${ppAstId(func)}(${ppExprArgs(args)(currPrecedence)})`; - }; + (currPrecedence) => + `${ppAstId(func)}(${ppExprArgs(args)(currPrecedence)})`; export const ppAstInitOf: ExprPrinter = ({ contract, args }) => - (currPrecedence) => { - return `initOf ${ppAstId(contract)}(${ppExprArgs(args)(currPrecedence)})`; - }; + (currPrecedence) => + `initOf ${ppAstId(contract)}(${ppExprArgs(args)(currPrecedence)})`; export const ppAstConditional: ExprPrinter = ({ condition, thenBranch, elseBranch }) => - (currPrecedence) => { - return `${ppAstExpression(condition, currPrecedence)} ? ${ppAstExpression(thenBranch, currPrecedence)} : ${ppAstExpression(elseBranch, currPrecedence)}`; - }; + (currPrecedence) => + `${ppAstExpression(condition, currPrecedence)} ? ${ppAstExpression(thenBranch, currPrecedence)} : ${ppAstExpression(elseBranch, currPrecedence)}`; export const ppAstStructInstance = ({ type, args }: A.AstStructInstance) => `${ppAstId(type)}{${args.map((x) => ppAstStructFieldInit(x)).join(", ")}}`; @@ -180,7 +169,10 @@ export const ppAstExpression = ( const result = ppAstExpressionVisitor(expr)(currPrecedence); - const needParens = parentPrecedence > 0 && currPrecedence > 0 && currPrecedence < parentPrecedence; + const needParens = + parentPrecedence > 0 && + currPrecedence > 0 && + currPrecedence < parentPrecedence; return needParens ? `(${result})` : result; }; @@ -301,15 +293,14 @@ export const ppAstModule: Printer = ]); }; +// BUG with } export const ppAstStruct: Printer = ({ name, fields }) => - (c) => { - // BUG with } - return c.concat([ + (c) => + c.concat([ c.row(`struct ${ppAstId(name)} `), c.braced(c.list(fields, ppAstFieldDecl)), ]); - }; export const ppAstContract: Printer = ({ name, traits, declarations, attributes }) => @@ -340,16 +331,14 @@ export const ppAstContract: Printer = export const ppAstPrimitiveTypeDecl: Printer = ({ name }) => - (c) => { - return c.row(`primitive ${ppAstId(name)};`); - }; + (c) => + c.row(`primitive ${ppAstId(name)};`); -export const ppAstFunctionDef: Printer = (node) => (c) => { - return c.concat([ +export const ppAstFunctionDef: Printer = (node) => (c) => + c.concat([ c.row(ppAstFunctionSignature(node)), ppStatementBlock(node.statements)(c), ]); -}; export const ppAsmShuffle = ({ args, ret }: A.AstAsmShuffle): string => { if (args.length === 0 && ret.length === 0) { @@ -364,14 +353,13 @@ export const ppAsmShuffle = ({ args, ret }: A.AstAsmShuffle): string => { }; export const ppAstAsmFunctionDef: Printer = - (node) => (c) => { - return c.concat([ + (node) => (c) => + c.concat([ c.row( `asm${ppAsmShuffle(node.shuffle)} ${ppAstFunctionSignature(node)} `, ), ppAsmInstructionsBlock(node.instructions)(c), ]); - }; export const ppAstNativeFunction: Printer = ({ name, nativeName, params, return: retTy, attributes }) => @@ -462,16 +450,14 @@ export const ppAstFieldDecl: Printer = export const ppAstReceiver: Printer = ({ selector, statements }) => - (c) => { - return c.concat([ + (c) => + c.concat([ c.row(`${ppAstReceiverHeader(selector)} `), ppStatementBlock(statements)(c), ]); - }; -export const ppAstFunctionDecl: Printer = (f) => (c) => { - return c.row(`${ppAstFunctionSignature(f)};`); -}; +export const ppAstFunctionDecl: Printer = (f) => (c) => + c.row(`${ppAstFunctionSignature(f)};`); export const ppAstConstDecl: Printer = ({ attributes, name, type }) => @@ -518,9 +504,8 @@ export const ppContractBody: Printer = export const ppAstImport: Printer = ({ path }) => - (c) => { - return c.row(`import "${path.value}";`); - }; + (c) => + c.row(`import "${path.value}";`); export const ppAstFunctionSignature = ({ name, @@ -567,14 +552,12 @@ export const ppAstFuncId = (func: A.AstFuncId): string => func.text; // Statements // -export const ppStatementBlock: Printer = (stmts) => (c) => { - return c.braced(c.list(stmts, ppAstStatement)); -}; +export const ppStatementBlock: Printer = (stmts) => (c) => + c.braced(c.list(stmts, ppAstStatement)); export const ppAsmInstructionsBlock: Printer = - (instructions) => (c) => { - return c.braced(instructions.map(c.row)); - }; + (instructions) => (c) => + c.braced(instructions.map(c.row)); export const ppAstStatementLet: Printer = ({ type, name, expression }) => @@ -587,35 +570,27 @@ export const ppAstStatementLet: Printer = export const ppAstStatementReturn: Printer = ({ expression }) => - (c) => { - return c.row( - `return ${expression ? ppAstExpression(expression) : ""};`, - ); - }; + (c) => + c.row(`return ${expression ? ppAstExpression(expression) : ""};`); export const ppAstStatementExpression: Printer = ({ expression }) => - (c) => { - return c.row(`${ppAstExpression(expression)};`); - }; + (c) => + c.row(`${ppAstExpression(expression)};`); export const ppAstStatementAssign: Printer = ({ path, expression }) => - (c) => { - return c.row( - `${ppAstExpression(path)} = ${ppAstExpression(expression)};`, - ); - }; + (c) => + c.row(`${ppAstExpression(path)} = ${ppAstExpression(expression)};`); export const ppAstStatementAugmentedAssign: Printer< A.AstStatementAugmentedAssign > = ({ path, op, expression }) => - (c) => { - return c.row( + (c) => + c.row( `${ppAstExpression(path)} ${op}= ${ppAstExpression(expression)};`, ); - }; export const ppAstCondition: Printer = ({ condition, trueStatements, falseStatements }) => @@ -637,59 +612,53 @@ export const ppAstCondition: Printer = export const ppAstStatementWhile: Printer = ({ condition, statements }) => - (c) => { - return c.concat([ + (c) => + c.concat([ c.row(`while (${ppAstExpression(condition)}) `), ppStatementBlock(statements)(c), ]); - }; export const ppAstStatementRepeat: Printer = ({ iterations, statements }) => - (c) => { - return c.concat([ + (c) => + c.concat([ c.row(`repeat (${ppAstExpression(iterations)}) `), ppStatementBlock(statements)(c), ]); - }; export const ppAstStatementUntil: Printer = ({ condition, statements }) => - (c) => { - return c.concat([ + (c) => + c.concat([ c.row(`do `), ppStatementBlock(statements)(c), c.row(` until (${ppAstExpression(condition)});`), ]); - }; export const ppAstStatementForEach: Printer = ({ keyName, valueName, map, statements }) => - (c) => { - return c.concat([ + (c) => + c.concat([ c.row( `foreach (${ppAstId(keyName)}, ${ppAstId(valueName)} in ${ppAstExpression(map)}) `, ), ppStatementBlock(statements)(c), ]); - }; export const ppAstStatementTry: Printer = ({ statements }) => - (c) => { - return c.concat([c.row(`try `), ppStatementBlock(statements)(c)]); - }; + (c) => + c.concat([c.row(`try `), ppStatementBlock(statements)(c)]); export const ppAstStatementTryCatch: Printer = ({ statements, catchName, catchStatements }) => - (c) => { - return c.concat([ + (c) => + c.concat([ c.row(`try `), ppStatementBlock(statements)(c), c.row(` catch (${ppAstId(catchName)}) `), ppStatementBlock(catchStatements)(c), ]); - }; export const ppAstStatementDestruct: Printer = ({ type, identifiers, expression }) => @@ -727,9 +696,8 @@ export const ppAstStatement: Printer = export const exprNode = (exprPrinter: (expr: T) => string): Printer => (node) => - (c) => { - return c.row(exprPrinter(node)); - }; + (c) => + c.row(exprPrinter(node)); export const ppAstNode: Printer = makeVisitor()({ op_binary: exprNode(ppAstExpression), @@ -795,13 +763,9 @@ export const ppAstNode: Printer = makeVisitor()({ * @param node The AST node to format. * @returns A string that represents the formatted AST node. */ -export const prettyPrint = (node: A.AstNode): string => { - // Default number of spaces per indentation level is 4 - return ( - ppAstNode(node)(createContext(4)) - // Initial level of indentation is 0 - .map((f) => f(0)) - // Lines are terminated with \n - .join("\n") - ); -}; +export const prettyPrint = (node: A.AstNode): string => + ppAstNode(node)(createContext(4)) + // Initial level of indentation is 0 + .map((f) => f(0)) + // Lines are terminated with \n + .join("\n");