From 61417ad3525d33c41e7d8423103e876a922d5f8b Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Wed, 7 Oct 2020 12:46:24 +0200 Subject: qmlformat: Make arrow functions one liners Fixes: QTBUG-87179 Change-Id: Ieb7dffab59923bcb2ce8745c499eff7de44134b1 Reviewed-by: Ulf Hermann --- tools/qmlformat/dumpastvisitor.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'tools/qmlformat') diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp index 47ea76b119..24286ba69b 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -321,6 +321,7 @@ QString DumpAstVisitor::parseFunctionExpression(FunctionExpression *functExpr, b { m_indentLevel++; QString result; + bool hasBraces = true; if (!functExpr->isArrowFunction) { result += omitFunction ? "" : "function"; @@ -343,12 +344,23 @@ QString DumpAstVisitor::parseFunctionExpression(FunctionExpression *functExpr, b if (functExpr->typeAnnotation != nullptr) result += " : " + parseType(functExpr->typeAnnotation->type); - result += " => {\n" + parseStatementList(functExpr->body); + result += " => "; + + if (functExpr->body == nullptr) { + result += "{}"; + } else if (functExpr->body->next == nullptr && functExpr->body->statement->kind == Node::Kind_ReturnStatement) { + m_indentLevel--; + result += parseExpression(cast(functExpr->body->statement)->expression); + hasBraces = false; + } else { + result += "{\n" + parseStatementList(functExpr->body); + } } - m_indentLevel--; - - result += formatLine("}", false); + if (hasBraces) { + m_indentLevel--; + result += formatLine("}", false); + } return result; -- cgit v1.2.3