diff options
Diffstat (limited to 'node_modules/tslint/lib/rules/strictTypePredicatesRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/strictTypePredicatesRule.js | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/node_modules/tslint/lib/rules/strictTypePredicatesRule.js b/node_modules/tslint/lib/rules/strictTypePredicatesRule.js index 526116dd3..0a4c1a41e 100644 --- a/node_modules/tslint/lib/rules/strictTypePredicatesRule.js +++ b/node_modules/tslint/lib/rules/strictTypePredicatesRule.js @@ -43,7 +43,7 @@ var Rule = /** @class */ (function (_super) { /* tslint:disable:object-literal-sort-keys */ Rule.metadata = { ruleName: "strict-type-predicates", - description: (_a = ["\n Warns for type predicates that are always true or always false.\n Works for 'typeof' comparisons to constants (e.g. 'typeof foo === \"string\"'), and equality comparison to 'null'/'undefined'.\n (TypeScript won't let you compare '1 === 2', but it has an exception for '1 === undefined'.)\n Does not yet work for 'instanceof'.\n Does *not* warn for 'if (x.y)' where 'x.y' is always truthy. For that, see strict-boolean-expressions.\n\n This rule requires `strictNullChecks` to work properly."], _a.raw = ["\n Warns for type predicates that are always true or always false.\n Works for 'typeof' comparisons to constants (e.g. 'typeof foo === \"string\"'), and equality comparison to 'null'/'undefined'.\n (TypeScript won't let you compare '1 === 2', but it has an exception for '1 === undefined'.)\n Does not yet work for 'instanceof'.\n Does *not* warn for 'if (x.y)' where 'x.y' is always truthy. For that, see strict-boolean-expressions.\n\n This rule requires \\`strictNullChecks\\` to work properly."], Lint.Utils.dedent(_a)), + description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Warns for type predicates that are always true or always false.\n Works for 'typeof' comparisons to constants (e.g. 'typeof foo === \"string\"'), and equality comparison to 'null'/'undefined'.\n (TypeScript won't let you compare '1 === 2', but it has an exception for '1 === undefined'.)\n Does not yet work for 'instanceof'.\n Does *not* warn for 'if (x.y)' where 'x.y' is always truthy. For that, see strict-boolean-expressions.\n\n This rule requires `strictNullChecks` to work properly."], ["\n Warns for type predicates that are always true or always false.\n Works for 'typeof' comparisons to constants (e.g. 'typeof foo === \"string\"'), and equality comparison to 'null'/'undefined'.\n (TypeScript won't let you compare '1 === 2', but it has an exception for '1 === undefined'.)\n Does not yet work for 'instanceof'.\n Does *not* warn for 'if (x.y)' where 'x.y' is always truthy. For that, see strict-boolean-expressions.\n\n This rule requires \\`strictNullChecks\\` to work properly."]))), optionsDescription: "Not configurable.", options: null, optionExamples: [true], @@ -78,7 +78,7 @@ function walk(ctx, checker) { } var exprType = checker.getTypeAtLocation(exprPred.expression); // TODO: could use checker.getBaseConstraintOfType to help with type parameters, but it's not publicly exposed. - if (Lint.isTypeFlagSet(exprType, ts.TypeFlags.Any | ts.TypeFlags.TypeParameter)) { + if (tsutils_1.isTypeFlagSet(exprType, ts.TypeFlags.Any | ts.TypeFlags.TypeParameter)) { return; } switch (exprPred.kind) { @@ -116,11 +116,19 @@ function getTypePredicateOneWay(left, right, isStrictEquals) { switch (right.kind) { case ts.SyntaxKind.TypeOfExpression: var expression = right.expression; - var kind = left.kind === ts.SyntaxKind.StringLiteral ? left.text : ""; - var predicate = getTypePredicateForKind(kind); + if (!tsutils_1.isLiteralExpression(left)) { + if (tsutils_1.isIdentifier(left) && left.text === "undefined" || + left.kind === ts.SyntaxKind.NullKeyword || + left.kind === ts.SyntaxKind.TrueKeyword || + left.kind === ts.SyntaxKind.FalseKeyword) { + return { kind: 2 /* TypeofTypo */ }; + } + return undefined; + } + var predicate = getTypePredicateForKind(left.text); return predicate === undefined ? { kind: 2 /* TypeofTypo */ } - : { kind: 0 /* Plain */, expression: expression, predicate: predicate, isNullOrUndefined: kind === "undefined" }; + : { kind: 0 /* Plain */, expression: expression, predicate: predicate, isNullOrUndefined: left.text === "undefined" }; case ts.SyntaxKind.NullKeyword: return nullOrUndefined(ts.TypeFlags.Null); case ts.SyntaxKind.Identifier: @@ -159,13 +167,13 @@ function getTypePredicateForKind(kind) { // It's an object if it's not any of the above. var allFlags_1 = ts.TypeFlags.Undefined | ts.TypeFlags.Void | ts.TypeFlags.BooleanLike | ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike | ts.TypeFlags.ESSymbol; - return function (type) { return !Lint.isTypeFlagSet(type, allFlags_1) && !isFunction(type); }; + return function (type) { return !tsutils_1.isTypeFlagSet(type, allFlags_1) && !isFunction(type); }; default: return undefined; } } function flagPredicate(testedFlag) { - return function (type) { return Lint.isTypeFlagSet(type, testedFlag); }; + return function (type) { return tsutils_1.isTypeFlagSet(type, testedFlag); }; } function isFunction(t) { if (t.getConstructSignatures().length !== 0 || t.getCallSignatures().length !== 0) { @@ -199,10 +207,10 @@ function testNonStrictNullUndefined(type) { var anyOther = false; for (var _i = 0, _a = unionParts(type); _i < _a.length; _i++) { var ty = _a[_i]; - if (Lint.isTypeFlagSet(ty, ts.TypeFlags.Null)) { + if (tsutils_1.isTypeFlagSet(ty, ts.TypeFlags.Null)) { anyNull = true; } - else if (Lint.isTypeFlagSet(ty, undefinedFlags)) { + else if (tsutils_1.isTypeFlagSet(ty, undefinedFlags)) { anyUndefined = true; } else { @@ -218,4 +226,4 @@ function testNonStrictNullUndefined(type) { function unionParts(type) { return tsutils_1.isUnionType(type) ? type.types : [type]; } -var _a; +var templateObject_1; |