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"
10
11#include <QtQml/private/qqmljslexer_p.h>
12#include <QtQml/private/qqmljsparser_p.h>
13#include <QtQml/private/qqmljsengine_p.h>
14#include <QtQml/private/qqmljsastvisitor_p.h>
15#include <QtQml/private/qqmljsast_p.h>
16#include <QtCore/QDir>
17#include <QtCore/QScopeGuard>
18#include <QtCore/QFileInfo>
19#include <QtCore/QRegularExpressionMatch>
20
21#include <algorithm>
22
23QT_BEGIN_NAMESPACE
24
25using namespace Qt::StringLiterals;
26
27namespace QQmlJS {
28namespace Dom {
29
38
43
48
50{
51 return m_path;
52}
53
55{
56 return m_path;
57}
58
59QCborValue pluginData(const QQmlDirParser::Plugin &pl, const QStringList &cNames)
60{
61 QCborArray names;
62 for (const QString &n : cNames)
63 names.append(n);
64 return QCborMap({ { QCborValue(QStringView(Fields::name)), pl.name },
65 { QStringView(Fields::path), pl.path },
66 { QStringView(Fields::classNames), names } });
67}
68
101
103{
104 static ErrorGroups res = { { DomItem::domErrorGroup, NewErrorGroup("JsFile"),
105 NewErrorGroup("Parsing") } };
106 return res;
107}
108
119
120void JsFile::writeOut(const DomItem &self, OutWriter &ow) const
121{
123 ow.ensureNewline(2);
127 }
128}
129
137
146
148{
149 Q_UNUSED(self);
150 lw.write(u".pragma").ensureSpace().write(u"library").ensureNewline();
151}
152
153void JsFile::LegacyImport::writeOut(const DomItem &self, OutWriter &lw) const
154{
155 const auto fLoc = FileLocations::treeOf(self);
156 // either filename or module uri must be present
158
159 lw.write(u".import").ensureSpace();
160 if (!uri.isEmpty()) {
162 if (!version.isEmpty()) {
164 }
165 } else {
166 lw.write(u"\"").write(fileName).write(u"\"").ensureSpace();
167 }
169
171}
172
173/*!
174 * \internal JsFile::writeOutDirectives
175 * \brief Performs writeOut of the .js Directives (.import, .pragma)
176 *
177 * Watch out!
178 * Currently directives in .js files do not have representative AST::Node-s (see QTBUG-119770),
179 * which makes it hard to preserve attached comments during the WriteOut process,
180 * because currently they are being attached to the first AST::Node.
181 * In case when the first AST::Node is absent, they are not collected, hence lost.
182 */
183void JsFile::writeOutDirectives(const DomItem &self, OutWriter &ow) const
184{
187 }
188 for (const auto &import : m_imports) {
190 }
191}
192
194{
195 auto res = std::make_shared<QmlFile>(*this);
196 return res;
197}
198
199/*!
200 \class QmlFile
201
202 A QmlFile, when loaded in a DomEnvironment that has the DomCreationOption::WithSemanticAnalysis,
203 will be lazily constructed. That means that its member m_lazyMembers is uninitialized, and will
204 only be populated when it is accessed (through a getter, a setter or the DomItem interface).
205
206 The reason for the laziness is that the qqmljsscopes are created lazily and at the same time as
207 the Dom QmlFile representations. So instead of eagerly generating all qqmljsscopes when
208 constructing the Dom, the QmlFile itself becomes lazy and will only be populated on demand at
209 the same time as the corresponding qqmljsscopes.
210
211 The QDeferredFactory<QQmlJSScope> will, when the qqmljsscope is populated, take care of
212 populating all fields of the QmlFile.
213 Therefore, population of the QmlFile is done by populating the qqmljsscope.
214
215*/
216
239
241{
242 static ErrorGroups res = { { DomItem::domErrorGroup, NewErrorGroup("QmlFile"),
243 NewErrorGroup("Parsing") } };
244 return res;
245}
246
260
268
270{
271 m_isValid = false;
273}
274
276{
278 for (const DomItem &p : self.field(Fields::pragmas).values()) {
279 p.writeOut(ow);
280 }
282
285 [](const DomItem& item1, const DomItem& item2) {
286 const auto uri1 = item1[Fields::uri].toString();
287 const auto uri2 = item2[Fields::uri].toString();
288 return uri1 < uri2;
289 });
290 }
291
292 for (const DomItem& i : imports) {
293 i.writeOut(ow);
294 }
295 ow.ensureNewline(2);
298}
299
300} // end namespace Dom
301} // end namespace QQmlJS
302
303QT_END_NAMESPACE
QCborValue pluginData(const QQmlDirParser::Plugin &pl, const QStringList &cNames)
#define NewErrorGroup(name)