QmlJS checks: Warn about inappropriate use of constructor functions.

Change-Id: Iaedaaa88915f2010bfdd0d2b5ca95f01f8663ed7
Reviewed-on: http://codereview.qt.nokia.com/4254
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@nokia.com>
This commit is contained in:
Christian Kamm
2011-09-06 12:59:30 +02:00
parent 4e60db9065
commit e850586b77
2 changed files with 65 additions and 4 deletions

View File

@@ -408,9 +408,10 @@ Check::Check(Document::Ptr doc, const ContextPtr &context)
, _scopeChain(doc, _context)
, _scopeBuilder(&_scopeChain)
, _options(WarnDangerousNonStrictEqualityChecks | WarnBlocks | WarnWith
| WarnVoid | WarnCommaExpression | WarnExpressionStatement
| WarnAssignInCondition | WarnUseBeforeDeclaration | WarnDuplicateDeclaration
| WarnCaseWithoutFlowControlEnd | ErrCheckTypeErrors)
| WarnVoid | WarnCommaExpression | WarnExpressionStatement
| WarnAssignInCondition | WarnUseBeforeDeclaration | WarnDuplicateDeclaration
| WarnCaseWithoutFlowControlEnd | WarnNonCapitalizedNew
| WarnCallsOfCapitalizedFunctions | ErrCheckTypeErrors)
, _lastValue(0)
{
}
@@ -891,6 +892,60 @@ bool Check::visit(DefaultClause *ast)
return true;
}
static QString functionName(ExpressionNode *ast, SourceLocation *location)
{
if (IdentifierExpression *id = cast<IdentifierExpression *>(ast)) {
if (id->name) {
*location = id->identifierToken;
return id->name->asString();
}
} else if (FieldMemberExpression *fme = cast<FieldMemberExpression *>(ast)) {
if (fme->name) {
*location = fme->identifierToken;
return fme->name->asString();
}
}
return QString();
}
void Check::checkNewExpression(ExpressionNode *ast)
{
if (!(_options & WarnNonCapitalizedNew))
return;
SourceLocation location;
const QString name = functionName(ast, &location);
if (name.isEmpty())
return;
if (!name.at(0).isUpper()) {
warning(location, tr("'new' should only be used with functions that start with an uppercase letter"));
}
}
bool Check::visit(NewExpression *ast)
{
checkNewExpression(ast->expression);
return true;
}
bool Check::visit(NewMemberExpression *ast)
{
checkNewExpression(ast->base);
return true;
}
bool Check::visit(CallExpression *ast)
{
// check for capitalized function name being called
if (_options & WarnCallsOfCapitalizedFunctions) {
SourceLocation location;
const QString name = functionName(ast->base, &location);
if (!name.isEmpty() && name.at(0).isUpper()) {
warning(location, tr("calls of functions that start with an uppercase letter should use 'new'"));
}
}
return true;
}
/// When something is changed here, also change ReadingContext::lookupProperty in
/// texttomodelmerger.cpp
/// ### Maybe put this into the context as a helper method.