Skip to content

Commit

Permalink
strict-type-predicates: allow comparing typeof with expression (palan…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and adidahiya committed Jan 9, 2018
1 parent 7a2bfbd commit 18fc973
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/rules/strictTypePredicatesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { isBinaryExpression, isTypeFlagSet, isUnionType } from "tsutils";
import { isBinaryExpression, isIdentifier, isLiteralExpression, isTypeFlagSet, isUnionType } from "tsutils";

import * as ts from "typescript";
import { showWarningOnce } from "../error";
Expand Down Expand Up @@ -130,11 +130,19 @@ function getTypePredicateOneWay(left: ts.Expression, right: ts.Expression, isStr
switch (right.kind) {
case ts.SyntaxKind.TypeOfExpression:
const expression = (right as ts.TypeOfExpression).expression;
const kind = left.kind === ts.SyntaxKind.StringLiteral ? (left as ts.StringLiteral).text : "";
const predicate = getTypePredicateForKind(kind);
if (!isLiteralExpression(left)) {
if (isIdentifier(left) && left.text === "undefined" ||
left.kind === ts.SyntaxKind.NullKeyword ||
left.kind === ts.SyntaxKind.TrueKeyword ||
left.kind === ts.SyntaxKind.FalseKeyword) {
return {kind: TypePredicateKind.TypeofTypo};
}
return undefined;
}
const predicate = getTypePredicateForKind(left.text);
return predicate === undefined
? { kind: TypePredicateKind.TypeofTypo }
: { kind: TypePredicateKind.Plain, expression, predicate, isNullOrUndefined: kind === "undefined" };
: { kind: TypePredicateKind.Plain, expression, predicate, isNullOrUndefined: left.text === "undefined" };

case ts.SyntaxKind.NullKeyword:
return nullOrUndefined(ts.TypeFlags.Null);
Expand Down
16 changes: 16 additions & 0 deletions test/rules/strict-type-predicates/strict-null-checks/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ declare function get<T>(): T;

typeof get<any>() === "orbject";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [typeof]

typeof get<string | number>() === `string`;
typeof get<string | number>() === `stirng`;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [typeof]

let a: string, b: string;
typeof a === typeof b;
typeof a === b;
a === typeof b;
typeof a === undefined;
~~~~~~~~~~~~~~~~~~~~~~ [F]

undefined === typeof a;
~~~~~~~~~~~~~~~~~~~~~~ [typeof]
null === typeof b;
~~~~~~~~~~~~~~~~~ [typeof]
}

[T]: Expression is always true.
Expand Down

0 comments on commit 18fc973

Please sign in to comment.