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
qqmljslintertypepropagator.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
7#include <private/qqmljsutils_p.h>
8
9#include <private/qqmljslintercodegen_p.h>
10
12
13using namespace Qt::StringLiterals;
14
16 const QV4::Compiler::JSUnitGenerator *unitGenerator, const QQmlJSTypeResolver *typeResolver,
17 QQmlJSLogger *logger, const QQmlJS::LinterContext &context, const BasicBlocks &basicBlocks,
18 const InstructionAnnotations &annotations, QQmlSA::PassManager *passManager)
22{
23}
24
26{
27 QQmlJSTypePropagator::generate_Ret();
28
29 if (m_function->isSignalHandler) {
30 // Signal handlers cannot return anything.
31 } else if (m_state.accumulatorIn().contains(m_typeResolver->voidType())) {
32 // You can always return undefined.
33 } else if (!m_returnType.isValid() && m_state.accumulatorIn().isValid()) {
34 if (m_function->isFullyTyped) {
35 // Do not complain if the function didn't have a valid annotation in the first place.
36 m_logger->log(u"Function without return type annotation returns %1"_s.arg(
37 m_state.accumulatorIn().containedTypeName()),
38 qmlIncompatibleType, currentFunctionSourceLocation());
39 }
40 } else if (!canConvertFromTo(m_state.accumulatorIn(), m_returnType)) {
41 m_logger->log(u"Cannot assign binding of type %1 to %2"_s.arg(
42 m_state.accumulatorIn().containedTypeName(),
43 m_returnType.containedTypeName()),
44 qmlIncompatibleType, currentFunctionSourceLocation());
45 }
46
47 const QQmlJS::SourceLocation location = m_function->isProperty
48 ? currentFunctionSourceLocation()
49 : currentNonEmptySourceLocation();
50 QQmlSA::PassManagerPrivate::get(m_passManager)
51 ->analyzeBinding(
52 QQmlJSScope::createQQmlSAElement(m_function->qmlScope.containedType()),
53 QQmlJSScope::createQQmlSAElement(m_state.accumulatorIn().containedType()),
54 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(location));
55}
56
58{
59 QQmlJSTypePropagator::generate_LoadQmlContextPropertyLookup(index);
60
61 Q_ASSERT(m_idMemberShadows);
62
63 const int nameIndex = m_jsUnitGenerator->lookupNameIndex(index);
64 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
65
66 const auto qmlScope = m_function->qmlScope.containedType();
67 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
68 QQmlJSScope::createQQmlSAElement(qmlScope), name,
69 QQmlJSScope::createQQmlSAElement(qmlScope),
70 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
71 currentNonEmptySourceLocation()));
72
73 const auto &accumulatorOut = m_state.accumulatorOut();
74 if (!accumulatorOut.isValid())
75 return;
76
77 // complain about renamed types in enums like MyType.Enum.EnumValue
78 if (accumulatorOut.variant() == QQmlJSRegisterContent::Attachment
79 || accumulatorOut.variant() == QQmlJSRegisterContent::MetaType) {
80 m_context.renamedComponents.handleRenamedType(accumulatorOut.scopeType(), name,
81 currentNonEmptySourceLocation(), m_logger);
82 }
83
84 const QQmlJSScope::ConstPtr scope = accumulatorOut.scopeType();
85 const QQmlJSScope::ConstPtr idScope = m_context.scopesById.scope(name, scope);
86 if (!idScope.isNull()) {
87 const auto log = [&](const auto &memberType, const auto &memberOwnerScope) {
88 IdMemberShadow idMemberShadow{ name, idScope, memberOwnerScope };
89
90 // Only warn once per shadowing instance, even for multiple usages.
91 if (m_idMemberShadows->contains(idMemberShadow))
92 return;
93
94 m_idMemberShadows->insert(std::move(idMemberShadow));
95 const auto useLoc = currentSourceLocation();
96 m_logger->log("Id for object %1 shadows %2 \"%3\". Rename one or the other."_L1
97 .arg(idScope->baseTypeName(), memberType, name),
98 qmlIdShadowsMember, useLoc);
99 m_logger->log("Note: Id defined here"_L1, qmlIdShadowsMember,
100 idScope->idSourceLocation(), true, true, {}, useLoc.startLine);
101 };
102
103 if (scope->hasProperty(name)) {
104 log("property"_L1, scope->ownerOfProperty(scope, name).scope);
105 } else if (scope->hasMethod(name)) {
106 const auto methods = scope->methods(name);
107 const auto &method = methods[0];
108 if (method.methodType() == QQmlSA::MethodType::Method)
109 log("method"_L1, scope->ownerOfMethod(scope, name).scope);
110 else if (method.methodType() == QQmlSA::MethodType::Signal)
111 log("signal"_L1, scope->ownerOfMethod(scope, name).scope);
112 }
113 }
114}
115
117{
118 QQmlJSTypePropagator::generate_GetOptionalLookup(index, offset);
119
120 auto suggMsg = "Consider using non-optional chaining instead: '?.' -> '.'"_L1;
121 auto suggestion = std::make_optional(QQmlJSFixSuggestion(suggMsg, currentSourceLocation()));
122 if (m_state.accumulatorOut().variant() == QQmlJSRegisterContent::Enum) {
123 m_logger->log("Redundant optional chaining for enum lookup"_L1, qmlRedundantOptionalChaining,
124 currentSourceLocation(), true, true, suggestion);
125 } else if (!m_state.accumulatorIn().containedType()->isReferenceType()
126 && !m_typeResolver->canHoldUndefined(m_state.accumulatorIn())) {
127 auto baseType = m_state.accumulatorIn().containedTypeName();
128 m_logger->log("Redundant optional chaining for lookup on non-voidable and non-nullable "_L1
129 "type %1"_L1.arg(baseType), qmlRedundantOptionalChaining,
130 currentSourceLocation(), true, true, suggestion);
131 }
132}
133
135{
136 QQmlJSTypePropagator::generate_StoreProperty(nameIndex, base);
137
138 auto callBase = m_state.registers[base].content;
139 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
140 const bool isAttached = callBase.variant() == QQmlJSRegisterContent::Attachment;
141
142 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
143 QQmlJSScope::createQQmlSAElement(callBase.containedType()),
144 propertyName,
145 QQmlJSScope::createQQmlSAElement(
146 m_state.accumulatorIn().containedType()),
147 QQmlJSScope::createQQmlSAElement(isAttached
148 ? callBase.attachee().containedType()
149 : m_function->qmlScope.containedType()),
150 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
151 currentNonEmptySourceLocation()));
152}
153
154void QQmlJSLinterTypePropagator::generate_CallProperty(int nameIndex, int base, int argc, int argv)
155{
156 QQmlJSTypePropagator::generate_CallProperty(nameIndex, base, argc, argv);
157
158 const auto saCheck = [&](const QString &propertyName, const QQmlJSScope::ConstPtr &baseType) {
159 const QQmlSA::Element saBaseType{ QQmlJSScope::createQQmlSAElement(baseType) };
160 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
161 m_function->qmlScope.containedType()) };
162 const QQmlSA::SourceLocation saLocation{
163 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
164 };
165
166 QQmlSA::PassManagerPrivate::get(m_passManager)
167 ->analyzeRead(saBaseType, propertyName, saContainedType, saLocation);
168 QQmlSA::PassManagerPrivate::get(m_passManager)
169 ->analyzeCall(saBaseType, propertyName, saContainedType, saLocation);
170 };
171
172 const auto callBase = m_state.registers[base].content;
173 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
174 const auto member = m_typeResolver->memberType(callBase, propertyName);
175
176 const bool isLoggingMethod = QQmlJSTypePropagator::isLoggingMethod(propertyName);
177 if (callBase.contains(m_typeResolver->mathObject()))
178 saCheck(propertyName, callBase.containedType());
179 else if (callBase.contains(m_typeResolver->consoleObject()) && isLoggingMethod)
180 saCheck(propertyName, callBase.containedType());
181 else if (!member.isMethod()) {
182 if (callBase.contains(m_typeResolver->jsValueType())
183 || callBase.contains(m_typeResolver->varType())) {
184 saCheck(propertyName, callBase.containedType());
185 }
186 }
187}
188
190{
191 QQmlJSTypePropagator::generate_CallPossiblyDirectEval(argc, argv);
192
193 const QQmlSA::SourceLocation saLocation{
194 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
195 };
196 const QQmlSA::Element saBaseType{ QQmlJSScope::createQQmlSAElement(
197 m_typeResolver->jsGlobalObject()) };
198 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
199 m_function->qmlScope.containedType()) };
200
201 QQmlSA::PassManagerPrivate::get(m_passManager)
202 ->analyzeCall(saBaseType, "eval"_L1, saContainedType, saLocation);
203}
204
205// Only to be called once a lookup has already failed
206QQmlJSLinterTypePropagator::PropertyResolution
207QQmlJSLinterTypePropagator::propertyResolution(QQmlJSScope::ConstPtr scope,
208 const QString &propertyName) const
209{
210 auto property = scope->property(propertyName);
211 if (!property.isValid())
212 return PropertyMissing;
213
214 QString errorType;
215 if (property.type().isNull())
216 errorType = u"found"_s;
217 else if (!property.type()->isFullyResolved())
218 errorType = u"fully resolved"_s;
219 else
220 return PropertyFullyResolved;
221
222 Q_ASSERT(!errorType.isEmpty());
223
224 m_logger->log(
225 u"Type \"%1\" of property \"%2\" not %3. This is likely due to a missing dependency entry or a type not being exposed declaratively."_s
226 .arg(property.typeName(), propertyName, errorType),
227 qmlUnresolvedType, currentSourceLocation());
228
229 return PropertyTypeUnresolved;
230}
231
232void QQmlJSLinterTypePropagator::handleUnqualifiedAccess(const QString &name, bool isMethod) const
233{
234 QQmlJSTypePropagator::handleUnqualifiedAccess(name, isMethod);
235
236 auto location = currentSourceLocation();
237
238 const auto qmlScopeContained = m_function->qmlScope.containedType();
239 if (qmlScopeContained->isInCustomParserParent()) {
240 // Only ignore custom parser based elements if it's not Connections.
241 if (qmlScopeContained->baseType().isNull()
242 || qmlScopeContained->baseType()->internalName() != u"QQmlConnections"_s)
243 return;
244 }
245
246 if (isMethod) {
247 if (isCallingProperty(qmlScopeContained, name))
248 return;
249 } else if (propertyResolution(qmlScopeContained, name) != PropertyMissing) {
250 return;
251 }
252
253 std::optional<QQmlJSFixSuggestion> suggestion;
254
255 const auto childScopes = m_function->qmlScope.containedType()->childScopes();
256 for (qsizetype i = 0, end = childScopes.size(); i < end; i++) {
257 auto &scope = childScopes[i];
258 if (location.offset > scope->sourceLocation().offset) {
259 if (i + 1 < end
260 && childScopes.at(i + 1)->sourceLocation().offset < location.offset)
261 continue;
262 if (scope->childScopes().size() == 0)
263 continue;
264
265 const auto jsId = scope->childScopes().first()->jsIdentifier(name);
266
267 if (jsId.has_value() && jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) {
268 const QQmlJSScope::JavaScriptIdentifier id = jsId.value();
269
270 QQmlJS::SourceLocation fixLocation = id.location;
271 Q_UNUSED(fixLocation)
272 fixLocation.length = 0;
273
274 const auto handler = m_typeResolver->signalHandlers()[id.location];
275
276 QString fixString = handler.isMultiline ? u"function("_s : u"("_s;
277 const auto parameters = handler.signalParameters;
278 for (int numParams = parameters.size(); numParams > 0; --numParams) {
279 fixString += parameters.at(parameters.size() - numParams);
280 if (numParams > 1)
281 fixString += u", "_s;
282 }
283
284 fixString += handler.isMultiline ? u") "_s : u") => "_s;
285 const auto msg = u"\"%1\" is ambiguous. Use a function instead: %2%3"_s.arg(
286 name, fixString, handler.isMultiline ? "{ ... }"_L1 : "..."_L1);
287 QQmlJSDocumentEdit documentEdit{ m_logger->filePath(), fixLocation, fixString };
288 suggestion = {{ msg, fixLocation, documentEdit }};
289 suggestion->setAutoApplicable();
290 }
291 break;
292 }
293 }
294
295 // Might be a delegate just missing a required property.
296 // This heuristic does not recognize all instances of this occurring but should be sufficient
297 // protection against wrongly suggesting to add an id to the view to access the model that way
298 // which is very misleading
299 const auto qmlScope = m_function->qmlScope.containedType();
300 if (name == u"model" || name == u"index") {
301 if (const QQmlJSScope::ConstPtr parent = qmlScope->parentScope(); !parent.isNull()) {
302 const auto bindings = parent->ownPropertyBindings(u"delegate"_s);
303
304 for (auto it = bindings.first; it != bindings.second; it++) {
305 if (!it->hasObject())
306 continue;
307 if (it->objectType() == qmlScope) {
308 suggestion = QQmlJSFixSuggestion {
309 "'%1' is implicitly injected into this delegate. "
310 "Add a required property '%1' to the delegate instead."_L1
311 .arg(name),
312 qmlScope->sourceLocation()
313 };
314 };
315
316 break;
317 }
318 }
319 }
320
321 if (!suggestion.has_value()) {
322 for (QQmlJSScope::ConstPtr scope = qmlScope; !scope.isNull(); scope = scope->parentScope()) {
323 if (scope->hasProperty(name)) {
324 QQmlJSScopesById::MostLikelyCallback<QString> id;
325 m_function->addressableScopes.possibleIds(scope, qmlScope,
326 QQmlJSScopesByIdOption::Default, id);
327
328 QQmlJS::SourceLocation fixLocation = location;
329 fixLocation.length = 0;
330 QString m = "%1 is a member of a parent element.\n You can qualify the "
331 "access with its id to avoid this warning%2.\n"_L1.arg(name);
332 m = m.arg(id.result.isEmpty() ? " (You first have to give the element an id)"_L1 : ""_L1);
333
334 suggestion = QQmlJSFixSuggestion{
335 m, fixLocation, { m_logger->filePath(), fixLocation,
336 (id.result.isEmpty() ? u"<id>."_s : (id.result + u'.')) }
337 };
338
339 if (!id.result.isEmpty())
340 suggestion->setAutoApplicable();
341 }
342 }
343 }
344
345 if (!suggestion.has_value() && !m_function->addressableScopes.componentsAreBound()
346 && m_function->addressableScopes.existsAnywhereInDocument(name)) {
347 const QLatin1String replacement = "pragma ComponentBehavior: Bound"_L1;
348 QQmlJSFixSuggestion bindComponents {
349 "Set \"%1\" in order to use IDs from outer components in nested components."_L1
350 .arg(replacement),
351 QQmlJS::s_documentOrigin,
352 QQmlJSDocumentEdit{ m_logger->filePath(), QQmlJS::s_documentOrigin, replacement + u'\n' }
353 };
354 bindComponents.setAutoApplicable();
355 suggestion = std::move(bindComponents);
356 }
357
358 if (!suggestion.has_value()) {
359 if (auto didYouMean = QQmlJSUtils::didYouMean(
360 name, qmlScope->properties().keys() + qmlScope->methods().keys(),
361 m_logger->filePath(), location);
362 didYouMean.has_value()) {
363 suggestion = std::move(didYouMean);
364 }
365 }
366
367 m_logger->log(QLatin1String("Unqualified access"), qmlUnqualified, location, true, true,
368 suggestion);
369}
370
371static bool shouldMentionRequiredProperties(const QQmlJSScope::ConstPtr &qmlScope)
372{
373 if (!qmlScope->isWrappedInImplicitComponent() && !qmlScope->isFileRootComponent()
374 && !qmlScope->isInlineComponent()) {
375 return false;
376 }
377
378 const auto properties = qmlScope->properties();
379 return std::none_of(properties.constBegin(), properties.constEnd(),
380 [&qmlScope](const QQmlJSMetaProperty &property) {
381 return qmlScope->isPropertyRequired(property.propertyName());
382 });
383}
384
386 const QString &name, bool isMethod) const
387{
388 QQmlJSTypePropagator::handleUnqualifiedAccessAndContextProperties(name, isMethod);
389
390 if (m_context.userContextProperties.isUnqualifiedAccessDisabled(name))
391 return;
392
393 const auto warningMessage = [&name, this]() {
394 QString result =
395 "Potential context property access detected."
396 " Context properties are discouraged in QML: use normal, required, or singleton properties instead."_L1;
397
398 if (shouldMentionRequiredProperties(m_function->qmlScope.containedType())) {
399 result.append(
400 "\nNote: '%1' assumed to be a potential context property because it is not declared as required property."_L1
401 .arg(name));
402 }
403 return result;
404 };
405
406 if (m_context.userContextProperties.isOnUsageWarned(name)) {
407 m_logger->log(warningMessage(), qmlContextProperties, currentSourceLocation());
408 return;
409 }
410
411 // name is not the name of a user context property, so emit the unqualified warning.
412 handleUnqualifiedAccess(name, isMethod);
413
414 const QList<QQmlJS::HeuristicContextProperty> definitions =
415 m_context.heuristicContextProperties.definitionsForName(name);
416 if (definitions.isEmpty())
417 return;
418 QString warning = warningMessage();
419 for (const auto &candidate : definitions) {
420 warning.append("\nNote: candidate context property declaration '%1' at %2:%3:%4"_L1.arg(
421 name, QDir::cleanPath(candidate.filename),
422 QString::number(candidate.location.startLine),
423 QString::number(candidate.location.startColumn)));
424 }
425 m_logger->log(warning, qmlContextProperties, currentSourceLocation());
426}
427
428void QQmlJSLinterTypePropagator::checkDeprecated(QQmlJSScope::ConstPtr scope, const QString &name,
429 bool isMethod) const
430{
431 QQmlJSTypePropagator::checkDeprecated(scope, name, isMethod);
432
433 Q_ASSERT(!scope.isNull());
434 auto qmlScope = QQmlJSScope::findCurrentQMLScope(scope);
435 if (qmlScope.isNull())
436 return;
437
438 QList<QQmlJSAnnotation> annotations;
439
440 QQmlJSMetaMethod method;
441
442 if (isMethod) {
443 const QList<QQmlJSMetaMethod> methods = qmlScope->methods(name);
444 if (methods.isEmpty())
445 return;
446 method = methods.constFirst();
447 annotations = method.annotations();
448 } else {
449 QQmlJSMetaProperty property = qmlScope->property(name);
450 if (!property.isValid())
451 return;
452 annotations = property.annotations();
453 }
454
455 auto deprecationAnn = std::find_if(
456 annotations.constBegin(), annotations.constEnd(),
457 [](const QQmlJSAnnotation &annotation) { return annotation.isDeprecation(); });
458
459 if (deprecationAnn == annotations.constEnd())
460 return;
461
462 QQQmlJSDeprecation deprecation = deprecationAnn->deprecation();
463
464 QString descriptor = name;
465 if (isMethod)
466 descriptor += u'(' + method.parameterNames().join(u", "_s) + u')';
467
468 QString message = "%1 \"%2\" is deprecated"_L1
469 .arg(isMethod ? u"Method"_s : u"Property"_s, descriptor);
470
471 if (!deprecation.reason.isEmpty())
472 message.append(QStringLiteral(" (Reason: %1)").arg(deprecation.reason));
473
474 m_logger->log(message, qmlDeprecated, currentSourceLocation());
475}
476
477bool QQmlJSLinterTypePropagator::isCallingProperty(QQmlJSScope::ConstPtr scope,
478 const QString &name) const
479{
480 const bool res = QQmlJSTypePropagator::isCallingProperty(scope, name);
481
482 if (const auto property = scope->property(name); property.isValid()) {
483 QString errorType;
484 if (property.type() == m_typeResolver->varType()) {
485 errorType = u"a var property. It may or may not be a method. "_s
486 u"Use a regular function instead."_s;
487 } else if (property.type() == m_typeResolver->jsValueType()) {
488 errorType = u"a QJSValue property. It may or may not be a method. "_s
489 u"Use a regular Q_INVOKABLE instead."_s;
490 } else {
491 errorType = u"not a method"_s;
492 }
493
494 m_logger->log(u"Property \"%1\" is %2"_s.arg(name, errorType),
495 qmlUseProperFunction, currentSourceLocation(), true, true, {});
496 }
497
498 return res;
499}
500
502{
503 const auto res = QQmlJSTypePropagator::handleImportNamespaceLookup(propertyName);
504
505 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
506 if (m_typeResolver->isPrefix(propertyName)) {
507 if (!accumulatorIn.containedType()->isReferenceType()) {
508 m_logger->log(u"Cannot use non-QObject type %1 to access prefixed import"_s.arg(
509 accumulatorIn.containedType()->internalName()),
510 qmlPrefixedImportType,
511 currentSourceLocation());
512 }
513 } else if (accumulatorIn.isImportNamespace()) {
514 m_logger->log(u"Type not found in namespace"_s, qmlUnresolvedType,
515 currentSourceLocation());
516 }
517
518 return res;
519}
520
521bool QQmlJSLinterTypePropagator::checkTypeResolved(const QQmlJSScope::ConstPtr &type)
522{
523 if (type->isFullyResolved() || type->isScript())
524 return true;
525
526 if (!m_context.knownUnresolvedTypes.hasSeen(type)) {
527
528 m_logger->log(QStringLiteral("Type %1 is used but it is not resolved")
529 .arg(QQmlJSUtils::getScopeName(type, type->scopeType())),
530 qmlUnresolvedType, currentSourceLocation());
531 }
532
533 return false;
534}
535
536void QQmlJSLinterTypePropagator::handleLookupError(const QString &propertyName)
537{
538 QQmlJSTypePropagator::handleLookupError(propertyName);
539
540 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
541 const QString typeName = accumulatorIn.containedTypeName();
542
543 if (typeName == u"QVariant")
544 return;
545 if (accumulatorIn.isList() && propertyName == u"length")
546 return;
547
548 auto baseType = accumulatorIn.containedType();
549 // Warn separately when a property is only not found because of a missing type
550
551 if (propertyResolution(baseType, propertyName) != PropertyMissing)
552 return;
553
554 if (baseType->isScript())
555 return;
556
557 std::optional<QQmlJSFixSuggestion> fixSuggestion;
558
559 if (auto suggestion = QQmlJSUtils::didYouMean(propertyName, baseType->properties().keys(),
560 m_logger->filePath(), currentSourceLocation());
561 suggestion.has_value()) {
562 fixSuggestion = std::move(suggestion);
563 }
564
565 if (!fixSuggestion.has_value()
566 && accumulatorIn.variant() == QQmlJSRegisterContent::MetaType) {
567
568 const QQmlJSScope::ConstPtr scopeType = accumulatorIn.scopeType();
569 const auto metaEnums = scopeType->enumerations();
570 const bool enforcesScoped = scopeType->enforcesScopedEnums();
571
572 QStringList enumKeys;
573 for (const QQmlJSMetaEnum &metaEnum : metaEnums) {
574 if (!enforcesScoped || !metaEnum.isScoped())
575 enumKeys << metaEnum.keys();
576 }
577
578 if (auto suggestion = QQmlJSUtils::didYouMean(
579 propertyName, enumKeys, m_logger->filePath(), currentSourceLocation());
580 suggestion.has_value()) {
581 fixSuggestion = std::move(suggestion);
582 }
583 }
584
585 if (checkTypeResolved(baseType)) {
586 m_logger->log(u"Member \"%1\" not found on type \"%2\""_s.arg(propertyName, typeName),
587 qmlMissingProperty, currentSourceLocation(), true, true, fixSuggestion);
588 }
589}
590
591bool QQmlJSLinterTypePropagator::checkForEnumProblems(QQmlJSRegisterContent base,
592 const QString &propertyName)
593{
594 const bool res = QQmlJSTypePropagator::checkForEnumProblems(base, propertyName);
595
596 if (base.isEnumeration()) {
597 const auto metaEnum = base.enumeration();
598 if (!metaEnum.hasKey(propertyName)) {
599 const auto fixSuggestion = QQmlJSUtils::didYouMean(
600 propertyName, metaEnum.keys(), m_logger->filePath(), currentSourceLocation());
601 const QString error = u"\"%1\" is not an entry of enum \"%2\"."_s
602 .arg(propertyName, metaEnum.name());
603 m_logger->log(error, qmlMissingEnumEntry, currentSourceLocation(), true, true,
604 fixSuggestion);
605 }
606 }
607
608 return res;
609}
610
612{
613 QQmlJSTypePropagator::generate_StoreNameCommon(nameIndex);
614
615 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
616 const QQmlJSRegisterContent in = m_state.accumulatorIn();
617 const bool isAttached = in.variant() == QQmlJSRegisterContent::Attachment;
618
619 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
620 QQmlJSScope::createQQmlSAElement(
621 m_state.accumulatorIn().containedType()),
622 name,
623 QQmlJSScope::createQQmlSAElement(isAttached
624 ? in.attachee().containedType()
625 : m_function->qmlScope.containedType()),
626 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
627 currentNonEmptySourceLocation()));
628
629 const auto qmlScope = m_function->qmlScope.containedType();
630 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
631 QQmlJSScope::createQQmlSAElement(qmlScope), name,
632 QQmlJSScope::createQQmlSAElement(in.containedType()),
633 QQmlJSScope::createQQmlSAElement(qmlScope),
634 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
635 currentNonEmptySourceLocation()));
636}
637
638void QQmlJSLinterTypePropagator::propagatePropertyLookup(const QString &name, int lookupIndex)
639{
640 QQmlJSTypePropagator::propagatePropertyLookup(name, lookupIndex);
641
642 const QQmlJSRegisterContent in = m_state.accumulatorIn();
643 const bool isAttached = in.variant() == QQmlJSRegisterContent::Attachment;
644
645 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
646 QQmlJSScope::createQQmlSAElement(
647 m_state.accumulatorIn().containedType()),
648 name,
649 QQmlJSScope::createQQmlSAElement(isAttached
650 ? in.attachee().containedType()
651 : m_function->qmlScope.containedType()),
652 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
653 currentNonEmptySourceLocation()));
654}
655
656void QQmlJSLinterTypePropagator::propagateCall(const QList<QQmlJSMetaMethod> &methods, int argc, int argv,
657 QQmlJSRegisterContent scope)
658{
659 QQmlJSTypePropagator::propagateCall(methods, argc, argv, scope);
660
661 QStringList errors;
662 const QQmlJSMetaMethod match = bestMatchForCall(methods, argc, argv, &errors);
663 if (!match.isValid())
664 return;
665
666 const QQmlSA::Element saBaseType = QQmlJSScope::createQQmlSAElement(scope.containedType());
667 const QQmlSA::SourceLocation saLocation{
668 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
669 };
670 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
671 m_function->qmlScope.containedType()) };
672
673 QQmlSA::PassManagerPrivate::get(m_passManager)
674 ->analyzeCall(saBaseType, match.methodName(), saContainedType, saLocation);
675}
676
678{
679 QQmlJSTypePropagator::propagateTranslationMethod_SAcheck(methodName);
680
681 QQmlSA::PassManagerPrivate::get(m_passManager)
682 ->analyzeCall(QQmlJSScope::createQQmlSAElement(m_typeResolver->jsGlobalObject()),
683 methodName,
684 QQmlJSScope::createQQmlSAElement(m_function->qmlScope.containedType()),
685 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
686 currentNonEmptySourceLocation()));
687}
688
689static bool mightContainStringOrNumberOrBoolean(const QQmlJSScope::ConstPtr &scope,
690 const QQmlJSTypeResolver *resolver)
691{
692 return scope == resolver->varType() || scope == resolver->jsValueType()
693 || scope == resolver->jsPrimitiveType();
694}
695
696static bool isStringOrNumberOrBoolean(const QQmlJSScope::ConstPtr &scope,
697 const QQmlJSTypeResolver *resolver)
698{
699 return scope == resolver->boolType() || scope == resolver->stringType()
700 || resolver->isNumeric(scope);
701}
702
703static bool isVoidOrUndefined(const QQmlJSScope::ConstPtr &scope,
704 const QQmlJSTypeResolver *resolver)
705{
706 return scope == resolver->nullType() || scope == resolver->voidType();
707}
708
709static bool requiresStrictEquality(const QQmlJSScope::ConstPtr &lhs,
710 const QQmlJSScope::ConstPtr &rhs,
711 const QQmlJSTypeResolver *resolver)
712{
713 if (lhs == rhs)
714 return false;
715
716 if (resolver->isNumeric(lhs) && resolver->isNumeric(rhs))
717 return false;
718
719 if (isVoidOrUndefined(lhs, resolver) || isVoidOrUndefined(rhs, resolver))
720 return false;
721
722 if (isStringOrNumberOrBoolean(lhs, resolver)
723 && !mightContainStringOrNumberOrBoolean(rhs, resolver)) {
724 return true;
725 }
726
727 if (isStringOrNumberOrBoolean(rhs, resolver)
728 && !mightContainStringOrNumberOrBoolean(lhs, resolver)) {
729 return true;
730 }
731
732 return false;
733}
734
736{
737 const QQmlJSScope::ConstPtr lhsType = checkedInputRegister(lhs).containedType();
738 const QQmlJSScope::ConstPtr rhsType = m_state.accumulatorIn().containedType();
739
740 if (!requiresStrictEquality(lhsType, rhsType, m_typeResolver))
741 return;
742
743 m_logger->log("== and != may perform type coercion, use === or !== to avoid it."_L1,
744 qmlEqualityTypeCoercion, currentNonEmptySourceLocation());
745}
746
747QT_END_NAMESPACE
QQmlJSLinterTypePropagator(const QV4::Compiler::JSUnitGenerator *unitGenerator, const QQmlJSTypeResolver *typeResolver, QQmlJSLogger *logger, const QQmlJS::LinterContext &linterContext, const BasicBlocks &basicBlocks={ }, const InstructionAnnotations &annotations={ }, QQmlSA::PassManager *passManager=nullptr)
void propagateCall(const QList< QQmlJSMetaMethod > &methods, int argc, int argv, QQmlJSRegisterContent scope) override
void handleUnqualifiedAccess(const QString &name, bool isMethod) const override
void generate_CallProperty(int nameIndex, int base, int argc, int argv) override
bool isCallingProperty(QQmlJSScope::ConstPtr scope, const QString &name) const override
void generate_CallPossiblyDirectEval(int argc, int argv) override
void generate_LoadQmlContextPropertyLookup(int index) override
void generate_StoreNameCommon(int nameIndex) override
void handleUnqualifiedAccessAndContextProperties(const QString &name, bool isMethod) const override
void generate_GetOptionalLookup(int index, int offset) override
bool checkForEnumProblems(QQmlJSRegisterContent base, const QString &propertyName) override
void generate_StoreProperty(int nameIndex, int base) override
void handleLookupError(const QString &propertyName) override
void propagateTranslationMethod_SAcheck(const QString &methodName) override
bool handleImportNamespaceLookup(const QString &propertyName) override
void propagatePropertyLookup(const QString &name, int lookupIndex=QQmlJSRegisterContent::InvalidLookupIndex) override
void checkDeprecated(QQmlJSScope::ConstPtr scope, const QString &name, bool isMethod) const override
Combined button and popup list for selecting options.
static bool isStringOrNumberOrBoolean(const QQmlJSScope::ConstPtr &scope, const QQmlJSTypeResolver *resolver)
static bool shouldMentionRequiredProperties(const QQmlJSScope::ConstPtr &qmlScope)
static bool isVoidOrUndefined(const QQmlJSScope::ConstPtr &scope, const QQmlJSTypeResolver *resolver)
static bool mightContainStringOrNumberOrBoolean(const QQmlJSScope::ConstPtr &scope, const QQmlJSTypeResolver *resolver)
static bool requiresStrictEquality(const QQmlJSScope::ConstPtr &lhs, const QQmlJSScope::ConstPtr &rhs, const QQmlJSTypeResolver *resolver)