27 QQmlJSTypePropagator::generate_Ret();
29 const auto accumulatorIn = m_state.accumulatorIn();
30 if (m_function->isSignalHandler) {
32 }
else if (accumulatorIn.contains(m_typeResolver->voidType())) {
34 }
else if (!m_returnType.isValid() && accumulatorIn.isValid()) {
35 if (m_function->isFullyTyped) {
37 m_logger->log(u"Function without return type annotation returns %1"_s.arg(
38 m_state.accumulatorIn().containedTypeName()),
39 qmlIncompatibleType, currentFunctionSourceLocation());
41 }
else if (!canConvertFromTo(accumulatorIn, m_returnType)) {
42 if (checkTypeResolved(accumulatorIn.containedType())) {
43 m_logger->log(u"Cannot assign binding of type %1 to %2"_s.arg(
44 m_state.accumulatorIn().containedTypeName(),
45 m_returnType.containedTypeName()),
46 qmlIncompatibleType, currentFunctionSourceLocation());
50 const QQmlJS::SourceLocation location = m_function->isProperty
51 ? currentFunctionSourceLocation()
52 : currentNonEmptySourceLocation();
53 QQmlSA::PassManagerPrivate::get(m_passManager)
55 QQmlJSScope::createQQmlSAElement(m_function->qmlScope.containedType()),
56 QQmlJSScope::createQQmlSAElement(m_state.accumulatorIn().containedType()),
57 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(location));
62 QQmlJSTypePropagator::generate_LoadQmlContextPropertyLookup(index);
64 Q_ASSERT(m_idMemberShadows);
66 const int nameIndex = m_jsUnitGenerator->lookupNameIndex(index);
67 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
69 const auto qmlScope = m_function->qmlScope.containedType();
70 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
71 QQmlJSScope::createQQmlSAElement(qmlScope), name,
72 QQmlJSScope::createQQmlSAElement(qmlScope),
73 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
74 currentNonEmptySourceLocation()));
76 const auto &accumulatorOut = m_state.accumulatorOut();
77 if (!accumulatorOut.isValid())
81 if (accumulatorOut.variant() == QQmlJSRegisterContent::Attachment
82 || accumulatorOut.variant() == QQmlJSRegisterContent::MetaType) {
83 m_context.renamedComponents.handleRenamedType(accumulatorOut.scopeType(), name,
84 currentNonEmptySourceLocation(), m_logger);
87 const QQmlJSScope::ConstPtr scope = accumulatorOut.scopeType();
88 const QQmlJSScope::ConstPtr idScope = m_context.scopesById.scope(name, scope);
89 if (!idScope.isNull()) {
90 const auto log = [&](
const auto &memberType,
const auto &memberOwnerScope) {
94 if (m_idMemberShadows->contains(idMemberShadow))
97 m_idMemberShadows->insert(std::move(idMemberShadow));
98 const auto useLoc = currentSourceLocation();
99 m_logger->log(
"Id for object %1 shadows %2 \"%3\". Rename one or the other."_L1
100 .arg(idScope->baseTypeName(), memberType, name),
101 qmlIdShadowsMember, useLoc);
102 m_logger->log(
"Note: Id defined here"_L1, qmlIdShadowsMember,
103 idScope->idSourceLocation(),
true,
true, {}, useLoc.startLine);
106 if (scope->hasProperty(name)) {
107 log(
"property"_L1, scope->ownerOfProperty(scope, name).scope);
108 }
else if (scope->hasMethod(name)) {
109 const auto methods = scope->methods(name);
110 const auto &method = methods[0];
111 if (method.methodType() == QQmlSA::MethodType::Method)
112 log(
"method"_L1, scope->ownerOfMethod(scope, name).scope);
113 else if (method.methodType() == QQmlSA::MethodType::Signal)
114 log(
"signal"_L1, scope->ownerOfMethod(scope, name).scope);
158 QQmlJSTypePropagator::generate_StoreProperty(nameIndex, base);
160 auto callBase = m_state.registers[base].content;
161 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
162 const bool isAttached = callBase.variant() == QQmlJSRegisterContent::Attachment;
164 checkWrite(callBase, propertyName);
166 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
167 QQmlJSScope::createQQmlSAElement(callBase.containedType()),
169 QQmlJSScope::createQQmlSAElement(
170 m_state.accumulatorIn().containedType()),
171 QQmlJSScope::createQQmlSAElement(isAttached
172 ? callBase.attachee().containedType()
173 : m_function->qmlScope.containedType()),
174 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
175 currentNonEmptySourceLocation()));
180 QQmlJSTypePropagator::generate_CallProperty(nameIndex, base, argc, argv);
182 const auto saCheck = [&](
const QString &propertyName,
const QQmlJSScope::ConstPtr &baseType) {
183 const QQmlSA::Element saBaseType{ QQmlJSScope::createQQmlSAElement(baseType) };
184 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
185 m_function->qmlScope.containedType()) };
186 const QQmlSA::SourceLocation saLocation{
187 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
190 QQmlSA::PassManagerPrivate::get(m_passManager)
191 ->analyzeRead(saBaseType, propertyName, saContainedType, saLocation);
192 QQmlSA::PassManagerPrivate::get(m_passManager)
193 ->analyzeCall(saBaseType, propertyName, saContainedType, saLocation);
196 const auto callBase = m_state.registers[base].content;
197 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
198 const auto member = m_typeResolver->memberType(callBase, propertyName);
200 const bool isLoggingMethod = QQmlJSTypePropagator::isLoggingMethod(propertyName);
201 if (callBase.contains(m_typeResolver->mathObject()))
202 saCheck(propertyName, callBase.containedType());
203 else if (callBase.contains(m_typeResolver->consoleObject()) && isLoggingMethod)
204 saCheck(propertyName, callBase.containedType());
205 else if (!member.isMethod()) {
206 if (callBase.contains(m_typeResolver->jsValueType())
207 || callBase.contains(m_typeResolver->varType())) {
208 saCheck(propertyName, callBase.containedType());
261 QQmlJSTypePropagator::handleUnqualifiedAccess(name, isMethod);
263 auto location = currentSourceLocation();
265 const auto qmlScopeContained = m_function->qmlScope.containedType();
266 if (qmlScopeContained->isInCustomParserParent()) {
268 if (qmlScopeContained->baseType().isNull()
269 || qmlScopeContained->baseType()->internalName() != u"QQmlConnections"_s)
274 if (isCallingProperty(qmlScopeContained, name))
276 }
else if (propertyResolution(qmlScopeContained, name) != PropertyMissing) {
280 std::optional<QQmlJSFixSuggestion> suggestion;
282 const auto childScopes = m_function->qmlScope.containedType()->childScopes();
283 for (qsizetype i = 0, end = childScopes.size(); i < end; i++) {
284 auto &scope = childScopes[i];
285 if (location.offset > scope->sourceLocation().offset) {
287 && childScopes.at(i + 1)->sourceLocation().offset < location.offset)
289 if (scope->childScopes().size() == 0)
292 const auto jsId = scope->childScopes().first()->jsIdentifier(name);
294 if (jsId.has_value() && jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) {
295 const QQmlJSScope::JavaScriptIdentifier id = jsId.value();
297 QQmlJS::SourceLocation fixLocation = id.location;
298 Q_UNUSED(fixLocation)
299 fixLocation.length = 0;
301 const auto handler = m_typeResolver->signalHandlers()[id.location];
303 QString fixString = handler.isMultiline ? u"function("_s : u"("_s;
304 const auto parameters = handler.signalParameters;
305 for (
int numParams = parameters.size(); numParams > 0; --numParams) {
306 fixString += parameters.at(parameters.size() - numParams);
308 fixString += u", "_s;
311 fixString += handler.isMultiline ? u") "_s : u") => "_s;
312 const auto msg = u"\"%1\" is ambiguous. Use a function instead: %2%3"_s.arg(
313 name, fixString, handler.isMultiline ?
"{ ... }"_L1 :
"..."_L1);
314 QQmlJSDocumentEdit documentEdit{ m_logger->filePath(), fixLocation, fixString };
315 suggestion = {{ msg, fixLocation, documentEdit }};
316 suggestion->setAutoApplicable();
326 const auto qmlScope = m_function->qmlScope.containedType();
327 if (name == u"model" || name == u"index") {
328 if (
const QQmlJSScope::ConstPtr parent = qmlScope->parentScope(); !parent.isNull()) {
329 const auto bindings = parent->ownPropertyBindings(u"delegate"_s);
331 for (
auto it = bindings.first; it != bindings.second; it++) {
332 if (!it->hasObject())
334 if (it->objectType() == qmlScope) {
335 suggestion = QQmlJSFixSuggestion {
336 "'%1' is implicitly injected into this delegate. "
337 "Add a required property '%1' to the delegate instead."_L1
339 qmlScope->sourceLocation()
348 if (!suggestion.has_value()) {
349 for (QQmlJSScope::ConstPtr scope = qmlScope; !scope.isNull(); scope = scope->parentScope()) {
350 if (scope->hasProperty(name)) {
351 QQmlJSScopesById::MostLikelyCallback<QString> id;
352 m_function->addressableScopes.possibleIds(scope, qmlScope,
353 QQmlJSScopesByIdOption::Default, id);
355 QQmlJS::SourceLocation fixLocation = location;
356 fixLocation.length = 0;
357 QString m =
"%1 is a member of a parent element.\n You can qualify the "
358 "access with its id to avoid this warning%2.\n"_L1.arg(name);
359 m = m.arg(id.result.isEmpty() ?
" (You first have to give the element an id)"_L1 :
""_L1);
361 suggestion = QQmlJSFixSuggestion{
362 m, fixLocation, { m_logger->filePath(), fixLocation,
363 (id.result.isEmpty() ? u"<id>."_s : (id.result + u'.')) }
366 if (!id.result.isEmpty())
367 suggestion->setAutoApplicable();
372 if (!suggestion.has_value() && !m_function->addressableScopes.componentsAreBound()
373 && m_function->addressableScopes.existsAnywhereInDocument(name)) {
374 const QLatin1String replacement =
"pragma ComponentBehavior: Bound"_L1;
375 QQmlJSFixSuggestion bindComponents {
376 "Set \"%1\" in order to use IDs from outer components in nested components."_L1
378 QQmlJS::s_documentOrigin,
379 QQmlJSDocumentEdit{ m_logger->filePath(), QQmlJS::s_documentOrigin, replacement + u'\n' }
381 bindComponents.setAutoApplicable();
382 suggestion = std::move(bindComponents);
385 if (!suggestion.has_value()) {
386 if (
auto didYouMean = QQmlJSUtils::didYouMean(
387 name, qmlScope->properties().keys() + qmlScope->methods().keys(),
388 m_logger->filePath(), location);
389 didYouMean.has_value()) {
390 suggestion = std::move(didYouMean);
394 if (name.front().isUpper()) {
395 Q_ASSERT(name.front() != u'_');
401 m_logger->log(
"%1 was not found."_L1.arg(name), qmlUnqualified, location,
true,
true,
404 m_logger->log(
"Unqualified access"_L1, qmlUnqualified, location,
true,
true, suggestion);
423 const QString &name,
bool isMethod)
const
425 QQmlJSTypePropagator::handleUnqualifiedAccessAndContextProperties(name, isMethod);
427 if (m_context.userContextProperties.isUnqualifiedAccessDisabled(name))
430 const auto warningMessage = [&name,
this]() {
432 "Potential context property access detected."
433 " Context properties are discouraged in QML: use normal, required, or singleton properties instead."_L1;
435 if (shouldMentionRequiredProperties(m_function->qmlScope.containedType())) {
437 "\nNote: '%1' assumed to be a potential context property because it is not declared as required property."_L1
443 if (m_context.userContextProperties.isOnUsageWarned(name)) {
444 m_logger->log(warningMessage(), qmlContextProperties, currentSourceLocation());
449 handleUnqualifiedAccess(name, isMethod);
451 const QList<QQmlJS::HeuristicContextProperty> definitions =
452 m_context.heuristicContextProperties.definitionsForName(name);
453 if (definitions.isEmpty())
455 QString warning = warningMessage();
456 for (
const auto &candidate : definitions) {
457 warning.append(
"\nNote: candidate context property declaration '%1' at %2:%3:%4"_L1.arg(
458 name, QDir::cleanPath(candidate.filename),
459 QString::number(candidate.location.startLine),
460 QString::number(candidate.location.startColumn)));
462 m_logger->log(warning, qmlContextProperties, currentSourceLocation());
468 QQmlJSTypePropagator::checkDeprecated(scope, name, isMethod);
470 Q_ASSERT(!scope.isNull());
471 auto qmlScope = QQmlJSScope::findCurrentQMLScope(scope);
472 if (qmlScope.isNull())
475 QList<QQmlJSAnnotation> annotations;
477 QQmlJSMetaMethod method;
480 const QList<QQmlJSMetaMethod> methods = qmlScope->methods(name);
481 if (methods.isEmpty())
483 method = methods.constFirst();
484 annotations = method.annotations();
486 QQmlJSMetaProperty property = qmlScope->property(name);
487 if (!property.isValid())
489 annotations = property.annotations();
492 auto deprecationAnn = std::find_if(
493 annotations.constBegin(), annotations.constEnd(),
494 [](
const QQmlJSAnnotation &annotation) {
return annotation.isDeprecation(); });
496 if (deprecationAnn == annotations.constEnd())
499 QQQmlJSDeprecation deprecation = deprecationAnn->deprecation();
501 QString descriptor = name;
503 descriptor += u'(' + method.parameterNames().join(u", "_s) + u')';
505 QString message =
"%1 \"%2\" is deprecated"_L1
506 .arg(isMethod ? u"Method"_s : u"Property"_s, descriptor);
508 if (!deprecation.reason.isEmpty())
509 message.append(QStringLiteral(
" (Reason: %1)").arg(deprecation.reason));
511 m_logger->log(message, qmlDeprecated, currentSourceLocation());
515 const QString &name)
const
517 const bool res = QQmlJSTypePropagator::isCallingProperty(scope, name);
519 if (
const auto property = scope->property(name); property.isValid()) {
521 if (property.type() == m_typeResolver->varType()) {
522 errorType = u"a var property. It may or may not be a method. "_s
523 u"Use a regular function instead."_s;
524 }
else if (property.type() == m_typeResolver->jsValueType()) {
525 errorType = u"a QJSValue property. It may or may not be a method. "_s
526 u"Use a regular Q_INVOKABLE instead."_s;
528 errorType = u"not a method"_s;
531 m_logger->log(u"Property \"%1\" is %2"_s.arg(name, errorType),
532 qmlUseProperFunction, currentSourceLocation(),
true,
true, {});
575 QQmlJSTypePropagator::handleLookupError(propertyName);
577 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
578 const QString typeName = accumulatorIn.containedTypeName();
580 if (typeName == u"QVariant")
582 if (accumulatorIn.isList() && propertyName == u"length")
585 auto baseType = accumulatorIn.containedType();
588 if (propertyResolution(baseType, propertyName) != PropertyMissing)
591 if (baseType->isScript())
594 std::optional<QQmlJSFixSuggestion> fixSuggestion;
596 if (
auto suggestion = QQmlJSUtils::didYouMean(propertyName, baseType->properties().keys(),
597 m_logger->filePath(), currentSourceLocation());
598 suggestion.has_value()) {
599 fixSuggestion = std::move(suggestion);
602 if (!fixSuggestion.has_value()
603 && accumulatorIn.variant() == QQmlJSRegisterContent::MetaType) {
605 const QQmlJSScope::ConstPtr scopeType = accumulatorIn.scopeType();
606 const auto metaEnums = scopeType->enumerations();
607 const bool enforcesScoped = scopeType->enforcesScopedEnums();
609 QStringList enumKeys;
610 for (
const QQmlJSMetaEnum &metaEnum : metaEnums) {
611 if (!enforcesScoped || !metaEnum.isScoped())
612 enumKeys << metaEnum.keys();
615 if (
auto suggestion = QQmlJSUtils::didYouMean(
616 propertyName, enumKeys, m_logger->filePath(), currentSourceLocation());
617 suggestion.has_value()) {
618 fixSuggestion = std::move(suggestion);
622 if (checkTypeResolved(baseType)) {
623 m_logger->log(u"Member \"%1\" not found on type \"%2\""_s.arg(propertyName, typeName),
624 qmlMissingProperty, currentSourceLocation(),
true,
true, fixSuggestion);
650 QQmlJSTypePropagator::generate_StoreNameCommon(nameIndex);
652 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
653 const QQmlJSRegisterContent in = m_state.accumulatorIn();
654 const bool isAttached = in.variant() == QQmlJSRegisterContent::Attachment;
656 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
657 QQmlJSScope::createQQmlSAElement(
658 m_state.accumulatorIn().containedType()),
660 QQmlJSScope::createQQmlSAElement(isAttached
661 ? in.attachee().containedType()
662 : m_function->qmlScope.containedType()),
663 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
664 currentNonEmptySourceLocation()));
666 const auto qmlScope = m_function->qmlScope.containedType();
667 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
668 QQmlJSScope::createQQmlSAElement(qmlScope), name,
669 QQmlJSScope::createQQmlSAElement(in.containedType()),
670 QQmlJSScope::createQQmlSAElement(qmlScope),
671 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
672 currentNonEmptySourceLocation()));
694 QQmlJSRegisterContent scope)
696 QQmlJSTypePropagator::propagateCall(methods, argc, argv, scope);
699 const QQmlJSMetaMethod match = bestMatchForCall(methods, argc, argv, &errors);
700 if (!match.isValid())
703 const QQmlSA::Element saBaseType = QQmlJSScope::createQQmlSAElement(scope.containedType());
704 const QQmlSA::SourceLocation saLocation{
705 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
707 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
708 m_function->qmlScope.containedType()) };
710 QQmlSA::PassManagerPrivate::get(m_passManager)
711 ->analyzeCall(saBaseType, match.methodName(), saContainedType, saLocation);