This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #11 - added no-extra-boolean-cast rule
- Loading branch information
Showing
5 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/// <reference path='helper.d.ts' /> | ||
// import {ts, Lint} from './helper'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static FAILURE_STRING = { | ||
if: 'redundant double negation in an if statement condition', | ||
do: 'redundant double negation in a do while loop condition', | ||
while: 'redundant double negation in a while loop condition', | ||
ternaryif: 'redundant double negation in a ternary condition', | ||
for: 'redundant double negation in a for loop condition', | ||
unaryCast: 'redundant multiple negation', | ||
objectCast: 'redundant double negation in call to Boolean()', | ||
newCast: 'redundant double negation in Boolean constructor call' | ||
}; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
const walker = new NoExtraBooleanCastWalker(sourceFile, this.getOptions()); | ||
return this.applyWithWalker(walker); | ||
} | ||
} | ||
|
||
class NoExtraBooleanCastWalker extends Lint.RuleWalker { | ||
protected visitPrefixUnaryExpression(node: ts.PrefixUnaryExpression) { | ||
this.validateNoExtraBoolean(node); | ||
super.visitPrefixUnaryExpression(node); | ||
} | ||
|
||
private validateNoExtraBoolean(node: ts.PrefixUnaryExpression) { | ||
const parent = node.parent; | ||
const grandparent = parent.parent; | ||
|
||
// Exit early if it's guaranteed not to match | ||
if (node.operator !== ts.SyntaxKind.ExclamationToken || | ||
parent.kind !== ts.SyntaxKind.PrefixUnaryExpression || | ||
(parent as ts.PrefixUnaryExpression).operator !== ts.SyntaxKind.ExclamationToken) { | ||
return; | ||
} | ||
|
||
if (grandparent.kind === ts.SyntaxKind.IfStatement) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.if)); | ||
} | ||
else if (grandparent.kind === ts.SyntaxKind.DoStatement) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.do)); | ||
} | ||
else if (grandparent.kind === ts.SyntaxKind.WhileStatement) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.while)); | ||
} | ||
else if (grandparent.kind === ts.SyntaxKind.ConditionalExpression && parent === (grandparent as ts.ConditionalExpression).condition) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.ternaryif)); | ||
} | ||
else if (grandparent.kind === ts.SyntaxKind.ForStatement && parent === (grandparent as ts.ForStatement).condition) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.for)); | ||
} | ||
else if (grandparent.kind === ts.SyntaxKind.PrefixUnaryExpression && (grandparent as ts.PrefixUnaryExpression).operator === ts.SyntaxKind.ExclamationToken) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.unaryCast)); | ||
} | ||
else if (grandparent.kind === ts.SyntaxKind.CallExpression && grandparent.getText().startsWith('Boolean')) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.objectCast)); | ||
} | ||
else if (grandparent.kind === ts.SyntaxKind.NewExpression && grandparent.getText().startsWith('new Boolean')) { | ||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING.newCast)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/// <reference path='../../../typings/mocha/mocha.d.ts' /> | ||
import {makeTest} from './helper'; | ||
|
||
const rule = 'no-extra-boolean-cast'; | ||
const scripts = { | ||
valid: [ | ||
'if (!foo) {}', | ||
'const x = !foo;', | ||
'const foo = true;', | ||
'const foo = !!bar;', | ||
'function foo() { return !!bar }', | ||
'const foo = bar ? !!x : !!y;`' | ||
], | ||
invalid: [ | ||
'if (!!foo) {}', | ||
'const foo = !!!bar;', | ||
'const foo = !!bar ? baz : bat;', | ||
'const foo = Boolean(!!bar);', | ||
'const foo = new Boolean(!!bar);', | ||
'while (!!foo) {}', | ||
'do {} while (!!foo);', | ||
'for (; !!foo; ) {}`' | ||
] | ||
}; | ||
|
||
describe(rule, function test() { | ||
it('should pass when using valid boolean casts outside of a boolean context', function testValid() { | ||
makeTest(rule, scripts.valid, true); | ||
}); | ||
|
||
it('should fail when using redundant boolean casts in a boolean context', function testInvalid() { | ||
makeTest(rule, scripts.invalid, false); | ||
}); | ||
}); |