QmlJSCheck: Add error for mixing translation functions

Change-Id: I0f5fd2edf2d8bf13938d9e18eadada5c8c1b85f2
Reviewed-by: Tapani Mattila <tapani.mattila@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Thomas Hartmann
2021-05-04 14:24:25 +02:00
parent 18ab828b6b
commit aba3a38a60
4 changed files with 25 additions and 2 deletions

View File

@@ -736,6 +736,7 @@ void Check::enableQmlDesignerUiFileChecks()
enableMessage(ErrBehavioursNotSupportedInQmlUi);
enableMessage(ErrStatesOnlyInRootItemInQmlUi);
enableMessage(ErrReferenceToParentItemNotSupportedInQmlUi);
enableMessage(ErrDoNotMixTranslationFunctionsInQmlUi);
}
void Check::disableQmlDesignerUiFileChecks()
@@ -747,6 +748,7 @@ void Check::disableQmlDesignerUiFileChecks()
disableMessage(ErrBehavioursNotSupportedInQmlUi);
disableMessage(ErrStatesOnlyInRootItemInQmlUi);
disableMessage(ErrReferenceToParentItemNotSupportedInQmlUi);
disableMessage(ErrDoNotMixTranslationFunctionsInQmlUi);
}
bool Check::preVisit(Node *ast)
@@ -1757,6 +1759,22 @@ bool Check::visit(CallExpression *ast)
if (!whiteListedFunction && !isMathFunction && !isDateFunction && !isDirectInConnectionsScope)
addMessage(ErrFunctionsNotSupportedInQmlUi, location);
if (translationFunctions.contains(name)) {
TranslationFunction translationFunction = noTranslationfunction;
if (name == "qsTr" || name == "qsTrNoOp")
translationFunction = qsTr;
else if (name == "qsTrId" || name == "qsTrIdNoOp")
translationFunction = qsTrId;
else if (name == "qsTranslate" || name == "qsTranslateNoOp")
translationFunction = qsTranslate;
if (lastTransLationfunction != noTranslationfunction
&& lastTransLationfunction != translationFunction)
addMessage(ErrDoNotMixTranslationFunctionsInQmlUi, location);
lastTransLationfunction = translationFunction;
}
static const QStringList globalFunctions = {"String", "Boolean", "Date", "Number", "Object", "Array", "Symbol", "Object", "Function", "RegExp",
"QT_TR_NOOP", "QT_TRANSLATE_NOOP", "QT_TRID_NOOP"};

View File

@@ -152,11 +152,14 @@ private:
bool wasSuppressed;
};
enum TranslationFunction { qsTr, qsTrId, qsTranslate, noTranslationfunction };
QHash< int, QList<MessageTypeAndSuppression> > m_disabledMessageTypesByLine;
bool _importsOk;
bool _inStatementBinding;
const Imports *_imports;
TranslationFunction lastTransLationfunction = noTranslationfunction;
};
} // namespace QmlJS

View File

@@ -239,6 +239,8 @@ StaticAnalysisMessages::StaticAnalysisMessages()
tr("States are only supported in the root item in a Qt Quick UI form."));
newMsg(ErrReferenceToParentItemNotSupportedInQmlUi, Error,
tr("Referencing the parent of the root item is not supported in a Qt Quick UI form."));
newMsg(ErrDoNotMixTranslationFunctionsInQmlUi, Error,
tr("Do not mix translation functions in a Qt Quick UI form."));
newMsg(StateCannotHaveChildItem, Error,
tr("A State cannot have a child item (%1)."), 1);
newMsg(WarnDuplicateImport, Warning,

View File

@@ -39,8 +39,7 @@ class DiagnosticMessage;
namespace StaticAnalysis {
enum Type
{
enum Type {
// Changing the numbers can break user code.
// When adding a new check, also add it to the documentation, currently
// in creator-code-syntax.qdoc.
@@ -106,6 +105,7 @@ enum Type
ErrBehavioursNotSupportedInQmlUi = 224,
ErrStatesOnlyInRootItemInQmlUi = 225,
ErrReferenceToParentItemNotSupportedInQmlUi = 226,
ErrDoNotMixTranslationFunctionsInQmlUi = 227,
ErrUnknownComponent = 300,
ErrCouldNotResolvePrototypeOf = 301,
ErrCouldNotResolvePrototype = 302,