diff options
| author | Olivier De Cannière <olivier.decanniere@qt.io> | 2026-09-30 09:31:41 +0200 |
|---|---|---|
| committer | Olivier De Cannière <olivier.decanniere@qt.io> | 2026-10-02 11:22:43 +0000 |
| commit | 3aa66f7d3601b2222d6042c812c5054463ac4e0a (patch) | |
| tree | 831c2c9a7069c6eecddee269e001d774fa8c9416 | |
| parent | 63c1bcf809d438711ecd484b3c679a4bcd9bc84a (diff) | |
qmldom: Make --reformat reformat something
It turned every file down. Two mistakes, each enough on its own.
What loadFile() hands its callback is the metadata about the file, of
which the file itself is a field, so the check that what was loaded is a
QmlFile was asking the metadata, which is an ExternalItemInfo and never
a QmlFile. Ask the file, the way the option that dumps the syntax tree
already does.
The index the loaded files are put at was never advanced, so every file
after the first was dropped and the slots they should have gone in
stayed empty. An empty slot is not a QmlFile either, which is the other
way the same message came out.
With both fixed it writes what qmlformat writes: --reformat over a file
gives what qmlformat --normalize gives, byte for byte, since the dom
writes the script of a file through the same formatter and the options
it reformats with are the normalising ones.
Amends 4cfb323389363bf12f84044b8c7501ef2f0441b9
Task-number: QTBUG-151040
Pick-to: 6.12 6.8
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Change-Id: Ibb38e0f422ebdf8991dbbc45208acbc1e4abf036
Reviewed-by: Semih Yavuz <semih.yavuz@qt.io>
| -rw-r--r-- | tests/auto/qmldom/reformatter/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tests/auto/qmldom/reformatter/tst_reformatter.h | 30 | ||||
| -rw-r--r-- | tools/qmldom/qmldomtool.cpp | 4 |
3 files changed, 35 insertions, 1 deletions
diff --git a/tests/auto/qmldom/reformatter/CMakeLists.txt b/tests/auto/qmldom/reformatter/CMakeLists.txt index 1b8cfb0d8a..9f6badfa5c 100644 --- a/tests/auto/qmldom/reformatter/CMakeLists.txt +++ b/tests/auto/qmldom/reformatter/CMakeLists.txt @@ -34,3 +34,5 @@ qt_internal_extend_target(tst_reformatter CONDITION ANDROID OR IOS DEFINES QT_REFORMATTERTEST_DATADIR=":/domdata" ) + +add_dependencies(tst_reformatter Qt6::qmldom) diff --git a/tests/auto/qmldom/reformatter/tst_reformatter.h b/tests/auto/qmldom/reformatter/tst_reformatter.h index b1f58ca8b4..f46f86c0c3 100644 --- a/tests/auto/qmldom/reformatter/tst_reformatter.h +++ b/tests/auto/qmldom/reformatter/tst_reformatter.h @@ -12,7 +12,10 @@ #include <QtTest/QTest> #include <QCborValue> #include <QDebug> +#include <QFile> #include <QLibraryInfo> +#include <QProcess> +#include <QTemporaryDir> #include <memory> @@ -939,6 +942,33 @@ private slots: QCOMPARE(formattedCode, expectedFormattedCode); } +#if QT_CONFIG(process) + void qmldomToolReformat() + { + const QByteArray code = "import QtQml\n\nQtObject {\n}\n"; + const QByteArray in = "\n\n" + code; + const QByteArray out = code; + + QTemporaryDir dir; + QVERIFY(dir.isValid()); + QFile file(dir.filePath("File.qml")); + QVERIFY(file.open(QIODevice::Text | QIODevice::ReadWrite)); + QCOMPARE_NE(file.write(in), -1); + file.close(); + + QProcess qmldom; + qmldom.setProgram(QLibraryInfo::path(QLibraryInfo::BinariesPath) + "/qmldom"_L1); + qmldom.setArguments({ "--reformat"_L1, file.fileName() }); + qmldom.start(); + QVERIFY(qmldom.waitForFinished()); + QCOMPARE(qmldom.exitStatus(), QProcess::NormalExit); + QCOMPARE(qmldom.exitCode(), 0); + + QVERIFY(file.open(QIODevice::Text | QIODevice::ReadOnly)); + QCOMPARE(file.readAll(), out); + } +#endif + private: }; diff --git a/tools/qmldom/qmldomtool.cpp b/tools/qmldom/qmldomtool.cpp index 5c38230ba2..07b63b3920 100644 --- a/tools/qmldom/qmldomtool.cpp +++ b/tools/qmldom/qmldomtool.cpp @@ -200,13 +200,15 @@ int main(int argc, char *argv[]) loadedFiles[iPos] = newIt; }, fileType); + ++iPos; } envPtr->loadPendingDependencies(); bool hadFailures = false; const qsizetype largestFileSizeToCheck = 32000; if (parser.isSet(reformatOption)) { - for (auto &qmlFile : loadedFiles) { + for (const DomItem &loadedFile : std::as_const(loadedFiles)) { + DomItem qmlFile = loadedFile.fileObject(); QString qmlFilePath = qmlFile.canonicalFilePath(); if (qmlFile.internalKind() != DomType::QmlFile) { qWarning() << "cannot reformat" << qmlFile.internalKindStr() << "(" << qmlFilePath |
