diff --git a/src/rules/noUnsafeAnyRule.ts b/src/rules/noUnsafeAnyRule.ts index c2d22d8e372..99a04db3bfc 100644 --- a/src/rules/noUnsafeAnyRule.ts +++ b/src/rules/noUnsafeAnyRule.ts @@ -60,6 +60,7 @@ function walk(ctx: Lint.WalkContext, checker: ts.TypeChecker): void { case ts.SyntaxKind.Parameter: { const { type, initializer } = node as ts.ParameterDeclaration; + // TODO handle destructuring if (initializer !== undefined) { return cb(initializer, /*anyOk*/ type !== undefined && type.kind === ts.SyntaxKind.AnyKeyword); } @@ -187,6 +188,34 @@ function walk(ctx: Lint.WalkContext, checker: ts.TypeChecker): void { return; } + case ts.SyntaxKind.IfStatement: { + const { expression, thenStatement, elseStatement } = node as ts.IfStatement; + cb(expression, true); // allow truthyness check + cb(thenStatement); + if (elseStatement !== undefined) { cb(elseStatement); } + return; + } + + case ts.SyntaxKind.PrefixUnaryExpression: { + const {operator, operand} = node as ts.PrefixUnaryExpression; + cb(operand, operator === ts.SyntaxKind.ExclamationToken); // allow falsyness check + check(); + return; + } + + case ts.SyntaxKind.ForStatement: { + const { initializer, condition, incrementor, statement } = node as ts.ForStatement; + if (initializer !== undefined) { cb(initializer); } + if (condition !== undefined) { cb(condition, true); } // allow truthyness check + if (incrementor !== undefined) { cb(incrementor); } + return cb(statement); + } + + case ts.SyntaxKind.DoStatement: + case ts.SyntaxKind.WhileStatement: + cb((node as ts.IterationStatement).statement); + return cb((node as ts.DoStatement | ts.WhileStatement).expression, true); + default: if (!(isExpression(node) && check())) { return ts.forEachChild(node, cb); @@ -230,6 +259,8 @@ function walk(ctx: Lint.WalkContext, checker: ts.TypeChecker): void { return cb(right); case ts.SyntaxKind.CommaToken: // Allow `any, any` + case ts.SyntaxKind.BarBarToken: // Allow `any || any` + case ts.SyntaxKind.AmpersandAmpersandToken: // Allow `any && any` cb(left, /*anyOk*/ true); return cb(right, /*anyOk*/ true); diff --git a/test/rules/no-unsafe-any/test.ts.lint b/test/rules/no-unsafe-any/test.ts.lint index 0d237d2b4d1..274cd989826 100644 --- a/test/rules/no-unsafe-any/test.ts.lint +++ b/test/rules/no-unsafe-any/test.ts.lint @@ -181,4 +181,11 @@ switch (x.y) { declare global {} +if (x) {} +if (!x) {} +if (!x || x) {} +for (;x;) {} +while (x) {} +do {} while (x); + [0]: Unsafe use of expression of type 'any'.