Skip to content

Commit

Permalink
qmlformat: Preserve variable declaration scope type
Browse files Browse the repository at this point in the history
If the user used for(let x;...) we need to preserve the let and must not
change it to var.

Pick-to: 6.2 6.3 6.4
Fixes: QTBUG-105361
Change-Id: I49fc3797505b569cc9b8a9138dd57ec7e70d3eb9
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
  • Loading branch information
Inkane committed Aug 11, 2022
1 parent f9280d4 commit 84dd339
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/qmldom/qqmldomreformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,27 @@ class Rewriter : protected BaseVisitor
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) {
Expand Down Expand Up @@ -647,7 +658,7 @@ class Rewriter : protected BaseVisitor
if (ast->initialiser) {
accept(ast->initialiser);
} else if (ast->declarations) {
out("var ");
outputScope(ast->declarations->declaration->scope);
accept(ast->declarations);
}
out("; "); // ast->firstSemicolonToken
Expand Down
8 changes: 8 additions & 0 deletions tests/auto/qml/qmlformat/data/forWithLet.formatted.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import QtQml

QtObject {
function foo() {
for (let i = 0; i < 5; ++i)
console.log(i);
}
}
8 changes: 8 additions & 0 deletions tests/auto/qml/qmlformat/data/forWithLet.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import QtQml

QtObject {
function foo() {
for (let i = 0; i < 5; ++i)
console.log(i);
}
}
3 changes: 3 additions & 0 deletions tests/auto/qml/qmlformat/tst_qmlformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 84dd339

Please sign in to comment.