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 void endVisit(QQmlJS::AST::UiProgram *ast) override;
63
64 bool safeInsertJSIdentifier(QQmlJSScope::Ptr &scope, const QString &name,
65 const QQmlJSScope::JavaScriptIdentifier &identifier) override;
66
67private:
68 struct SeenImport
69 {
70 QStringView filename;
71 QString uri;
72 QTypeRevision version;
73 QStringView id;
74 QQmlJS::AST::UiImport *uiImport;
75
76 SeenImport(QQmlJS::AST::UiImport *i) : filename(i->fileName), id(i->importId), uiImport(i)
77 {
78 if (i->importUri)
79 uri = i->importUri->toString();
80 if (i->version)
81 version = i->version->version;
82 }
83 friend bool comparesEqual(const SeenImport &lhs, const SeenImport &rhs) noexcept
84 {
85 return lhs.filename == rhs.filename && lhs.uri == rhs.uri
86 && lhs.version == rhs.version && lhs.id == rhs.id;
87 }
88 Q_DECLARE_EQUALITY_COMPARABLE(SeenImport)
89
90 friend size_t qHash(const SeenImport &i, size_t seed = 0)
91 {
92 return qHashMulti(seed, i.filename, i.uri, i.version, i.id);
93 }
94 };
95 QQmlJS::Engine *m_engine = nullptr;
96 QSet<SeenImport> m_seenImports;
97 QSet<std::pair<const QQmlJSScope *, QString>> misplacedJSIdentifiers;
98 std::vector<QQmlJS::AST::Node *> m_ancestryIncludingCurrentNode;
99
100 void handleDuplicateEnums(QQmlJS::AST::UiEnumMemberList *members, QStringView key,
101 const QQmlJS::SourceLocation &location);
102 void warnCaseNoFlowControl(QQmlJS::SourceLocation caseToken) const;
103 void checkCaseFallthrough(QQmlJS::AST::StatementList *statements, SourceLocation errorLoc,
104 SourceLocation nextLoc);
106 const QString &name, const QQmlJS::AST::Statement *statement,
107 const QQmlJS::AST::UiPublicMember *associatedPropertyDefinition = nullptr) override;
108 void handleLiteralBinding(const QQmlJSMetaPropertyBinding &binding,
109 const AST::UiPublicMember *associatedPropertyDefinition) override;
110
111 void checkIdShadows();
112};
113
114} // namespace QQmlJS
115
116QT_END_NAMESPACE
117
118#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
void endVisit(QQmlJS::AST::UiProgram *ast) 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(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 allCodePathsReturnInsideCase(Node *statement)
Q_DECLARE_FLAGS(OverrideInformations, OverrideInformation)
static SourceLocation confusingMinuses(BinaryExpression *exp)
void warnForShadowsInCurrentScope(const QQmlJSScope::ConstPtr &scopeWithId, const QString &name, const QQmlJSScope::ConstPtr &currentScope, const QQmlJS::SourceLocation &location, MethodOrProperty mode, QQmlJSLogger *logger)
static constexpr QLatin1String s_property
static constexpr QLatin1String s_signal
static bool canHaveUselessExpressionStatement(Node *parent)
static void warnForPropertyShadowingInBase(const QQmlJSScope::ConstPtr &base, const QString &name, const QQmlJS::SourceLocation &location, OverrideInformations overrideFlags, QQmlJSLogger *logger)
\inmodule QtQmlCompiler
MethodType
Definition qqmlsa.h:48
QQmlSA::MethodType QQmlJSMetaMethodType