diff options
author | Luca Di Sera <luca.disera@qt.io> | 2024-04-09 18:00:47 +0200 |
---|---|---|
committer | Luca Di Sera <luca.disera@qt.io> | 2024-04-11 15:28:45 +0200 |
commit | 0ead48409ec7b376646667b1d63a54de00060a83 (patch) | |
tree | abbe084ec1d6772d258f973fdc9364a55a545796 /tests | |
parent | 85b7937b70ee7d2fbf02e7bdff12a01cf12e084c (diff) |
qmlformat: Preserve up to 1 empty line between statements
When `qmlformat` processes a `ScriptExpression` such as the `onClicked`
body in the following snippet:
```
import QtQuick
MouseArea {
onClicked: {
console.log("start");
console.log("end");
}
}
```
It will remove every empty lines between statements.
For example, in the above snippet, the two log statements will be on
successive lines in the formatted output, removing the empty line
in-between them in the original version.
`qmlformat` will now, instead, allow up to 1 empty line to be preserved
between statements, if at least one empty line is present in the
original version.
The formatting for `StatementList`s in `ScriptExpression`s is handled by
the relevant overload of `ScriptFormatter::visit`.
The code relative to inserting newlines in-between statements in the
list was modified to take into account the possible presence of
additional empty lines in between the statements, outputting an adequate
amount of newlines to preserve up to 1 empty line.
The utility method `ScriptFormatter::newLine`, which is used to output a
single newline character, was modified to take a parameter representing
the amount of newlines that should be added, allowing multiple newline
characters to be written by a single call.
A non-exhaustive test-case for the new behavior was added to track it.
Certain pre-existing snapshot test-cases were modified to be in line
with the new output of `qmlformat`.
Fixes: QTBUG-123864
Change-Id: I552d1f7ce2c035b9087cb60a3b6e3480c5ae969a
Reviewed-by: Dmitrii Akshintsev <dmitrii.akshintsev@qt.io>
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Diffstat (limited to 'tests')
11 files changed, 76 insertions, 0 deletions
diff --git a/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml index edbb12c6e6..de6a23f9c7 100644 --- a/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml +++ b/tests/auto/qml/qmlformat/data/ecmaScriptClassInQml.formatted.qml @@ -4,11 +4,13 @@ Item { function f() { var count = 0; + class Person { constructor(name){ this._name = name; } } + class Employee extends Person { constructor(name, age){ super(name); diff --git a/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml b/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml index 3cb99b704a..3f6807834f 100644 --- a/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml +++ b/tests/auto/qml/qmlformat/data/escapeChars.formatted.qml @@ -6,6 +6,7 @@ Item { let a = { "\"": "\\" }; + let patron = { "\\\"\n\n": "\?\?\\\"", "": "", diff --git a/tests/auto/qml/qmlformat/data/forOf.formatted.qml b/tests/auto/qml/qmlformat/data/forOf.formatted.qml index fa9ff9c631..0cc4f9fecd 100644 --- a/tests/auto/qml/qmlformat/data/forOf.formatted.qml +++ b/tests/auto/qml/qmlformat/data/forOf.formatted.qml @@ -3,6 +3,7 @@ import QtQml 2.0 QtObject { Component.onCompleted: { var list = [[1, 2], [3, 4], [5, 6]]; + for (const [x, y] of list) console.log("X: " + x + "; Y: " + y); for (let [x, y] of list) diff --git a/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml b/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml index 5536ecf513..fc1915f647 100644 --- a/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml +++ b/tests/auto/qml/qmlformat/data/nestedFunctions.formatted.qml @@ -1,11 +1,13 @@ Item { function a() { function nested() {} + foo(); } function b() { function nested() {} + bar(); } } diff --git a/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml b/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml index 4ff5a40a23..ebb125f36d 100644 --- a/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml +++ b/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml @@ -26,6 +26,7 @@ Item { x(); } } + if (x && y) if (x < y) return 0; diff --git a/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml b/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml index 3feac73688..94e97076b1 100644 --- a/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml +++ b/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml @@ -14,6 +14,7 @@ QtObject { push }] = array; const [a4, b4, ...[c, d]] = array; + const obj = { _a: 1, _b: 2 @@ -52,6 +53,7 @@ QtObject { push }] = array; [a, b, ...[c, d]] = array; + const obj = { _a: 1, _b: 2 @@ -64,6 +66,7 @@ QtObject { a: a1, b: b1 } = obj); + const complicatedObject = { a: 1, b: { @@ -75,6 +78,7 @@ QtObject { }, g: [7, 8, 9] }; + const { patron, b: { @@ -104,8 +108,10 @@ QtObject { } } }; + myLambda(myObject); }; + myFunction(({ a, b: { diff --git a/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js b/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js index 5368f74172..b7414de053 100644 --- a/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js +++ b/tests/auto/qml/qmlformat/data/twoFunctions.formatted.js @@ -4,6 +4,7 @@ function one() { a = 5; } } + function two(a, b) { for (; b < 5; ++b) { a = a * b; diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index f1a7ffbc4a..0bf19bbe9f 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -390,6 +390,7 @@ void TestQmlformat::testFormat_data() QTest::newRow("javascriptBlock") << "javascriptBlock.qml" << "javascriptBlock.formatted.qml" << QStringList{} << RunOption::OnCopy; + //plainJS QTest::newRow("nestedLambdaWithIfElse") << "lambdaWithIfElseInsideLambda.js" diff --git a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml index fe3403cef1..ab722f94be 100644 --- a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml +++ b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted.qml @@ -29,6 +29,7 @@ Item { if (y == 6) // if comment console.log("pippo"); + a + b; // comment // footer diff --git a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml index 9a5aba106e..c6e33f9389 100644 --- a/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml +++ b/tests/auto/qmldom/domdata/reformatter/commentedFileReformatted2.qml @@ -29,6 +29,7 @@ Item { if (y == 6) // if comment console.log("pippo"); + a + b; // comment // footer diff --git a/tests/auto/qmldom/reformatter/tst_reformatter.h b/tests/auto/qmldom/reformatter/tst_reformatter.h index 630cef03ed..31d80097c1 100644 --- a/tests/auto/qmldom/reformatter/tst_reformatter.h +++ b/tests/auto/qmldom/reformatter/tst_reformatter.h @@ -705,6 +705,65 @@ private slots: QCOMPARE(formattedMethod, expectedFormattedMethod); } + void statementList_data() + { + QTest::addColumn<QString>("codeToBeFormatted"); + QTest::addColumn<QString>("expectedFormattedCode"); + + QTest::newRow("StatementsOnTheSameLine") + << QStringLiteral(u"a=1;b=1;") << QStringLiteral(u"a = 1;\nb = 1;"); + + QTest::newRow("StatementsOnSuccessiveLines") + << QStringLiteral(u"a=1;\nb=1;") << QStringLiteral(u"a = 1;\nb = 1;"); + + QTest::newRow("EmptyLineBetweenStatements") + << QStringLiteral(u"a=1;\n\nb=1;") << QStringLiteral(u"a = 1;\n\nb = 1;"); + + QTest::newRow("MultipleEmptyLinesBetweenStatements") + << QStringLiteral(u"a=1;\n\n\n\n\n\nb=1;") << QStringLiteral(u"a = 1;\n\nb = 1;"); + + QTest::newRow("MultilineStatementWithStatementOnTheFollowingLine") + << QStringLiteral(u"console.log(\n\n);\nb = 1;") + << QStringLiteral(u"console.log();\nb = 1;"); + + QTest::newRow("StatementWithPostCommentAndStatementOnTheFollowingLine") + << QStringLiteral(u"a=1;//\nb=1;") << QStringLiteral(u"a = 1;//\nb = 1;"); + + QTest::newRow("StatementWithPostCommentAndEmptyLineToNextStatement") + << QStringLiteral(u"a=1;//\n\nb=1;") << QStringLiteral(u"a = 1;//\n\nb = 1;"); + + QTest::newRow("StatementWithPostCommentAndMultipleEmptyLinesToNextStatement") + << QStringLiteral(u"a=1;//\n\n\n\n\nb=1;") << QStringLiteral(u"a = 1;//\n\nb = 1;"); + + QTest::newRow("StatementsWithCommentInBetweenThem") + << QStringLiteral(u"a=1;\n//\nb=1;") << QStringLiteral(u"a = 1;\n//\nb = 1;"); + + QTest::newRow("StatementsWithCommentAndSingleEmptyLineInBetweenThem") + << QStringLiteral(u"a=1;\n\n//\n\nb=1;") + << QStringLiteral(u"a = 1;\n\n//\n\nb = 1;"); + + QTest::newRow("StatementsWithCommentAndMultipleEmptyLinesInBetweenThem") + << QStringLiteral(u"a=1;\n\n\n\n//\n\n\nb=1;") + << QStringLiteral(u"a = 1;\n\n//\n\nb = 1;"); + + QTest::newRow("StatementWithSingleEmptyLineAndPreCommentOnNextStatement") + << QStringLiteral(u"a=1;\n\n//\nb=1;") << QStringLiteral(u"a = 1;\n\n//\nb = 1;"); + + QTest::newRow("StatementWithMultipleEmptyLinesAndPreCommentOnNextStatement") + << QStringLiteral(u"a=1;\n\n\n\n\n\n\n\n//\nb=1;") + << QStringLiteral(u"a = 1;\n\n//\nb = 1;"); + } + + void statementList() + { + QFETCH(QString, codeToBeFormatted); + QFETCH(QString, expectedFormattedCode); + + QString formattedCode = formatJSCode(codeToBeFormatted); + + QCOMPARE(formattedCode, expectedFormattedCode); + } + private: }; |