2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2016 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sergio Martins <sergio.martins@kdab.com>
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2014-07-09 12:28:23 +00:00
|
|
|
|
2021-05-12 16:32:57 +00:00
|
|
|
#include "../shared/qqmltoolingsettings.h"
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2021-02-22 15:12:57 +00:00
|
|
|
#include <QtQmlCompiler/private/qqmljsresourcefilemapper_p.h>
|
2021-07-05 10:48:49 +00:00
|
|
|
#include <QtQmlCompiler/private/qqmljscompiler_p.h>
|
2022-02-03 11:01:35 +00:00
|
|
|
#include <QtQmlCompiler/private/qqmljslinter_p.h>
|
2021-02-22 15:12:57 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qdebug.h>
|
|
|
|
#include <QtCore/qfile.h>
|
|
|
|
#include <QtCore/qfileinfo.h>
|
|
|
|
#include <QtCore/qcoreapplication.h>
|
2020-09-29 14:55:15 +00:00
|
|
|
#include <QtCore/qdiriterator.h>
|
2021-06-01 09:03:12 +00:00
|
|
|
#include <QtCore/qjsonobject.h>
|
|
|
|
#include <QtCore/qjsonarray.h>
|
|
|
|
#include <QtCore/qjsondocument.h>
|
|
|
|
#include <QtCore/qscopeguard.h>
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2017-01-23 08:01:22 +00:00
|
|
|
#if QT_CONFIG(commandlineparser)
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qcommandlineparser.h>
|
2017-01-23 08:01:22 +00:00
|
|
|
#endif
|
2014-07-09 12:28:23 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qlibraryinfo.h>
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2021-06-01 09:03:12 +00:00
|
|
|
#include <cstdio>
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2021-12-07 17:48:08 +00:00
|
|
|
constexpr int JSON_LOGGING_FORMAT_REVISION = 3;
|
2021-06-01 09:03:12 +00:00
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
int main(int argv, char *argc[])
|
|
|
|
{
|
2022-08-26 15:40:24 +00:00
|
|
|
QHashSeed::setDeterministicGlobalSeed();
|
2022-05-30 12:52:03 +00:00
|
|
|
QList<QQmlJSLogger::Category> categories;
|
2021-03-30 10:58:26 +00:00
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
QCoreApplication app(argv, argc);
|
|
|
|
QCoreApplication::setApplicationName("qmllint");
|
2021-06-03 11:03:37 +00:00
|
|
|
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
|
2014-07-09 12:28:23 +00:00
|
|
|
QCommandLineParser parser;
|
2021-05-12 16:32:57 +00:00
|
|
|
QQmlToolingSettings settings(QLatin1String("qmllint"));
|
2021-03-30 10:58:26 +00:00
|
|
|
parser.setApplicationDescription(QLatin1String(R"(QML syntax verifier and analyzer
|
|
|
|
|
|
|
|
All warnings can be set to three levels:
|
2021-11-19 11:28:44 +00:00
|
|
|
disable - Fully disables the warning.
|
2021-03-30 10:58:26 +00:00
|
|
|
info - Displays the warning but does not influence the return code.
|
|
|
|
warning - Displays the warning and leads to a non-zero exit code if encountered.
|
|
|
|
)"));
|
2021-08-25 12:14:38 +00:00
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addVersionOption();
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
QCommandLineOption silentOption(QStringList() << "s" << "silent",
|
|
|
|
QLatin1String("Don't output syntax errors"));
|
2014-07-09 12:28:23 +00:00
|
|
|
parser.addOption(silentOption);
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2021-06-01 09:03:12 +00:00
|
|
|
QCommandLineOption jsonOption(QStringList() << "json",
|
2022-11-28 07:55:40 +00:00
|
|
|
QLatin1String("Write output as JSON to file (or use the special "
|
|
|
|
"filename '-' to write to stdout)"),
|
2022-04-06 12:40:48 +00:00
|
|
|
QLatin1String("file"), QString());
|
2021-06-01 09:03:12 +00:00
|
|
|
parser.addOption(jsonOption);
|
|
|
|
|
2021-05-12 16:32:57 +00:00
|
|
|
QCommandLineOption writeDefaultsOption(
|
|
|
|
QStringList() << "write-defaults",
|
|
|
|
QLatin1String("Writes defaults settings to .qmllint.ini and exits (Warning: This "
|
|
|
|
"will overwrite any existing settings and comments!)"));
|
|
|
|
parser.addOption(writeDefaultsOption);
|
|
|
|
|
|
|
|
QCommandLineOption ignoreSettings(QStringList() << "ignore-settings",
|
|
|
|
QLatin1String("Ignores all settings files and only takes "
|
|
|
|
"command line options into consideration"));
|
|
|
|
parser.addOption(ignoreSettings);
|
|
|
|
|
2022-07-12 08:39:31 +00:00
|
|
|
QCommandLineOption moduleOption({ QStringLiteral("M"), QStringLiteral("module") },
|
|
|
|
QStringLiteral("Lint modules instead of files"));
|
|
|
|
parser.addOption(moduleOption);
|
|
|
|
|
2021-02-22 15:12:57 +00:00
|
|
|
QCommandLineOption resourceOption(
|
|
|
|
{ QStringLiteral("resource") },
|
|
|
|
QStringLiteral("Look for related files in the given resource file"),
|
|
|
|
QStringLiteral("resource"));
|
|
|
|
parser.addOption(resourceOption);
|
2021-05-12 16:32:57 +00:00
|
|
|
const QString &resourceSetting = QLatin1String("ResourcePath");
|
|
|
|
settings.addOption(resourceSetting);
|
2021-02-22 15:12:57 +00:00
|
|
|
|
2020-09-29 14:55:15 +00:00
|
|
|
QCommandLineOption qmlImportPathsOption(
|
2019-06-14 12:21:25 +00:00
|
|
|
QStringList() << "I"
|
|
|
|
<< "qmldirs",
|
2020-09-29 14:55:15 +00:00
|
|
|
QLatin1String("Look for QML modules in specified directory"),
|
2019-06-14 12:21:25 +00:00
|
|
|
QLatin1String("directory"));
|
2020-09-29 14:55:15 +00:00
|
|
|
parser.addOption(qmlImportPathsOption);
|
2021-05-12 16:32:57 +00:00
|
|
|
const QString qmlImportPathsSetting = QLatin1String("AdditionalQmlImportPaths");
|
|
|
|
settings.addOption(qmlImportPathsSetting);
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2021-03-25 11:19:11 +00:00
|
|
|
QCommandLineOption qmlImportNoDefault(
|
|
|
|
QStringList() << "bare",
|
|
|
|
QLatin1String("Do not include default import directories or the current directory. "
|
|
|
|
"This may be used to run qmllint on a project using a different Qt version."));
|
|
|
|
parser.addOption(qmlImportNoDefault);
|
2021-10-26 17:21:43 +00:00
|
|
|
const QString qmlImportNoDefaultSetting = QLatin1String("DisableDefaultImports");
|
|
|
|
settings.addOption(qmlImportNoDefaultSetting, false);
|
2021-03-25 11:19:11 +00:00
|
|
|
|
2021-12-09 10:22:52 +00:00
|
|
|
QCommandLineOption qmldirFilesOption(
|
2020-03-26 09:57:12 +00:00
|
|
|
QStringList() << "i"
|
|
|
|
<< "qmltypes",
|
2021-12-09 10:22:52 +00:00
|
|
|
QLatin1String("Import the specified qmldir files. By default, the qmldir file found "
|
|
|
|
"in the current directory is used if present. If no qmldir file is found,"
|
|
|
|
"but qmltypes files are, those are imported instead. When this option is "
|
|
|
|
"set, you have to explicitly add the qmldir or any qmltypes files in the "
|
|
|
|
"current directory if you want it to be used. Importing qmltypes files "
|
|
|
|
"without their corresponding qmldir file is inadvisable."),
|
|
|
|
QLatin1String("qmldirs"));
|
|
|
|
parser.addOption(qmldirFilesOption);
|
|
|
|
const QString qmldirFilesSetting = QLatin1String("OverwriteImportTypes");
|
|
|
|
settings.addOption(qmldirFilesSetting);
|
2020-03-26 09:57:12 +00:00
|
|
|
|
2021-11-15 09:36:53 +00:00
|
|
|
QCommandLineOption absolutePath(
|
|
|
|
QStringList() << "absolute-path",
|
|
|
|
QLatin1String("Use absolute paths for logging instead of relative ones."));
|
|
|
|
absolutePath.setFlags(QCommandLineOption::HiddenFromHelp);
|
|
|
|
parser.addOption(absolutePath);
|
|
|
|
|
2022-03-22 14:28:55 +00:00
|
|
|
QCommandLineOption fixFile(QStringList() << "f"
|
|
|
|
<< "fix",
|
|
|
|
QLatin1String("Automatically apply fix suggestions"));
|
2022-02-28 14:51:24 +00:00
|
|
|
parser.addOption(fixFile);
|
|
|
|
|
2022-03-22 14:28:55 +00:00
|
|
|
QCommandLineOption dryRun(QStringList() << "dry-run",
|
|
|
|
QLatin1String("Only print out the contents of the file after fix "
|
|
|
|
"suggestions without applying them"));
|
2022-02-28 14:51:24 +00:00
|
|
|
parser.addOption(dryRun);
|
|
|
|
|
2022-03-16 12:19:54 +00:00
|
|
|
QCommandLineOption listPluginsOption(QStringList() << "list-plugins",
|
|
|
|
QLatin1String("List all available plugins"));
|
|
|
|
parser.addOption(listPluginsOption);
|
|
|
|
|
2022-04-11 15:48:36 +00:00
|
|
|
QCommandLineOption pluginsDisable(
|
|
|
|
QStringList() << "D"
|
|
|
|
<< "disable-plugins",
|
|
|
|
QLatin1String("List of qmllint plugins to disable (all to disable all plugins)"),
|
|
|
|
QLatin1String("plugins"));
|
2022-03-16 12:19:54 +00:00
|
|
|
parser.addOption(pluginsDisable);
|
2022-04-11 15:48:36 +00:00
|
|
|
const QString pluginsDisableSetting = QLatin1String("DisablePlugins");
|
|
|
|
settings.addOption(pluginsDisableSetting);
|
2022-03-16 12:19:54 +00:00
|
|
|
|
|
|
|
QCommandLineOption pluginPathsOption(
|
|
|
|
QStringList() << "P"
|
|
|
|
<< "plugin-paths",
|
|
|
|
QLatin1String("Look for qmllint plugins in specified directory"),
|
|
|
|
QLatin1String("directory"));
|
|
|
|
parser.addOption(pluginPathsOption);
|
|
|
|
|
2022-05-30 12:52:03 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
parser.addPositionalArgument(QLatin1String("files"),
|
|
|
|
QLatin1String("list of qml or js files to verify"));
|
2022-05-30 12:52:03 +00:00
|
|
|
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);
|
2014-07-09 12:28:23 +00:00
|
|
|
|
2021-05-12 16:32:57 +00:00
|
|
|
if (parser.isSet(writeDefaultsOption)) {
|
|
|
|
return settings.writeDefaults() ? 0 : 1;
|
|
|
|
}
|
2021-03-30 10:58:26 +00:00
|
|
|
|
2021-05-12 16:32:57 +00:00
|
|
|
auto updateLogLevels = [&]() {
|
2022-05-30 12:52:03 +00:00
|
|
|
for (auto &category : categories) {
|
|
|
|
if (category.isDefault)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const QString &key = category.id().name().toString();
|
|
|
|
const QString &settingsName = QStringLiteral("Warnings/") + category.settingsName;
|
2021-05-12 16:32:57 +00:00
|
|
|
if (parser.isSet(key) || settings.isSet(settingsName)) {
|
|
|
|
const QString value = parser.isSet(key) ? parser.value(key)
|
|
|
|
: settings.value(settingsName).toString();
|
2022-01-10 13:52:30 +00:00
|
|
|
|
|
|
|
// 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.
|
2022-05-30 12:52:03 +00:00
|
|
|
if (category.levelToString() == value && !parser.isSet(key))
|
2022-01-10 13:52:30 +00:00
|
|
|
continue;
|
|
|
|
|
2022-05-30 12:52:03 +00:00
|
|
|
if (!category.setLevel(value)) {
|
|
|
|
qWarning() << "Invalid logging level" << value << "provided for"
|
|
|
|
<< category.id().name().toString()
|
2021-05-12 16:32:57 +00:00
|
|
|
<< "(allowed are: disable, info, warning)";
|
|
|
|
parser.showHelp(-1);
|
|
|
|
}
|
2021-03-30 10:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 16:32:57 +00:00
|
|
|
};
|
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
bool silent = parser.isSet(silentOption);
|
2021-11-15 09:36:53 +00:00
|
|
|
bool useAbsolutePath = parser.isSet(absolutePath);
|
2021-06-01 09:03:12 +00:00
|
|
|
bool useJson = parser.isSet(jsonOption);
|
2021-03-30 10:58:26 +00:00
|
|
|
|
2021-03-25 11:19:11 +00:00
|
|
|
// use host qml import path as a sane default if not explicitly disabled
|
2023-01-23 09:03:00 +00:00
|
|
|
QStringList defaultImportPaths = { QDir::currentPath() };
|
|
|
|
|
|
|
|
if (parser.isSet(resourceOption)) {
|
|
|
|
defaultImportPaths.append(QLatin1String(":/qt-project.org/imports"));
|
|
|
|
defaultImportPaths.append(QLatin1String(":/qt/qml"));
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultImportPaths.append(QLibraryInfo::path(QLibraryInfo::QmlImportsPath));
|
2020-03-26 09:57:12 +00:00
|
|
|
|
2021-10-26 17:21:43 +00:00
|
|
|
QStringList qmlImportPaths =
|
|
|
|
parser.isSet(qmlImportNoDefault) ? QStringList {} : defaultImportPaths;
|
2021-03-25 11:19:11 +00:00
|
|
|
|
2021-12-09 10:22:52 +00:00
|
|
|
QStringList defaultQmldirFiles;
|
|
|
|
if (parser.isSet(qmldirFilesOption)) {
|
|
|
|
defaultQmldirFiles = parser.values(qmldirFilesOption);
|
2020-09-29 14:55:15 +00:00
|
|
|
} else {
|
2021-12-09 10:22:52 +00:00
|
|
|
// If nothing given explicitly, use the qmldir file from the current directory.
|
|
|
|
QFileInfo qmldirFile(QStringLiteral("qmldir"));
|
|
|
|
if (qmldirFile.isFile()) {
|
|
|
|
defaultQmldirFiles.append(qmldirFile.absoluteFilePath());
|
|
|
|
} else {
|
|
|
|
// If no qmldir file is found, use the qmltypes files
|
|
|
|
// from the current directory for backwards compatibility.
|
|
|
|
QDirIterator it(".", {"*.qmltypes"}, QDir::Files);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
defaultQmldirFiles.append(it.fileInfo().absoluteFilePath());
|
|
|
|
}
|
2021-01-21 12:55:37 +00:00
|
|
|
}
|
2020-09-29 14:55:15 +00:00
|
|
|
}
|
2021-12-09 10:22:52 +00:00
|
|
|
QStringList qmldirFiles = defaultQmldirFiles;
|
2020-03-26 09:57:12 +00:00
|
|
|
|
2021-10-26 17:21:43 +00:00
|
|
|
const QStringList defaultResourceFiles =
|
|
|
|
parser.isSet(resourceOption) ? parser.values(resourceOption) : QStringList {};
|
|
|
|
QStringList resourceFiles = defaultResourceFiles;
|
2021-02-22 15:12:57 +00:00
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
bool success = true;
|
2022-03-16 12:19:54 +00:00
|
|
|
|
|
|
|
QStringList pluginPaths = { QQmlJSLinter::defaultPluginPath() };
|
|
|
|
|
|
|
|
if (parser.isSet(pluginPathsOption))
|
|
|
|
pluginPaths << parser.values(pluginPathsOption);
|
|
|
|
|
|
|
|
QQmlJSLinter linter(qmlImportPaths, pluginPaths, useAbsolutePath);
|
|
|
|
|
2022-05-30 12:52:03 +00:00
|
|
|
for (const QQmlJSLinter::Plugin &plugin : linter.plugins()) {
|
|
|
|
for (const QQmlJSLogger::Category &category : plugin.categories())
|
|
|
|
addCategory(category);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parser.unknownOptionNames().isEmpty())
|
|
|
|
parser.process(app);
|
|
|
|
|
|
|
|
updateLogLevels();
|
|
|
|
|
2022-03-16 12:19:54 +00:00
|
|
|
if (parser.isSet(listPluginsOption)) {
|
|
|
|
const std::vector<QQmlJSLinter::Plugin> &plugins = linter.plugins();
|
|
|
|
if (!plugins.empty()) {
|
2022-04-11 10:31:24 +00:00
|
|
|
qInfo().nospace().noquote() << "Plugin\t\t\tBuilt-in?\tVersion\tAuthor\t\tDescription";
|
2022-03-16 12:19:54 +00:00
|
|
|
for (const QQmlJSLinter::Plugin &plugin : plugins) {
|
2022-04-11 10:31:24 +00:00
|
|
|
qInfo().nospace().noquote()
|
|
|
|
<< plugin.name() << "\t\t\t" << (plugin.isBuiltin() ? "Yes" : "No")
|
|
|
|
<< "\t\t" << plugin.version() << "\t" << plugin.author() << "\t\t"
|
|
|
|
<< plugin.description();
|
2022-03-16 12:19:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-30 12:52:03 +00:00
|
|
|
qWarning() << "No plugins installed.";
|
2022-03-16 12:19:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto positionalArguments = parser.positionalArguments();
|
|
|
|
if (positionalArguments.isEmpty()) {
|
|
|
|
parser.showHelp(-1);
|
|
|
|
}
|
2021-05-14 15:34:01 +00:00
|
|
|
|
2021-06-01 09:03:12 +00:00
|
|
|
QJsonArray jsonFiles;
|
|
|
|
|
2021-05-12 16:32:57 +00:00
|
|
|
for (const QString &filename : positionalArguments) {
|
|
|
|
if (!parser.isSet(ignoreSettings)) {
|
|
|
|
settings.search(filename);
|
|
|
|
updateLogLevels();
|
2021-10-26 17:21:43 +00:00
|
|
|
|
|
|
|
const QDir fileDir = QFileInfo(filename).absoluteDir();
|
|
|
|
auto addAbsolutePaths = [&](QStringList &list, const QStringList &entries) {
|
|
|
|
for (const QString &file : entries)
|
|
|
|
list << (QFileInfo(file).isAbsolute() ? file : fileDir.filePath(file));
|
|
|
|
};
|
|
|
|
|
|
|
|
resourceFiles = defaultResourceFiles;
|
|
|
|
|
|
|
|
addAbsolutePaths(resourceFiles, settings.value(resourceSetting).toStringList());
|
|
|
|
|
2021-12-09 10:22:52 +00:00
|
|
|
qmldirFiles = defaultQmldirFiles;
|
|
|
|
if (settings.isSet(qmldirFilesSetting)
|
|
|
|
&& !settings.value(qmldirFilesSetting).toStringList().isEmpty()) {
|
|
|
|
qmldirFiles = {};
|
|
|
|
addAbsolutePaths(qmldirFiles,
|
|
|
|
settings.value(qmldirFilesSetting).toStringList());
|
2021-10-26 17:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.isSet(qmlImportNoDefault)
|
|
|
|
|| (settings.isSet(qmlImportNoDefaultSetting)
|
|
|
|
&& settings.value(qmlImportNoDefaultSetting).toBool())) {
|
|
|
|
qmlImportPaths = {};
|
|
|
|
} else {
|
|
|
|
qmlImportPaths = defaultImportPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.isSet(qmlImportPathsOption))
|
|
|
|
qmlImportPaths << parser.values(qmlImportPathsOption);
|
|
|
|
|
|
|
|
addAbsolutePaths(qmlImportPaths, settings.value(qmlImportPathsSetting).toStringList());
|
2022-03-16 12:19:54 +00:00
|
|
|
|
2022-04-11 15:48:36 +00:00
|
|
|
QSet<QString> disabledPlugins;
|
2022-03-16 12:19:54 +00:00
|
|
|
|
2022-04-11 15:48:36 +00:00
|
|
|
if (parser.isSet(pluginsDisable)) {
|
|
|
|
for (const QString &plugin : parser.values(pluginsDisable))
|
|
|
|
disabledPlugins << plugin.toLower();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.isSet(pluginsDisableSetting)) {
|
|
|
|
for (const QString &plugin : settings.value(pluginsDisableSetting).toStringList())
|
|
|
|
disabledPlugins << plugin.toLower();
|
|
|
|
}
|
|
|
|
|
|
|
|
linter.setPluginsEnabled(!disabledPlugins.contains("all"));
|
|
|
|
|
|
|
|
if (!linter.pluginsEnabled())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto &plugins = linter.plugins();
|
|
|
|
|
|
|
|
for (auto &plugin : plugins)
|
|
|
|
plugin.setEnabled(!disabledPlugins.contains(plugin.name().toLower()));
|
2021-05-12 16:32:57 +00:00
|
|
|
}
|
2022-01-18 15:16:20 +00:00
|
|
|
|
2022-02-28 14:51:24 +00:00
|
|
|
const bool isFixing = parser.isSet(fixFile);
|
|
|
|
|
2022-07-12 08:39:31 +00:00
|
|
|
QQmlJSLinter::LintResult lintResult;
|
|
|
|
|
|
|
|
if (parser.isSet(moduleOption)) {
|
|
|
|
lintResult = linter.lintModule(filename, silent, useJson ? &jsonFiles : nullptr);
|
|
|
|
} else {
|
|
|
|
lintResult = linter.lintFile(filename, nullptr, silent || isFixing,
|
|
|
|
useJson ? &jsonFiles : nullptr, qmlImportPaths,
|
|
|
|
qmldirFiles, resourceFiles, categories);
|
|
|
|
}
|
2022-02-28 14:51:24 +00:00
|
|
|
success &= (lintResult == QQmlJSLinter::LintSuccess);
|
|
|
|
|
|
|
|
if (isFixing) {
|
|
|
|
if (lintResult != QQmlJSLinter::LintSuccess && lintResult != QQmlJSLinter::HasWarnings)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QString fixedCode;
|
|
|
|
const QQmlJSLinter::FixResult result = linter.applyFixes(&fixedCode, silent);
|
|
|
|
|
|
|
|
if (result != QQmlJSLinter::NothingToFix && result != QQmlJSLinter::FixSuccess) {
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.isSet(dryRun)) {
|
|
|
|
QTextStream(stdout) << fixedCode;
|
|
|
|
} else {
|
|
|
|
if (result == QQmlJSLinter::NothingToFix) {
|
|
|
|
if (!silent)
|
|
|
|
qWarning().nospace() << "Nothing to fix in " << filename;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
const QString backupFile = filename + u".bak"_s;
|
2022-02-28 14:51:24 +00:00
|
|
|
if (QFile::exists(backupFile) && !QFile::remove(backupFile)) {
|
|
|
|
if (!silent) {
|
|
|
|
qWarning().nospace() << "Failed to remove old backup file " << backupFile
|
|
|
|
<< ", aborting";
|
|
|
|
}
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!QFile::copy(filename, backupFile)) {
|
|
|
|
if (!silent) {
|
|
|
|
qWarning().nospace()
|
|
|
|
<< "Failed to create backup file " << backupFile << ", aborting";
|
|
|
|
}
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
|
|
if (!silent) {
|
|
|
|
qWarning().nospace() << "Failed to open " << filename
|
|
|
|
<< " for writing:" << file.errorString();
|
|
|
|
}
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QByteArray data = fixedCode.toUtf8();
|
|
|
|
if (file.write(data) != data.size()) {
|
|
|
|
if (!silent) {
|
|
|
|
qWarning().nospace() << "Failed to write new contents to " << filename
|
|
|
|
<< ": " << file.errorString();
|
|
|
|
}
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!silent) {
|
|
|
|
qDebug().nospace() << "Applied fixes to " << filename << ". Backup created at "
|
|
|
|
<< backupFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 09:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (useJson) {
|
|
|
|
QJsonObject result;
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
result[u"revision"_s] = JSON_LOGGING_FORMAT_REVISION;
|
|
|
|
result[u"files"_s] = jsonFiles;
|
2021-06-01 09:03:12 +00:00
|
|
|
|
2022-04-06 12:40:48 +00:00
|
|
|
QString fileName = parser.value(jsonOption);
|
|
|
|
|
|
|
|
const QByteArray json = QJsonDocument(result).toJson(QJsonDocument::Compact);
|
|
|
|
|
|
|
|
if (fileName == u"-") {
|
|
|
|
QTextStream(stdout) << QString::fromUtf8(json);
|
|
|
|
} else {
|
|
|
|
QFile file(fileName);
|
|
|
|
file.open(QFile::WriteOnly);
|
|
|
|
file.write(json);
|
|
|
|
}
|
2021-05-12 16:32:57 +00:00
|
|
|
}
|
2021-06-01 09:03:12 +00:00
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
return success ? 0 : -1;
|
|
|
|
}
|