@@ -3,6 +3,7 @@ use oxc_diagnostics::OxcDiagnostic;
33use oxc_macros:: declare_oxc_lint;
44use oxc_span:: { GetSpan , Span } ;
55use oxc_syntax:: operator:: UnaryOperator ;
6+ use schemars:: JsonSchema ;
67
78use crate :: { AstNode , context:: LintContext , rule:: Rule } ;
89
@@ -23,11 +24,33 @@ fn invalid_value(help: Option<&'static str>, span: Span) -> OxcDiagnostic {
2324 d
2425}
2526
26- #[ derive( Debug , Clone , Default ) ]
27+ #[ derive( Debug , Clone , Default , JsonSchema ) ]
28+ #[ serde( rename_all = "camelCase" , default ) ]
2729pub struct ValidTypeof {
28- /// true requires typeof expressions to only be compared to string literals or other typeof expressions, and disallows comparisons to any other value.
30+ /// The `requireStringLiterals` option when set to `true`, allows the comparison of `typeof`
31+ /// expressions with only string literals or other `typeof` expressions, and disallows
32+ /// comparisons to any other value. Default is `false`.
33+ ///
34+ /// With `requireStringLiterals` set to `true`, the following are examples of **incorrect** code:
35+ /// ```js
36+ /// typeof foo === undefined
37+ /// typeof bar == Object
38+ /// typeof baz === "strnig"
39+ /// typeof qux === "some invalid type"
40+ /// typeof baz === anotherVariable
41+ /// typeof foo == 5
42+ /// ```
43+ ///
44+ /// With `requireStringLiterals` set to `true`, the following are examples of **correct** code:
45+ /// ```js
46+ /// typeof foo === "undefined"
47+ /// typeof bar == "object"
48+ /// typeof baz === "string"
49+ /// typeof bar === typeof qux
50+ /// ```
2951 require_string_literals : bool ,
3052}
53+
3154declare_oxc_lint ! (
3255 /// ### What it does
3356 ///
@@ -57,38 +80,11 @@ declare_oxc_lint!(
5780 /// typeof foo === baz
5881 /// typeof bar === typeof qux
5982 /// ```
60- ///
61- /// ### Options
62- ///
63- /// #### requireStringLiterals
64- ///
65- /// `{ type: boolean, default: false }`
66- ///
67- /// The `requireStringLiterals` option when set to `true`, allows the comparison of `typeof`
68- /// expressions with only string literals or other `typeof` expressions, and disallows
69- /// comparisons to any other value. Default is `false`.
70- ///
71- /// With `requireStringLiterals` set to `true` the following are examples of incorrect code:
72- /// ```js
73- /// typeof foo === undefined
74- /// typeof bar == Object
75- /// typeof baz === "strnig"
76- /// typeof qux === "some invalid type"
77- /// typeof baz === anotherVariable
78- /// typeof foo == 5
79- /// ```
80- ///
81- /// With `requireStringLiterals` set to `true` the following are examples of correct code:
82- /// ```js
83- /// typeof foo === "undefined"
84- /// typeof bar == "object"
85- /// typeof baz === "string"
86- /// typeof bar === typeof qux
87- /// ```
8883 ValidTypeof ,
8984 eslint,
9085 correctness,
91- conditional_fix
86+ conditional_fix,
87+ config = ValidTypeof ,
9288) ;
9389
9490impl Rule for ValidTypeof {
0 commit comments