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
qqmljslintervisitor_p.h
Go to the documentation of this file.
1// Copyright (C) 2025 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 QQMLJSLINTERVISITOR_P_H
6#define QQMLJSLINTERVISITOR_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
18#include <private/qqmljsimportvisitor_p.h>
19
20#include <private/qqmljsengine_p.h>
21
22QT_BEGIN_NAMESPACE
23
24namespace QQmlJS {
25
26/*!
27 \internal
28 Extends QQmlJSImportVisitor with extra warnings that are required for linting but unrelated to
29 QQmlJSImportVisitor actual task that is constructing QQmlJSScopes. One example of such warnings
30 are purely syntactic checks, or warnings that don't affect compilation.
31 */
32class LinterVisitor final : public QQmlJSImportVisitor
33{
34public:
35 LinterVisitor(QQmlJSImporter *importer, QQmlJSLogger *logger,
36 const QString &implicitImportDirectory,
37 const QStringList &qmldirFiles = QStringList(), QQmlJS::Engine *engine = nullptr);
38
39protected:
40 using QQmlJSImportVisitor::endVisit;
41 using QQmlJSImportVisitor::visit;
42
43 bool preVisit(QQmlJS::AST::Node *) override;
44 void postVisit(QQmlJS::AST::Node *) override;
46
48
49 bool visit(QQmlJS::AST::StringLiteral *) override;
50 bool visit(AST::CommaExpression *) override;
51 bool visit(QQmlJS::AST::NewMemberExpression *) override;
52 bool visit(QQmlJS::AST::VoidExpression *ast) override;
53 bool visit(QQmlJS::AST::BinaryExpression *) override;
54 bool visit(QQmlJS::AST::UiImport *import) override;
55 bool visit(QQmlJS::AST::UiEnumDeclaration *uied) override;
56 bool visit(QQmlJS::AST::CaseBlock *) override;
57 bool visit(QQmlJS::AST::ExpressionStatement *ast) override;
58 bool visit(QQmlJS::AST::FunctionDeclaration *fdecl) override;
59 bool visit(QQmlJS::AST::FunctionExpression *fexpr) override;
60 bool visit(QQmlJS::AST::UiPublicMember *publicMember) override;
61
62 bool safeInsertJSIdentifier(QQmlJSScope::Ptr &scope, const QString &name,
63 const QQmlJSScope::JavaScriptIdentifier &identifier) override;
64
65private:
66 struct SeenImport
67 {
68 QStringView filename;
69 QString uri;
70 QTypeRevision version;
71 QStringView id;
72 QQmlJS::AST::UiImport *uiImport;
73
74 SeenImport(QQmlJS::AST::UiImport *i) : filename(i->fileName), id(i->importId), uiImport(i)
75 {
76 if (i->importUri)
77 uri = i->importUri->toString();
78 if (i->version)
79 version = i->version->version;
80 }
81 friend bool comparesEqual(const SeenImport &lhs, const SeenImport &rhs) noexcept
82 {
83 return lhs.filename == rhs.filename && lhs.uri == rhs.uri
84 && lhs.version == rhs.version && lhs.id == rhs.id;
85 }
86 Q_DECLARE_EQUALITY_COMPARABLE(SeenImport)
87
88 friend size_t qHash(const SeenImport &i, size_t seed = 0)
89 {
90 return qHashMulti(seed, i.filename, i.uri, i.version, i.id);
91 }
92 };
93 QQmlJS::Engine *m_engine = nullptr;
94 QSet<SeenImport> m_seenImports;
95 QSet<std::pair<const QQmlJSScope *, QString>> misplacedJSIdentifiers;
96 std::vector<QQmlJS::AST::Node *> m_ancestryIncludingCurrentNode;
97
98 void handleDuplicateEnums(QQmlJS::AST::UiEnumMemberList *members, QStringView key,
99 const QQmlJS::SourceLocation &location);
100 void warnCaseNoFlowControl(QQmlJS::SourceLocation caseToken) const;
101 void checkCaseFallthrough(QQmlJS::AST::StatementList *statements, SourceLocation errorLoc,
102 SourceLocation nextLoc);
104 const QString &name, const QQmlJS::AST::Statement *statement,
105 const QQmlJS::AST::UiPublicMember *associatedPropertyDefinition = nullptr) override;
106 void handleLiteralBinding(const QQmlJSMetaPropertyBinding &binding,
107 const AST::UiPublicMember *associatedPropertyDefinition) override;
108 void handleUselessExpressionStatement(const AST::ExpressionStatement *ast);
109};
110
111} // namespace QQmlJS
112
113QT_END_NAMESPACE
114
115#endif // QQMLJSLINTERVISITOR_P_H
void postVisit(QQmlJS::AST::Node *) override
void handleLiteralBinding(const QQmlJSMetaPropertyBinding &binding, const AST::UiPublicMember *associatedPropertyDefinition) override
LinterVisitor(QQmlJSImporter *importer, QQmlJSLogger *logger, const QString &implicitImportDirectory, const QStringList &qmldirFiles=QStringList(), QQmlJS::Engine *engine=nullptr)
BindingExpressionParseResult parseBindingExpression(const QString &name, const QQmlJS::AST::Statement *statement, const QQmlJS::AST::UiPublicMember *associatedPropertyDefinition=nullptr) override
bool safeInsertJSIdentifier(QQmlJSScope::Ptr &scope, const QString &name, const QQmlJSScope::JavaScriptIdentifier &identifier) override
bool visit(QQmlJS::AST::StringLiteral *) override
QQmlJS::AST::Node * astParentOfVisitedNode() const
bool preVisit(QQmlJS::AST::Node *) override
static void warnForMethodShadowingInBase(const QQmlJSScope::ConstPtr &base, const QString &name, const QQmlJS::SourceLocation &location, QQmlJSLogger *logger)
static bool isUselessExpressionStatement_impl(const ExpressionNode *ast)
static void warnForDuplicates(const QQmlJSScope::ConstPtr &scope, const QString &name, QLatin1String type, const QQmlJS::SourceLocation &location, OverrideInformations overrideFlags, QQmlJSLogger *logger)
static constexpr QLatin1String s_method
static void warnAboutLiteralConstructors(NewMemberExpression *expression, QQmlJSLogger *logger)
static SourceLocation confusingPluses(BinaryExpression *exp)
static bool isUselessExpressionStatement(const ExpressionStatement *ast)
static bool allCodePathsReturnInsideCase(Node *statement)
Q_DECLARE_FLAGS(OverrideInformations, OverrideInformation)
static SourceLocation confusingMinuses(BinaryExpression *exp)
static QList< const Statement * > possibleLastStatements(const StatementList *ast)
static constexpr QLatin1String s_property
static constexpr QLatin1String s_signal
static void warnForPropertyShadowingInBase(const QQmlJSScope::ConstPtr &base, const QString &name, const QQmlJS::SourceLocation &location, OverrideInformations overrideFlags, QQmlJSLogger *logger)
Combined button and popup list for selecting options.