forked from qt-creator/qt-creator
QmlJS check: Extend "don't use var" performance warning.
* now also recognizes color, rect, point, size, vector3d * to do detect these correctly, set the return types on a number of builtin Qt.* functions * add test Change-Id: Ieaeb73be208af2d47e1bb4fa1485dc876705ee20 Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
This commit is contained in:
@@ -768,10 +768,21 @@ bool Check::visit(UiPublicMember *ast)
|
|||||||
QString preferedType;
|
QString preferedType;
|
||||||
if (init->asNumberValue())
|
if (init->asNumberValue())
|
||||||
preferedType = tr("'int' or 'real'");
|
preferedType = tr("'int' or 'real'");
|
||||||
if (init->asStringValue())
|
else if (init->asStringValue())
|
||||||
preferedType = QLatin1String("'string'");
|
preferedType = QLatin1String("'string'");
|
||||||
if (init->asBooleanValue())
|
else if (init->asBooleanValue())
|
||||||
preferedType = QLatin1String("'bool'");
|
preferedType = QLatin1String("'bool'");
|
||||||
|
else if (init->asColorValue())
|
||||||
|
preferedType = QLatin1String("'color'");
|
||||||
|
else if (init == _context->valueOwner()->qmlPointObject())
|
||||||
|
preferedType = QLatin1String("'point'");
|
||||||
|
else if (init == _context->valueOwner()->qmlRectObject())
|
||||||
|
preferedType = QLatin1String("'rect'");
|
||||||
|
else if (init == _context->valueOwner()->qmlSizeObject())
|
||||||
|
preferedType = QLatin1String("'size'");
|
||||||
|
else if (init == _context->valueOwner()->qmlVector3DObject())
|
||||||
|
preferedType = QLatin1String("'vector3d'");
|
||||||
|
|
||||||
if (!preferedType.isEmpty())
|
if (!preferedType.isEmpty())
|
||||||
addMessage(HintPreferNonVarPropertyType, ast->typeToken, preferedType);
|
addMessage(HintPreferNonVarPropertyType, ast->typeToken, preferedType);
|
||||||
}
|
}
|
||||||
|
@@ -849,47 +849,6 @@ void ValueOwner::initializePrototypes()
|
|||||||
f->setOptionalNamedArgumentCount(2);
|
f->setOptionalNamedArgumentCount(2);
|
||||||
_globalObject->setMember("JSON", json);
|
_globalObject->setMember("JSON", json);
|
||||||
|
|
||||||
// global Qt object, in alphabetic order
|
|
||||||
_qtObject = newObject(/*prototype */ 0);
|
|
||||||
addFunction(_qtObject, QLatin1String("atob"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("btoa"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("createComponent"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("createQmlObject"), 3);
|
|
||||||
addFunction(_qtObject, QLatin1String("darker"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("fontFamilies"), 0);
|
|
||||||
addFunction(_qtObject, QLatin1String("formatDate"), 2);
|
|
||||||
addFunction(_qtObject, QLatin1String("formatDateTime"), 2);
|
|
||||||
addFunction(_qtObject, QLatin1String("formatTime"), 2);
|
|
||||||
addFunction(_qtObject, QLatin1String("hsla"), 4);
|
|
||||||
addFunction(_qtObject, QLatin1String("include"), 2);
|
|
||||||
addFunction(_qtObject, QLatin1String("isQtObject"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("lighter"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("md5"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("openUrlExternally"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("point"), 2);
|
|
||||||
addFunction(_qtObject, QLatin1String("quit"), 0);
|
|
||||||
addFunction(_qtObject, QLatin1String("rect"), 4);
|
|
||||||
addFunction(_qtObject, QLatin1String("resolvedUrl"), 1);
|
|
||||||
addFunction(_qtObject, QLatin1String("rgba"), 4);
|
|
||||||
addFunction(_qtObject, QLatin1String("size"), 2);
|
|
||||||
addFunction(_qtObject, QLatin1String("tint"), 2);
|
|
||||||
addFunction(_qtObject, QLatin1String("vector3d"), 3);
|
|
||||||
_globalObject->setMember(QLatin1String("Qt"), _qtObject);
|
|
||||||
|
|
||||||
// firebug/webkit compat
|
|
||||||
ObjectValue *consoleObject = newObject(/*prototype */ 0);
|
|
||||||
addFunction(consoleObject, QLatin1String("log"), 1);
|
|
||||||
addFunction(consoleObject, QLatin1String("debug"), 1);
|
|
||||||
_globalObject->setMember(QLatin1String("console"), consoleObject);
|
|
||||||
|
|
||||||
// translation functions
|
|
||||||
addFunction(_globalObject, QLatin1String("qsTr"), 3);
|
|
||||||
addFunction(_globalObject, QLatin1String("QT_TR_NOOP"), 1);
|
|
||||||
addFunction(_globalObject, QLatin1String("qsTranslate"), 5);
|
|
||||||
addFunction(_globalObject, QLatin1String("QT_TRANSLATE_NOOP"), 2);
|
|
||||||
addFunction(_globalObject, QLatin1String("qsTrId"), 2);
|
|
||||||
addFunction(_globalObject, QLatin1String("QT_TRID_NOOP"), 1);
|
|
||||||
|
|
||||||
// QML objects
|
// QML objects
|
||||||
_qmlFontObject = newObject(/*prototype =*/ 0);
|
_qmlFontObject = newObject(/*prototype =*/ 0);
|
||||||
_qmlFontObject->setClassName(QLatin1String("Font"));
|
_qmlFontObject->setClassName(QLatin1String("Font"));
|
||||||
@@ -928,6 +887,47 @@ void ValueOwner::initializePrototypes()
|
|||||||
_qmlVector3DObject->setMember("x", realValue());
|
_qmlVector3DObject->setMember("x", realValue());
|
||||||
_qmlVector3DObject->setMember("y", realValue());
|
_qmlVector3DObject->setMember("y", realValue());
|
||||||
_qmlVector3DObject->setMember("z", realValue());
|
_qmlVector3DObject->setMember("z", realValue());
|
||||||
|
|
||||||
|
// global Qt object, in alphabetic order
|
||||||
|
_qtObject = newObject(/*prototype */ 0);
|
||||||
|
addFunction(_qtObject, QLatin1String("atob"), &_stringValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("btoa"), &_stringValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("createComponent"), 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("createQmlObject"), 3);
|
||||||
|
addFunction(_qtObject, QLatin1String("darker"), &_colorValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("fontFamilies"), 0);
|
||||||
|
addFunction(_qtObject, QLatin1String("formatDate"), &_stringValue, 2);
|
||||||
|
addFunction(_qtObject, QLatin1String("formatDateTime"), &_stringValue, 2);
|
||||||
|
addFunction(_qtObject, QLatin1String("formatTime"), &_stringValue, 2);
|
||||||
|
addFunction(_qtObject, QLatin1String("hsla"), &_colorValue, 4);
|
||||||
|
addFunction(_qtObject, QLatin1String("include"), 2);
|
||||||
|
addFunction(_qtObject, QLatin1String("isQtObject"), &_booleanValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("lighter"), &_colorValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("md5"), &_stringValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("openUrlExternally"), &_booleanValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("point"), _qmlPointObject, 2);
|
||||||
|
addFunction(_qtObject, QLatin1String("quit"), 0);
|
||||||
|
addFunction(_qtObject, QLatin1String("rect"), _qmlRectObject, 4);
|
||||||
|
addFunction(_qtObject, QLatin1String("resolvedUrl"), &_urlValue, 1);
|
||||||
|
addFunction(_qtObject, QLatin1String("rgba"), &_colorValue, 4);
|
||||||
|
addFunction(_qtObject, QLatin1String("size"), _qmlSizeObject, 2);
|
||||||
|
addFunction(_qtObject, QLatin1String("tint"), &_colorValue, 2);
|
||||||
|
addFunction(_qtObject, QLatin1String("vector3d"), _qmlVector3DObject, 3);
|
||||||
|
_globalObject->setMember(QLatin1String("Qt"), _qtObject);
|
||||||
|
|
||||||
|
// firebug/webkit compat
|
||||||
|
ObjectValue *consoleObject = newObject(/*prototype */ 0);
|
||||||
|
addFunction(consoleObject, QLatin1String("log"), 1);
|
||||||
|
addFunction(consoleObject, QLatin1String("debug"), 1);
|
||||||
|
_globalObject->setMember(QLatin1String("console"), consoleObject);
|
||||||
|
|
||||||
|
// translation functions
|
||||||
|
addFunction(_globalObject, QLatin1String("qsTr"), 3);
|
||||||
|
addFunction(_globalObject, QLatin1String("QT_TR_NOOP"), 1);
|
||||||
|
addFunction(_globalObject, QLatin1String("qsTranslate"), 5);
|
||||||
|
addFunction(_globalObject, QLatin1String("QT_TRANSLATE_NOOP"), 2);
|
||||||
|
addFunction(_globalObject, QLatin1String("qsTrId"), 2);
|
||||||
|
addFunction(_globalObject, QLatin1String("QT_TRID_NOOP"), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ObjectValue *ValueOwner::qmlKeysObject()
|
const ObjectValue *ValueOwner::qmlKeysObject()
|
||||||
|
20
tests/auto/qml/codemodel/check/avoid-var.qml
Normal file
20
tests/auto/qml/codemodel/check/avoid-var.qml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import QtQuick 1.0
|
||||||
|
|
||||||
|
Item {
|
||||||
|
property int x: 10
|
||||||
|
property var x: 10 // 311 14 16
|
||||||
|
property string x: "abc"
|
||||||
|
property var x: "abc" // 311 14 16
|
||||||
|
property string x: true
|
||||||
|
property var x: true // 311 14 16
|
||||||
|
property color x: Qt.rgba(1, 1, 1, 1)
|
||||||
|
property var x: Qt.rgba(1, 1, 1, 1) // 311 14 16
|
||||||
|
property point x: Qt.point(1, 1)
|
||||||
|
property var x: Qt.point(1, 1) // 311 14 16
|
||||||
|
property rect x: Qt.rect(1, 1, 1, 1)
|
||||||
|
property var x: Qt.rect(1, 1, 1, 1) // 311 14 16
|
||||||
|
property size x: Qt.size(1, 1)
|
||||||
|
property var x: Qt.size(1, 1) // 311 14 16
|
||||||
|
property vector3d x: Qt.vector3d(1, 1, 1)
|
||||||
|
property var x: Qt.vector3d(1, 1, 1) // 311 14 16
|
||||||
|
}
|
@@ -4,6 +4,7 @@ DEFINES+=QTCREATORDIR=\\\"$$IDE_SOURCE_TREE\\\"
|
|||||||
DEFINES+=TESTSRCDIR=\\\"$$PWD\\\"
|
DEFINES+=TESTSRCDIR=\\\"$$PWD\\\"
|
||||||
|
|
||||||
include($$IDE_SOURCE_TREE/src/libs/utils/utils.pri)
|
include($$IDE_SOURCE_TREE/src/libs/utils/utils.pri)
|
||||||
|
include($$IDE_SOURCE_TREE/src/libs/languageutils/languageutils.pri)
|
||||||
include($$IDE_SOURCE_TREE/src/libs/qmljs/qmljs.pri)
|
include($$IDE_SOURCE_TREE/src/libs/qmljs/qmljs.pri)
|
||||||
|
|
||||||
TARGET = tst_codemodel_check
|
TARGET = tst_codemodel_check
|
||||||
|
@@ -172,8 +172,10 @@ void tst_Check::test()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (expectedMessages.size() > messages.size()) {
|
if (expectedMessages.size() > messages.size()) {
|
||||||
Message missingMessage = expectedMessages.at(messages.size());
|
for (int i = messages.size(); i < expectedMessages.size(); ++i) {
|
||||||
qDebug() << "expected more messages: " << missingMessage.location.startLine << missingMessage.message;
|
Message missingMessage = expectedMessages.at(i);
|
||||||
|
qDebug() << "expected message of type" << missingMessage.type << "on line" << missingMessage.location.startLine;
|
||||||
|
}
|
||||||
QFAIL("more messages expected");
|
QFAIL("more messages expected");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user