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
19namespace QQmlJS {
20void defaultTypeReader(QQmlJSImporter *importer, const QString &filePath,
21 const QSharedPointer<QQmlJSScope> &scope)
22{
23 using namespace QQmlJS::AST;
24 const QFileInfo info{ filePath };
25 const QString baseName = info.baseName();
26 scope->setInternalName(baseName.endsWith(QStringLiteral(".ui")) ? baseName.chopped(3)
27 : baseName);
28
29 QQmlJS::Engine engine;
30 QQmlJS::Lexer lexer(&engine);
31
32 const QString lowerSuffix = info.suffix().toLower();
33 const bool isESModule = lowerSuffix == QLatin1String("mjs");
34 const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
35
36 QFile file(filePath);
37 if (!file.open(QFile::ReadOnly))
38 return;
39
40 QString code = QString::fromUtf8(file.readAll());
41 file.close();
42
43 lexer.setCode(code, /*line = */ 1, /*qmlMode=*/ !isJavaScript);
44 QQmlJS::Parser parser(&engine);
45
46 isJavaScript ? (isESModule ? parser.parseModule() : parser.parseProgram()) : parser.parse();
47
48 QQmlJS::AST::Node *rootNode = parser.rootNode();
49
50 QQmlJSLogger logger;
51 logger.setFilePath(filePath);
52 logger.setCode(code);
53 logger.setSilent(true);
54 logger.setIsDisabled(true);
55
56 importer->runImportVisitor(rootNode,
57 { scope,
58 &logger,
59 QQmlJSImportVisitor::implicitImportDirectory(
60 filePath, importer->resourceFileMapper()),
61 { } });
62}
63} // namespace QQmlJS
64
65QT_END_NAMESPACE
void defaultTypeReader(QQmlJSImporter *importer, const QString &filePath, const QSharedPointer< QQmlJSScope > &scope)
Combined button and popup list for selecting options.