QmlJS checks: Add check for spacing around binary operators.

Adapted from QtChecker.

Change-Id: I434582314e6431c56c9628a5a642bbfb032dfc2f
Reviewed-on: http://codereview.qt-project.org/5857
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@nokia.com>
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
This commit is contained in:
Christian Kamm
2011-09-30 10:07:37 +02:00
parent da123becaf
commit aeadbcae40

View File

@@ -518,6 +518,7 @@ Check::Check(Document::Ptr doc, const ContextPtr &context)
disableMessage(HintAnonymousFunctionSpacing);
disableMessage(HintDeclareVarsInOneLine);
disableMessage(HintDeclarationsShouldBeAtStartOfFunction);
disableMessage(HintBinaryOperatorSpacing);
}
Check::~Check()
@@ -853,12 +854,22 @@ static bool shouldAvoidNonStrictEqualityCheck(const Value *lhs, const Value *rhs
bool Check::visit(BinaryExpression *ast)
{
const QString source = _doc->source();
// check spacing
SourceLocation op = ast->operatorToken;
if ((op.begin() > 0 && !source.at(op.begin() - 1).isSpace())
|| (int(op.end()) < source.size() && !source.at(op.end()).isSpace())) {
addMessage(HintBinaryOperatorSpacing, op);
}
// check ==, !=
if (ast->op == QSOperator::Equal || ast->op == QSOperator::NotEqual) {
Evaluate eval(&_scopeChain);
const Value *lhs = eval(ast->left);
const Value *rhs = eval(ast->right);
if (shouldAvoidNonStrictEqualityCheck(lhs, rhs)
|| shouldAvoidNonStrictEqualityCheck(rhs, lhs)) {
const Value *lhsValue = eval(ast->left);
const Value *rhsValue = eval(ast->right);
if (shouldAvoidNonStrictEqualityCheck(lhsValue, rhsValue)
|| shouldAvoidNonStrictEqualityCheck(rhsValue, lhsValue)) {
addMessage(MaybeWarnEqualityTypeCoercion, ast->operatorToken);
}
}