Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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);
36 return false;
37
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.setFileName(m_file);
52 logger.setCode(code);
53 logger.setSilent(true);
54
55 m_importer->runImportVisitor(rootNode,
56 { scope,
57 &logger,
59 m_file, m_importer->resourceFileMapper()),
60 {} });
61 return success && rootNode;
62}
63
void close() override
Calls QFileDevice::flush() and closes the file.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
static QString implicitImportDirectory(const QString &localFile, QQmlJSResourceFileMapper *mapper)
void runImportVisitor(QQmlJS::AST::Node *rootNode, const ImportVisitorPrerequisites &prerequisites)
QQmlJSResourceFileMapper * resourceFileMapper() const
void setCode(const QString &code)
void setSilent(bool silent)
void setFileName(const QString &fileName)
bool operator()(const QSharedPointer< QQmlJSScope > &scope)
void setCode(const QString &code, int lineno, bool qmlMode=true, CodeContinuation codeContinuation=CodeContinuation::Reset)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString chopped(qsizetype n) const &
Definition qstring.h:398
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5506
Combined button and popup list for selecting options.
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
QFile file
[0]
QHostInfo info
[0]
QJSEngine engine
[0]