Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

[feat] add no-negated-condition-rule #365

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/rules/terNoNegatedConditionRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';

const RULE_NAME = 'ter-no-negated-condition';
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = 'Unexpected negated condition.';
public static metadata: Lint.IRuleMetadata = {
ruleName: RULE_NAME,
hasFix: false,
description: 'Disallows negated conditions for if statements with else branch and ternary expressions',
rationale: Lint.Utils.dedent`
Negated conditions are more difficult to understand.
Code can be made more readable by inverting the condition instead.`,
optionsDescription: '',
options: {},
optionExamples: [
Lint.Utils.dedent`
"${RULE_NAME}": true
`
],
typescriptOnly: false,
type: 'style'
};

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walk(sourceFile, this.getOptions()));
}
}

class Walk extends Lint.RuleWalker {
protected visitIfStatement(node: ts.IfStatement) {
if (this.hasElseWithoutCondition(node) && (
this.isSingleOperatorNegatedCondition(node.expression) ? this.isConditionNegated(node.expression) : this.isConditionNotEqual(node.expression as ts.BinaryExpression)
)) {
this.addFailureAtNode(
node,
Rule.FAILURE_STRING
);
}
super.visitIfStatement(node);
}

protected visitConditionalExpression(node: ts.ConditionalExpression) {
if ((this.isSingleOperatorNegatedCondition(node.condition) && this.isConditionNegated(node.condition)) || (this.isConditionNotEqual(node.condition as ts.BinaryExpression))) {
this.addFailureAtNode(
node,
Rule.FAILURE_STRING
);
}
super.visitConditionalExpression(node);
}

private hasElseWithoutCondition(node: ts.IfStatement) {
return node.elseStatement && node.elseStatement.getFirstToken() && node.elseStatement.getFirstToken()!.kind !== ts.SyntaxKind.IfKeyword;
}

private isSingleOperatorNegatedCondition(condition: ts.ConditionalExpression['condition']) {
// Child count should be 2 so we can ensure that whole condition is negated(e.g. !(true && true) instead of !true && false)
return condition.getChildCount() === 2;
}

private isConditionNegated(condition: ts.ConditionalExpression['condition']) {
return condition.getFirstToken() && condition.getFirstToken()!.kind === ts.SyntaxKind.ExclamationToken;
}

private isConditionNotEqual(condition: ts.BinaryExpression) {
return condition.operatorToken && (condition.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken ||
condition.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken);
}
}
56 changes: 56 additions & 0 deletions src/test/rules/terNoNegatedConditionRuleTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { RuleTester, Position } from './ruleTester';
// ESLint Tests: https://github.com/eslint/eslint/blob/master/lib/rules/no-negated-condition.js

const ruleTester = new RuleTester('ter-no-negated-condition');

const noNegatedConditionError = {
failure: 'Unexpected negated condition.',
startPosition: new Position(0),
endPosition: new Position(0)
};

ruleTester.addTestGroup('valid', 'should pass when condition is not negated or cannot be converted', [
'if (a) {}',
'if (a) {} else {}',
'if (!a) {}',
'if (!a) {} else if (b) {}',
'if (!a) {} else if (b) {} else {}',
'if (a == b) {}',
'if (a == b) {} else {}',
'if (a != b) {}',
'if (a != b) {} else if (b) {}',
'if (a != b) {} else if (b) {} else {}',
'if (a !== b) {}',
'if (a === b) {} else {}',
'a ? b : c',
'!a && b ? c : d'
]);

ruleTester.addTestGroup('invalid', 'should fail condition is negated and second branch is available', [
{
code: 'if (!a) {;} else {;}',
errors: [noNegatedConditionError]
},
{
code: 'if (a != b) {;} else {;}',
errors: [noNegatedConditionError]
},
{
code: 'if (a !== b) {;} else {;}',
errors: [noNegatedConditionError]
},
{
code: '!a ? b : c',
errors: [noNegatedConditionError]
},
{
code: 'a != b ? c : d',
errors: [noNegatedConditionError]
},
{
code: 'a !== b ? c : d',
errors: [noNegatedConditionError]
}
]);

ruleTester.runTests();