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// Qt-Security score:significant
4
7
8#include <QtQml/private/qqmljsast_p.h>
9#include <QtQml/private/qqmljsengine_p.h>
10#include <QtQml/private/qqmljslexer_p.h>
11#include <QtQml/private/qqmljsparser_p.h>
12#include <QtQml/private/qqmlimportresolver_p.h>
13
14#include <QtCore/qfileinfo.h>
15#include <QtCore/qdebug.h>
16
18
19bool QQmlJSTypeReader::operator ()(const QSharedPointer<QQmlJSScope> &scope)
20{
21 using namespace QQmlJS::AST;
22 const QFileInfo info { m_file };
23 const QString baseName = info.baseName();
24 scope->setInternalName(baseName.endsWith(QStringLiteral(".ui")) ? baseName.chopped(3)
25 : baseName);
26
27 QQmlJS::Engine engine;
28 QQmlJS::Lexer lexer(&engine);
29
30 const QString lowerSuffix = info.suffix().toLower();
31 const bool isESModule = lowerSuffix == QLatin1String("mjs");
32 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
33
34
35 QFile file(m_file);
36 if (!file.open(QFile::ReadOnly))
37 return false;
38
39 QString code = QString::fromUtf8(file.readAll());
40 file.close();
41
42 lexer.setCode(code, /*line = */ 1, /*qmlMode=*/ !isJavaScript);
43 QQmlJS::Parser parser(&engine);
44
45 const bool success = isJavaScript ? (isESModule ? parser.parseModule()
46 : parser.parseProgram())
47 : parser.parse();
48
49 QQmlJS::AST::Node *rootNode = parser.rootNode();
50
51 QQmlJSLogger logger;
52 logger.setFilePath(m_file);
53 logger.setCode(code);
54 logger.setSilent(true);
55 logger.setIsDisabled(true);
56
57 m_importer->runImportVisitor(rootNode,
58 { scope,
59 &logger,
60 QQmlJSImportVisitor::implicitImportDirectory(
61 m_file, m_importer->resourceFileMapper()),
62 {} });
63 return success && rootNode;
64}
65
66QT_END_NAMESPACE
bool operator()(const QSharedPointer< QQmlJSScope > &scope)