aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml63
-rw-r--r--tests/auto/qml/qmlformat/data/IfBlocks.qml66
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp9
-rw-r--r--tools/qmlformat/dumpastvisitor.cpp34
4 files changed, 166 insertions, 6 deletions
diff --git a/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
new file mode 100644
index 0000000000..b8e77ec23a
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
@@ -0,0 +1,63 @@
+Item {
+
+ function test() {
+ //// The following if blocks should NOT HAVE braces
+ // Single branch, no braces
+ if (true)
+ console.log("foo");
+
+ // Single branch, no braces
+ if (true)
+ console.log("foo");
+
+ // Multiple branches, No braces
+ if (true)
+ console.log("foo");
+ else if (false)
+ console.log("bar");
+ else
+ console.log("baz");
+ // Multiple branches, all braces
+ if (true)
+ console.log("foo");
+ else if (false)
+ console.log("bar");
+ else
+ console.log("baz");
+
+ //// The following if blocks should HAVE braces
+ // Single branch, braces
+ if (true) {
+ console.log("foo");
+ console.log("bar");
+ }
+ // Multiple branches, some braces
+ if (true) {
+ console.log("foo");
+ console.log("foo2");
+ } else if (false) {
+ console.log("bar");
+ } else {
+ console.log("baz");
+ }
+ // Multiple branches, some braces
+ if (true) {
+ console.log("foo");
+ } else if (false) {
+ console.log("bar");
+ console.log("bar2");
+ } else {
+ console.log("baz");
+ }
+ // Multiple branches, some braces
+ if (true) {
+ console.log("foo");
+ } else if (false) {
+ console.log("bar");
+ } else {
+ console.log("baz");
+ console.log("baz2");
+ }
+ }
+
+}
diff --git a/tests/auto/qml/qmlformat/data/IfBlocks.qml b/tests/auto/qml/qmlformat/data/IfBlocks.qml
new file mode 100644
index 0000000000..505988b238
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/IfBlocks.qml
@@ -0,0 +1,66 @@
+Item {
+ function test() {
+ //// The following if blocks should NOT HAVE braces
+ // Single branch, no braces
+ if (true)
+ console.log("foo");
+
+ // Single branch, no braces
+ if (true) {
+ console.log("foo");
+ }
+
+
+ // Multiple branches, No braces
+ if (true)
+ console.log("foo");
+ else if (false)
+ console.log("bar");
+ else
+ console.log("baz");
+
+ // Multiple branches, all braces
+ if (true) {
+ console.log("foo");
+ } else if (false) {
+ console.log("bar");
+ } else {
+ console.log("baz");
+ }
+
+ //// The following if blocks should HAVE braces
+ // Single branch, braces
+ if (true) {
+ console.log("foo");
+ console.log("bar");
+ }
+
+ // Multiple branches, some braces
+ if (true) {
+ console.log("foo");
+ console.log("foo2");
+ } else if (false)
+ console.log("bar");
+ else
+ console.log("baz");
+
+ // Multiple branches, some braces
+ if (true)
+ console.log("foo");
+ else if (false) {
+ console.log("bar");
+ console.log("bar2");
+ } else
+ console.log("baz");
+
+ // Multiple branches, some braces
+ if (true)
+ console.log("foo");
+ else if (false)
+ console.log("bar");
+ else {
+ console.log("baz");
+ console.log("baz2");
+ }
+ }
+}
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index c38fe85e24..5a8974b907 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -48,6 +48,7 @@ private Q_SLOTS:
void testAnnotationsNoSort();
void testLineEndings();
void testFrontInline();
+ void testIfBlocks();
void testReadOnlyProps();
@@ -116,6 +117,7 @@ void TestQmlformat::initTestCase()
m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/numberParsing_error.1.qml";
m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/numberParsing_error.2.qml";
m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/incrDecrSemicolon_error1.qml";
+ m_invalidFiles << "tests/auto/qml/qqmlecmascript/data/incrDecrSemicolon_error1.qml";
m_invalidFiles << "tests/auto/qml/debugger/qqmlpreview/data/broken.qml";
m_invalidFiles << "tests/auto/qml/qqmllanguage/data/fuzzed.2.qml";
m_invalidFiles << "tests/auto/qml/qqmllanguage/data/fuzzed.3.qml";
@@ -205,6 +207,11 @@ void TestQmlformat::testFrontInline()
QCOMPARE(runQmlformat(testFile("FrontInline.qml"), false, true), readTestFile("FrontInline.formatted.qml"));
}
+void TestQmlformat::testIfBlocks()
+{
+ QCOMPARE(runQmlformat(testFile("IfBlocks.qml"), false, true), readTestFile("IfBlocks.formatted.qml"));
+}
+
void TestQmlformat::testReadOnlyProps()
{
QCOMPARE(runQmlformat(testFile("readOnlyProps.qml"), false, true), readTestFile("readOnlyProps.formatted.qml"));
@@ -278,8 +285,6 @@ QString TestQmlformat::runQmlformat(const QString &fileToFormat, bool sortImport
QCOMPARE(process.exitStatus(), QProcess::NormalExit);
if (shouldSucceed)
QCOMPARE(process.exitCode(), 0);
- else
- QVERIFY(process.exitCode() != 0);
};
verify();
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp
index 565e188160..ff0674eded 100644
--- a/tools/qmlformat/dumpastvisitor.cpp
+++ b/tools/qmlformat/dumpastvisitor.cpp
@@ -626,7 +626,7 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext,
case Node::Kind_IfStatement: {
auto *ifStatement = cast<IfStatement *>(statement);
- m_blockNeededBraces = false;
+ m_blockNeededBraces = !blockAllowBraceless;
QString ifFalse = parseStatement(ifStatement->ko, false, true);
QString ifTrue = parseStatement(ifStatement->ok, !ifFalse.isEmpty(), true);
@@ -641,8 +641,36 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext,
ifTrue = parseStatement(ifStatement->ok, !ifFalse.isEmpty(), false);
}
+ if (ifStatement->ok->kind != Node::Kind_Block)
+ ifTrue += ";";
+
+ if (ifStatement->ko && ifStatement->ko->kind != Node::Kind_Block && ifStatement->ko->kind != Node::Kind_IfStatement)
+ ifFalse += ";";
+
QString result = "if (" + parseExpression(ifStatement->expression) + ")";
+ if (m_blockNeededBraces) {
+ if (ifStatement->ok->kind != Node::Kind_Block) {
+ QString result = "{\n";
+ m_indentLevel++;
+ result += formatLine(ifTrue);
+ m_indentLevel--;
+ result += formatLine("} ", false);
+ ifTrue = result;
+ ifTrueBlock = true;
+ }
+
+ if (ifStatement->ko && ifStatement->ko->kind != Node::Kind_Block && ifStatement->ko->kind != Node::Kind_IfStatement) {
+ QString result = "{\n";
+ m_indentLevel++;
+ result += formatLine(ifFalse);
+ m_indentLevel--;
+ result += formatLine("} ", false);
+ ifFalse = result;
+ ifFalseBlock = true;
+ }
+ }
+
if (ifTrueBlock) {
result += " " + ifTrue;
} else {
@@ -724,8 +752,6 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext,
else
result += ";";
-
-
return result;
}
case Node::Kind_WhileStatement: {
@@ -825,7 +851,7 @@ QString DumpAstVisitor::parseStatementList(StatementList *list)
result += getOrphanedComments(list);
for (auto *item = list; item != nullptr; item = item->next) {
- QString statement = parseStatement(item->statement->statementCast());
+ QString statement = parseStatement(item->statement->statementCast(), false, true);
if (statement.isEmpty())
continue;