diff options
author | Moody Liu <mooodyhunter@outlook.com> | 2022-06-24 17:30:55 +0100 |
---|---|---|
committer | Moody Liu <mooodyhunter@outlook.com> | 2022-07-08 14:55:38 +0100 |
commit | 43b7daa1f7085f108d0ff6224f34c2b8a40f16c5 (patch) | |
tree | efa2dacfdf56ed6564f2d74bc4c0a94e162eecb2 /tools/qmldom | |
parent | 843b153e6e78095a0bea839f7cbb4e09c47ea3b2 (diff) |
qmldom: add support to dump a QML file's syntax tree
with the help of existing QmlAstDumper, this will help develop various
utilities for qml files, such as qmlls
[ChangeLog][QML][qmldom] Add support to dump AST of a QML file
Change-Id: Ia5a6023724a41361279414887e0d82f398b1c773
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@qt.io>
Diffstat (limited to 'tools/qmldom')
-rw-r--r-- | tools/qmldom/qmldomtool.cpp | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/tools/qmldom/qmldomtool.cpp b/tools/qmldom/qmldomtool.cpp index 1e8de35f5d..998f55d21f 100644 --- a/tools/qmldom/qmldomtool.cpp +++ b/tools/qmldom/qmldomtool.cpp @@ -14,6 +14,7 @@ #include <QtQmlDom/private/qqmldomoutwriter_p.h> #include <QtQmlDom/private/qqmldomelements_p.h> #include <QtQmlDom/private/qqmldomfieldfilter_p.h> +#include <QtQmlDom/private/qqmldomastdumper_p.h> #include <cstdio> #include <optional> @@ -109,6 +110,10 @@ int main(int argc, char *argv[]) QLatin1String("nBackups")); parser.addOption(nBackupsOption); + QCommandLineOption dumpAstOption(QStringList() << "dump-ast", + QLatin1String("Dumps the AST of the given QML file.")); + parser.addOption(dumpAstOption); + parser.addPositionalArgument(QLatin1String("files"), QLatin1String("list of qml or js files to verify")); @@ -254,7 +259,25 @@ int main(int argc, char *argv[]) } hadFailures = hadFailures || !bool(res); } - } else if (parser.isSet(dumpOption) || !parser.isSet(reformatOption)) { + } else if (parser.isSet(dumpAstOption)) { + if (pathsToDump.length() > 1) { + qWarning() << "--dump-ast can only be used with a single file"; + return 1; + } + for (auto &fileItem : loadedFiles) { + const auto file = fileItem.fileObject().ownerAs<QmlFile>(); + if (!file) { + qWarning() << "cannot dump AST for" << fileItem.canonicalPath(); + qWarning() << "is it a valid QML file?"; + continue; + } + const QString ast = + QQmlJS::Dom::astNodeDump(file->ast(), AstDumperOption::DumpNode, 1, 0); + QTextStream ts(stdout); + ts << ast << Qt::flush; + } + } else if (parser.isSet(dumpOption) || !parser.isSet(reformatOption) + || !parser.isSet(dumpAstOption)) { qDebug() << "will dump\n"; QTextStream ts(stdout); auto sink = [&ts](QStringView v) { |