This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
strict-type-predicates: warn if strictNullChecks is not enabled #2786
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import { isBinaryExpression, isUnionType } from "tsutils"; | ||
|
||
import * as ts from "typescript"; | ||
import { showWarningOnce } from "../error"; | ||
import * as Lint from "../index"; | ||
|
||
// tslint:disable:no-bitwise | ||
|
@@ -31,7 +32,9 @@ export class Rule extends Lint.Rules.TypedRule { | |
Works for 'typeof' comparisons to constants (e.g. 'typeof foo === "string"'), and equality comparison to 'null'/'undefined'. | ||
(TypeScript won't let you compare '1 === 2', but it has an exception for '1 === undefined'.) | ||
Does not yet work for 'instanceof'. | ||
Does *not* warn for 'if (x.y)' where 'x.y' is always truthy. For that, see strict-boolean-expressions.`, | ||
Does *not* warn for 'if (x.y)' where 'x.y' is always truthy. For that, see strict-boolean-expressions. | ||
|
||
This rule requires \`strictNullChecks\` to work properly.`, | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: [true], | ||
|
@@ -52,6 +55,10 @@ export class Rule extends Lint.Rules.TypedRule { | |
} | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | ||
if (!Lint.isStrictNullChecksEnabled(program.getCompilerOptions())) { | ||
showWarningOnce("strict-type-predicates does not work without strictNullChecks"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please suggest a better warning message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine. Would probably be more clear if it said |
||
return []; | ||
} | ||
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program.getTypeChecker())); | ||
} | ||
} | ||
|
3 changes: 2 additions & 1 deletion
3
test/rules/strict-boolean-expressions/no-null-checks-allow-null-union/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs" | ||
"strict": true, | ||
"strictNullChecks": false | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
test/rules/strict-boolean-expressions/no-null-checks-allow-number-string/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs" | ||
"strictNullChecks": false | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
test/rules/strict-type-predicates/no-strict-null-checks/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
declare function get<T>(): T; | ||
|
||
// typeof undefined | ||
{ | ||
typeof get<boolean>() === "undefined"; | ||
|
||
typeof get<void>() === "undefined"; | ||
|
||
typeof get<boolean | undefined>() === "undefined"; | ||
|
||
declare const c: any; | ||
typeof get<any>() === "undefined"; | ||
|
||
// 'undefined' is not assignable to '{}' | ||
typeof get<{}>() === "undefined"; | ||
} | ||
|
||
// typeof boolean | ||
{ | ||
declare const a: boolean; | ||
typeof get<boolean>() === "boolean"; | ||
|
||
typeof get<Boolean>() === "boolean"; | ||
|
||
typeof get<boolean | undefined>() === "boolean"; | ||
|
||
typeof get<{}>() === "boolean"; | ||
} | ||
|
||
// typeof number | ||
{ | ||
enum E {} | ||
|
||
typeof get<E>() === "number"; | ||
|
||
typeof get<Number>() === "number"; | ||
} | ||
|
||
// typeof string | ||
{ | ||
typeof get<"abc" | "def">() === "string"; | ||
|
||
typeof get<String>() === "string"; | ||
|
||
typeof get<"abc" | undefined>() === "string"; | ||
} | ||
|
||
// typeof symbol | ||
{ | ||
typeof get<symbol>() === "symbol"; | ||
|
||
typeof get<string>() === "symbol"; | ||
|
||
typeof get<symbol | string>() === "symbol"; | ||
} | ||
|
||
// typeof function | ||
{ | ||
typeof get<() => void>() === "function"; | ||
|
||
typeof get<Function>() === "function"; | ||
|
||
|
||
typeof get<number>() === "function"; | ||
|
||
typeof get<number | (() => void)>() === "function"; | ||
|
||
class X {} | ||
typeof X === "function"; | ||
typeof X === "object"; | ||
|
||
// Works with union | ||
class Foo { } | ||
typeof get<typeof Foo | object> === "function"; | ||
} | ||
|
||
// typeof object | ||
{ | ||
typeof get<boolean | number | string | symbol | (() => void) | Function>() === "object"; | ||
|
||
typeof get<{}> === "object"; | ||
} | ||
|
||
// === null / undefined | ||
{ | ||
get<number | undefined>() === null; | ||
|
||
get<number | null>() === null; | ||
|
||
get<number | null>() === undefined; | ||
|
||
get<number | undefined>() === undefined; | ||
|
||
// 'null' and 'undefined' are not assignable to '{}' | ||
|
||
get<{}>() === null; | ||
|
||
get<{}>() === undefined; | ||
|
||
get<string | undefined>() == null; | ||
|
||
get<string | null>() == undefined; | ||
|
||
get<string | null>() == null; | ||
|
||
get<string | undefined>() == undefined; | ||
|
||
get<string | undefined>() != null; | ||
|
||
get<{}>() == null; | ||
|
||
get<string | null | undefined>() == null; | ||
get<string | null | undefined>() != undefined; | ||
|
||
get<null|undefined>() == null; | ||
get<null|undefined>() != undefined; | ||
} | ||
|
||
// negation | ||
{ | ||
get<number | undefined>() !== null; | ||
|
||
get<number | null>() !== null; | ||
|
||
get<number | null>() !== undefined; | ||
|
||
get<number | undefined>() !== undefined; | ||
|
||
typeof get<string>() !== "string"; | ||
} | ||
|
||
// reverse left/right | ||
{ | ||
"string" === typeof get<number>(); | ||
|
||
undefined === get<void>(); | ||
} | ||
|
||
// type parameters | ||
{ | ||
function f<T>(t: T) { | ||
typeof t === "boolean"; | ||
} | ||
|
||
// TODO: Would be nice to catch this. | ||
function g<T extends string>(t: T) { | ||
typeof t === "boolean"; | ||
} | ||
} | ||
|
||
// Detects bad typeof | ||
{ | ||
typeof get<boolean>() === true; | ||
|
||
typeof get<any>() === "orbject"; | ||
} |
5 changes: 5 additions & 0 deletions
5
test/rules/strict-type-predicates/no-strict-null-checks/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
// strictNullChecks not enabled | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
test/rules/strict-type-predicates/strict-null-checks/tslint.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"strict-type-predicates": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this the right file for this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay. We'll probably have to split apart this file some time in the future