2014-07-09 12:28:23 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** Copyright (C) 2016 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sergio Martins <sergio.martins@kdab.com>
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-07-09 12:28:23 +00:00
|
|
|
**
|
|
|
|
** This file is part of the plugins of the Qt Toolkit.
|
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2014-07-09 12:28:23 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 11:23:05 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2014-07-09 12:28:23 +00:00
|
|
|
**
|
2016-01-19 11:23:05 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2014-07-09 12:28:23 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
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>
|
|
|
|
|
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[])
|
|
|
|
{
|
2021-05-14 15:34:01 +00:00
|
|
|
qSetGlobalQHashSeed(0);
|
2021-03-30 10:58:26 +00:00
|
|
|
QMap<QString, QQmlJSLogger::Option> options = QQmlJSLogger::options();
|
|
|
|
|
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
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
QLatin1String("Output linting errors as JSON"));
|
|
|
|
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);
|
|
|
|
|
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-02-28 14:51:24 +00:00
|
|
|
QCommandLineOption fixFile(QStringList()
|
|
|
|
<< "f"
|
|
|
|
<< "fix" << QLatin1String("Automatically apply fix suggestions"));
|
|
|
|
parser.addOption(fixFile);
|
|
|
|
|
|
|
|
QCommandLineOption dryRun(QStringList()
|
|
|
|
<< "dry-run"
|
|
|
|
<< QLatin1String("Only print out the contents of the file after fix "
|
|
|
|
"suggestions without applying them"));
|
|
|
|
parser.addOption(dryRun);
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
parser.addPositionalArgument(QLatin1String("files"),
|
|
|
|
QLatin1String("list of qml or js files to verify"));
|
2014-07-09 12:28:23 +00:00
|
|
|
|
|
|
|
parser.process(app);
|
|
|
|
|
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 = [&]() {
|
|
|
|
for (auto it = options.begin(); it != options.end(); ++it) {
|
|
|
|
const QString &key = it.key();
|
|
|
|
const QString &settingsName = QStringLiteral("Warnings/") + it.value().m_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();
|
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.
|
|
|
|
if (option.levelToString() == value && !parser.isSet(key))
|
|
|
|
continue;
|
|
|
|
|
2021-05-12 16:32:57 +00:00
|
|
|
if (!option.setLevel(value)) {
|
|
|
|
qWarning() << "Invalid logging level" << value << "provided for" << it.key()
|
|
|
|
<< "(allowed are: disable, info, warning)";
|
|
|
|
parser.showHelp(-1);
|
|
|
|
}
|
2021-03-30 10:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 16:32:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
updateLogLevels();
|
2021-03-30 10:58:26 +00:00
|
|
|
|
2016-08-11 10:37:27 +00:00
|
|
|
const auto positionalArguments = parser.positionalArguments();
|
|
|
|
if (positionalArguments.isEmpty()) {
|
2014-07-09 12:28:23 +00:00
|
|
|
parser.showHelp(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-10-26 17:21:43 +00:00
|
|
|
QStringList defaultImportPaths =
|
|
|
|
QStringList { QLibraryInfo::path(QLibraryInfo::QmlImportsPath), QDir::currentPath() };
|
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-02-03 11:01:35 +00:00
|
|
|
QQmlJSLinter linter(qmlImportPaths, useAbsolutePath);
|
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());
|
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);
|
|
|
|
|
|
|
|
QQmlJSLinter::LintResult lintResult = linter.lintFile(
|
|
|
|
filename, nullptr, silent || isFixing, useJson ? &jsonFiles : nullptr,
|
|
|
|
qmlImportPaths, qmldirFiles, resourceFiles, options);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString backupFile = filename + u".bak"_qs;
|
|
|
|
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;
|
|
|
|
|
|
|
|
result[u"revision"_qs] = JSON_LOGGING_FORMAT_REVISION;
|
|
|
|
result[u"files"_qs] = jsonFiles;
|
|
|
|
|
|
|
|
QTextStream(stdout) << QString::fromUtf8(
|
|
|
|
QJsonDocument(result).toJson(QJsonDocument::Compact));
|
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;
|
|
|
|
}
|