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$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
#include "findwarnings.h"
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2021-02-22 15:12:57 +00:00
|
|
|
#include <QtQmlCompiler/private/qqmljsresourcefilemapper_p.h>
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtQml/private/qqmljslexer_p.h>
|
|
|
|
#include <QtQml/private/qqmljsparser_p.h>
|
|
|
|
#include <QtQml/private/qqmljsengine_p.h>
|
|
|
|
#include <QtQml/private/qqmljsastvisitor_p.h>
|
|
|
|
#include <QtQml/private/qqmljsast_p.h>
|
|
|
|
|
|
|
|
#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>
|
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-06-14 12:21:25 +00:00
|
|
|
#ifndef QT_BOOTSTRAPPED
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qlibraryinfo.h>
|
2019-06-14 12:21:25 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-30 10:58:26 +00:00
|
|
|
static bool lint_file(const QString &filename, const bool silent,
|
2021-02-22 15:12:57 +00:00
|
|
|
const QStringList &qmlImportPaths, const QStringList &qmltypesFiles,
|
2021-03-30 10:58:26 +00:00
|
|
|
const QString &resourceFile, const QMap<QString, QQmlJSLogger::Option> &options)
|
2014-07-09 12:28:23 +00:00
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
2019-09-19 09:15:47 +00:00
|
|
|
if (!silent)
|
|
|
|
qWarning() << "Failed to open file" << filename << file.error();
|
2014-07-09 12:28:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString code = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
QQmlJS::Engine engine;
|
|
|
|
QQmlJS::Lexer lexer(&engine);
|
|
|
|
|
|
|
|
QFileInfo info(filename);
|
2019-10-15 08:39:40 +00:00
|
|
|
const QString lowerSuffix = info.suffix().toLower();
|
|
|
|
const bool isESModule = lowerSuffix == QLatin1String("mjs");
|
2019-11-11 17:18:04 +00:00
|
|
|
const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
|
|
|
|
|
|
|
|
lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/ !isJavaScript);
|
2014-07-09 12:28:23 +00:00
|
|
|
QQmlJS::Parser parser(&engine);
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
bool success = isJavaScript ? (isESModule ? parser.parseModule() : parser.parseProgram())
|
|
|
|
: parser.parse();
|
2014-07-09 12:28:23 +00:00
|
|
|
|
|
|
|
if (!success && !silent) {
|
2016-08-11 10:37:27 +00:00
|
|
|
const auto diagnosticMessages = parser.diagnosticMessages();
|
|
|
|
for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
|
2019-11-11 17:18:04 +00:00
|
|
|
qWarning().noquote() << QString::fromLatin1("%1:%2 : %3")
|
2020-02-27 09:49:14 +00:00
|
|
|
.arg(filename).arg(m.loc.startLine).arg(m.message);
|
2014-07-09 12:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:10:53 +00:00
|
|
|
if (success && !isJavaScript) {
|
2021-02-22 15:12:57 +00:00
|
|
|
const auto check = [&](QQmlJSResourceFileMapper *mapper) {
|
|
|
|
QQmlJSImporter importer(qmlImportPaths, mapper);
|
2021-03-25 14:34:42 +00:00
|
|
|
FindWarningVisitor v { &importer, qmltypesFiles, code, filename, silent };
|
|
|
|
|
2021-03-30 10:58:26 +00:00
|
|
|
for (auto it = options.cbegin(); it != options.cend(); ++it) {
|
|
|
|
v.logger().setCategoryDisabled(it.value().m_category, it.value().m_disabled);
|
|
|
|
v.logger().setCategoryLevel(it.value().m_category, it.value().m_level);
|
|
|
|
}
|
2021-03-25 14:34:42 +00:00
|
|
|
|
2021-02-22 15:12:57 +00:00
|
|
|
parser.rootNode()->accept(&v);
|
|
|
|
success = v.check();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (resourceFile.isEmpty()) {
|
|
|
|
check(nullptr);
|
|
|
|
} else {
|
|
|
|
QQmlJSResourceFileMapper mapper({ resourceFile });
|
|
|
|
check(&mapper);
|
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 12:28:23 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argv, char *argc[])
|
|
|
|
{
|
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");
|
|
|
|
QCoreApplication::setApplicationVersion("1.0");
|
2017-01-23 08:01:22 +00:00
|
|
|
#if QT_CONFIG(commandlineparser)
|
2014-07-09 12:28:23 +00:00
|
|
|
QCommandLineParser parser;
|
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:
|
|
|
|
disabled - Fully disables the warning.
|
|
|
|
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.
|
|
|
|
)"));
|
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-03-30 10:58:26 +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"));
|
|
|
|
parser.addOption(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove after Qt 6.2
|
2020-04-24 13:10:53 +00:00
|
|
|
QCommandLineOption disableCheckUnqualified(QStringList() << "no-unqualified-id",
|
2021-03-30 10:58:26 +00:00
|
|
|
QLatin1String("Don't warn about unqualified identifiers (deprecated, please use --unqualified disable instead)"));
|
2020-04-24 13:10:53 +00:00
|
|
|
parser.addOption(disableCheckUnqualified);
|
|
|
|
|
|
|
|
QCommandLineOption disableCheckWithStatement(QStringList() << "no-with-statement",
|
2021-03-30 10:58:26 +00:00
|
|
|
QLatin1String("Don't warn about with statements (deprecated, please use --with-statements disable instead)"));
|
2020-04-24 13:10:53 +00:00
|
|
|
parser.addOption(disableCheckWithStatement);
|
|
|
|
|
|
|
|
QCommandLineOption disableCheckInheritanceCycle(QStringList() << "no-inheritance-cycle",
|
2021-03-30 10:58:26 +00:00
|
|
|
QLatin1String("Don't warn about inheritance cycles (deprecated, please use --inheritance-cycle disable instead"));
|
2020-04-24 13:10:53 +00:00
|
|
|
parser.addOption(disableCheckInheritanceCycle);
|
2019-06-14 12:21:25 +00:00
|
|
|
|
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);
|
|
|
|
|
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);
|
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);
|
|
|
|
|
2020-03-26 09:57:12 +00:00
|
|
|
QCommandLineOption qmltypesFilesOption(
|
|
|
|
QStringList() << "i"
|
|
|
|
<< "qmltypes",
|
2020-09-29 14:55:15 +00:00
|
|
|
QLatin1String("Include the specified qmltypes files. By default, all qmltypes files "
|
|
|
|
"found in the current directory are used. When this option is set, you "
|
|
|
|
"have to explicitly add files from the current directory if you want "
|
|
|
|
"them to be used."),
|
2020-03-26 09:57:12 +00:00
|
|
|
QLatin1String("qmltypes"));
|
|
|
|
parser.addOption(qmltypesFilesOption);
|
|
|
|
|
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-03-30 10:58:26 +00:00
|
|
|
for (auto it = options.begin(); it != options.end(); ++it) {
|
|
|
|
if (parser.isSet(it.key())) {
|
|
|
|
const QString value = parser.value(it.key());
|
|
|
|
auto &option = it.value();
|
|
|
|
|
|
|
|
if (!option.setLevel(value)) {
|
|
|
|
qWarning() << "Invalid logging level" << value << "provided for" << it.key() << "(allowed are: disable, info, warning)";
|
|
|
|
parser.showHelp(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-30 10:58:26 +00:00
|
|
|
|
|
|
|
// TODO: Remove after Qt 6.2
|
|
|
|
bool NoWarnUnqualified = parser.isSet(disableCheckUnqualified);
|
|
|
|
bool NoWarnWithStatement = parser.isSet(disableCheckWithStatement);
|
|
|
|
bool NoWarnInheritanceCycle = parser.isSet(disableCheckInheritanceCycle);
|
|
|
|
|
|
|
|
if (NoWarnUnqualified) {
|
|
|
|
options[QStringLiteral("unqualified")].m_disabled = true;
|
|
|
|
qWarning() << "Warning: --no-unqualified-id is deprecated. See --help.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NoWarnWithStatement) {
|
|
|
|
options[QStringLiteral("with")].m_disabled = true;
|
|
|
|
qWarning() << "Warning: --no-with-statement is deprecated. See --help.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NoWarnInheritanceCycle) {
|
|
|
|
options[QStringLiteral("inheritance-cycle")].m_disabled = true;
|
|
|
|
qWarning() << "Warning: --no-inheritance-cycle is deprecated. See --help.";
|
|
|
|
}
|
2020-04-24 13:10:53 +00:00
|
|
|
|
2021-03-25 11:19:11 +00:00
|
|
|
// use host qml import path as a sane default if not explicitly disabled
|
|
|
|
QStringList qmlImportPaths = parser.isSet(qmlImportNoDefault)
|
|
|
|
? QStringList {}
|
2019-11-11 17:18:04 +00:00
|
|
|
# ifndef QT_BOOTSTRAPPED
|
2021-02-05 17:12:42 +00:00
|
|
|
: QStringList { QLibraryInfo::path(QLibraryInfo::QmlImportsPath), QDir::currentPath() };
|
2019-11-11 17:18:04 +00:00
|
|
|
# else
|
2020-09-29 14:55:15 +00:00
|
|
|
: QStringList { QDir::currentPath() };
|
2019-11-11 17:18:04 +00:00
|
|
|
# endif
|
2020-03-26 09:57:12 +00:00
|
|
|
|
2021-03-25 11:19:11 +00:00
|
|
|
if (parser.isSet(qmlImportPathsOption))
|
|
|
|
qmlImportPaths << parser.values(qmlImportPathsOption);
|
|
|
|
|
2020-09-29 14:55:15 +00:00
|
|
|
QStringList qmltypesFiles;
|
|
|
|
if (parser.isSet(qmltypesFilesOption)) {
|
|
|
|
qmltypesFiles = parser.values(qmltypesFilesOption);
|
|
|
|
} else {
|
|
|
|
// If none are given explicitly, use the qmltypes files from the current directory.
|
|
|
|
QDirIterator it(".", {"*.qmltypes"}, QDir::Files);
|
2021-01-21 12:55:37 +00:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
2020-09-29 14:55:15 +00:00
|
|
|
qmltypesFiles.append(it.fileInfo().absoluteFilePath());
|
2021-01-21 12:55:37 +00:00
|
|
|
}
|
2020-09-29 14:55:15 +00:00
|
|
|
}
|
2020-03-26 09:57:12 +00:00
|
|
|
|
2021-02-22 15:12:57 +00:00
|
|
|
const QString resourceFile = parser.value(resourceOption);
|
|
|
|
|
2017-01-23 08:01:22 +00:00
|
|
|
#else
|
|
|
|
bool silent = false;
|
2020-04-24 13:10:53 +00:00
|
|
|
bool warnUnqualified = true;
|
|
|
|
bool warnWithStatement = true;
|
|
|
|
bool warnInheritanceCycle = true;
|
2020-09-29 14:55:15 +00:00
|
|
|
QStringList qmlImportPahs {};
|
|
|
|
QStringList qmltypesFiles {};
|
2017-01-23 08:01:22 +00:00
|
|
|
#endif
|
2014-07-09 12:28:23 +00:00
|
|
|
bool success = true;
|
2017-01-23 08:01:22 +00:00
|
|
|
#if QT_CONFIG(commandlineparser)
|
2016-08-11 10:37:27 +00:00
|
|
|
for (const QString &filename : positionalArguments)
|
2017-01-23 08:01:22 +00:00
|
|
|
#else
|
2017-01-25 23:34:21 +00:00
|
|
|
const auto arguments = app.arguments();
|
|
|
|
for (const QString &filename : arguments)
|
2017-01-23 08:01:22 +00:00
|
|
|
#endif
|
2021-03-30 10:58:26 +00:00
|
|
|
success &= lint_file(filename, silent, qmlImportPaths, qmltypesFiles, resourceFile, options);
|
2014-07-09 12:28:23 +00:00
|
|
|
|
|
|
|
return success ? 0 : -1;
|
|
|
|
}
|