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
qqmldomexternalitems.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qqmldomtop_p.h"
9
10#include <QtQml/private/qqmljslexer_p.h>
11#include <QtQml/private/qqmljsparser_p.h>
12#include <QtQml/private/qqmljsengine_p.h>
13#include <QtQml/private/qqmljsastvisitor_p.h>
14#include <QtQml/private/qqmljsast_p.h>
15#include <QtCore/QDir>
16#include <QtCore/QScopeGuard>
17#include <QtCore/QFileInfo>
18#include <QtCore/QRegularExpressionMatch>
19
20#include <algorithm>
21
22QT_BEGIN_NAMESPACE
23
24using namespace Qt::StringLiterals;
25
26namespace QQmlJS {
27namespace Dom {
28
37
42
47
49{
50 return m_path;
51}
52
54{
55 return m_path;
56}
57
58QCborValue pluginData(const QQmlDirParser::Plugin &pl, const QStringList &cNames)
59{
60 QCborArray names;
61 for (const QString &n : cNames)
62 names.append(n);
63 return QCborMap({ { QCborValue(fieldNameToString(Fields::name)), pl.name },
64 { fieldNameToString(Fields::path), pl.path },
65 { fieldNameToString(Fields::classNames), names } });
66}
67
97
99{
100 static ErrorGroups res = { { DomItem::domErrorGroup, NewErrorGroup("JsFile"),
101 NewErrorGroup("Parsing") } };
102 return res;
103}
104
115
116void JsFile::writeOut(const DomItem &self, OutWriter &ow) const
117{
119 ow.ensureNewline(2);
123 }
124}
125
133
142
144{
145 Q_UNUSED(self);
146 lw.write(u".pragma").ensureSpace().write(u"library").ensureNewline();
147}
148
149void JsFile::LegacyImport::writeOut(const DomItem &self, OutWriter &lw) const
150{
151 const auto fLoc = FileLocations::treeOf(self);
152 // either filename or module uri must be present
154
155 lw.write(u".import").ensureSpace();
156 if (!uri.isEmpty()) {
158 if (!version.isEmpty()) {
160 }
161 } else {
162 lw.write(u"\"").write(fileName).write(u"\"").ensureSpace();
163 }
165
167}
168
169/*!
170 * \internal JsFile::writeOutDirectives
171 * \brief Performs writeOut of the .js Directives (.import, .pragma)
172 *
173 * Watch out!
174 * Currently directives in .js files do not have representative AST::Node-s (see QTBUG-119770),
175 * which makes it hard to preserve attached comments during the WriteOut process,
176 * because currently they are being attached to the first AST::Node.
177 * In case when the first AST::Node is absent, they are not collected, hence lost.
178 */
179void JsFile::writeOutDirectives(const DomItem &self, OutWriter &ow) const
180{
183 }
184 for (const auto &import : m_imports) {
186 }
187}
188
190{
191 auto res = std::make_shared<QmlFile>(*this);
192 return res;
193}
194
195/*!
196 \class QmlFile
197
198 A QmlFile, when loaded in a DomEnvironment that has the DomCreationOption::WithSemanticAnalysis,
199 will be lazily constructed. That means that its member m_lazyMembers is uninitialized, and will
200 only be populated when it is accessed (through a getter, a setter or the DomItem interface).
201
202 The reason for the laziness is that the qqmljsscopes are created lazily and at the same time as
203 the Dom QmlFile representations. So instead of eagerly generating all qqmljsscopes when
204 constructing the Dom, the QmlFile itself becomes lazy and will only be populated on demand at
205 the same time as the corresponding qqmljsscopes.
206
207 The QDeferredFactory<QQmlJSScope> will, when the qqmljsscope is populated, take care of
208 populating all fields of the QmlFile.
209 Therefore, population of the QmlFile is done by populating the qqmljsscope.
210
211*/
212
231
233{
234 static ErrorGroups res = { { DomItem::domErrorGroup, NewErrorGroup("QmlFile"),
235 NewErrorGroup("Parsing") } };
236 return res;
237}
238
252
260
262{
263 m_isValid = false;
265}
266
268{
270 for (const DomItem &p : self.field(Fields::pragmas).values()) {
271 p.writeOut(ow);
272 }
274
277 [](const DomItem& item1, const DomItem& item2) {
278 const auto uri1 = item1[Fields::uri].toString();
279 const auto uri2 = item2[Fields::uri].toString();
280 return uri1 < uri2;
281 });
282 }
283
284 for (const DomItem& i : imports) {
285 i.writeOut(ow);
286 }
287 ow.ensureNewline(2);
290}
291
292} // end namespace Dom
293} // end namespace QQmlJS
294
295QT_END_NAMESPACE
QCborValue pluginData(const QQmlDirParser::Plugin &pl, const QStringList &cNames)
#define NewErrorGroup(name)