diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2022-08-05 10:37:24 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-08-11 11:04:24 +0000 |
commit | 7c5570ca8c010b2f8fecd2073dd039e665b83804 (patch) | |
tree | 7e858e8097737cf6641f91c0da024045b18cfbfd | |
parent | de7679f38b056211a2bf65fdb916ae35e36ca85d (diff) |
qmlformat: Preserve variable declaration scope type
If the user used for(let x;...) we need to preserve the let and must not
change it to var.
Fixes: QTBUG-105361
Change-Id: I49fc3797505b569cc9b8a9138dd57ec7e70d3eb9
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
(cherry picked from commit 84dd339e2eb3385eb143f9d5ab282a135635052d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/qmldom/qqmldomreformatter.cpp | 27 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/forWithLet.formatted.qml | 8 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/data/forWithLet.qml | 8 | ||||
-rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat.cpp | 3 |
4 files changed, 38 insertions, 8 deletions
diff --git a/src/qmldom/qqmldomreformatter.cpp b/src/qmldom/qqmldomreformatter.cpp index 4408d3e3a4..bb76f8f772 100644 --- a/src/qmldom/qqmldomreformatter.cpp +++ b/src/qmldom/qqmldomreformatter.cpp @@ -552,16 +552,27 @@ protected: return false; } + + void outputScope(VariableScope scope) { + switch (scope) { + case VariableScope::Const: + out("const "); + break; + case VariableScope::Let: + out("let "); + break; + case VariableScope::Var: + out("var "); + break; + default: + break; + } + } + bool visit(PatternElement *ast) override { if (ast->isForDeclaration) { - if (ast->scope == VariableScope::Var) { - out("var "); - } else if (ast->scope == VariableScope::Let) { - out("let "); - } else if (ast->scope == VariableScope::Const) { - out("const "); - } + outputScope(ast->scope); } accept(ast->bindingTarget); switch (ast->type) { @@ -647,7 +658,7 @@ protected: if (ast->initialiser) { accept(ast->initialiser); } else if (ast->declarations) { - out("var "); + outputScope(ast->declarations->declaration->scope); accept(ast->declarations); } out("; "); // ast->firstSemicolonToken diff --git a/tests/auto/qml/qmlformat/data/forWithLet.formatted.qml b/tests/auto/qml/qmlformat/data/forWithLet.formatted.qml new file mode 100644 index 0000000000..5fecc1d180 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/forWithLet.formatted.qml @@ -0,0 +1,8 @@ +import QtQml + +QtObject { + function foo() { + for (let i = 0; i < 5; ++i) + console.log(i); + } +} diff --git a/tests/auto/qml/qmlformat/data/forWithLet.qml b/tests/auto/qml/qmlformat/data/forWithLet.qml new file mode 100644 index 0000000000..afed76ebce --- /dev/null +++ b/tests/auto/qml/qmlformat/data/forWithLet.qml @@ -0,0 +1,8 @@ +import QtQml + +QtObject { +function foo() { +for (let i = 0; i < 5; ++i) +console.log(i); +} +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 458a36fc54..9d7beb23a7 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -273,6 +273,9 @@ void TestQmlformat::testFormat_data() QTest::newRow("settings") << "settings/Example1.qml" << "settings/Example1.formatted_mac_cr.qml" << QStringList {} << RunOption::OrigToCopy; + QTest::newRow("forWithLet") + << "forWithLet.qml" + << "forWithLet.formatted.qml" << QStringList {} << RunOption::OnCopy; } void TestQmlformat::testFormat() |