Skip to content

Commit d661e38

Browse files
committed
qmlformat: Fix property names with escape chars
Formatter was breaking the string literal property names if they consisted some escape sequences. Remove the complicated logic for rewriting, instead use the same literal that was scanned and assigned to propertyName. Pick-to: 6.5 Fixes: QTBUG-113776 Change-Id: I87d8c31d4893001e784536302e33f1f517f21acd Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> (cherry picked from commit 4c059a1) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit cf99be5)
1 parent 7eba78c commit d661e38

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

src/qmldom/qqmldomreformatter.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,7 @@ class Rewriter : protected BaseVisitor
309309
PatternProperty *assignment = AST::cast<PatternProperty *>(it->property);
310310
if (assignment) {
311311
preVisit(assignment);
312-
const bool isStringLike = [this](const SourceLocation &loc) {
313-
const auto name = loc2Str(loc);
314-
if (name.first() == name.last()) {
315-
if (name.first() == QStringLiteral("\'")
316-
|| name.first() == QStringLiteral("\""))
317-
return true;
318-
}
319-
return false;
320-
}(assignment->name->propertyNameToken);
321-
322-
if (isStringLike)
323-
out("\"");
324-
325312
accept(assignment->name);
326-
if (isStringLike)
327-
out("\"");
328-
329313
bool useInitializer = false;
330314
const bool bindingIdentifierExist = !assignment->bindingIdentifier.isEmpty();
331315
if (assignment->colonToken.length > 0) {
@@ -396,7 +380,7 @@ class Rewriter : protected BaseVisitor
396380
}
397381
bool visit(StringLiteralPropertyName *ast) override
398382
{
399-
out(ast->id.toString());
383+
out(ast->propertyNameToken);
400384
return true;
401385
}
402386
bool visit(NumericLiteralPropertyName *ast) override
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import QtQuick
2+
3+
Item {
4+
x: {
5+
const s = "\"";
6+
let a = {
7+
"\"": "\\"
8+
};
9+
let patron = {
10+
"\\\"\n\n": "\?\?\\\"",
11+
"": "",
12+
"\'\"\n": 1
13+
};
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import QtQuick
2+
3+
Item {
4+
x: {
5+
const s = "\""
6+
let a = {
7+
"\"": "\\"
8+
};
9+
10+
let patron = {
11+
"\\\"\n\n" : "\?\?\\\"","": "", "\'\"\n":1
12+
};
13+
14+
15+
}
16+
}

tests/auto/qml/qmlformat/tst_qmlformat.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ void TestQmlformat::testFormat_data()
343343
QTest::newRow("ellipsisFunctionArgument")
344344
<< "ellipsisFunctionArgument.qml"
345345
<< "ellipsisFunctionArgument.formatted.qml" << QStringList{} << RunOption::OnCopy;
346+
QTest::newRow("escapeChars")
347+
<< "escapeChars.qml"
348+
<< "escapeChars.formatted.qml" << QStringList{} << RunOption::OnCopy;
346349
}
347350

348351
void TestQmlformat::testFormat()

0 commit comments

Comments
 (0)