diff options
-rw-r--r-- | tests/auto/qml/qmlformat/data/arrowFunctions.formatted.qml | 5 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/arrowFunctions.qml | 5 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat.cpp | 2 | ||||
-rw-r--r-- | tools/qmlformat/dumpastvisitor.cpp | 20 |
4 files changed, 28 insertions, 4 deletions
diff --git a/tests/auto/qml/qmlformat/data/arrowFunctions.formatted.qml b/tests/auto/qml/qmlformat/data/arrowFunctions.formatted.qml new file mode 100644 index 0000000000..e4bc1f6747 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/arrowFunctions.formatted.qml @@ -0,0 +1,5 @@ +Item { + arrow1: (x) => x + arrow2: (x) => x + x + arrow3: () => x > 3 ? x * x : x +} diff --git a/tests/auto/qml/qmlformat/data/arrowFunctions.qml b/tests/auto/qml/qmlformat/data/arrowFunctions.qml new file mode 100644 index 0000000000..e4bc1f6747 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/arrowFunctions.qml @@ -0,0 +1,5 @@ +Item { + arrow1: (x) => x + arrow2: (x) => x + x + arrow3: () => x > 3 ? x * x : x +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 45a51dec21..dfe41a77d8 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -237,6 +237,8 @@ void TestQmlformat::testFormat_data() << "propertyNames.formatted.qml" << false << true; QTest::newRow("empty object") << "emptyObject.qml" << "emptyObject.formatted.qml" << false << true; + QTest::newRow("arrow functions") << "arrowFunctions.qml" + << "arrowFunctions.formatted.qml" << false << true; } void TestQmlformat::testFormat() 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<ReturnStatement *>(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; |