Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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"
5
#
include
"qqmldomoutwriter_p.h"
6
#
include
"qqmldomcomments_p.h"
7
#
include
"qqmldomelements_p.h"
8
#
include
"qqmldom_utils_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
22
QT_BEGIN_NAMESPACE
23
24
using
namespace
Qt::StringLiterals;
25
26
namespace
QQmlJS
{
27
namespace
Dom
{
28
29
ExternalOwningItem
::
ExternalOwningItem
(
30
const
QString
&
filePath
,
const
QDateTime
&
lastDataUpdateAt
,
const
Path
&
path
,
31
int
derivedFrom
,
const
QString
&
code
)
32
:
OwningItem
(
derivedFrom
,
lastDataUpdateAt
),
33
m_canonicalFilePath
(
filePath
),
34
m_code
(
code
),
35
m_path
(
path
)
36
{}
37
38
QString
ExternalOwningItem
::
canonicalFilePath
(
const
DomItem
&)
const
39
{
40
return
m_canonicalFilePath
;
41
}
42
43
QString
ExternalOwningItem
::
canonicalFilePath
()
const
44
{
45
return
m_canonicalFilePath
;
46
}
47
48
Path
ExternalOwningItem
::
canonicalPath
(
const
DomItem
&)
const
49
{
50
return
m_path
;
51
}
52
53
Path
ExternalOwningItem
::
canonicalPath
()
const
54
{
55
return
m_path
;
56
}
57
58
QCborValue
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
68
JsFile
::
JsFile
(
69
const
QString
&
filePath
,
const
QString
&
code
,
const
QDateTime
&
lastDataUpdateAt
,
70
int
derivedFrom
)
71
:
ExternalOwningItem
(
filePath
,
lastDataUpdateAt
,
Paths
::
qmlFilePath
(
filePath
),
derivedFrom
,
72
code
)
73
{
74
m_engine
=
std
::
make_shared
<
QQmlJS
::
Engine
>();
75
LegacyDirectivesCollector
directivesCollector
(*
this
);
76
m_engine
->
setDirectives
(&
directivesCollector
);
77
78
bool
isESM
=
filePath
.
endsWith
(u".mjs",
Qt
::
CaseInsensitive
);
79
const
QQmlJS
::
Parser
::
Result
parsed
=
QQmlJS
::
Parser
::
parseCode
(
80
m_engine
.
get
(),
code
,
81
isESM
?
QQmlJS
::
Parser
::
Goal
::
Module
:
QQmlJS
::
Parser
::
Goal
::
Script
);
82
setIsValid
(
parsed
.
success
);
83
84
for
(
const
DiagnosticMessage
&
msg
:
parsed
.
diagnosticMessages
) {
85
addErrorLocal
(
86
std
::
move
(
myParsingErrors
().
errorMessage
(
msg
).
withFile
(
filePath
).
withPath
(
m_path
)));
87
}
88
89
auto
astComments
=
std
::
make_shared
<
AstComments
>(
m_engine
);
90
91
CommentCollector
collector
;
92
collector
.
collectComments
(
m_engine
,
parsed
.
rootNode
,
astComments
);
93
m_script
=
std
::
make_shared
<
ScriptExpression
>(
code
,
m_engine
,
parsed
.
rootNode
,
astComments
,
94
isESM
?
ScriptExpression
::
ExpressionType
::
ESMCode
95
:
ScriptExpression
::
ExpressionType
::
JSCode
);
96
}
97
98
ErrorGroups
JsFile
::
myParsingErrors
()
99
{
100
static
ErrorGroups
res
= { {
DomItem
::
domErrorGroup
,
NewErrorGroup
(
"JsFile"
),
101
NewErrorGroup
(
"Parsing"
) } };
102
return
res
;
103
}
104
105
bool
JsFile
::
iterateDirectSubpaths
(
const
DomItem
&
self
,
DirectVisitor
visitor
)
const
106
{
107
bool
cont
=
ExternalOwningItem
::
iterateDirectSubpaths
(
self
,
visitor
);
108
cont
=
cont
&&
self
.
invokeVisitorOnField
(
visitor
,
Fields
::
fileLocationsTree
,
m_fileLocationsTree
);
109
if
(
m_script
)
110
cont
=
cont
&&
visitor
(
Fields
::
expression
, [
this
, &
self
]() {
111
return
self
.
subOwnerItem
(
Fields
::
expression
,
m_script
);
112
});
113
return
cont
;
114
}
115
116
void
JsFile
::
writeOut
(
const
DomItem
&
self
,
OutWriter
&
ow
)
const
117
{
118
writeOutDirectives
(
self
,
ow
);
119
ow
.
ensureNewline
(2);
120
if
(
DomItem
script
=
self
.
field
(
Fields
::
expression
)) {
121
ow
.
ensureNewline
();
122
script
.
writeOut
(
ow
);
123
}
124
}
125
126
void
JsFile
::
addFileImport
(
const
QString
&
jsfile
,
const
QString
&
module
)
127
{
128
LegacyImport
import
;
129
import
.
fileName
=
jsfile
;
130
import
.
asIdentifier
=
module
;
131
m_imports
.
append
(
std
::
move
(
import
));
132
}
133
134
void
JsFile
::
addModuleImport
(
const
QString
&
uri
,
const
QString
&
version
,
const
QString
&
module
)
135
{
136
LegacyImport
import
;
137
import
.
uri
=
uri
;
138
import
.
version
=
version
;
139
import
.
asIdentifier
=
module
;
140
m_imports
.
append
(
std
::
move
(
import
));
141
}
142
143
void
JsFile
::
LegacyPragmaLibrary
::
writeOut
(
const
DomItem
&
self
,
OutWriter
&
lw
)
const
144
{
145
Q_UNUSED
(
self
);
146
lw
.
write
(u".pragma").
ensureSpace
().
write
(u"library").
ensureNewline
();
147
}
148
149
void
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
153
Q_ASSERT
(!
fileName
.
isEmpty
() || !
uri
.
isEmpty
());
154
155
lw
.
write
(u".import").
ensureSpace
();
156
if
(!
uri
.
isEmpty
()) {
157
lw
.
write
(
uri
).
ensureSpace
();
158
if
(!
version
.
isEmpty
()) {
159
lw
.
write
(
version
).
ensureSpace
();
160
}
161
}
else
{
162
lw
.
write
(u"\"").
write
(
fileName
).
write
(u"\"").
ensureSpace
();
163
}
164
lw
.
writeRegion
(
fLoc
,
AsTokenRegion
).
ensureSpace
().
write
(
asIdentifier
);
165
166
lw
.
ensureNewline
();
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
*/
179
void
JsFile
::
writeOutDirectives
(
const
DomItem
&
self
,
OutWriter
&
ow
)
const
180
{
181
if
(
m_pragmaLibrary
.
has_value
()) {
182
m_pragmaLibrary
->
writeOut
(
self
,
ow
);
183
}
184
for
(
const
auto
&
import
:
m_imports
) {
185
import
.
writeOut
(
self
,
ow
);
186
}
187
}
188
189
std
::
shared_ptr
<
OwningItem
>
QmlFile
::
doCopy
(
const
DomItem
&)
const
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
213
QmlFile
::
QmlFile
(
214
const
QString
&
filePath
,
const
QString
&
code
,
const
QDateTime
&
lastDataUpdateAt
,
215
int
derivedFrom
,
RecoveryOption
option
)
216
:
ExternalOwningItem
(
filePath
,
lastDataUpdateAt
,
Paths
::
qmlFilePath
(
filePath
),
derivedFrom
,
217
code
),
218
m_engine
(
new
QQmlJS
::
Engine
)
219
{
220
const
bool
recovery
=
option
==
EnableParserRecovery
;
221
const
QQmlJS
::
Parser
::
Result
parsed
=
QQmlJS
::
Parser
::
parseCode
(
222
m_engine
.
get
(),
code
,
QQmlJS
::
Parser
::
Goal
::
QmlDocument
,
223
QQmlJS
::
Parser
::
OptionsBuilder
().
setRecovery
(
recovery
).
build
());
224
m_isValid
=
parsed
.
success
;
225
for
(
const
DiagnosticMessage
&
msg
:
parsed
.
diagnosticMessages
) {
226
addErrorLocal
(
227
std
::
move
(
myParsingErrors
().
errorMessage
(
msg
).
withFile
(
filePath
).
withPath
(
m_path
)));
228
}
229
m_ast
=
QQmlJS
::
AST
::
cast
<
QQmlJS
::
AST
::
UiProgram
*>(
parsed
.
rootNode
);
230
}
231
232
ErrorGroups
QmlFile
::
myParsingErrors
()
233
{
234
static
ErrorGroups
res
= { {
DomItem
::
domErrorGroup
,
NewErrorGroup
(
"QmlFile"
),
235
NewErrorGroup
(
"Parsing"
) } };
236
return
res
;
237
}
238
239
bool
QmlFile
::
iterateDirectSubpaths
(
const
DomItem
&
self
,
DirectVisitor
visitor
)
const
240
{
241
auto
&
members
=
lazyMembers
();
242
bool
cont
=
ExternalOwningItem
::
iterateDirectSubpaths
(
self
,
visitor
);
243
cont
=
cont
&&
self
.
invokeVisitorOnField
(
visitor
,
Fields
::
components
,
members
.
m_components
);
244
cont
=
cont
&&
self
.
invokeVisitorOnField
(
visitor
,
Fields
::
pragmas
,
members
.
m_pragmas
);
245
cont
=
cont
&&
self
.
invokeVisitorOnField
(
visitor
,
Fields
::
imports
,
members
.
m_imports
);
246
cont
=
cont
&&
self
.
invokeVisitorOnField
(
visitor
,
Fields
::
fileLocationsTree
,
247
members
.
m_fileLocationsTree
);
248
cont
=
cont
&&
self
.
invokeVisitorOnField
(
visitor
,
Fields
::
comments
,
members
.
m_comments
);
249
cont
=
cont
&&
self
.
invokeVisitorOnField
(
visitor
,
Fields
::
astComments
,
members
.
m_astComments
);
250
return
cont
;
251
}
252
253
DomItem
QmlFile
::
field
(
const
DomItem
&
self
,
FieldName
name
)
const
254
{
255
ensurePopulated
();
256
if
(
name
==
Fields
::
components
)
257
return
self
.
wrap
(
Fields
::
components
,
lazyMembers
().
m_components
);
258
return
DomBase
::
field
(
self
,
name
);
259
}
260
261
void
QmlFile
::
addError
(
const
DomItem
&
self
,
ErrorMessage
&&
msg
)
262
{
263
m_isValid
=
false
;
264
self
.
containingObject
().
addError
(
std
::
move
(
msg
));
265
}
266
267
void
QmlFile
::
writeOut
(
const
DomItem
&
self
,
OutWriter
&
ow
)
const
268
{
269
ensurePopulated
();
270
for
(
const
DomItem
&
p
:
self
.
field
(
Fields
::
pragmas
).
values
()) {
271
p
.
writeOut
(
ow
);
272
}
273
auto
imports
=
self
.
field
(
Fields
::
imports
).
values
();
274
275
if
(
ow
.
lineWriter
.
options
().
sortImports
) {
276
std
::
stable_sort
(
imports
.
begin
(),
imports
.
end
(),
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);
288
DomItem
mainC
=
self
.
field
(
Fields
::
components
).
key
(
QString
()).
index
(0);
289
mainC
.
writeOut
(
ow
);
290
}
291
292
}
// end namespace Dom
293
}
// end namespace QQmlJS
294
295
QT_END_NAMESPACE
QQmlJS::Dom
Definition
qqmljsimportvisitor_p.h:38
QQmlJS::Dom::pluginData
QCborValue pluginData(const QQmlDirParser::Plugin &pl, const QStringList &cNames)
Definition
qqmldomexternalitems.cpp:58
QQmlJS
Definition
qv4bytecodegenerator_p.h:27
NewErrorGroup
#define NewErrorGroup(name)
Definition
qqmldomerrormessage_p.h:43
qtdeclarative
src
qmldom
qqmldomexternalitems.cpp
Generated on
for Qt by
1.18.0