From 7c5570ca8c010b2f8fecd2073dd039e665b83804 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Fri, 5 Aug 2022 10:37:24 +0200 Subject: 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 (cherry picked from commit 84dd339e2eb3385eb143f9d5ab282a135635052d) Reviewed-by: Qt Cherry-pick Bot --- src/qmldom/qqmldomreformatter.cpp | 27 +++++++++++++++------- .../qml/qmlformat/data/forWithLet.formatted.qml | 8 +++++++ tests/auto/qml/qmlformat/data/forWithLet.qml | 8 +++++++ tests/auto/qml/qmlformat/tst_qmlformat.cpp | 3 +++ 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 tests/auto/qml/qmlformat/data/forWithLet.formatted.qml create mode 100644 tests/auto/qml/qmlformat/data/forWithLet.qml 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() -- cgit v1.2.3