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
16QT_BEGIN_NAMESPACE
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(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
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 prerequisites: { scope,
57 &logger,
58 QQmlJSImportVisitor::implicitImportDirectory(
59 localFile: m_file, mapper: m_importer->resourceFileMapper()),
60 {} });
61 return success && rootNode;
62}
63
64QT_END_NAMESPACE
65

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtdeclarative/src/qmlcompiler/qqmljstypereader.cpp