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
132 Result lintModule(const QString &uri, LintOptions options, const QStringList &qmlImportPaths,
133 const QStringList &resourceFiles);
134
135 static FixResult applyFixes(const QQmlJSLogger *logger, QString *fixedCode, bool silent);
136
138 {
139 return m_plugins;
140 }
141 void setPlugins(std::vector<Plugin> plugins) { m_plugins = std::move(plugins); }
142
143 void setPluginsEnabled(bool enablePlugins) { m_enablePlugins = enablePlugins; }
144 bool pluginsEnabled() const { return m_enablePlugins; }
145 bool useAbsolutePath() const { return m_useAbsolutePath; }
146
147 void clearCache() { m_importer.clearCache(); }
148
149private:
150 void lintFileImpl(const QString &filename);
151 Result lintModuleImpl(const QString &uri, LintOptions options,
152 const QStringList &qmlImportPaths, const QStringList &resourceFiles);
153 void setupLoggingCategoriesInLogger(QQmlJSLogger *logger,
154 const QList<QQmlJS::LoggerCategory> &categories);
155 void parseComments(QQmlJSLogger *logger, const QList<QQmlJS::SourceLocation> &comments);
156 void updateUserContextProperties(const QString &fileNamej);
157 void updateHeuristicContextProperties(const QString &fileName);
158 void typeReader(const QString &filename);
159
160 bool m_useAbsolutePath;
161 bool m_enablePlugins;
162protected:
164private:
165 QString m_fileContents;
166 std::vector<Plugin> m_plugins;
167
168 struct LintInfo
169 {
170 const QString *fileContents;
171 LintOptions options;
172 QStringList qmlImportPaths;
173 QStringList qmldirFiles;
174 std::optional<QQmlJSResourceFileMapper> resourceMapper;
175 QList<QQmlJS::LoggerCategory> categories;
176
177 QQmlJSScope::Ptr handle;
178 std::optional<QQmlJS::LinterVisitor> visitor;
179 Result result;
180
181 QQmlJS::Engine engine; // needs to outlive the lintFileInBatch() call.
182 };
183
184 std::unordered_map<QString, LintInfo> m_lintInfo;
185
186 QQmlToolingSettings m_userContextPropertySettings =
187 QQmlToolingSettings(QStringLiteral("contextProperties"));
188 QQmlJS::UserContextProperties m_cachedUserContextProperties;
189 QQmlToolingSettings::Searcher m_heuristicContextPropertySearcher =
190 QQmlToolingSettings::Searcher(QStringLiteral(".qt/contextPropertyDump.ini"),
191 QStringLiteral("contextPropertyDump.ini"));
192 QQmlJS::HeuristicContextProperties m_cachedHeuristicContextProperties;
193};
194
195QT_END_NAMESPACE
196
197#endif // QMLJSLINTER_P_H
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
Result lintModule(const QString &uri, LintOptions options, const QStringList &qmlImportPaths, const QStringList &resourceFiles)
void setPluginsEnabled(bool enablePlugins)
bool useAbsolutePath() const
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)
bool prepareFileForBatchLinting(const QString &filename, const QString *fileContents, LintOptions options, const QStringList &qmlImportPaths, const QStringList &qmldirFiles, const QStringList &resourceFiles, const QList< QQmlJS::LoggerCategory > &categories)
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 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