Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
no-unsafe-any: Allow to switch on a value of type any (#2836)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and adidahiya committed May 30, 2017
1 parent 8ea3c35 commit a510150
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/rules/noUnsafeAnyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ function walk(ctx: Lint.WalkContext<void>, checker: ts.TypeChecker): void {
return;
}

case ts.SyntaxKind.SwitchStatement: {
const { expression, caseBlock: { clauses } } = node as ts.SwitchStatement;
// Allow `switch (x) {}` where `x` is any
cb(expression, /*anyOk*/ true);
for (const clause of clauses) {
if (clause.kind === ts.SyntaxKind.CaseClause) {
// Allow `case x:` where `x` is any
cb(clause.expression, /*anyOk*/ true);
}
for (const statement of clause.statements) {
cb(statement);
}
}
break;
}

default:
if (!(isExpression(node) && check())) {
return ts.forEachChild(node, cb);
Expand Down
12 changes: 12 additions & 0 deletions test/rules/no-unsafe-any/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,16 @@ const obj: { x: number, y: number } = {
~ [0]
};

switch (x) {}
switch (x.y) {
~ [0]
case x:
x.y;
~ [0]
break;
case x.y:
~ [0]
break;
}

[0]: Unsafe use of expression of type 'any'.

0 comments on commit a510150

Please sign in to comment.