diff options
Diffstat (limited to 'node_modules/tslint/lib/rules/noUnsafeAnyRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/noUnsafeAnyRule.js | 96 |
1 files changed, 79 insertions, 17 deletions
diff --git a/node_modules/tslint/lib/rules/noUnsafeAnyRule.js b/node_modules/tslint/lib/rules/noUnsafeAnyRule.js index f07eff92b..60ba3aabb 100644 --- a/node_modules/tslint/lib/rules/noUnsafeAnyRule.js +++ b/node_modules/tslint/lib/rules/noUnsafeAnyRule.js @@ -17,6 +17,7 @@ */ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); +var tsutils_1 = require("tsutils"); var ts = require("typescript"); var Lint = require("../index"); var Rule = (function (_super) { @@ -27,21 +28,21 @@ var Rule = (function (_super) { Rule.prototype.applyWithProgram = function (sourceFile, program) { return this.applyWithFunction(sourceFile, function (ctx) { return walk(ctx, program.getTypeChecker()); }); }; + /* tslint:disable:object-literal-sort-keys */ + Rule.metadata = { + ruleName: "no-unsafe-any", + description: (_a = ["\n Warns when using an expression of type 'any' in a dynamic way.\n Uses are only allowed if they would work for `{} | null | undefined`.\n Type casts and tests are allowed.\n Expressions that work on all values (such as `\"\" + x`) are allowed."], _a.raw = ["\n Warns when using an expression of type 'any' in a dynamic way.\n Uses are only allowed if they would work for \\`{} | null | undefined\\`.\n Type casts and tests are allowed.\n Expressions that work on all values (such as \\`\"\" + x\\`) are allowed."], Lint.Utils.dedent(_a)), + optionsDescription: "Not configurable.", + options: null, + optionExamples: [true], + type: "functionality", + typescriptOnly: true, + requiresTypeInfo: true, + }; + /* tslint:enable:object-literal-sort-keys */ + Rule.FAILURE_STRING = "Unsafe use of expression of type 'any'."; return Rule; }(Lint.Rules.TypedRule)); -/* tslint:disable:object-literal-sort-keys */ -Rule.metadata = { - ruleName: "no-unsafe-any", - description: (_a = ["\n Warns when using an expression of type 'any' in a dynamic way.\n Uses are only allowed if they would work for `{} | null | undefined`.\n Type casts and tests are allowed.\n Expressions that work on all values (such as `\"\" + x`) are allowed."], _a.raw = ["\n Warns when using an expression of type 'any' in a dynamic way.\n Uses are only allowed if they would work for \\`{} | null | undefined\\`.\n Type casts and tests are allowed.\n Expressions that work on all values (such as \\`\"\" + x\\`) are allowed."], Lint.Utils.dedent(_a)), - optionsDescription: "Not configurable.", - options: null, - optionExamples: [true], - type: "functionality", - typescriptOnly: true, - requiresTypeInfo: true, -}; -/* tslint:enable:object-literal-sort-keys */ -Rule.FAILURE_STRING = "Unsafe use of expression of type 'any'."; exports.Rule = Rule; function walk(ctx, checker) { if (ctx.sourceFile.isDeclarationFile) { @@ -57,6 +58,7 @@ function walk(ctx, checker) { return cb(node.expression, anyOk); case ts.SyntaxKind.Parameter: { var _a = node, type = _a.type, initializer = _a.initializer; + // TODO handle destructuring if (initializer !== undefined) { return cb(initializer, /*anyOk*/ type !== undefined && type.kind === ts.SyntaxKind.AnyKeyword); } @@ -66,6 +68,7 @@ function walk(ctx, checker) { // Ignore label return cb(node.statement); case ts.SyntaxKind.BreakStatement: // Ignore label + case ts.SyntaxKind.ContinueStatement: // Ignore types case ts.SyntaxKind.InterfaceDeclaration: case ts.SyntaxKind.TypeAliasDeclaration: @@ -149,14 +152,71 @@ function walk(ctx, checker) { } return; } + case ts.SyntaxKind.SwitchStatement: { + var _h = node, expression = _h.expression, clauses = _h.caseBlock.clauses; + // Allow `switch (x) {}` where `x` is any + cb(expression, /*anyOk*/ true); + for (var _j = 0, clauses_1 = clauses; _j < clauses_1.length; _j++) { + var clause = clauses_1[_j]; + if (clause.kind === ts.SyntaxKind.CaseClause) { + // Allow `case x:` where `x` is any + cb(clause.expression, /*anyOk*/ true); + } + for (var _k = 0, _l = clause.statements; _k < _l.length; _k++) { + var statement = _l[_k]; + cb(statement); + } + } + break; + } + case ts.SyntaxKind.ModuleDeclaration: { + // In `declare global { ... }`, don't mark `global` as unsafe any. + var body = node.body; + if (body !== undefined) { + cb(body); + } + return; + } + case ts.SyntaxKind.IfStatement: { + var _m = node, expression = _m.expression, thenStatement = _m.thenStatement, elseStatement = _m.elseStatement; + cb(expression, true); // allow truthyness check + cb(thenStatement); + if (elseStatement !== undefined) { + cb(elseStatement); + } + return; + } + case ts.SyntaxKind.PrefixUnaryExpression: { + var _o = node, operator = _o.operator, operand = _o.operand; + cb(operand, operator === ts.SyntaxKind.ExclamationToken); // allow falsyness check + check(); + return; + } + case ts.SyntaxKind.ForStatement: { + var _p = node, initializer = _p.initializer, condition = _p.condition, incrementor = _p.incrementor, statement = _p.statement; + 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.statement); + return cb(node.expression, true); default: - if (!(ts.isExpression(node) && check())) { + if (!(tsutils_1.isExpression(node) && check())) { return ts.forEachChild(node, cb); } return; } function check() { - var isUnsafe = anyOk !== true && isNodeAny(node, checker); + var isUnsafe = !anyOk && isNodeAny(node, checker); if (isUnsafe) { ctx.addFailureAtNode(node, Rule.FAILURE_STRING); } @@ -184,9 +244,11 @@ function walk(ctx, checker) { return; } switch (operatorToken.kind) { - case ts.SyntaxKind.InstanceOfKeyword: + case ts.SyntaxKind.InstanceOfKeyword:// Allow test return cb(right); - case ts.SyntaxKind.CommaToken: + 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); case ts.SyntaxKind.EqualsToken: |