Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljslinter_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
5#ifndef QMLJSLINTER_P_H
6#define QMLJSLINTER_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17
19#include <private/qqmljscontextproperties_p.h>
20#include <private/qqmljsimporter_p.h>
21#include <private/qqmljslintertypepropagator_p.h>
22#include <private/qqmljslogger_p.h>
23#include <private/qqmljssourcelocation_p.h>
24#include <private/qqmljstypepropagator_p.h>
25#include <private/qqmljsusercontextproperties_p.h>
26#include <private/qqmltoolingsettings_p.h>
27
28#include <QtCore/qjsonarray.h>
29#include <QtCore/qstring.h>
30#include <QtCore/qmap.h>
31#include <QtCore/qscopedpointer.h>
32
33#include <vector>
34
35QT_BEGIN_NAMESPACE
36
37class QPluginLoader;
38struct QStaticPlugin;
39
40namespace QQmlSA {
41class LintPlugin;
42}
43
45{
46public:
47 QQmlJSLinter(const QStringList &importPaths, const QStringList &extraPluginPaths = {},
48 bool useAbsolutePath = false);
49
52 Silent = 1,
54 };
57
58 class Plugin
59 {
61 public:
62 Plugin() = default;
63 Plugin(Plugin &&plugin) noexcept;
64
65#if QT_CONFIG(library)
67#endif
68 Plugin(const QStaticPlugin &plugin);
69 ~Plugin();
70
71 const QString &name() const { return m_name; }
72 const QString &description() const { return m_description; }
73 const QString &version() const { return m_version; }
74 const QString &author() const { return m_author; }
76 {
77 return m_categories;
78 }
79 bool isValid() const { return m_isValid; }
80 bool isInternal() const
81 {
82 return m_isInternal;
83 }
84
85 bool isEnabled() const
86 {
87 return m_isEnabled;
88 }
89 void setEnabled(bool isEnabled)
90 {
91 m_isEnabled = isEnabled;
92 }
93
94 private:
95 friend class QQmlJSLinter;
96
97 bool parseMetaData(const QJsonObject &metaData, QString pluginName);
98
99 QString m_name;
100 QString m_description;
101 QString m_version;
102 QString m_author;
103
104 QList<QQmlJS::LoggerCategory> m_categories;
105 QQmlSA::LintPlugin *m_instance;
106 std::unique_ptr<QPluginLoader> m_loader;
107 bool m_isInternal =
108 false; // Internal plugins are those developed and maintained inside the Qt project
109 bool m_isValid = false;
110 bool m_isEnabled = true;
111 };
112
113 static std::vector<Plugin> loadPlugins(QStringList paths);
114
124 Result lintFileInBatch(const QString &filename);
125
126 bool prepareFileForBatchLinting(const QString &filename, const QString *fileContents,
127 LintOptions options, const QStringList &qmlImportPaths,
128 const QStringList &qmldirFiles,
129 const QStringList &resourceFiles,
130 const QList<QQmlJS::LoggerCategory> &categories,
131 qsizetype maxWarnings);
132
133 Result lintModule(const QString &uri, LintOptions options, const QStringList &qmlImportPaths,
134 const QStringList &resourceFiles, qsizetype maxWarnings);
135
136 static FixResult applyFixes(const QQmlJSLogger *logger, QString *fixedCode, bool silent);
137
139 {
140 return m_plugins;
141 }
142 void setPlugins(std::vector<Plugin> plugins) { m_plugins = std::move(plugins); }
143
144 void setPluginsEnabled(bool enablePlugins) { m_enablePlugins = enablePlugins; }
145 bool pluginsEnabled() const { return m_enablePlugins; }
146 bool useAbsolutePath() const { return m_useAbsolutePath; }
147
148 void clearCache() { m_importer.clearCache(); }
149
150private:
151 void lintFileImpl(const QString &filename);
152 Result lintModuleImpl(const QString &uri, LintOptions options,
153 const QStringList &qmlImportPaths, const QStringList &resourceFiles,
154 qsizetype maxWarnings);
155 void setupLoggingCategoriesInLogger(QQmlJSLogger *logger,
156 const QList<QQmlJS::LoggerCategory> &categories);
157 void parseComments(QQmlJSLogger *logger, const QList<QQmlJS::SourceLocation> &comments);
158 void updateUserContextProperties(const QString &fileNamej);
159 void updateHeuristicContextProperties(const QString &fileName);
160 void typeReader(const QString &filename);
161
162 bool m_useAbsolutePath;
163 bool m_enablePlugins;
164protected:
166private:
167 QString m_fileContents;
168 std::vector<Plugin> m_plugins;
169
170 struct LintInfo
171 {
172 const QString *fileContents;
173 LintOptions options;
174 QStringList qmlImportPaths;
175 QStringList qmldirFiles;
176 std::optional<QQmlJSResourceFileMapper> resourceMapper;
177 QList<QQmlJS::LoggerCategory> categories;
178
179 QQmlJSScope::Ptr handle;
180 std::optional<QQmlJS::LinterVisitor> visitor;
181 Result result;
182
183 QQmlJS::Engine engine; // needs to outlive the lintFileInBatch() call.
184
185 qsizetype maxWarnings;
186 bool isESModule;
187 bool isJavaScript;
188 };
189
190 std::unordered_map<QString, LintInfo> m_lintInfo;
191
192 QQmlToolingSettings m_userContextPropertySettings =
193 QQmlToolingSettings(QStringLiteral("contextProperties"));
194 QQmlJS::UserContextProperties m_cachedUserContextProperties;
195 QQmlToolingSettings::Searcher m_heuristicContextPropertySearcher =
196 QQmlToolingSettings::Searcher(QStringLiteral(".qt/contextPropertyDump.ini"),
197 QStringLiteral("contextPropertyDump.ini"));
198 QQmlJS::HeuristicContextProperties m_cachedHeuristicContextProperties;
199};
200
201QT_END_NAMESPACE
202
203#endif // QMLJSLINTER_P_H
void reportParserWarnings(const QString &, QQmlJS::SourceLocation, const QString &) override
void reportVarUsedBeforeDeclaration(const QString &name, const QString &fileName, QQmlJS::SourceLocation declarationLocation, QQmlJS::SourceLocation accessLocation) override
void reportFunctionUsedBeforeDeclaration(const QString &name, const QString &fileName, QQmlJS::SourceLocation declarationLocation, QQmlJS::SourceLocation accessLocation) override
UnreachableVisitor * unreachableVisitor() override
CodegenWarningInterface(QQmlJSLogger *logger)
bool visit(QQmlJS::AST::FunctionDeclaration *functionDeclaration) override
void throwRecursionDepthError() override
void setPassManager(QQmlSA::PassManager *passManager)
const QList< QQmlJS::LoggerCategory > categories() const
bool isInternal() const
Plugin(Plugin &&plugin) noexcept
Plugin(const QStaticPlugin &plugin)
const QString & name() const
const QString & version() const
const QString & author() const
void setEnabled(bool isEnabled)
const QString & description() const
void setPluginsEnabled(bool enablePlugins)
bool prepareFileForBatchLinting(const QString &filename, const QString *fileContents, LintOptions options, const QStringList &qmlImportPaths, const QStringList &qmldirFiles, const QStringList &resourceFiles, const QList< QQmlJS::LoggerCategory > &categories, qsizetype maxWarnings)
bool useAbsolutePath() const
Result lintModule(const QString &uri, LintOptions options, const QStringList &qmlImportPaths, const QStringList &resourceFiles, qsizetype maxWarnings)
Result lintFileInBatch(const QString &filename)
static std::vector< Plugin > loadPlugins(QStringList paths)
QQmlJSLinter(const QStringList &importPaths, const QStringList &extraPluginPaths={}, bool useAbsolutePath=false)
std::vector< Plugin > & plugins()
void setPlugins(std::vector< Plugin > plugins)
QQmlJSImporter m_importer
Q_DECLARE_FLAGS(LintOptions, LintOption)
static FixResult applyFixes(const QQmlJSLogger *logger, QString *fixedCode, bool silent)
bool pluginsEnabled() const
UnreachableVisitor(QQmlJSLogger *logger)
void throwRecursionDepthError() override
bool containsFunctionDeclaration(QQmlJS::AST::Node *node)
bool visit(QQmlJS::AST::StatementList *unreachable) override
\inmodule QtQmlCompiler
Combined button and popup list for selecting options.
static void checkMaxWarnings(QQmlJSLogger *logger, qsizetype maxWarnings)
static void addJsonWarning(QJsonArray &warnings, const QQmlJS::DiagnosticMessage &message, QAnyStringView id, const std::optional< QQmlJSFixSuggestion > &suggestion={})
static void processMessages(const QQmlJSLogger &logger, QJsonArray &warnings)
std::unique_ptr< QQmlJSLogger > logger