aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmllint/main.cpp
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2022-05-30 14:52:03 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2022-06-30 11:23:36 +0200
commita9038a6cc6ba79feefa38213b89d9b5ce73cb12d (patch)
tree280ccc9682fc37d6914cf58130b4f728dc5fb236 /tools/qmllint/main.cpp
parent10fa5f6bfe07c26e33e9284bdec753fde0436fc2 (diff)
QQmlJSLogger: Switch to an ID based system
This change makes qmljslogger use an ID based system for categorizing logging entries instead of using an enum. This allows plugins to register their own logging categories after the fact. It's also necessary for us to later show the warning ID when printing warnings and for creating documentation for each ID entry. Currently not every ID maps cleanly to only one type of warning, this has to be cleaned up in a follow-up change. Task-number: QTBUG-103453 Change-Id: I4cac6be7ca165b938e0ea032d077823bf17baf75 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools/qmllint/main.cpp')
-rw-r--r--tools/qmllint/main.cpp80
1 files changed, 56 insertions, 24 deletions
diff --git a/tools/qmllint/main.cpp b/tools/qmllint/main.cpp
index 6171d92360..a5e1361fbb 100644
--- a/tools/qmllint/main.cpp
+++ b/tools/qmllint/main.cpp
@@ -32,7 +32,7 @@ constexpr int JSON_LOGGING_FORMAT_REVISION = 3;
int main(int argv, char *argc[])
{
qSetGlobalQHashSeed(0);
- QMap<QString, QQmlJSLogger::Option> options = QQmlJSLogger::options();
+ QList<QQmlJSLogger::Category> categories;
QCoreApplication app(argv, argc);
QCoreApplication::setApplicationName("qmllint");
@@ -47,17 +47,6 @@ All warnings can be set to three levels:
warning - Displays the warning and leads to a non-zero exit code if encountered.
)"));
- for (auto it = options.cbegin(); it != options.cend(); ++it) {
- QCommandLineOption option(
- it.key(),
- it.value().m_description
- + QStringLiteral(" (default: %1)").arg(it.value().levelToString()),
- QStringLiteral("level"), it.value().levelToString());
- parser.addOption(option);
- settings.addOption(QStringLiteral("Warnings/") + it.value().m_settingsName,
- it.value().levelToString());
- }
-
parser.addHelpOption();
parser.addVersionOption();
@@ -156,30 +145,65 @@ All warnings can be set to three levels:
QLatin1String("directory"));
parser.addOption(pluginPathsOption);
+ auto addCategory = [&](const QQmlJSLogger::Category &category) {
+ categories.push_back(category);
+ if (category.isDefault)
+ return;
+ QCommandLineOption option(
+ category.id().name().toString(),
+ category.description
+ + QStringLiteral(" (default: %1)").arg(category.levelToString()),
+ QStringLiteral("level"), category.levelToString());
+ if (category.ignored)
+ option.setFlags(QCommandLineOption::HiddenFromHelp);
+ parser.addOption(option);
+ settings.addOption(QStringLiteral("Warnings/") + category.settingsName,
+ category.levelToString());
+ };
+
+ for (const auto &category : QQmlJSLogger::defaultCategories()) {
+ addCategory(category);
+ }
+
parser.addPositionalArgument(QLatin1String("files"),
QLatin1String("list of qml or js files to verify"));
- parser.process(app);
+ if (!parser.parse(app.arguments())) {
+ if (parser.unknownOptionNames().isEmpty()) {
+ qWarning().noquote() << parser.errorText();
+ return 1;
+ }
+ }
+
+ // Since we can't use QCommandLineParser::process(), we need to handle version and help manually
+ if (parser.isSet("version"))
+ parser.showVersion();
+
+ if (parser.isSet("help") || parser.isSet("help-all"))
+ parser.showHelp(0);
if (parser.isSet(writeDefaultsOption)) {
return settings.writeDefaults() ? 0 : 1;
}
auto updateLogLevels = [&]() {
- for (auto it = options.begin(); it != options.end(); ++it) {
- const QString &key = it.key();
- const QString &settingsName = QStringLiteral("Warnings/") + it.value().m_settingsName;
+ for (auto &category : categories) {
+ if (category.isDefault)
+ continue;
+
+ const QString &key = category.id().name().toString();
+ const QString &settingsName = QStringLiteral("Warnings/") + category.settingsName;
if (parser.isSet(key) || settings.isSet(settingsName)) {
const QString value = parser.isSet(key) ? parser.value(key)
: settings.value(settingsName).toString();
- auto &option = it.value();
// Do not try to set the levels if it's due to a default config option.
// This way we can tell which options have actually been overwritten by the user.
- if (option.levelToString() == value && !parser.isSet(key))
+ if (category.levelToString() == value && !parser.isSet(key))
continue;
- if (!option.setLevel(value)) {
- qWarning() << "Invalid logging level" << value << "provided for" << it.key()
+ if (!category.setLevel(value)) {
+ qWarning() << "Invalid logging level" << value << "provided for"
+ << category.id().name().toString()
<< "(allowed are: disable, info, warning)";
parser.showHelp(-1);
}
@@ -187,8 +211,6 @@ All warnings can be set to three levels:
}
};
- updateLogLevels();
-
bool silent = parser.isSet(silentOption);
bool useAbsolutePath = parser.isSet(absolutePath);
bool useJson = parser.isSet(jsonOption);
@@ -233,6 +255,16 @@ All warnings can be set to three levels:
QQmlJSLinter linter(qmlImportPaths, pluginPaths, useAbsolutePath);
+ for (const QQmlJSLinter::Plugin &plugin : linter.plugins()) {
+ for (const QQmlJSLogger::Category &category : plugin.categories())
+ addCategory(category);
+ }
+
+ if (!parser.unknownOptionNames().isEmpty())
+ parser.process(app);
+
+ updateLogLevels();
+
if (parser.isSet(listPluginsOption)) {
const std::vector<QQmlJSLinter::Plugin> &plugins = linter.plugins();
if (!plugins.empty()) {
@@ -244,7 +276,7 @@ All warnings can be set to three levels:
<< plugin.description();
}
} else {
- qInfo() << "No plugins installed.";
+ qWarning() << "No plugins installed.";
}
return 0;
}
@@ -319,7 +351,7 @@ All warnings can be set to three levels:
QQmlJSLinter::LintResult lintResult = linter.lintFile(
filename, nullptr, silent || isFixing, useJson ? &jsonFiles : nullptr,
- qmlImportPaths, qmldirFiles, resourceFiles, options);
+ qmlImportPaths, qmldirFiles, resourceFiles, categories);
success &= (lintResult == QQmlJSLinter::LintSuccess);
if (isFixing) {