boolean-trivia lint rule: Lint for null/undefined too

This commit is contained in:
Andy Hanson
2017-04-07 13:13:39 -07:00
parent 3029b8fe38
commit 7320891933
24 changed files with 92 additions and 68 deletions

View File

@@ -8,37 +8,63 @@ export class Rule extends Lint.Rules.AbstractRule {
}
function walk(ctx: Lint.WalkContext<void>): void {
ts.forEachChild(ctx.sourceFile, recur);
function recur(node: ts.Node): void {
ts.forEachChild(ctx.sourceFile, function recur(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.CallExpression) {
checkCall(node as ts.CallExpression);
}
ts.forEachChild(node, recur);
}
});
function checkCall(node: ts.CallExpression): void {
for (const arg of node.arguments) {
if (arg.kind !== ts.SyntaxKind.TrueKeyword && arg.kind !== ts.SyntaxKind.FalseKeyword) {
continue;
switch (arg.kind) {
case ts.SyntaxKind.TrueKeyword:
case ts.SyntaxKind.FalseKeyword:
case ts.SyntaxKind.NullKeyword:
break;
case ts.SyntaxKind.Identifier:
if ((arg as ts.Identifier).originalKeywordKind !== ts.SyntaxKind.UndefinedKeyword) {
continue;
}
break;
default:
continue;
}
if (node.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
const methodName = (node.expression as ts.PropertyAccessExpression).name.text
const methodName = (node.expression as ts.PropertyAccessExpression).name.text;
// Skip certain method names whose parameter names are not informative
if (methodName === 'set' ||
methodName === 'equal' ||
methodName === 'fail' ||
methodName === 'isTrue' ||
methodName === 'assert') {
if (methodName.indexOf("set") === 0) {
continue;
}
switch (methodName) {
case "apply":
case "assert":
case "call":
case "equal":
case "fail":
case "isTrue":
case "output":
case "stringify":
continue;
}
}
else if (node.expression.kind === ts.SyntaxKind.Identifier) {
const functionName = (node.expression as ts.Identifier).text;
// Skip certain function names whose parameter names are not informative
if (functionName === 'assert') {
if (functionName.indexOf("set") === 0) {
continue;
}
switch (functionName) {
case "assert":
case "contains":
case "createAnonymousType":
case "createImportSpecifier":
case "createProperty":
case "createSignature":
case "resolveName":
continue;
}
}
const ranges = ts.getLeadingCommentRanges(arg.getFullText(), 0);