aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmllint/main.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2019-06-14 14:21:25 +0200
committerFabian Kosmale <fabian.kosmale@qt.io>2019-07-12 15:31:16 +0200
commit392521048ce6ef43a127b3dba199eee58557b1f6 (patch)
tree5dcf3c0343ecb34f299ceb468aba7f4d442d8dd7 /tools/qmllint/main.cpp
parentde0d91abbbcf58a66018a08ca77bb4d63a5efda1 (diff)
Extend linter to check for unqualified ids
The linter has gained a new option (-U/--check-unqualified). If run with this option, it warns about occurrences of unqualified identifiers. Furthermore, it attempts to detect the reason for why the identifier can be used unqalified: - If the id originates from the root element, it suggests to qualify the access either with the root element's id, or with "parent" if applicable. - If the id is the parameter of a signal, it suggests to use functions in the handler, instead of relying on the signal parameters to be "magically" injected into scope. The linter does not attempt to handle with statements, but warns the user instead that they are a bad idea. Change-Id: I9aaf28c37595d84886a1071d49b86799b222a617 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tools/qmllint/main.cpp')
-rw-r--r--tools/qmllint/main.cpp42
1 files changed, 39 insertions, 3 deletions
diff --git a/tools/qmllint/main.cpp b/tools/qmllint/main.cpp
index bf0ceb54b7..235ec16c6e 100644
--- a/tools/qmllint/main.cpp
+++ b/tools/qmllint/main.cpp
@@ -34,12 +34,19 @@
#endif
#include <QCoreApplication>
-#include <private/qv4staticvalue_p.h>
+#ifndef QT_BOOTSTRAPPED
+#include <QLibraryInfo>
+#endif
+
#include <private/qqmljslexer_p.h>
#include <private/qqmljsparser_p.h>
#include <private/qqmljsengine_p.h>
+#include <private/qqmljsastvisitor_p.h>
+#include <private/qqmljsast_p.h>
+
+#include "findunqualified.h"
-static bool lint_file(const QString &filename, bool silent)
+static bool lint_file(const QString &filename, const bool silent, const bool warnUnqualied, QStringList const &qmltypeDirs)
{
QFile file(filename);
if (!file.open(QFile::ReadOnly)) {
@@ -67,6 +74,13 @@ static bool lint_file(const QString &filename, bool silent)
}
}
+ if (success && !isJavaScript && warnUnqualied) {
+ auto root = parser.rootNode();
+ FindUnqualifiedIDVisitor v { qmltypeDirs, code, filename};
+ root->accept(&v);
+ success = v.check();
+ }
+
return success;
}
@@ -80,8 +94,20 @@ int main(int argv, char *argc[])
parser.setApplicationDescription(QLatin1String("QML syntax verifier"));
parser.addHelpOption();
parser.addVersionOption();
+
QCommandLineOption silentOption(QStringList() << "s" << "silent", QLatin1String("Don't output syntax errors"));
parser.addOption(silentOption);
+
+ QCommandLineOption checkUnqualified(QStringList() << "U" << "check-unqualified", QLatin1String("Warn about unqualified identifiers"));
+ parser.addOption(checkUnqualified);
+
+ QCommandLineOption qmltypesDirsOption(
+ QStringList() << "I"
+ << "qmldirs",
+ QLatin1String("Look for qmltypes files in specified directory"),
+ QLatin1String("directory"));
+ parser.addOption(qmltypesDirsOption);
+
parser.addPositionalArgument(QLatin1String("files"), QLatin1String("list of qml or js files to verify"));
parser.process(app);
@@ -92,8 +118,18 @@ int main(int argv, char *argc[])
}
bool silent = parser.isSet(silentOption);
+ bool warnUnqualified = parser.isSet(checkUnqualified);
+ // use host qml import path as a sane default if nothing else has been provided
+ QStringList qmltypeDirs = parser.isSet(qmltypesDirsOption) ? parser.values(qmltypesDirsOption)
+#ifndef QT_BOOTSTRAPPED
+ : QStringList{QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath)};
+#else
+ : QStringList{};
+#endif
#else
bool silent = false;
+ bool warnUnqualified = false;
+ QStringList qmltypeDirs {};
#endif
bool success = true;
#if QT_CONFIG(commandlineparser)
@@ -102,7 +138,7 @@ int main(int argv, char *argc[])
const auto arguments = app.arguments();
for (const QString &filename : arguments)
#endif
- success &= lint_file(filename, silent);
+ success &= lint_file(filename, silent, warnUnqualified, qmltypeDirs);
return success ? 0 : -1;
}