2019-06-14 12:21:25 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** This file is part of the tools applications of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "findunqualified.h"
|
|
|
|
#include "scopetree.h"
|
2019-11-11 17:18:04 +00:00
|
|
|
#include "typedescriptionreader.h"
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtQml/private/qqmljsast_p.h>
|
|
|
|
#include <QtQml/private/qqmljslexer_p.h>
|
|
|
|
#include <QtQml/private/qqmljsparser_p.h>
|
|
|
|
#include <QtQml/private/qv4codegen_p.h>
|
|
|
|
#include <QtQml/private/qqmldirparser_p.h>
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
#include <QtCore/qfile.h>
|
|
|
|
#include <QtCore/qdiriterator.h>
|
|
|
|
#include <QtCore/qscopedvaluerollback.h>
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
static const QString prefixedName(const QString &prefix, const QString &name)
|
|
|
|
{
|
|
|
|
return prefix.isEmpty() ? name : (prefix + QLatin1Char('.') + name);
|
|
|
|
}
|
|
|
|
|
2019-09-13 09:34:08 +00:00
|
|
|
static QQmlDirParser createQmldirParserForFile(const QString &filename)
|
|
|
|
{
|
|
|
|
QFile f(filename);
|
|
|
|
f.open(QFile::ReadOnly);
|
|
|
|
QQmlDirParser parser;
|
|
|
|
parser.parse(f.readAll());
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
static TypeDescriptionReader createQmltypesReaderForFile(const QString &filename)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
QFile f(filename);
|
|
|
|
f.open(QFile::ReadOnly);
|
2019-11-11 17:18:04 +00:00
|
|
|
TypeDescriptionReader reader { filename, f.readAll() };
|
2019-06-14 12:21:25 +00:00
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::enterEnvironment(ScopeType type, QString name)
|
|
|
|
{
|
2019-11-11 17:18:04 +00:00
|
|
|
m_currentScope = m_currentScope->createNewChildScope(type, std::move(name));
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::leaveEnvironment()
|
|
|
|
{
|
|
|
|
m_currentScope = m_currentScope->parentScope();
|
|
|
|
}
|
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
void FindUnqualifiedIDVisitor::parseHeaders(QQmlJS::AST::UiHeaderItemList *header)
|
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
|
|
|
|
while (header) {
|
|
|
|
if (auto import = cast<UiImport *>(header->headerItem)) {
|
|
|
|
if (import->version) {
|
|
|
|
QString path;
|
|
|
|
auto uri = import->importUri;
|
|
|
|
while (uri) {
|
|
|
|
path.append(uri->name);
|
|
|
|
path.append("/");
|
|
|
|
uri = uri->next;
|
|
|
|
}
|
|
|
|
path.chop(1);
|
|
|
|
QString prefix = QLatin1String("");
|
|
|
|
if (import->asToken.isValid()) {
|
|
|
|
prefix += import->importId + QLatin1Char('.');
|
|
|
|
}
|
|
|
|
importHelper(path, prefix, import->version->majorVersion,
|
|
|
|
import->version->minorVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header = header->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::parseMembers(QQmlJS::AST::UiObjectMemberList *member,
|
|
|
|
ScopeTree *scope)
|
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
|
|
|
|
// member should be the sole element
|
|
|
|
Q_ASSERT(!member->next);
|
|
|
|
Q_ASSERT(member && member->member->kind == UiObjectMember::Kind_UiObjectDefinition);
|
|
|
|
auto definition = cast<UiObjectDefinition *>(member->member);
|
|
|
|
auto qualifiedId = definition->qualifiedTypeNameId;
|
|
|
|
while (qualifiedId && qualifiedId->next) {
|
|
|
|
qualifiedId = qualifiedId->next;
|
|
|
|
}
|
|
|
|
scope->setSuperclassName(qualifiedId->name.toString());
|
|
|
|
UiObjectMemberList *initMembers = definition->initializer->members;
|
|
|
|
while (initMembers) {
|
|
|
|
switch (initMembers->member->kind) {
|
|
|
|
case UiObjectMember::Kind_UiArrayBinding: {
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UiObjectMember::Kind_UiEnumDeclaration: {
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UiObjectMember::Kind_UiObjectBinding: {
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UiObjectMember::Kind_UiObjectDefinition: {
|
|
|
|
// creates nothing accessible
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UiObjectMember::Kind_UiPublicMember: {
|
|
|
|
auto publicMember = cast<UiPublicMember *>(initMembers->member);
|
|
|
|
switch (publicMember->type) {
|
|
|
|
case UiPublicMember::Signal: {
|
|
|
|
UiParameterList *param = publicMember->parameters;
|
|
|
|
MetaMethod method;
|
|
|
|
method.setMethodType(MetaMethod::Signal);
|
|
|
|
method.setMethodName(publicMember->name.toString());
|
|
|
|
while (param) {
|
|
|
|
method.addParameter(param->name.toString(), param->type->name.toString());
|
|
|
|
param = param->next;
|
|
|
|
}
|
|
|
|
scope->addMethod(method);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UiPublicMember::Property: {
|
|
|
|
MetaProperty prop {
|
|
|
|
publicMember->name.toString(),
|
|
|
|
publicMember->typeModifier.toString(),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
scope->addProperty(prop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UiObjectMember::Kind_UiScriptBinding: {
|
|
|
|
// does not create anything new, ignore
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UiObjectMember::Kind_UiSourceElement: {
|
|
|
|
auto sourceElement = cast<UiSourceElement *>(initMembers->member);
|
|
|
|
if (FunctionExpression *fexpr = sourceElement->sourceElement->asFunctionDefinition()) {
|
|
|
|
MetaMethod method;
|
|
|
|
method.setMethodName(fexpr->name.toString());
|
|
|
|
method.setMethodType(MetaMethod::Method);
|
|
|
|
FormalParameterList *parameters = fexpr->formals;
|
|
|
|
while (parameters) {
|
|
|
|
method.addParameter(parameters->element->bindingIdentifier.toString(), "");
|
|
|
|
parameters = parameters->next;
|
|
|
|
}
|
|
|
|
scope->addMethod(method);
|
|
|
|
} else if (ClassExpression *clexpr =
|
|
|
|
sourceElement->sourceElement->asClassDefinition()) {
|
|
|
|
const MetaProperty prop { clexpr->name.toString(), "", false, false, false, 1 };
|
|
|
|
scope->addProperty(prop);
|
|
|
|
} else if (cast<VariableStatement *>(sourceElement->sourceElement)) {
|
|
|
|
// nothing to do
|
|
|
|
} else {
|
|
|
|
const auto loc = sourceElement->firstSourceLocation();
|
|
|
|
m_colorOut.writeUncolored(
|
|
|
|
"unsupportedd sourceElement at "
|
|
|
|
+ QString::fromLatin1("%1:%2: ").arg(loc.startLine).arg(loc.startColumn)
|
|
|
|
+ QString::number(sourceElement->sourceElement->kind));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
m_colorOut.writeUncolored("unsupported element of kind "
|
|
|
|
+ QString::number(initMembers->member->kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initMembers = initMembers->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::parseProgram(QQmlJS::AST::Program *program, ScopeTree *scope)
|
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
for (auto *statement = program->statements; statement; statement = statement->next) {
|
|
|
|
if (auto *function = cast<FunctionDeclaration *>(statement->statement)) {
|
|
|
|
MetaMethod method(function->name.toString());
|
|
|
|
method.setMethodType(MetaMethod::Method);
|
|
|
|
for (auto *parameters = function->formals; parameters; parameters = parameters->next)
|
|
|
|
method.addParameter(parameters->element->bindingIdentifier.toString(), "");
|
|
|
|
scope->addMethod(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 09:34:08 +00:00
|
|
|
enum ImportVersion { FullyVersioned, PartiallyVersioned, Unversioned, BasePath };
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
QStringList completeImportPaths(const QString &uri, const QString &basePath, int vmaj, int vmin)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
static const QLatin1Char Slash('/');
|
|
|
|
static const QLatin1Char Backslash('\\');
|
|
|
|
|
|
|
|
const QVector<QStringRef> parts = uri.splitRef(QLatin1Char('.'), QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
QStringList qmlDirPathsPaths;
|
|
|
|
// fully & partially versioned parts + 1 unversioned for each base path
|
2019-11-14 16:08:15 +00:00
|
|
|
qmlDirPathsPaths.reserve(2 * parts.count() + 1);
|
2019-06-14 12:21:25 +00:00
|
|
|
|
|
|
|
auto versionString = [](int vmaj, int vmin, ImportVersion version)
|
|
|
|
{
|
|
|
|
if (version == FullyVersioned) {
|
|
|
|
// extension with fully encoded version number (eg. MyModule.3.2)
|
2019-11-11 17:18:04 +00:00
|
|
|
return QString::fromLatin1(".%1.%2").arg(vmaj).arg(vmin);
|
|
|
|
}
|
|
|
|
if (version == PartiallyVersioned) {
|
2019-06-14 12:21:25 +00:00
|
|
|
// extension with encoded version major (eg. MyModule.3)
|
2019-11-11 17:18:04 +00:00
|
|
|
return QString::fromLatin1(".%1").arg(vmaj);
|
|
|
|
}
|
|
|
|
// else extension without version number (eg. MyModule)
|
2019-06-14 12:21:25 +00:00
|
|
|
return QString();
|
|
|
|
};
|
2019-11-11 17:18:04 +00:00
|
|
|
auto joinStringRefs = [](const QVector<QStringRef> &refs, const QChar &sep) {
|
2019-06-14 12:21:25 +00:00
|
|
|
QString str;
|
|
|
|
for (auto it = refs.cbegin(); it != refs.cend(); ++it) {
|
|
|
|
if (it != refs.cbegin())
|
|
|
|
str += sep;
|
|
|
|
str += *it;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2019-12-11 14:31:55 +00:00
|
|
|
const ImportVersion initial = (vmin >= 0)
|
|
|
|
? FullyVersioned
|
|
|
|
: (vmaj >= 0 ? PartiallyVersioned : Unversioned);
|
|
|
|
for (int version = initial; version <= BasePath; ++version) {
|
2019-06-14 12:21:25 +00:00
|
|
|
const QString ver = versionString(vmaj, vmin, static_cast<ImportVersion>(version));
|
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
QString dir = basePath;
|
|
|
|
if (!dir.endsWith(Slash) && !dir.endsWith(Backslash))
|
|
|
|
dir += Slash;
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
if (version == BasePath) {
|
|
|
|
qmlDirPathsPaths += dir;
|
|
|
|
} else {
|
|
|
|
// append to the end
|
|
|
|
qmlDirPathsPaths += dir + joinStringRefs(parts, Slash) + ver;
|
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
if (version < Unversioned) {
|
|
|
|
// insert in the middle
|
|
|
|
for (int index = parts.count() - 2; index >= 0; --index) {
|
|
|
|
qmlDirPathsPaths += dir + joinStringRefs(parts.mid(0, index + 1), Slash)
|
|
|
|
+ ver + Slash
|
|
|
|
+ joinStringRefs(parts.mid(index + 1), Slash);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return qmlDirPathsPaths;
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
static const QLatin1String SlashQmldir = QLatin1String("/qmldir");
|
|
|
|
static const QLatin1String SlashAppDotQmltypes = QLatin1String("/app.qmltypes");
|
|
|
|
static const QLatin1String SlashLibDotQmltypes = QLatin1String("/lib.qmltypes");
|
|
|
|
static const QLatin1String SlashPluginsDotQmltypes = QLatin1String("/plugins.qmltypes");
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
void FindUnqualifiedIDVisitor::readQmltypes(const QString &filename,
|
|
|
|
FindUnqualifiedIDVisitor::Import &result)
|
|
|
|
{
|
|
|
|
auto reader = createQmltypesReaderForFile(filename);
|
|
|
|
auto succ = reader(&result.objects, &result.moduleApis, &result.dependencies);
|
|
|
|
if (!succ)
|
|
|
|
m_colorOut.writeUncolored(reader.errorMessage());
|
|
|
|
}
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2019-11-14 16:08:15 +00:00
|
|
|
FindUnqualifiedIDVisitor::Import FindUnqualifiedIDVisitor::readQmldir(const QString &path)
|
|
|
|
{
|
|
|
|
Import result;
|
|
|
|
auto reader = createQmldirParserForFile(path + SlashQmldir);
|
|
|
|
const auto imports = reader.imports();
|
|
|
|
for (const QString &import : imports)
|
|
|
|
result.dependencies.append(import);
|
|
|
|
|
|
|
|
QHash<QString, ScopeTree *> qmlComponents;
|
|
|
|
const auto components = reader.components();
|
|
|
|
for (auto it = components.begin(), end = components.end(); it != end; ++it) {
|
|
|
|
const QString filePath = path + QLatin1Char('/') + it->fileName;
|
|
|
|
if (!QFile::exists(filePath)) {
|
|
|
|
m_colorOut.write(QLatin1String("warning: "), Warning);
|
|
|
|
m_colorOut.write(it->fileName + QLatin1String(" is listed as component in ")
|
|
|
|
+ path + SlashQmldir
|
|
|
|
+ QLatin1String(" but does not exist.\n"));
|
|
|
|
continue;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
|
|
|
|
auto mo = qmlComponents.find(it.key());
|
|
|
|
if (mo == qmlComponents.end())
|
2019-11-08 17:43:31 +00:00
|
|
|
mo = qmlComponents.insert(it.key(), localFile2ScopeTree(filePath));
|
2019-11-14 16:08:15 +00:00
|
|
|
|
|
|
|
(*mo)->addExport(
|
|
|
|
it.key(), reader.typeNamespace(),
|
|
|
|
ComponentVersion(it->majorVersion, it->minorVersion));
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
for (auto it = qmlComponents.begin(), end = qmlComponents.end(); it != end; ++it)
|
|
|
|
result.objects.insert( it.key(), ScopeTree::ConstPtr(it.value()));
|
|
|
|
|
|
|
|
if (!reader.plugins().isEmpty() && QFile::exists(path + SlashPluginsDotQmltypes))
|
|
|
|
readQmltypes(path + SlashPluginsDotQmltypes, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::processImport(const QString &prefix, const FindUnqualifiedIDVisitor::Import &import)
|
|
|
|
{
|
|
|
|
for (auto const &dependency : qAsConst(import.dependencies)) {
|
2019-06-14 12:21:25 +00:00
|
|
|
auto const split = dependency.split(" ");
|
2019-11-11 17:18:04 +00:00
|
|
|
auto const &id = split.at(0);
|
2019-12-11 14:31:55 +00:00
|
|
|
if (split.length() > 1) {
|
|
|
|
const auto version = split.at(1).split('.');
|
|
|
|
importHelper(id, QString(),
|
|
|
|
version.at(0).toInt(),
|
|
|
|
version.length() > 1 ? version.at(1).toInt() : -1);
|
|
|
|
} else {
|
|
|
|
importHelper(id, QString(), -1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2019-11-14 16:08:15 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
// add objects
|
2019-11-14 16:08:15 +00:00
|
|
|
for (auto it = import.objects.begin(); it != import.objects.end(); ++it) {
|
|
|
|
const auto &val = it.value();
|
2019-11-08 17:43:31 +00:00
|
|
|
m_exportedName2Scope.insert(prefixedName(prefix, val->className()), val);
|
2019-11-11 17:18:04 +00:00
|
|
|
|
|
|
|
const auto exports = val->exports();
|
|
|
|
for (const auto &valExport : exports)
|
2019-11-08 17:43:31 +00:00
|
|
|
m_exportedName2Scope.insert(prefixedName(prefix, valExport.type()), val);
|
2019-11-11 17:18:04 +00:00
|
|
|
|
|
|
|
const auto enums = val->enums();
|
|
|
|
for (const auto &valEnum : enums)
|
|
|
|
m_currentScope->addEnum(valEnum);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:31:55 +00:00
|
|
|
void FindUnqualifiedIDVisitor::importHelper(const QString &module, const QString &prefix,
|
|
|
|
int major, int minor)
|
2019-11-14 16:08:15 +00:00
|
|
|
{
|
2019-12-11 14:31:55 +00:00
|
|
|
const QString id = QString(module).replace(QLatin1Char('/'), QLatin1Char('.'));
|
2019-11-14 16:08:15 +00:00
|
|
|
QPair<QString, QString> importId { id, prefix };
|
|
|
|
if (m_alreadySeenImports.contains(importId))
|
|
|
|
return;
|
|
|
|
m_alreadySeenImports.insert(importId);
|
|
|
|
|
|
|
|
for (const QString &qmltypeDir : m_qmltypeDirs) {
|
|
|
|
auto qmltypesPaths = completeImportPaths(id, qmltypeDir, major, minor);
|
|
|
|
|
|
|
|
for (auto const &qmltypesPath : qmltypesPaths) {
|
|
|
|
if (QFile::exists(qmltypesPath + SlashQmldir)) {
|
|
|
|
processImport(prefix, readQmldir(qmltypesPath));
|
|
|
|
|
|
|
|
// break so that we don't import unversioned qml components
|
|
|
|
// in addition to versioned ones
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Import result;
|
|
|
|
if (QFile::exists(qmltypesPath + SlashAppDotQmltypes))
|
|
|
|
readQmltypes(qmltypesPath + SlashAppDotQmltypes, result);
|
|
|
|
else if (QFile::exists(qmltypesPath + SlashLibDotQmltypes))
|
|
|
|
readQmltypes(qmltypesPath + SlashLibDotQmltypes, result);
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
processImport(prefix, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
ScopeTree *FindUnqualifiedIDVisitor::localFile2ScopeTree(const QString &filePath)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
2019-11-11 17:18:04 +00:00
|
|
|
auto scope = new ScopeTree(ScopeType::QMLScope);
|
2019-11-08 17:43:31 +00:00
|
|
|
const QFileInfo info { filePath };
|
|
|
|
QString baseName = info.baseName();
|
2019-11-11 17:18:04 +00:00
|
|
|
scope->setClassName(baseName.endsWith(".ui") ? baseName.chopped(3) : baseName);
|
2019-06-14 12:21:25 +00:00
|
|
|
QFile file(filePath);
|
2019-11-11 17:18:04 +00:00
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
return scope;
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
QString code = file.readAll();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
QQmlJS::Engine engine;
|
|
|
|
QQmlJS::Lexer lexer(&engine);
|
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
const QString lowerSuffix = info.suffix().toLower();
|
|
|
|
const bool isJavaScript = (lowerSuffix == QLatin1String("js") || lowerSuffix == QLatin1String("mjs"));
|
|
|
|
const bool isESModule = lowerSuffix == QLatin1String("mjs");
|
|
|
|
lexer.setCode(code, /*line = */ 1, /*qmlMode=*/ !isJavaScript);
|
2019-06-14 12:21:25 +00:00
|
|
|
QQmlJS::Parser parser(&engine);
|
2019-11-08 17:43:31 +00:00
|
|
|
|
|
|
|
const bool success = isJavaScript ? (isESModule ? parser.parseModule()
|
|
|
|
: parser.parseProgram())
|
|
|
|
: parser.parse();
|
|
|
|
if (!success)
|
2019-11-11 17:18:04 +00:00
|
|
|
return scope;
|
2019-11-08 17:43:31 +00:00
|
|
|
|
|
|
|
if (!isJavaScript) {
|
|
|
|
QQmlJS::AST::UiProgram *program = parser.ast();
|
|
|
|
parseHeaders(program->headers);
|
|
|
|
parseMembers(program->members, scope);
|
|
|
|
} else {
|
|
|
|
// TODO: Anything special to do with ES modules here?
|
|
|
|
parseProgram(QQmlJS::AST::cast<QQmlJS::AST::Program *>(parser.rootNode()), scope);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
2019-11-08 17:43:31 +00:00
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
return scope;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
void FindUnqualifiedIDVisitor::importFileOrDirectory(const QString &fileOrDirectory,
|
|
|
|
const QString &prefix)
|
2019-08-16 15:03:30 +00:00
|
|
|
{
|
2019-11-08 17:43:31 +00:00
|
|
|
QString name = fileOrDirectory;
|
|
|
|
|
|
|
|
if (QFileInfo(name).isRelative())
|
|
|
|
name = QDir(QFileInfo { m_filePath }.path()).filePath(name);
|
|
|
|
|
|
|
|
if (QFileInfo(name).isFile()) {
|
|
|
|
m_exportedName2Scope.insert(prefix, ScopeTree::ConstPtr(localFile2ScopeTree(name)));
|
|
|
|
return;
|
|
|
|
}
|
2019-08-16 15:03:30 +00:00
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
QDirIterator it { name, QStringList() << QLatin1String("*.qml"), QDir::NoFilter };
|
2019-08-16 15:03:30 +00:00
|
|
|
while (it.hasNext()) {
|
2019-11-08 17:43:31 +00:00
|
|
|
ScopeTree::ConstPtr scope(localFile2ScopeTree(it.next()));
|
2020-01-09 16:27:38 +00:00
|
|
|
if (!scope->className().isEmpty())
|
2019-11-08 17:43:31 +00:00
|
|
|
m_exportedName2Scope.insert(prefixedName(prefix, scope->className()), scope);
|
2019-08-16 15:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
void FindUnqualifiedIDVisitor::importExportedNames(const QStringRef &prefix, QString name)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
2020-01-09 16:27:38 +00:00
|
|
|
ScopeTree::ConstPtr scope = m_exportedName2Scope.value(m_exportedName2Scope.contains(name)
|
|
|
|
? name
|
|
|
|
: prefix + QLatin1Char('.') + name);
|
2019-11-11 17:18:04 +00:00
|
|
|
if (scope) {
|
|
|
|
const auto properties = scope->properties();
|
|
|
|
for (const auto &property : properties)
|
|
|
|
m_currentScope->insertPropertyIdentifier(property);
|
|
|
|
|
|
|
|
m_currentScope->addMethods(scope->methods());
|
|
|
|
name = scope->superclassName();
|
|
|
|
if (name.isEmpty() || name == QLatin1String("QObject"))
|
2019-06-14 12:21:25 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2019-07-23 14:10:24 +00:00
|
|
|
m_colorOut.write(QLatin1String("warning: "), Warning);
|
2019-11-11 17:18:04 +00:00
|
|
|
m_colorOut.write(name + QLatin1String(" was not found."
|
|
|
|
" Did you add all import paths?\n"));
|
2019-07-23 14:10:24 +00:00
|
|
|
m_unknownImports.insert(name);
|
2019-06-14 12:21:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::throwRecursionDepthError()
|
|
|
|
{
|
2019-08-16 08:14:49 +00:00
|
|
|
m_colorOut.write(QStringLiteral("Error"), Error);
|
|
|
|
m_colorOut.write(QStringLiteral("Maximum statement or expression depth exceeded"), Error);
|
|
|
|
m_visitFailed = true;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::UiProgram *)
|
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::QMLScope, "program");
|
2019-11-11 17:18:04 +00:00
|
|
|
QHash<QString, ScopeTree::ConstPtr> objects;
|
|
|
|
QList<ModuleApiInfo> moduleApis;
|
2019-06-14 12:21:25 +00:00
|
|
|
QStringList dependencies;
|
|
|
|
for (auto const &dir : m_qmltypeDirs) {
|
|
|
|
QDirIterator it { dir, QStringList() << QLatin1String("builtins.qmltypes"), QDir::NoFilter,
|
|
|
|
QDirIterator::Subdirectories };
|
|
|
|
while (it.hasNext()) {
|
2019-09-13 09:34:08 +00:00
|
|
|
auto reader = createQmltypesReaderForFile(it.next());
|
2019-06-14 12:21:25 +00:00
|
|
|
auto succ = reader(&objects, &moduleApis, &dependencies);
|
2019-09-19 09:15:47 +00:00
|
|
|
if (!succ)
|
|
|
|
m_colorOut.writeUncolored(reader.errorMessage());
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// add builtins
|
|
|
|
for (auto ob_it = objects.begin(); ob_it != objects.end(); ++ob_it) {
|
|
|
|
auto val = ob_it.value();
|
2019-11-11 17:18:04 +00:00
|
|
|
|
|
|
|
const auto exports = val->exports();
|
|
|
|
for (const auto &valExport : exports)
|
2020-01-09 16:27:38 +00:00
|
|
|
m_exportedName2Scope.insert(valExport.type(), val);
|
2019-11-11 17:18:04 +00:00
|
|
|
|
|
|
|
const auto enums = val->enums();
|
|
|
|
for (const auto &valEnum : enums)
|
|
|
|
m_currentScope->addEnum(valEnum);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
// add "self" (as we only ever check the first part of a qualified identifier, we get away with
|
2019-11-11 17:18:04 +00:00
|
|
|
// using an empty ScopeTree
|
2020-01-09 16:27:38 +00:00
|
|
|
m_exportedName2Scope.insert(QFileInfo { m_filePath }.baseName(), {});
|
2019-06-14 12:21:25 +00:00
|
|
|
|
2019-11-08 17:43:31 +00:00
|
|
|
importFileOrDirectory(".", QString());
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::UiProgram *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::ClassExpression *ast)
|
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, ast->name.toString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::ClassExpression *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::ClassDeclaration *ast)
|
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, ast->name.toString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::ClassDeclaration *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::ForStatement *)
|
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "forloop");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::ForStatement *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::ForEachStatement *)
|
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "foreachloop");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::ForEachStatement *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::Block *)
|
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "block");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::Block *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::CaseBlock *)
|
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "case");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::CaseBlock *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:24:24 +00:00
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::Catch *catchStatement)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "catch");
|
2019-11-11 17:18:04 +00:00
|
|
|
m_currentScope->insertJSIdentifier(catchStatement->patternElement->bindingIdentifier.toString(),
|
|
|
|
QQmlJS::AST::VariableScope::Let);
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::Catch *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::WithStatement *withStatement)
|
|
|
|
{
|
2019-11-11 17:18:04 +00:00
|
|
|
m_colorOut.write(QString::fromLatin1("Warning: "), Warning);
|
|
|
|
m_colorOut.write(QString::fromLatin1(
|
|
|
|
"%1:%2: with statements are strongly discouraged in QML "
|
|
|
|
"and might cause false positives when analysing unqalified identifiers\n")
|
|
|
|
.arg(withStatement->firstSourceLocation().startLine)
|
|
|
|
.arg(withStatement->firstSourceLocation().startColumn),
|
|
|
|
Normal);
|
2019-06-14 12:21:25 +00:00
|
|
|
enterEnvironment(ScopeType::JSLexicalScope, "with");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::WithStatement *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:31:51 +00:00
|
|
|
static QString signalName(const QStringRef &handlerName)
|
|
|
|
{
|
|
|
|
if (handlerName.startsWith("on") && handlerName.size() > 2) {
|
|
|
|
QString signal = handlerName.mid(2).toString();
|
|
|
|
for (int i = 0; i < signal.length(); ++i) {
|
|
|
|
QCharRef ch = signal[i];
|
|
|
|
if (ch.isLower())
|
|
|
|
return QString();
|
|
|
|
if (ch.isUpper()) {
|
|
|
|
ch = ch.toLower();
|
|
|
|
return signal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::UiScriptBinding *uisb)
|
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
auto name = uisb->qualifiedId->name;
|
|
|
|
if (name == QLatin1String("id")) {
|
|
|
|
// found id
|
2019-11-11 17:18:04 +00:00
|
|
|
auto expstat = cast<ExpressionStatement *>(uisb->statement);
|
|
|
|
auto identexp = cast<IdentifierExpression *>(expstat->expression);
|
2019-06-14 12:21:25 +00:00
|
|
|
QString elementName = m_currentScope->name();
|
2020-01-09 16:27:38 +00:00
|
|
|
m_qmlid2scope.insert(identexp->name.toString(), m_exportedName2Scope.value(elementName));
|
2019-11-11 17:18:04 +00:00
|
|
|
if (m_currentScope->isVisualRootScope())
|
2019-06-14 12:21:25 +00:00
|
|
|
m_rootId = identexp->name.toString();
|
2019-08-16 09:31:51 +00:00
|
|
|
} else {
|
|
|
|
const QString signal = signalName(name);
|
|
|
|
if (signal.isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!m_currentScope->methods().contains(signal)) {
|
|
|
|
m_currentScope->addUnmatchedSignalHandler(name.toString(), uisb->firstSourceLocation());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto statement = uisb->statement;
|
2019-06-14 12:21:25 +00:00
|
|
|
if (statement->kind == Node::Kind::Kind_ExpressionStatement) {
|
2019-11-11 17:18:04 +00:00
|
|
|
if (cast<ExpressionStatement *>(statement)->expression->asFunctionDefinition()) {
|
2019-06-14 12:21:25 +00:00
|
|
|
// functions are already handled
|
|
|
|
// they do not get names inserted according to the signal, but access their formal
|
|
|
|
// parameters
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 09:31:51 +00:00
|
|
|
|
|
|
|
auto method = m_currentScope->methods()[signal];
|
|
|
|
for (auto const ¶m : method.parameterNames()) {
|
|
|
|
const auto firstSourceLocation = statement->firstSourceLocation();
|
|
|
|
bool hasMultilineStatementBody
|
|
|
|
= statement->lastSourceLocation().startLine > firstSourceLocation.startLine;
|
|
|
|
m_currentScope->insertSignalIdentifier(param, method, firstSourceLocation,
|
|
|
|
hasMultilineStatementBody);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::UiPublicMember *uipm)
|
|
|
|
{
|
|
|
|
// property bool inactive: !active
|
|
|
|
// extract name inactive
|
2019-11-11 17:18:04 +00:00
|
|
|
m_currentScope->insertPropertyIdentifier(MetaProperty(
|
|
|
|
uipm->name.toString(),
|
|
|
|
// TODO: signals, complex types etc.
|
|
|
|
uipm->memberType ? uipm->memberType->name.toString() : QString(),
|
|
|
|
uipm->typeModifier == QLatin1String("list"),
|
|
|
|
!uipm->isReadonlyMember,
|
|
|
|
false, 0));
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::IdentifierExpression *idexp)
|
|
|
|
{
|
|
|
|
auto name = idexp->name;
|
2019-11-11 17:18:04 +00:00
|
|
|
if (!m_exportedName2Scope.contains(name.toString())) {
|
2019-06-14 12:21:25 +00:00
|
|
|
m_currentScope->addIdToAccssedIfNotInParentScopes(
|
2019-07-23 14:10:24 +00:00
|
|
|
{ name.toString(), idexp->firstSourceLocation() }, m_unknownImports);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-11 17:18:04 +00:00
|
|
|
FindUnqualifiedIDVisitor::FindUnqualifiedIDVisitor(QStringList qmltypeDirs, QString code,
|
|
|
|
QString fileName, bool silent)
|
2019-06-14 12:21:25 +00:00
|
|
|
: m_rootScope(new ScopeTree { ScopeType::JSFunctionScope, "global" }),
|
|
|
|
m_currentScope(m_rootScope.get()),
|
2019-11-11 17:18:04 +00:00
|
|
|
m_qmltypeDirs(std::move(qmltypeDirs)),
|
|
|
|
m_code(std::move(code)),
|
2019-06-14 12:21:25 +00:00
|
|
|
m_rootId(QLatin1String("<id>")),
|
2019-11-11 17:18:04 +00:00
|
|
|
m_filePath(std::move(fileName)),
|
2019-09-19 09:15:47 +00:00
|
|
|
m_colorOut(silent)
|
2019-06-14 12:21:25 +00:00
|
|
|
{
|
|
|
|
// setup color output
|
2019-11-11 17:18:04 +00:00
|
|
|
m_colorOut.insertMapping(Error, ColorOutput::RedForeground);
|
|
|
|
m_colorOut.insertMapping(Warning, ColorOutput::PurpleForeground);
|
|
|
|
m_colorOut.insertMapping(Info, ColorOutput::BlueForeground);
|
|
|
|
m_colorOut.insertMapping(Normal, ColorOutput::DefaultColor);
|
|
|
|
m_colorOut.insertMapping(Hint, ColorOutput::GreenForeground);
|
2019-06-14 12:21:25 +00:00
|
|
|
QLatin1String jsGlobVars[] = {
|
2019-07-24 06:42:17 +00:00
|
|
|
/* Not listed on the MDN page; browser and QML extensions: */
|
|
|
|
// console/debug api
|
|
|
|
QLatin1String("console"), QLatin1String("print"),
|
|
|
|
// garbage collector
|
|
|
|
QLatin1String("gc"),
|
|
|
|
// i18n
|
2019-11-11 17:18:04 +00:00
|
|
|
QLatin1String("qsTr"), QLatin1String("qsTrId"), QLatin1String("QT_TR_NOOP"),
|
|
|
|
QLatin1String("QT_TRANSLATE_NOOP"), QLatin1String("QT_TRID_NOOP"),
|
2019-07-24 06:42:17 +00:00
|
|
|
// XMLHttpRequest
|
|
|
|
QLatin1String("XMLHttpRequest")
|
2019-06-14 12:21:25 +00:00
|
|
|
};
|
2019-11-11 17:18:04 +00:00
|
|
|
for (const char **globalName = QV4::Compiler::Codegen::s_globalNames;
|
|
|
|
*globalName != nullptr;
|
|
|
|
++globalName) {
|
|
|
|
m_currentScope->insertJSIdentifier(QString::fromLatin1(*globalName),
|
|
|
|
QQmlJS::AST::VariableScope::Const);
|
2019-07-24 06:42:17 +00:00
|
|
|
}
|
2019-06-14 12:21:25 +00:00
|
|
|
for (const auto& jsGlobVar: jsGlobVars)
|
|
|
|
m_currentScope->insertJSIdentifier(jsGlobVar, QQmlJS::AST::VariableScope::Const);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::check()
|
|
|
|
{
|
2019-08-16 08:14:49 +00:00
|
|
|
if (m_visitFailed)
|
|
|
|
return false;
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
// now that all ids are known, revisit any Connections whose target were perviously unknown
|
2019-11-11 17:18:04 +00:00
|
|
|
for (auto const &outstandingConnection: m_outstandingConnections) {
|
|
|
|
auto targetScope = m_qmlid2scope[outstandingConnection.targetName];
|
|
|
|
if (outstandingConnection.scope)
|
|
|
|
outstandingConnection.scope->addMethods(targetScope->methods());
|
2019-06-14 12:21:25 +00:00
|
|
|
QScopedValueRollback<ScopeTree*> rollback(m_currentScope, outstandingConnection.scope);
|
|
|
|
outstandingConnection.uiod->initializer->accept(this);
|
|
|
|
}
|
2019-11-11 17:18:04 +00:00
|
|
|
return m_rootScope->recheckIdentifiers(m_code, m_qmlid2scope, m_rootScope.get(), m_rootId,
|
|
|
|
m_colorOut);
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::VariableDeclarationList *vdl)
|
|
|
|
{
|
|
|
|
while (vdl) {
|
|
|
|
m_currentScope->insertJSIdentifier(vdl->declaration->bindingIdentifier.toString(),
|
|
|
|
vdl->declaration->scope);
|
|
|
|
vdl = vdl->next;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::visitFunctionExpressionHelper(QQmlJS::AST::FunctionExpression *fexpr)
|
|
|
|
{
|
|
|
|
using namespace QQmlJS::AST;
|
2019-11-11 17:18:04 +00:00
|
|
|
auto name = fexpr->name.toString();
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
if (m_currentScope->scopeType() == ScopeType::QMLScope)
|
|
|
|
m_currentScope->addMethod(MetaMethod(name, QLatin1String("void")));
|
|
|
|
else
|
2019-06-14 12:21:25 +00:00
|
|
|
m_currentScope->insertJSIdentifier(name, VariableScope::Const);
|
2019-11-11 17:18:04 +00:00
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, name);
|
|
|
|
} else {
|
|
|
|
enterEnvironment(ScopeType::JSFunctionScope, QLatin1String("<anon>"));
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::FunctionExpression *fexpr)
|
|
|
|
{
|
|
|
|
visitFunctionExpressionHelper(fexpr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::FunctionExpression *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::FunctionDeclaration *fdecl)
|
|
|
|
{
|
|
|
|
visitFunctionExpressionHelper(fdecl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::FunctionDeclaration *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::FormalParameterList *fpl)
|
|
|
|
{
|
|
|
|
for (auto const &boundName : fpl->boundNames()) {
|
|
|
|
m_currentScope->insertJSIdentifier(boundName.id, QQmlJS::AST::VariableScope::Const);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::UiImport *import)
|
|
|
|
{
|
|
|
|
// construct path
|
|
|
|
QString prefix = QLatin1String("");
|
|
|
|
if (import->asToken.isValid()) {
|
2019-11-08 17:43:31 +00:00
|
|
|
prefix += import->importId;
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
auto dirname = import->fileName.toString();
|
2019-08-16 15:03:30 +00:00
|
|
|
if (!dirname.isEmpty())
|
2019-11-08 17:43:31 +00:00
|
|
|
importFileOrDirectory(dirname, prefix);
|
2019-08-16 15:03:30 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
QString path {};
|
|
|
|
if (!import->importId.isEmpty()) {
|
2019-11-11 17:18:04 +00:00
|
|
|
// TODO: do not put imported ids into the same space as qml IDs
|
|
|
|
m_qmlid2scope.insert(import->importId.toString(), {});
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
if (import->version) {
|
|
|
|
auto uri = import->importUri;
|
|
|
|
while (uri) {
|
|
|
|
path.append(uri->name);
|
|
|
|
path.append("/");
|
|
|
|
uri = uri->next;
|
|
|
|
}
|
|
|
|
path.chop(1);
|
|
|
|
|
|
|
|
importHelper(path, prefix, import->version->majorVersion, import->version->minorVersion);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::UiEnumDeclaration *uied)
|
|
|
|
{
|
2019-11-11 17:18:04 +00:00
|
|
|
MetaEnum qmlEnum(uied->name.toString());
|
|
|
|
for (const auto *member = uied->members; member; member = member->next)
|
|
|
|
qmlEnum.addKey(member->member.toString());
|
|
|
|
m_currentScope->addEnum(qmlEnum);
|
2019-06-14 12:21:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::UiObjectBinding *uiob)
|
|
|
|
{
|
|
|
|
// property QtObject __styleData: QtObject {...}
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
QString name {};
|
|
|
|
auto id = uiob->qualifiedTypeNameId;
|
|
|
|
QStringRef prefix = uiob->qualifiedTypeNameId->name;
|
|
|
|
while (id) {
|
|
|
|
name += id->name.toString() + QLatin1Char('.');
|
|
|
|
id = id->next;
|
|
|
|
}
|
|
|
|
name.chop(1);
|
2019-11-11 17:18:04 +00:00
|
|
|
|
|
|
|
const MetaProperty prop(uiob->qualifiedId->name.toString(), name, false, true, true, 0);
|
|
|
|
m_currentScope->addProperty(prop);
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
enterEnvironment(ScopeType::QMLScope, name);
|
|
|
|
importExportedNames(prefix, name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::UiObjectBinding *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::UiObjectDefinition *uiod)
|
|
|
|
{
|
2019-11-11 17:18:04 +00:00
|
|
|
using namespace QQmlJS::AST;
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
QString name {};
|
|
|
|
auto id = uiod->qualifiedTypeNameId;
|
|
|
|
QStringRef prefix = uiod->qualifiedTypeNameId->name;
|
|
|
|
while (id) {
|
|
|
|
name += id->name.toString() + QLatin1Char('.');
|
|
|
|
id = id->next;
|
|
|
|
}
|
|
|
|
name.chop(1);
|
|
|
|
enterEnvironment(ScopeType::QMLScope, name);
|
|
|
|
if (name.isLower())
|
|
|
|
return false; // Ignore grouped properties for now
|
2019-11-11 17:18:04 +00:00
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
importExportedNames(prefix, name);
|
|
|
|
if (name.endsWith("Connections")) {
|
|
|
|
QString target;
|
|
|
|
auto member = uiod->initializer->members;
|
|
|
|
while (member) {
|
|
|
|
if (member->member->kind == QQmlJS::AST::Node::Kind_UiScriptBinding) {
|
|
|
|
auto asBinding = static_cast<QQmlJS::AST::UiScriptBinding*>(member->member);
|
|
|
|
if (asBinding->qualifiedId->name == QLatin1String("target")) {
|
|
|
|
if (asBinding->statement->kind == QQmlJS::AST::Node::Kind_ExpressionStatement) {
|
|
|
|
auto expr = static_cast<QQmlJS::AST::ExpressionStatement*>(asBinding->statement)->expression;
|
|
|
|
if (auto idexpr = QQmlJS::AST::cast<QQmlJS::AST::IdentifierExpression*>(expr)) {
|
|
|
|
target = idexpr->name.toString();
|
|
|
|
} else {
|
|
|
|
// more complex expressions are not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
member = member->next;
|
|
|
|
}
|
2019-11-11 17:18:04 +00:00
|
|
|
ScopeTree::ConstPtr targetScope;
|
2019-06-14 12:21:25 +00:00
|
|
|
if (target.isEmpty()) {
|
|
|
|
// no target set, connection comes from parentF
|
|
|
|
ScopeTree* scope = m_currentScope;
|
|
|
|
do {
|
|
|
|
scope = scope->parentScope(); // TODO: rename method
|
|
|
|
} while (scope->scopeType() != ScopeType::QMLScope);
|
2020-01-09 16:27:38 +00:00
|
|
|
targetScope = m_exportedName2Scope.value(scope->name());
|
2019-06-14 12:21:25 +00:00
|
|
|
} else {
|
|
|
|
// there was a target, check if we already can find it
|
2019-11-11 17:18:04 +00:00
|
|
|
auto scopeIt = m_qmlid2scope.find(target);
|
|
|
|
if (scopeIt != m_qmlid2scope.end()) {
|
|
|
|
targetScope = *scopeIt;
|
2019-06-14 12:21:25 +00:00
|
|
|
} else {
|
|
|
|
m_outstandingConnections.push_back({target, m_currentScope, uiod});
|
|
|
|
return false; // visit children later once target is known
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 17:18:04 +00:00
|
|
|
if (targetScope)
|
|
|
|
m_currentScope->addMethods(targetScope->methods());
|
2019-06-14 12:21:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-17 15:13:18 +00:00
|
|
|
bool FindUnqualifiedIDVisitor::visit(QQmlJS::AST::PatternElement *element)
|
|
|
|
{
|
|
|
|
if (element->isVariableDeclaration()) {
|
|
|
|
QQmlJS::AST::BoundNames names;
|
|
|
|
element->boundNames(&names);
|
|
|
|
for (const auto &name : names)
|
|
|
|
m_currentScope->insertJSIdentifier(name.id, element->scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:21:25 +00:00
|
|
|
void FindUnqualifiedIDVisitor::endVisit(QQmlJS::AST::UiObjectDefinition *)
|
|
|
|
{
|
|
|
|
leaveEnvironment();
|
|
|
|
}
|