diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-09-30 14:44:24 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-02 15:33:10 +0200 |
commit | 7957752447fbd2581521b13156ef29ffc80ad7bf (patch) | |
tree | 05a669e525b4eadc706fff3cbd40f6da9602f23d /tools/shared | |
parent | b078890608344f0556a0ff50aab62ede83e0e180 (diff) |
qmllint: Unify injected and "normal" JavaScript identifiers
The specifics of how to warn about the injected identifiers are moved
out of ScopeTree as that is not related to the structure of the scopes.
Change-Id: I26418c3fa492da8339abf045a4034a8464b7bbb8
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools/shared')
-rw-r--r-- | tools/shared/scopetree.cpp | 38 | ||||
-rw-r--r-- | tools/shared/scopetree.h | 31 |
2 files changed, 39 insertions, 30 deletions
diff --git a/tools/shared/scopetree.cpp b/tools/shared/scopetree.cpp index e7ce631b1d..933f3f88d7 100644 --- a/tools/shared/scopetree.cpp +++ b/tools/shared/scopetree.cpp @@ -49,28 +49,21 @@ ScopeTree::Ptr ScopeTree::create(ScopeType type, const ScopeTree::Ptr &parentSco return childScope; } -void ScopeTree::insertJSIdentifier(const QString &id, ScopeType scope) +void ScopeTree::insertJSIdentifier(const QString &name, const JavaScriptIdentifier &identifier) { Q_ASSERT(m_scopeType != ScopeType::QMLScope); - Q_ASSERT(scope != ScopeType::QMLScope); - if (scope != ScopeType::JSFunctionScope || m_scopeType == ScopeType::JSFunctionScope) { - m_jsIdentifiers.insert(id); + if (identifier.kind == JavaScriptIdentifier::LexicalScoped + || identifier.kind == JavaScriptIdentifier::Injected + || m_scopeType == ScopeType::JSFunctionScope) { + m_jsIdentifiers.insert(name, identifier); } else { auto targetScope = parentScope(); while (targetScope->m_scopeType != ScopeType::JSFunctionScope) targetScope = targetScope->parentScope(); - targetScope->m_jsIdentifiers.insert(id); + targetScope->m_jsIdentifiers.insert(name, identifier); } } -void ScopeTree::insertSignalIdentifier(const QString &id, const MetaMethod &method, - const QQmlJS::SourceLocation &loc, - bool hasMultilineHandlerBody) -{ - Q_ASSERT(m_scopeType == ScopeType::QMLScope); - m_injectedSignalIdentifiers.insert(id, {method, loc, hasMultilineHandlerBody}); -} - void ScopeTree::insertPropertyIdentifier(const MetaProperty &property) { addProperty(property); @@ -127,9 +120,22 @@ bool ScopeTree::isIdInCurrentJSScopes(const QString &id) const bool ScopeTree::isIdInjectedFromSignal(const QString &id) const { - if (m_scopeType == ScopeType::QMLScope) - return m_injectedSignalIdentifiers.contains(id); - return findCurrentQMLScope(parentScope())->m_injectedSignalIdentifiers.contains(id); + const auto found = findJSIdentifier(id); + return found.has_value() && found->kind == JavaScriptIdentifier::Injected; +} + +std::optional<JavaScriptIdentifier> ScopeTree::findJSIdentifier(const QString &id) const +{ + for (const auto *scope = this; scope; scope = scope->parentScope().data()) { + if (scope->m_scopeType == ScopeType::JSFunctionScope + || scope->m_scopeType == ScopeType::JSLexicalScope) { + auto it = scope->m_jsIdentifiers.find(id); + if (it != scope->m_jsIdentifiers.end()) + return *it; + } + } + + return std::optional<JavaScriptIdentifier>{}; } void ScopeTree::resolveTypes(const QHash<QString, ScopeTree::ConstPtr> &contextualTypes) diff --git a/tools/shared/scopetree.h b/tools/shared/scopetree.h index 37eee26d16..b751815172 100644 --- a/tools/shared/scopetree.h +++ b/tools/shared/scopetree.h @@ -48,6 +48,8 @@ #include <QtCore/qstring.h> #include <QtCore/qversionnumber.h> +#include <optional> + enum class ScopeType { JSFunctionScope, @@ -55,11 +57,17 @@ enum class ScopeType QMLScope }; -struct MethodUsage +struct JavaScriptIdentifier { - MetaMethod method; - QQmlJS::SourceLocation loc; - bool hasMultilineHandlerBody; + enum Kind { + Parameter, + FunctionScoped, + LexicalScoped, + Injected + }; + + Kind kind = FunctionScoped; + QQmlJS::SourceLocation location; }; class ScopeTree @@ -115,9 +123,8 @@ public: ScopeTree::Ptr parentScope() const { return m_parentScope.toStrongRef(); } - void insertJSIdentifier(const QString &id, ScopeType scope); - void insertSignalIdentifier(const QString &id, const MetaMethod &method, - const QQmlJS::SourceLocation &loc, bool hasMultilineHandlerBody); + void insertJSIdentifier(const QString &name, const JavaScriptIdentifier &identifier); + // inserts property as qml identifier as well as the corresponding void insertPropertyIdentifier(const MetaProperty &prop); void addUnmatchedSignalHandler(const QString &handler, @@ -196,23 +203,19 @@ public: bool isIdInCurrentJSScopes(const QString &id) const; bool isIdInjectedFromSignal(const QString &id) const; + std::optional<JavaScriptIdentifier> findJSIdentifier(const QString &id) const; + QVector<ScopeTree::Ptr> childScopes() const { return m_childScopes; } - QMultiHash<QString, MethodUsage> injectedSignalIdentifiers() const - { - return m_injectedSignalIdentifiers; - } - void resolveTypes(const QHash<QString, ConstPtr> &contextualTypes); private: ScopeTree(ScopeType type, const ScopeTree::Ptr &parentScope = ScopeTree::Ptr()); - QSet<QString> m_jsIdentifiers; - QMultiHash<QString, MethodUsage> m_injectedSignalIdentifiers; + QHash<QString, JavaScriptIdentifier> m_jsIdentifiers; QMultiHash<QString, MetaMethod> m_methods; QHash<QString, MetaProperty> m_properties; |