2021-11-17 10:25:36 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of the tools applications of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
|
|
|
** 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
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-03 11:01:35 +00:00
|
|
|
#include "qqmljslinter_p.h"
|
2021-11-17 10:25:36 +00:00
|
|
|
|
2022-02-03 11:01:35 +00:00
|
|
|
#include "qqmljslintercodegen_p.h"
|
2021-11-17 10:25:36 +00:00
|
|
|
|
|
|
|
#include <QtQmlCompiler/private/qqmljsimporter_p.h>
|
2021-12-16 16:15:21 +00:00
|
|
|
#include <QtQmlCompiler/private/qqmljsimportvisitor_p.h>
|
2022-04-27 07:19:54 +00:00
|
|
|
#include <QtQmlCompiler/private/qqmljsliteralbindingcheck_p.h>
|
2021-11-17 10:25:36 +00:00
|
|
|
|
|
|
|
#include <QtCore/qjsonobject.h>
|
|
|
|
#include <QtCore/qfileinfo.h>
|
|
|
|
#include <QtCore/qloggingcategory.h>
|
2022-03-16 12:19:54 +00:00
|
|
|
#include <QtCore/qpluginloader.h>
|
|
|
|
#include <QtCore/qlibraryinfo.h>
|
|
|
|
#include <QtCore/qdir.h>
|
2022-04-11 09:33:52 +00:00
|
|
|
#include <QtCore/private/qduplicatetracker_p.h>
|
2022-05-02 14:51:01 +00:00
|
|
|
#include <QtCore/qscopedpointer.h>
|
2022-04-11 09:33:52 +00:00
|
|
|
|
2022-03-16 12:19:54 +00:00
|
|
|
#include <QtQmlCompiler/private/qqmlsa_p.h>
|
|
|
|
|
|
|
|
#if QT_CONFIG(library)
|
|
|
|
# include <QtCore/qdiriterator.h>
|
|
|
|
# include <QtCore/qlibrary.h>
|
|
|
|
#endif
|
2021-11-17 10:25:36 +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 <QtQml/private/qqmljsdiagnosticmessage_p.h>
|
|
|
|
|
2022-03-16 12:19:54 +00:00
|
|
|
|
2021-11-17 10:25:36 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2022-02-03 11:01:35 +00:00
|
|
|
class CodegenWarningInterface final : public QV4::Compiler::CodegenWarningInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CodegenWarningInterface(QQmlJSLogger *logger) : m_logger(logger) { }
|
|
|
|
|
|
|
|
void reportVarUsedBeforeDeclaration(const QString &name, const QString &fileName,
|
|
|
|
QQmlJS::SourceLocation declarationLocation,
|
|
|
|
QQmlJS::SourceLocation accessLocation) override
|
|
|
|
{
|
|
|
|
Q_UNUSED(fileName)
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
m_logger->log(
|
2022-03-21 09:21:18 +00:00
|
|
|
u"Variable \"%1\" is used here before its declaration. The declaration is at %2:%3."_s
|
2022-02-03 11:01:35 +00:00
|
|
|
.arg(name)
|
|
|
|
.arg(declarationLocation.startLine)
|
|
|
|
.arg(declarationLocation.startColumn),
|
|
|
|
Log_Type, accessLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QQmlJSLogger *m_logger;
|
|
|
|
};
|
|
|
|
|
2022-03-16 12:19:54 +00:00
|
|
|
QString QQmlJSLinter::defaultPluginPath()
|
|
|
|
{
|
|
|
|
return QLibraryInfo::path(QLibraryInfo::PluginsPath) + QDir::separator() + u"qmllint";
|
|
|
|
}
|
|
|
|
|
|
|
|
QQmlJSLinter::QQmlJSLinter(const QStringList &importPaths, const QStringList &pluginPaths,
|
|
|
|
bool useAbsolutePath)
|
|
|
|
: m_useAbsolutePath(useAbsolutePath),
|
|
|
|
m_enablePlugins(true),
|
|
|
|
m_importer(importPaths, nullptr, true)
|
|
|
|
{
|
2022-04-07 10:35:48 +00:00
|
|
|
m_plugins = loadPlugins(pluginPaths);
|
2022-03-16 12:19:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 14:59:24 +00:00
|
|
|
QQmlJSLinter::Plugin::Plugin(QQmlJSLinter::Plugin &&plugin) noexcept
|
2022-03-16 12:19:54 +00:00
|
|
|
{
|
|
|
|
m_name = plugin.m_name;
|
|
|
|
m_author = plugin.m_author;
|
|
|
|
m_description = plugin.m_description;
|
|
|
|
m_version = plugin.m_version;
|
|
|
|
m_instance = plugin.m_instance;
|
|
|
|
m_loader = plugin.m_loader;
|
|
|
|
m_isValid = plugin.m_isValid;
|
|
|
|
m_isBuiltin = plugin.m_isBuiltin;
|
|
|
|
|
|
|
|
// Mark the old Plugin as invalid and make sure it doesn't delete the loader
|
|
|
|
plugin.m_loader = nullptr;
|
|
|
|
plugin.m_instance = nullptr;
|
|
|
|
plugin.m_isValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if QT_CONFIG(library)
|
|
|
|
QQmlJSLinter::Plugin::Plugin(QString path)
|
|
|
|
{
|
|
|
|
m_loader = new QPluginLoader(path);
|
|
|
|
if (!parseMetaData(m_loader->metaData(), path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QObject *object = m_loader->instance();
|
|
|
|
if (!object)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_instance = qobject_cast<QQmlSA::LintPlugin *>(object);
|
|
|
|
if (!m_instance)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_isValid = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QQmlJSLinter::Plugin::Plugin(const QStaticPlugin &staticPlugin)
|
2021-11-17 10:25:36 +00:00
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
if (!parseMetaData(staticPlugin.metaData(), u"built-in"_s))
|
2022-03-16 12:19:54 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_instance = qobject_cast<QQmlSA::LintPlugin *>(staticPlugin.instance());
|
|
|
|
if (!m_instance)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_isValid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QQmlJSLinter::Plugin::~Plugin()
|
|
|
|
{
|
|
|
|
#if QT_CONFIG(library)
|
|
|
|
if (m_loader != nullptr) {
|
|
|
|
m_loader->unload();
|
|
|
|
m_loader->deleteLater();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QQmlJSLinter::Plugin::parseMetaData(const QJsonObject &metaData, QString pluginName)
|
|
|
|
{
|
|
|
|
const QString pluginIID = QStringLiteral(QmlLintPluginInterface_iid);
|
|
|
|
|
|
|
|
if (metaData[u"IID"].toString() != pluginIID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QJsonObject pluginMetaData = metaData[u"MetaData"].toObject();
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
for (const QString &requiredKey : { u"name"_s, u"version"_s, u"author"_s }) {
|
2022-03-16 12:19:54 +00:00
|
|
|
if (!pluginMetaData.contains(requiredKey)) {
|
|
|
|
qWarning() << pluginName << "is missing the required " << requiredKey
|
|
|
|
<< "metadata, skipping";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_name = pluginMetaData[u"name"].toString();
|
|
|
|
m_author = pluginMetaData[u"author"].toString();
|
|
|
|
m_version = pluginMetaData[u"version"].toString();
|
2022-03-21 09:21:18 +00:00
|
|
|
m_description = pluginMetaData[u"description"].toString(u"-/-"_s);
|
2022-03-16 12:19:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<QQmlJSLinter::Plugin> QQmlJSLinter::loadPlugins(QStringList paths)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<Plugin> plugins;
|
|
|
|
|
2022-04-11 09:33:52 +00:00
|
|
|
QDuplicateTracker<QString> seenPlugins;
|
|
|
|
|
2022-03-16 12:19:54 +00:00
|
|
|
for (const QStaticPlugin &staticPlugin : QPluginLoader::staticPlugins()) {
|
|
|
|
Plugin plugin(staticPlugin);
|
2022-04-11 09:33:52 +00:00
|
|
|
if (!plugin.isValid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (seenPlugins.hasSeen(plugin.name().toLower())) {
|
|
|
|
qWarning() << "Two plugins named" << plugin.name()
|
|
|
|
<< "present, make sure no plugins are duplicated. The second plugin will "
|
|
|
|
"not be loaded.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins.push_back(std::move(plugin));
|
2022-03-16 12:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if QT_CONFIG(library)
|
|
|
|
for (const QString &pluginDir : paths) {
|
|
|
|
QDirIterator it { pluginDir };
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
auto potentialPlugin = it.next();
|
|
|
|
|
|
|
|
if (!QLibrary::isLibrary(potentialPlugin))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Plugin plugin(potentialPlugin);
|
|
|
|
|
2022-04-11 09:33:52 +00:00
|
|
|
if (!plugin.isValid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (seenPlugins.hasSeen(plugin.name().toLower())) {
|
|
|
|
qWarning() << "Two plugins named" << plugin.name()
|
|
|
|
<< "present, make sure no plugins are duplicated. The second plugin "
|
|
|
|
"will not be loaded.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins.push_back(std::move(plugin));
|
2022-03-16 12:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return plugins;
|
2021-11-17 10:25:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 11:01:35 +00:00
|
|
|
void QQmlJSLinter::parseComments(QQmlJSLogger *logger,
|
|
|
|
const QList<QQmlJS::SourceLocation> &comments)
|
2021-12-16 16:15:21 +00:00
|
|
|
{
|
|
|
|
QHash<int, QSet<QQmlJSLoggerCategory>> disablesPerLine;
|
|
|
|
QHash<int, QSet<QQmlJSLoggerCategory>> enablesPerLine;
|
|
|
|
QHash<int, QSet<QQmlJSLoggerCategory>> oneLineDisablesPerLine;
|
|
|
|
|
|
|
|
const QString code = logger->code();
|
|
|
|
const QStringList lines = code.split(u'\n');
|
|
|
|
|
|
|
|
for (const auto &loc : comments) {
|
|
|
|
const QString comment = code.mid(loc.offset, loc.length);
|
|
|
|
if (!comment.startsWith(u" qmllint ") && !comment.startsWith(u"qmllint "))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QStringList words = comment.split(u' ');
|
|
|
|
if (words.constFirst().isEmpty())
|
|
|
|
words.removeFirst();
|
|
|
|
|
|
|
|
const QString command = words.at(1);
|
|
|
|
|
|
|
|
QSet<QQmlJSLoggerCategory> categories;
|
|
|
|
for (qsizetype i = 2; i < words.size(); i++) {
|
|
|
|
const QString category = words.at(i);
|
|
|
|
const auto option = logger->options().constFind(category);
|
|
|
|
if (option != logger->options().constEnd())
|
|
|
|
categories << option->m_category;
|
|
|
|
else
|
2022-03-21 09:21:18 +00:00
|
|
|
logger->log(u"qmllint directive on unknown category \"%1\""_s.arg(category),
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
Log_Syntax, loc);
|
2021-12-16 16:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (categories.isEmpty()) {
|
|
|
|
for (const auto &option : logger->options())
|
|
|
|
categories << option.m_category;
|
|
|
|
}
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
if (command == u"disable"_s) {
|
2021-12-16 16:15:21 +00:00
|
|
|
const QString line = lines[loc.startLine - 1];
|
|
|
|
const QString preComment = line.left(line.indexOf(comment) - 2);
|
|
|
|
|
|
|
|
bool lineHasContent = false;
|
|
|
|
for (qsizetype i = 0; i < preComment.size(); i++) {
|
|
|
|
if (!preComment[i].isSpace()) {
|
|
|
|
lineHasContent = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineHasContent)
|
|
|
|
oneLineDisablesPerLine[loc.startLine] |= categories;
|
|
|
|
else
|
|
|
|
disablesPerLine[loc.startLine] |= categories;
|
2022-03-21 09:21:18 +00:00
|
|
|
} else if (command == u"enable"_s) {
|
2021-12-16 16:15:21 +00:00
|
|
|
enablesPerLine[loc.startLine + 1] |= categories;
|
|
|
|
} else {
|
2022-03-21 09:21:18 +00:00
|
|
|
logger->log(u"Invalid qmllint directive \"%1\" provided"_s.arg(command), Log_Syntax,
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
loc);
|
2021-12-16 16:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disablesPerLine.isEmpty() && oneLineDisablesPerLine.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QSet<QQmlJSLoggerCategory> currentlyDisabled;
|
|
|
|
for (qsizetype i = 1; i <= lines.length(); i++) {
|
|
|
|
currentlyDisabled.unite(disablesPerLine[i]).subtract(enablesPerLine[i]);
|
|
|
|
|
|
|
|
currentlyDisabled.unite(oneLineDisablesPerLine[i]);
|
|
|
|
|
|
|
|
if (!currentlyDisabled.isEmpty())
|
|
|
|
logger->ignoreWarnings(i, currentlyDisabled);
|
|
|
|
|
|
|
|
currentlyDisabled.subtract(oneLineDisablesPerLine[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 14:51:24 +00:00
|
|
|
QQmlJSLinter::LintResult QQmlJSLinter::lintFile(const QString &filename,
|
|
|
|
const QString *fileContents, const bool silent,
|
|
|
|
QJsonArray *json, const QStringList &qmlImportPaths,
|
|
|
|
const QStringList &qmldirFiles,
|
|
|
|
const QStringList &resourceFiles,
|
|
|
|
const QMap<QString, QQmlJSLogger::Option> &options)
|
2021-11-17 10:25:36 +00:00
|
|
|
{
|
2021-11-23 14:52:44 +00:00
|
|
|
// Make sure that we don't expose an old logger if we return before a new one is created.
|
|
|
|
m_logger.reset();
|
|
|
|
|
2021-11-17 10:25:36 +00:00
|
|
|
QJsonArray warnings;
|
|
|
|
QJsonObject result;
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
QScopeGuard jsonOutput([&] {
|
|
|
|
if (!json)
|
|
|
|
return;
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
result[u"filename"_s] = QFileInfo(filename).absoluteFilePath();
|
2021-11-17 10:25:36 +00:00
|
|
|
result[u"warnings"] = warnings;
|
|
|
|
result[u"success"] = success;
|
|
|
|
|
|
|
|
json->append(result);
|
|
|
|
});
|
|
|
|
|
2021-12-07 17:48:08 +00:00
|
|
|
auto addJsonWarning = [&](const QQmlJS::DiagnosticMessage &message,
|
|
|
|
const std::optional<FixSuggestion> &suggestion = {}) {
|
2021-11-17 10:25:36 +00:00
|
|
|
QJsonObject jsonMessage;
|
|
|
|
|
|
|
|
QString type;
|
|
|
|
switch (message.type) {
|
|
|
|
case QtDebugMsg:
|
2022-03-21 09:21:18 +00:00
|
|
|
type = u"debug"_s;
|
2021-11-17 10:25:36 +00:00
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
2022-03-21 09:21:18 +00:00
|
|
|
type = u"warning"_s;
|
2021-11-17 10:25:36 +00:00
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
2022-03-21 09:21:18 +00:00
|
|
|
type = u"critical"_s;
|
2021-11-17 10:25:36 +00:00
|
|
|
break;
|
|
|
|
case QtFatalMsg:
|
2022-03-21 09:21:18 +00:00
|
|
|
type = u"fatal"_s;
|
2021-11-17 10:25:36 +00:00
|
|
|
break;
|
|
|
|
case QtInfoMsg:
|
2022-03-21 09:21:18 +00:00
|
|
|
type = u"info"_s;
|
2021-11-17 10:25:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-03-21 09:21:18 +00:00
|
|
|
type = u"unknown"_s;
|
2021-11-17 10:25:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
jsonMessage[u"type"_s] = type;
|
2021-11-17 10:25:36 +00:00
|
|
|
|
|
|
|
if (message.loc.isValid()) {
|
2022-03-21 09:21:18 +00:00
|
|
|
jsonMessage[u"line"_s] = static_cast<int>(message.loc.startLine);
|
|
|
|
jsonMessage[u"column"_s] = static_cast<int>(message.loc.startColumn);
|
|
|
|
jsonMessage[u"charOffset"_s] = static_cast<int>(message.loc.offset);
|
|
|
|
jsonMessage[u"length"_s] = static_cast<int>(message.loc.length);
|
2021-11-17 10:25:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
jsonMessage[u"message"_s] = message.message;
|
2021-11-17 10:25:36 +00:00
|
|
|
|
2021-12-07 17:48:08 +00:00
|
|
|
QJsonArray suggestions;
|
|
|
|
if (suggestion.has_value()) {
|
|
|
|
for (const auto &fix : suggestion->fixes) {
|
|
|
|
QJsonObject jsonFix;
|
|
|
|
jsonFix[u"message"] = fix.message;
|
2022-03-21 09:21:18 +00:00
|
|
|
jsonFix[u"line"_s] = static_cast<int>(fix.cutLocation.startLine);
|
|
|
|
jsonFix[u"column"_s] = static_cast<int>(fix.cutLocation.startColumn);
|
|
|
|
jsonFix[u"charOffset"_s] = static_cast<int>(fix.cutLocation.offset);
|
|
|
|
jsonFix[u"length"_s] = static_cast<int>(fix.cutLocation.length);
|
|
|
|
jsonFix[u"replacement"_s] = fix.replacementString;
|
2022-02-24 09:28:52 +00:00
|
|
|
jsonFix[u"isHint"] = fix.isHint;
|
2022-03-22 17:28:22 +00:00
|
|
|
if (!fix.fileName.isEmpty())
|
|
|
|
jsonFix[u"fileName"] = fix.fileName;
|
2021-12-07 17:48:08 +00:00
|
|
|
suggestions << jsonFix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jsonMessage[u"suggestions"] = suggestions;
|
|
|
|
|
2021-11-17 10:25:36 +00:00
|
|
|
warnings << jsonMessage;
|
|
|
|
};
|
|
|
|
|
2021-11-23 14:52:44 +00:00
|
|
|
QString code;
|
|
|
|
|
|
|
|
if (fileContents == nullptr) {
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
if (json) {
|
2022-02-03 11:01:35 +00:00
|
|
|
addJsonWarning(
|
|
|
|
QQmlJS::DiagnosticMessage { QStringLiteral("Failed to open file %1: %2")
|
|
|
|
.arg(filename, file.errorString()),
|
|
|
|
QtCriticalMsg, QQmlJS::SourceLocation() });
|
2021-11-23 14:52:44 +00:00
|
|
|
success = false;
|
|
|
|
} else if (!silent) {
|
|
|
|
qWarning() << "Failed to open file" << filename << file.error();
|
|
|
|
}
|
2022-02-28 14:51:24 +00:00
|
|
|
return FailedToOpen;
|
2021-11-17 10:25:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:52:44 +00:00
|
|
|
code = QString::fromUtf8(file.readAll());
|
|
|
|
file.close();
|
|
|
|
} else {
|
|
|
|
code = *fileContents;
|
|
|
|
}
|
2021-11-17 10:25:36 +00:00
|
|
|
|
2022-02-28 14:51:24 +00:00
|
|
|
m_fileContents = code;
|
|
|
|
|
2021-11-17 10:25:36 +00:00
|
|
|
QQmlJS::Engine engine;
|
|
|
|
QQmlJS::Lexer lexer(&engine);
|
|
|
|
|
|
|
|
QFileInfo info(filename);
|
|
|
|
const QString lowerSuffix = info.suffix().toLower();
|
|
|
|
const bool isESModule = lowerSuffix == QLatin1String("mjs");
|
|
|
|
const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
|
|
|
|
|
|
|
|
lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/!isJavaScript);
|
|
|
|
QQmlJS::Parser parser(&engine);
|
|
|
|
|
|
|
|
success = isJavaScript ? (isESModule ? parser.parseModule() : parser.parseProgram())
|
|
|
|
: parser.parse();
|
|
|
|
|
2021-12-07 17:48:08 +00:00
|
|
|
if (!success) {
|
2021-11-17 10:25:36 +00:00
|
|
|
const auto diagnosticMessages = parser.diagnosticMessages();
|
|
|
|
for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
|
|
|
|
if (json) {
|
|
|
|
addJsonWarning(m);
|
2021-12-07 17:48:08 +00:00
|
|
|
} else if (!silent) {
|
|
|
|
qWarning().noquote() << QString::fromLatin1("%1:%2:%3: %4")
|
2021-11-17 10:25:36 +00:00
|
|
|
.arg(filename)
|
|
|
|
.arg(m.loc.startLine)
|
2021-12-07 17:48:08 +00:00
|
|
|
.arg(m.loc.startColumn)
|
2021-11-17 10:25:36 +00:00
|
|
|
.arg(m.message);
|
|
|
|
}
|
|
|
|
}
|
2022-02-28 14:51:24 +00:00
|
|
|
return FailedToParse;
|
2021-11-17 10:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (success && !isJavaScript) {
|
2021-12-15 09:25:40 +00:00
|
|
|
const auto processMessages = [&]() {
|
|
|
|
if (json) {
|
|
|
|
for (const auto &error : m_logger->errors())
|
|
|
|
addJsonWarning(error, error.fixSuggestion);
|
|
|
|
for (const auto &warning : m_logger->warnings())
|
|
|
|
addJsonWarning(warning, warning.fixSuggestion);
|
|
|
|
for (const auto &info : m_logger->infos())
|
|
|
|
addJsonWarning(info, info.fixSuggestion);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-17 10:25:36 +00:00
|
|
|
const auto check = [&](QQmlJSResourceFileMapper *mapper) {
|
|
|
|
if (m_importer.importPaths() != qmlImportPaths)
|
|
|
|
m_importer.setImportPaths(qmlImportPaths);
|
|
|
|
|
|
|
|
m_importer.setResourceFileMapper(mapper);
|
|
|
|
|
2021-11-18 13:26:29 +00:00
|
|
|
m_logger.reset(new QQmlJSLogger);
|
|
|
|
m_logger->setFileName(m_useAbsolutePath ? info.absoluteFilePath() : filename);
|
|
|
|
m_logger->setCode(code);
|
|
|
|
m_logger->setSilent(silent || json);
|
2022-03-31 09:26:09 +00:00
|
|
|
QQmlJSScope::Ptr target = QQmlJSScope::create();
|
|
|
|
QQmlJSImportVisitor v { target, &m_importer, m_logger.get(),
|
2021-12-16 16:15:21 +00:00
|
|
|
QQmlJSImportVisitor::implicitImportDirectory(
|
|
|
|
m_logger->fileName(), m_importer.resourceFileMapper()),
|
|
|
|
qmldirFiles };
|
|
|
|
|
|
|
|
parseComments(m_logger.get(), engine.comments());
|
2021-11-17 10:25:36 +00:00
|
|
|
|
|
|
|
for (auto it = options.cbegin(); it != options.cend(); ++it) {
|
2022-01-10 13:52:30 +00:00
|
|
|
if (!it.value().m_changed)
|
|
|
|
continue;
|
|
|
|
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
m_logger->setCategoryIgnored(it.value().m_category, it.value().m_ignored);
|
2021-11-23 14:52:44 +00:00
|
|
|
m_logger->setCategoryLevel(it.value().m_category, it.value().m_level);
|
2021-11-17 10:25:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 13:26:29 +00:00
|
|
|
QQmlJSTypeResolver typeResolver(&m_importer);
|
2021-11-23 15:05:05 +00:00
|
|
|
|
2022-02-03 11:01:35 +00:00
|
|
|
// Type resolving is using document parent mode here so that it produces fewer false
|
|
|
|
// positives on the "parent" property of QQuickItem. It does produce a few false
|
|
|
|
// negatives this way because items can be reparented. Furthermore, even if items are
|
|
|
|
// not reparented, the document parent may indeed not be their visual parent. See
|
|
|
|
// QTBUG-95530. Eventually, we'll need cleverer logic to deal with this.
|
2021-11-18 13:26:29 +00:00
|
|
|
typeResolver.setParentMode(QQmlJSTypeResolver::UseDocumentParent);
|
2022-05-06 12:38:36 +00:00
|
|
|
// We don't need to create tracked types and such as we are just linting the code here
|
|
|
|
// and not actually compiling it. The duplicated scopes would cause issues during
|
|
|
|
// linting.
|
|
|
|
typeResolver.setCloneMode(QQmlJSTypeResolver::DoNotCloneTypes);
|
2021-11-23 15:05:05 +00:00
|
|
|
|
2021-11-18 13:26:29 +00:00
|
|
|
typeResolver.init(&v, parser.rootNode());
|
2022-03-16 12:19:54 +00:00
|
|
|
|
2022-04-27 07:19:54 +00:00
|
|
|
QQmlJSLiteralBindingCheck literalCheck;
|
|
|
|
literalCheck.run(&v, &typeResolver);
|
|
|
|
|
2022-05-02 14:51:01 +00:00
|
|
|
QScopedPointer<QQmlSA::PassManager> passMan;
|
|
|
|
|
2022-04-12 08:05:37 +00:00
|
|
|
if (m_enablePlugins) {
|
2022-05-02 14:51:01 +00:00
|
|
|
passMan.reset(new QQmlSA::PassManager(&v, &typeResolver));
|
2022-04-12 08:05:37 +00:00
|
|
|
|
|
|
|
for (const Plugin &plugin : m_plugins) {
|
2022-04-11 15:48:36 +00:00
|
|
|
if (!plugin.isValid() || !plugin.isEnabled())
|
2022-04-12 08:05:37 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
QQmlSA::LintPlugin *instance = plugin.m_instance;
|
|
|
|
Q_ASSERT(instance);
|
2022-05-02 14:51:01 +00:00
|
|
|
instance->registerPasses(passMan.get(), v.result());
|
2022-04-12 08:05:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 14:51:01 +00:00
|
|
|
passMan->analyze(v.result());
|
2022-04-12 08:05:37 +00:00
|
|
|
}
|
2022-03-16 12:19:54 +00:00
|
|
|
|
2021-12-16 16:15:21 +00:00
|
|
|
success = !m_logger->hasWarnings() && !m_logger->hasErrors();
|
2021-11-17 10:25:36 +00:00
|
|
|
|
2021-12-15 09:25:40 +00:00
|
|
|
if (m_logger->hasErrors()) {
|
|
|
|
processMessages();
|
2021-11-17 10:25:36 +00:00
|
|
|
return;
|
2021-12-15 09:25:40 +00:00
|
|
|
}
|
2021-11-17 10:25:36 +00:00
|
|
|
|
|
|
|
QQmlJSTypeInfo typeInfo;
|
2021-11-18 13:26:29 +00:00
|
|
|
|
|
|
|
const QStringList resourcePaths = mapper
|
|
|
|
? mapper->resourcePaths(QQmlJSResourceFileMapper::localFileFilter(filename))
|
|
|
|
: QStringList();
|
2022-02-03 11:01:35 +00:00
|
|
|
const QString resolvedPath =
|
|
|
|
(resourcePaths.size() == 1) ? u':' + resourcePaths.first() : filename;
|
2021-11-18 13:26:29 +00:00
|
|
|
|
2022-02-03 11:01:35 +00:00
|
|
|
QQmlJSLinterCodegen codegen { &m_importer, resolvedPath, qmldirFiles, m_logger.get(),
|
|
|
|
&typeInfo };
|
2021-11-23 15:05:05 +00:00
|
|
|
codegen.setTypeResolver(std::move(typeResolver));
|
2022-05-02 14:51:01 +00:00
|
|
|
if (passMan)
|
|
|
|
codegen.setPassManager(passMan.get());
|
2021-11-17 10:25:36 +00:00
|
|
|
QQmlJSSaveFunction saveFunction = [](const QV4::CompiledData::SaveableUnitPointer &,
|
|
|
|
const QQmlJSAotFunctionMap &,
|
|
|
|
QString *) { return true; };
|
|
|
|
|
|
|
|
QQmlJSCompileError error;
|
|
|
|
|
2022-03-21 09:21:18 +00:00
|
|
|
QLoggingCategory::setFilterRules(u"qt.qml.compiler=false"_s);
|
2021-11-17 10:25:36 +00:00
|
|
|
|
2021-11-23 14:52:44 +00:00
|
|
|
CodegenWarningInterface interface(m_logger.get());
|
2021-12-13 12:19:33 +00:00
|
|
|
qCompileQmlFile(filename, saveFunction, &codegen, &error, true, &interface,
|
|
|
|
fileContents);
|
2021-11-17 10:25:36 +00:00
|
|
|
|
2022-01-18 12:08:09 +00:00
|
|
|
QList<QQmlJS::DiagnosticMessage> warnings = m_importer.takeGlobalWarnings();
|
|
|
|
|
|
|
|
if (!warnings.isEmpty()) {
|
Redesign QQmlJSLogger internals
High-level goal: be able to reuse existing infrastructure
"as is" to configure semantic analysis categories in tools
(qmllint, qmltc, qmlsc, etc.)
To achieve that, simplify the logging to always "log"
something, without explicitly specifying the severity. The
severity is now baked into the category (and we can extend
those to cover different cases)
One slight deviation is the cache generation which likes
to do its own thing at present. Provide a "forced logging"
option where we can specify which severify we want. The
hope is that this gets removed at some point
Particular list of (noteworthy) changes:
* No more "thresholding" by the level (this is rarely needed
and is actually questionable). Instead, we can ignore a
particular category explicitly
* Category levels are repurposed as category severities
(at least from the high-level picture that always should've
been this way)
* log{Warning,Info,Critical} removed. We use category severity
instead
* "category error" makes zero sense so removed: if our severity
is:
- QtWarningMsg (qmllint), it is already an "error"
- QtCriticalMsg (compilers), it is already an "error"
* Align m_output and m_{infos,warnings,errors} stored information
* Accept the fact that we don't support QtDebugMsg and QtFatalMsg
* Additional categories added to cover for places where the same
category would be both an error and not an error
Task-number: QTBUG-100052
Change-Id: I3cd5d17d58be204f48428877bed053f756ac40a8
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
2022-02-03 12:45:18 +00:00
|
|
|
m_logger->log(QStringLiteral("Type warnings occurred while evaluating file:"),
|
|
|
|
Log_Import, QQmlJS::SourceLocation());
|
|
|
|
m_logger->processMessages(warnings, Log_Import);
|
2022-01-18 12:08:09 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:52:44 +00:00
|
|
|
success &= !m_logger->hasWarnings() && !m_logger->hasErrors();
|
2021-11-17 10:25:36 +00:00
|
|
|
|
2021-12-15 09:25:40 +00:00
|
|
|
processMessages();
|
2021-11-17 10:25:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (resourceFiles.isEmpty()) {
|
|
|
|
check(nullptr);
|
|
|
|
} else {
|
|
|
|
QQmlJSResourceFileMapper mapper(resourceFiles);
|
|
|
|
check(&mapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 14:51:24 +00:00
|
|
|
return success ? LintSuccess : HasWarnings;
|
|
|
|
}
|
|
|
|
|
|
|
|
QQmlJSLinter::FixResult QQmlJSLinter::applyFixes(QString *fixedCode, bool silent)
|
|
|
|
{
|
|
|
|
Q_ASSERT(fixedCode != nullptr);
|
|
|
|
|
|
|
|
// This means that the necessary analysis for applying fixes hasn't run for some reason
|
|
|
|
// (because it was JS file, a syntax error etc.). We can't procede without it and if an error
|
|
|
|
// has occurred that has to be handled by the caller of lintFile(). Just say that there is
|
|
|
|
// nothing to fix.
|
|
|
|
if (m_logger == nullptr)
|
|
|
|
return NothingToFix;
|
|
|
|
|
|
|
|
QString code = m_fileContents;
|
|
|
|
|
|
|
|
QList<FixSuggestion::Fix> fixesToApply;
|
|
|
|
|
|
|
|
QFileInfo info(m_logger->fileName());
|
|
|
|
const QString currentFileAbsolutePath = info.absoluteFilePath();
|
|
|
|
|
|
|
|
const QString lowerSuffix = info.suffix().toLower();
|
|
|
|
const bool isESModule = lowerSuffix == QLatin1String("mjs");
|
|
|
|
const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
|
|
|
|
|
|
|
|
if (isESModule || isJavaScript)
|
|
|
|
return NothingToFix;
|
|
|
|
|
|
|
|
for (const auto &messages : { m_logger->infos(), m_logger->warnings(), m_logger->errors() })
|
|
|
|
for (const Message &msg : messages) {
|
|
|
|
if (!msg.fixSuggestion.has_value())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const auto &fix : msg.fixSuggestion->fixes) {
|
|
|
|
if (fix.isHint)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore fix suggestions for other files
|
|
|
|
if (!fix.fileName.isEmpty()
|
|
|
|
&& QFileInfo(fix.fileName).absoluteFilePath() != currentFileAbsolutePath) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fixesToApply << fix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fixesToApply.isEmpty())
|
|
|
|
return NothingToFix;
|
|
|
|
|
|
|
|
std::sort(fixesToApply.begin(), fixesToApply.end(),
|
|
|
|
[](FixSuggestion::Fix &a, FixSuggestion::Fix &b) {
|
|
|
|
return a.cutLocation.offset < b.cutLocation.offset;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto it = fixesToApply.begin(); it + 1 != fixesToApply.end(); it++) {
|
|
|
|
QQmlJS::SourceLocation srcLocA = it->cutLocation;
|
|
|
|
QQmlJS::SourceLocation srcLocB = (it + 1)->cutLocation;
|
|
|
|
if (srcLocA.offset + srcLocA.length > srcLocB.offset) {
|
|
|
|
if (!silent)
|
|
|
|
qWarning() << "Fixes for two warnings are overlapping, aborting. Please file a bug "
|
|
|
|
"report.";
|
|
|
|
return FixError;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int offsetChange = 0;
|
|
|
|
|
|
|
|
for (const auto &fix : fixesToApply) {
|
|
|
|
qsizetype cutLocation = fix.cutLocation.offset + offsetChange;
|
|
|
|
QString before = code.left(cutLocation);
|
|
|
|
QString after = code.mid(cutLocation + fix.cutLocation.length);
|
|
|
|
|
|
|
|
code = before + fix.replacementString + after;
|
|
|
|
offsetChange += fix.replacementString.length() - fix.cutLocation.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
QQmlJS::Engine engine;
|
|
|
|
QQmlJS::Lexer lexer(&engine);
|
|
|
|
|
|
|
|
lexer.setCode(code, /*lineno = */ 1, /*qmlMode=*/!isJavaScript);
|
|
|
|
QQmlJS::Parser parser(&engine);
|
|
|
|
|
|
|
|
bool success = parser.parse();
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
const auto diagnosticMessages = parser.diagnosticMessages();
|
|
|
|
|
|
|
|
if (!silent) {
|
|
|
|
qDebug() << "File became unparseable after suggestions were applied. Please file a bug "
|
|
|
|
"report.";
|
|
|
|
} else {
|
|
|
|
return FixError;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const QQmlJS::DiagnosticMessage &m : diagnosticMessages) {
|
|
|
|
qWarning().noquote() << QString::fromLatin1("%1:%2:%3: %4")
|
|
|
|
.arg(m_logger->fileName())
|
|
|
|
.arg(m.loc.startLine)
|
|
|
|
.arg(m.loc.startColumn)
|
|
|
|
.arg(m.message);
|
|
|
|
}
|
|
|
|
return FixError;
|
|
|
|
}
|
|
|
|
|
|
|
|
*fixedCode = code;
|
|
|
|
return FixSuccess;
|
2021-11-17 10:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|