@@ -31,24 +31,60 @@ pub struct ValidTypeof {
31
31
}
32
32
declare_oxc_lint ! (
33
33
/// ### What it does
34
- /// Enforce comparing `typeof` expressions against valid strings
34
+ ///
35
+ /// Enforce comparing `typeof` expressions against valid strings.
35
36
///
36
37
/// ### Why is this bad?
37
- /// It is usually a typing mistake to compare the result of a `typeof`
38
- /// operator to other string literals.
39
38
///
40
- /// ### Example
39
+ /// For a vast majority of use cases, the result of the `typeof` operator is one of the
40
+ /// following string literals: `"undefined"`, `"object"`, `"boolean"`, `"number"`, `"string"`,
41
+ /// `"function"`, `"symbol"`, and `"bigint"`. It is usually a typing mistake to compare the
42
+ /// result of a `typeof` operator to other string literals.
43
+ ///
44
+ /// ### Examples
45
+ ///
46
+ /// Examples of **incorrect** code for this rule:
41
47
/// ```js
42
- /// // requireStringLiterals: false
43
- /// // incorrect:
44
48
/// typeof foo === "strnig"
45
- /// // correct:
49
+ /// typeof foo == "undefimed"
50
+ /// typeof bar != "nunber" // spellchecker:disable-line
51
+ /// typeof bar !== "fucntion" // spellchecker:disable-line
52
+ /// ```
53
+ ///
54
+ /// Examples of **correct** code for this rule:
55
+ /// ```js
46
56
/// typeof foo === "string"
57
+ /// typeof bar == "undefined"
47
58
/// typeof foo === baz
59
+ /// typeof bar === typeof qux
60
+ /// ```js
48
61
///
49
- /// // requireStringLiterals: true
50
- /// // incorrect:
51
- /// typeof foo === baz
62
+ /// ### Options
63
+ ///
64
+ /// #### requireStringLiterals
65
+ ///
66
+ /// `{ type: boolean, default: false }`
67
+ ///
68
+ /// The `requireStringLiterals` option when set to `true`, allows the comparison of `typeof`
69
+ /// expressions with only string literals or other `typeof` expressions, and disallows
70
+ /// comparisons to any other value. Default is `false`.
71
+ ///
72
+ /// With `requireStringLiterals` set to `true` the following are examples of incorrect code:
73
+ /// ```js
74
+ /// typeof foo === undefined
75
+ /// typeof bar == Object
76
+ /// typeof baz === "strnig"
77
+ /// typeof qux === "some invalid type"
78
+ /// typeof baz === anotherVariable
79
+ /// typeof foo == 5
80
+ /// ```
81
+ ///
82
+ /// With `requireStringLiterals` set to `true` the following are examples of correct code:
83
+ /// ```js
84
+ /// typeof foo === "undefined"
85
+ /// typeof bar == "object"
86
+ /// typeof baz === "string"
87
+ /// typeof bar === typeof qux
52
88
/// ```
53
89
ValidTypeof ,
54
90
eslint,
0 commit comments