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
qqmljstypereader.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
6
7#include <QtQml/private/qqmljsast_p.h>
8#include <QtQml/private/qqmljsengine_p.h>
9#include <QtQml/private/qqmljslexer_p.h>
10#include <QtQml/private/qqmljsparser_p.h>
11#include <QtQml/private/qqmlimportresolver_p.h>
12
13#include <QtCore/qfileinfo.h>
14#include <QtCore/qdebug.h>
15
17
18bool QQmlJSTypeReader::operator ()(const QSharedPointer<QQmlJSScope> &scope)
19{
20 using namespace QQmlJS::AST;
21 const QFileInfo info { m_file };
22 const QString baseName = info.baseName();
23 scope->setInternalName(baseName.endsWith(QStringLiteral(".ui")) ? baseName.chopped(3)
24 : baseName);
25
26 QQmlJS::Engine engine;
27 QQmlJS::Lexer lexer(&engine);
28
29 const QString lowerSuffix = info.suffix().toLower();
30 const bool isESModule = lowerSuffix == QLatin1String("mjs");
31 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
32
33
34 QFile file(m_file);
35 if (!file.open(QFile::ReadOnly))
36 return false;
37
38 QString code = QString::fromUtf8(file.readAll());
39 file.close();
40
41 lexer.setCode(code, /*line = */ 1, /*qmlMode=*/ !isJavaScript);
42 QQmlJS::Parser parser(&engine);
43
44 const bool success = isJavaScript ? (isESModule ? parser.parseModule()
45 : parser.parseProgram())
46 : parser.parse();
47
48 QQmlJS::AST::Node *rootNode = parser.rootNode();
49
50 QQmlJSLogger logger;
51 logger.setFilePath(m_file);
52 logger.setCode(code);
53 logger.setSilent(true);
54
55 m_importer->runImportVisitor(rootNode,
56 { scope,
57 &logger,
58 QQmlJSImportVisitor::implicitImportDirectory(
59 m_file, m_importer->resourceFileMapper()),
60 {} });
61 return success && rootNode;
62}
63
64QT_END_NAMESPACE
bool operator()(const QSharedPointer< QQmlJSScope > &scope)
Combined button and popup list for selecting options.