Commit be9d25ec authored by Christian Kamm's avatar Christian Kamm
Browse files

QmlJS checks: Warn when using some builtin constructors.

Number, String, Array, Object, Function and Boolean should not
be used.

Migrated from QtChecker.

Change-Id: I8aee41f12389196ed49c44e26eb04d3fac040a2b
Reviewed-on: http://codereview.qt-project.org/5859


Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: default avatarFawzi Mohamed <fawzi.mohamed@nokia.com>
parent c82d53d4
......@@ -1122,6 +1122,33 @@ bool Check::visit(NewExpression *ast)
bool Check::visit(NewMemberExpression *ast)
{
checkNewExpression(ast->base);
// check for Number, Boolean, etc constructor usage
if (IdentifierExpression *idExp = cast<IdentifierExpression *>(ast->base)) {
const QStringRef name = idExp->name;
if (name == QLatin1String("Number")) {
addMessage(WarnNumberConstructor, idExp->identifierToken);
} else if (name == QLatin1String("Boolean")) {
addMessage(WarnBooleanConstructor, idExp->identifierToken);
} else if (name == QLatin1String("String")) {
addMessage(WarnStringConstructor, idExp->identifierToken);
} else if (name == QLatin1String("Object")) {
addMessage(WarnObjectConstructor, idExp->identifierToken);
} else if (name == QLatin1String("Array")) {
bool ok = false;
if (ast->arguments && ast->arguments->expression && !ast->arguments->next) {
Evaluate evaluate(&_scopeChain);
const Value *arg = evaluate(ast->arguments->expression);
if (arg->asNumberValue() || arg->asUndefinedValue())
ok = true;
}
if (!ok)
addMessage(WarnArrayConstructor, idExp->identifierToken);
} else if (name == QLatin1String("Function")) {
addMessage(WarnFunctionConstructor, idExp->identifierToken);
}
}
return true;
}
......
import QtQuick 1.0
Item {
function foo() {
var x = new Number(); // 308 21 26
x = new String(); // 110 17 22
x = new Array(); // 112 17 21
x = new Object(); // 111 17 22
x = new Function(); // 113 17 24
x = new Boolean() // 109 17 23
x = new Date()
x = new Array(6)
}
}
......@@ -8,7 +8,7 @@ Item {
}
function foo() {
throw new Object()
throw {}
x() // 28 9 11
x()
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment