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 | |
4 | #include "qqmljstypereader_p.h" |
5 | #include "qqmljsimportvisitor_p.h" |
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 | |
16 | QT_BEGIN_NAMESPACE |
17 | |
18 | bool 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(n: 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(flags: QFile::ReadOnly)) |
36 | return false; |
37 | |
38 | QString code = QString::fromUtf8(ba: file.readAll()); |
39 | file.close(); |
40 | |
41 | lexer.setCode(code, /*line = */ lineno: 1, /*qmlMode=*/ !isJavaScript); |
42 | QQmlJS::Parser parser(&engine); |
43 | |
44 | const bool success = isJavaScript ? (isESModule ? parser.parseModule() |
45 | : parser.parseProgram()) |
46 | : parser.parse(); |
47 | if (!success) |
48 | return false; |
49 | |
50 | QQmlJS::AST::Node *rootNode = parser.rootNode(); |
51 | if (!rootNode) |
52 | return false; |
53 | |
54 | QQmlJSLogger logger; |
55 | logger.setFileName(m_file); |
56 | logger.setCode(code); |
57 | logger.setSilent(true); |
58 | |
59 | auto membersVisitor = m_importer->makeImportVisitor( |
60 | target: scope, importer: m_importer, logger: &logger, |
61 | implicitImportDirectory: QQmlJSImportVisitor::implicitImportDirectory(localFile: m_file, mapper: m_importer->resourceFileMapper())); |
62 | rootNode->accept(visitor: membersVisitor.get()); |
63 | return true; |
64 | } |
65 | |
66 | QT_END_NAMESPACE |
67 |