27 QQmlJSTypePropagator::generate_Ret();
29 if (m_function->isSignalHandler) {
31 }
else if (m_state.accumulatorIn().contains(m_typeResolver->voidType())) {
33 }
else if (!m_returnType.isValid() && m_state.accumulatorIn().isValid()) {
34 if (m_function->isFullyTyped) {
36 m_logger->log(u"Function without return type annotation returns %1"_s.arg(
37 m_state.accumulatorIn().containedTypeName()),
38 qmlIncompatibleType, currentFunctionSourceLocation());
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());
47 const QQmlJS::SourceLocation location = m_function->isProperty
48 ? currentFunctionSourceLocation()
49 : currentNonEmptySourceLocation();
50 QQmlSA::PassManagerPrivate::get(m_passManager)
52 QQmlJSScope::createQQmlSAElement(m_function->qmlScope.containedType()),
53 QQmlJSScope::createQQmlSAElement(m_state.accumulatorIn().containedType()),
54 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(location));
59 QQmlJSTypePropagator::generate_LoadQmlContextPropertyLookup(index);
61 Q_ASSERT(m_idMemberShadows);
63 const int nameIndex = m_jsUnitGenerator->lookupNameIndex(index);
64 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
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()));
73 const auto &accumulatorOut = m_state.accumulatorOut();
74 if (!accumulatorOut.isValid())
78 if (accumulatorOut.variant() == QQmlJSRegisterContent::Attachment
79 || accumulatorOut.variant() == QQmlJSRegisterContent::MetaType) {
80 m_context.renamedComponents.handleRenamedType(accumulatorOut.scopeType(), name,
81 currentNonEmptySourceLocation(), m_logger);
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) {
91 if (m_idMemberShadows->contains(idMemberShadow))
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);
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);
155 QQmlJSTypePropagator::generate_StoreProperty(nameIndex, base);
157 auto callBase = m_state.registers[base].content;
158 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
159 const bool isAttached = callBase.variant() == QQmlJSRegisterContent::Attachment;
161 checkWrite(callBase, propertyName);
163 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
164 QQmlJSScope::createQQmlSAElement(callBase.containedType()),
166 QQmlJSScope::createQQmlSAElement(
167 m_state.accumulatorIn().containedType()),
168 QQmlJSScope::createQQmlSAElement(isAttached
169 ? callBase.attachee().containedType()
170 : m_function->qmlScope.containedType()),
171 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
172 currentNonEmptySourceLocation()));
177 QQmlJSTypePropagator::generate_CallProperty(nameIndex, base, argc, argv);
179 const auto saCheck = [&](
const QString &propertyName,
const QQmlJSScope::ConstPtr &baseType) {
180 const QQmlSA::Element saBaseType{ QQmlJSScope::createQQmlSAElement(baseType) };
181 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
182 m_function->qmlScope.containedType()) };
183 const QQmlSA::SourceLocation saLocation{
184 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
187 QQmlSA::PassManagerPrivate::get(m_passManager)
188 ->analyzeRead(saBaseType, propertyName, saContainedType, saLocation);
189 QQmlSA::PassManagerPrivate::get(m_passManager)
190 ->analyzeCall(saBaseType, propertyName, saContainedType, saLocation);
193 const auto callBase = m_state.registers[base].content;
194 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
195 const auto member = m_typeResolver->memberType(callBase, propertyName);
197 const bool isLoggingMethod = QQmlJSTypePropagator::isLoggingMethod(propertyName);
198 if (callBase.contains(m_typeResolver->mathObject()))
199 saCheck(propertyName, callBase.containedType());
200 else if (callBase.contains(m_typeResolver->consoleObject()) && isLoggingMethod)
201 saCheck(propertyName, callBase.containedType());
202 else if (!member.isMethod()) {
203 if (callBase.contains(m_typeResolver->jsValueType())
204 || callBase.contains(m_typeResolver->varType())) {
205 saCheck(propertyName, callBase.containedType());
255 QQmlJSTypePropagator::handleUnqualifiedAccess(name, isMethod);
257 auto location = currentSourceLocation();
259 const auto qmlScopeContained = m_function->qmlScope.containedType();
260 if (qmlScopeContained->isInCustomParserParent()) {
262 if (qmlScopeContained->baseType().isNull()
263 || qmlScopeContained->baseType()->internalName() != u"QQmlConnections"_s)
268 if (isCallingProperty(qmlScopeContained, name))
270 }
else if (propertyResolution(qmlScopeContained, name) != PropertyMissing) {
274 std::optional<QQmlJSFixSuggestion> suggestion;
276 const auto childScopes = m_function->qmlScope.containedType()->childScopes();
277 for (qsizetype i = 0, end = childScopes.size(); i < end; i++) {
278 auto &scope = childScopes[i];
279 if (location.offset > scope->sourceLocation().offset) {
281 && childScopes.at(i + 1)->sourceLocation().offset < location.offset)
283 if (scope->childScopes().size() == 0)
286 const auto jsId = scope->childScopes().first()->jsIdentifier(name);
288 if (jsId.has_value() && jsId->kind == QQmlJSScope::JavaScriptIdentifier::Injected) {
289 const QQmlJSScope::JavaScriptIdentifier id = jsId.value();
291 QQmlJS::SourceLocation fixLocation = id.location;
292 Q_UNUSED(fixLocation)
293 fixLocation.length = 0;
295 const auto handler = m_typeResolver->signalHandlers()[id.location];
297 QString fixString = handler.isMultiline ? u"function("_s : u"("_s;
298 const auto parameters = handler.signalParameters;
299 for (
int numParams = parameters.size(); numParams > 0; --numParams) {
300 fixString += parameters.at(parameters.size() - numParams);
302 fixString += u", "_s;
305 fixString += handler.isMultiline ? u") "_s : u") => "_s;
306 const auto msg = u"\"%1\" is ambiguous. Use a function instead: %2%3"_s.arg(
307 name, fixString, handler.isMultiline ?
"{ ... }"_L1 :
"..."_L1);
308 QQmlJSDocumentEdit documentEdit{ m_logger->filePath(), fixLocation, fixString };
309 suggestion = {{ msg, fixLocation, documentEdit }};
310 suggestion->setAutoApplicable();
320 const auto qmlScope = m_function->qmlScope.containedType();
321 if (name == u"model" || name == u"index") {
322 if (
const QQmlJSScope::ConstPtr parent = qmlScope->parentScope(); !parent.isNull()) {
323 const auto bindings = parent->ownPropertyBindings(u"delegate"_s);
325 for (
auto it = bindings.first; it != bindings.second; it++) {
326 if (!it->hasObject())
328 if (it->objectType() == qmlScope) {
329 suggestion = QQmlJSFixSuggestion {
330 "'%1' is implicitly injected into this delegate. "
331 "Add a required property '%1' to the delegate instead."_L1
333 qmlScope->sourceLocation()
342 if (!suggestion.has_value()) {
343 for (QQmlJSScope::ConstPtr scope = qmlScope; !scope.isNull(); scope = scope->parentScope()) {
344 if (scope->hasProperty(name)) {
345 QQmlJSScopesById::MostLikelyCallback<QString> id;
346 m_function->addressableScopes.possibleIds(scope, qmlScope,
347 QQmlJSScopesByIdOption::Default, id);
349 QQmlJS::SourceLocation fixLocation = location;
350 fixLocation.length = 0;
351 QString m =
"%1 is a member of a parent element.\n You can qualify the "
352 "access with its id to avoid this warning%2.\n"_L1.arg(name);
353 m = m.arg(id.result.isEmpty() ?
" (You first have to give the element an id)"_L1 :
""_L1);
355 suggestion = QQmlJSFixSuggestion{
356 m, fixLocation, { m_logger->filePath(), fixLocation,
357 (id.result.isEmpty() ? u"<id>."_s : (id.result + u'.')) }
360 if (!id.result.isEmpty())
361 suggestion->setAutoApplicable();
366 if (!suggestion.has_value() && !m_function->addressableScopes.componentsAreBound()
367 && m_function->addressableScopes.existsAnywhereInDocument(name)) {
368 const QLatin1String replacement =
"pragma ComponentBehavior: Bound"_L1;
369 QQmlJSFixSuggestion bindComponents {
370 "Set \"%1\" in order to use IDs from outer components in nested components."_L1
372 QQmlJS::s_documentOrigin,
373 QQmlJSDocumentEdit{ m_logger->filePath(), QQmlJS::s_documentOrigin, replacement + u'\n' }
375 bindComponents.setAutoApplicable();
376 suggestion = std::move(bindComponents);
379 if (!suggestion.has_value()) {
380 if (
auto didYouMean = QQmlJSUtils::didYouMean(
381 name, qmlScope->properties().keys() + qmlScope->methods().keys(),
382 m_logger->filePath(), location);
383 didYouMean.has_value()) {
384 suggestion = std::move(didYouMean);
388 if (name.front().isUpper()) {
389 Q_ASSERT(name.front() != u'_');
395 m_logger->log(
"%1 was not found."_L1.arg(name), qmlUnqualified, location,
true,
true,
398 m_logger->log(
"Unqualified access"_L1, qmlUnqualified, location,
true,
true, suggestion);
417 const QString &name,
bool isMethod)
const
419 QQmlJSTypePropagator::handleUnqualifiedAccessAndContextProperties(name, isMethod);
421 if (m_context.userContextProperties.isUnqualifiedAccessDisabled(name))
424 const auto warningMessage = [&name,
this]() {
426 "Potential context property access detected."
427 " Context properties are discouraged in QML: use normal, required, or singleton properties instead."_L1;
429 if (shouldMentionRequiredProperties(m_function->qmlScope.containedType())) {
431 "\nNote: '%1' assumed to be a potential context property because it is not declared as required property."_L1
437 if (m_context.userContextProperties.isOnUsageWarned(name)) {
438 m_logger->log(warningMessage(), qmlContextProperties, currentSourceLocation());
443 handleUnqualifiedAccess(name, isMethod);
445 const QList<QQmlJS::HeuristicContextProperty> definitions =
446 m_context.heuristicContextProperties.definitionsForName(name);
447 if (definitions.isEmpty())
449 QString warning = warningMessage();
450 for (
const auto &candidate : definitions) {
451 warning.append(
"\nNote: candidate context property declaration '%1' at %2:%3:%4"_L1.arg(
452 name, QDir::cleanPath(candidate.filename),
453 QString::number(candidate.location.startLine),
454 QString::number(candidate.location.startColumn)));
456 m_logger->log(warning, qmlContextProperties, currentSourceLocation());
462 QQmlJSTypePropagator::checkDeprecated(scope, name, isMethod);
464 Q_ASSERT(!scope.isNull());
465 auto qmlScope = QQmlJSScope::findCurrentQMLScope(scope);
466 if (qmlScope.isNull())
469 QList<QQmlJSAnnotation> annotations;
471 QQmlJSMetaMethod method;
474 const QList<QQmlJSMetaMethod> methods = qmlScope->methods(name);
475 if (methods.isEmpty())
477 method = methods.constFirst();
478 annotations = method.annotations();
480 QQmlJSMetaProperty property = qmlScope->property(name);
481 if (!property.isValid())
483 annotations = property.annotations();
486 auto deprecationAnn = std::find_if(
487 annotations.constBegin(), annotations.constEnd(),
488 [](
const QQmlJSAnnotation &annotation) {
return annotation.isDeprecation(); });
490 if (deprecationAnn == annotations.constEnd())
493 QQQmlJSDeprecation deprecation = deprecationAnn->deprecation();
495 QString descriptor = name;
497 descriptor += u'(' + method.parameterNames().join(u", "_s) + u')';
499 QString message =
"%1 \"%2\" is deprecated"_L1
500 .arg(isMethod ? u"Method"_s : u"Property"_s, descriptor);
502 if (!deprecation.reason.isEmpty())
503 message.append(QStringLiteral(
" (Reason: %1)").arg(deprecation.reason));
505 m_logger->log(message, qmlDeprecated, currentSourceLocation());
509 const QString &name)
const
511 const bool res = QQmlJSTypePropagator::isCallingProperty(scope, name);
513 if (
const auto property = scope->property(name); property.isValid()) {
515 if (property.type() == m_typeResolver->varType()) {
516 errorType = u"a var property. It may or may not be a method. "_s
517 u"Use a regular function instead."_s;
518 }
else if (property.type() == m_typeResolver->jsValueType()) {
519 errorType = u"a QJSValue property. It may or may not be a method. "_s
520 u"Use a regular Q_INVOKABLE instead."_s;
522 errorType = u"not a method"_s;
525 m_logger->log(u"Property \"%1\" is %2"_s.arg(name, errorType),
526 qmlUseProperFunction, currentSourceLocation(),
true,
true, {});
569 QQmlJSTypePropagator::handleLookupError(propertyName);
571 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
572 const QString typeName = accumulatorIn.containedTypeName();
574 if (typeName == u"QVariant")
576 if (accumulatorIn.isList() && propertyName == u"length")
579 auto baseType = accumulatorIn.containedType();
582 if (propertyResolution(baseType, propertyName) != PropertyMissing)
585 if (baseType->isScript())
588 std::optional<QQmlJSFixSuggestion> fixSuggestion;
590 if (
auto suggestion = QQmlJSUtils::didYouMean(propertyName, baseType->properties().keys(),
591 m_logger->filePath(), currentSourceLocation());
592 suggestion.has_value()) {
593 fixSuggestion = std::move(suggestion);
596 if (!fixSuggestion.has_value()
597 && accumulatorIn.variant() == QQmlJSRegisterContent::MetaType) {
599 const QQmlJSScope::ConstPtr scopeType = accumulatorIn.scopeType();
600 const auto metaEnums = scopeType->enumerations();
601 const bool enforcesScoped = scopeType->enforcesScopedEnums();
603 QStringList enumKeys;
604 for (
const QQmlJSMetaEnum &metaEnum : metaEnums) {
605 if (!enforcesScoped || !metaEnum.isScoped())
606 enumKeys << metaEnum.keys();
609 if (
auto suggestion = QQmlJSUtils::didYouMean(
610 propertyName, enumKeys, m_logger->filePath(), currentSourceLocation());
611 suggestion.has_value()) {
612 fixSuggestion = std::move(suggestion);
616 if (checkTypeResolved(baseType)) {
617 m_logger->log(u"Member \"%1\" not found on type \"%2\""_s.arg(propertyName, typeName),
618 qmlMissingProperty, currentSourceLocation(),
true,
true, fixSuggestion);
644 QQmlJSTypePropagator::generate_StoreNameCommon(nameIndex);
646 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
647 const QQmlJSRegisterContent in = m_state.accumulatorIn();
648 const bool isAttached = in.variant() == QQmlJSRegisterContent::Attachment;
650 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeRead(
651 QQmlJSScope::createQQmlSAElement(
652 m_state.accumulatorIn().containedType()),
654 QQmlJSScope::createQQmlSAElement(isAttached
655 ? in.attachee().containedType()
656 : m_function->qmlScope.containedType()),
657 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
658 currentNonEmptySourceLocation()));
660 const auto qmlScope = m_function->qmlScope.containedType();
661 QQmlSA::PassManagerPrivate::get(m_passManager)->analyzeWrite(
662 QQmlJSScope::createQQmlSAElement(qmlScope), name,
663 QQmlJSScope::createQQmlSAElement(in.containedType()),
664 QQmlJSScope::createQQmlSAElement(qmlScope),
665 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(
666 currentNonEmptySourceLocation()));
688 QQmlJSRegisterContent scope)
690 QQmlJSTypePropagator::propagateCall(methods, argc, argv, scope);
693 const QQmlJSMetaMethod match = bestMatchForCall(methods, argc, argv, &errors);
694 if (!match.isValid())
697 const QQmlSA::Element saBaseType = QQmlJSScope::createQQmlSAElement(scope.containedType());
698 const QQmlSA::SourceLocation saLocation{
699 QQmlSA::SourceLocationPrivate::createQQmlSASourceLocation(currentSourceLocation())
701 const QQmlSA::Element saContainedType{ QQmlJSScope::createQQmlSAElement(
702 m_function->qmlScope.containedType()) };
704 QQmlSA::PassManagerPrivate::get(m_passManager)
705 ->analyzeCall(saBaseType, match.methodName(), saContainedType, saLocation);