UNPKG

7.05 kBJavaScriptView Raw
1/**
2 * @fileoverview Ensures that the results of typeof are compared against a valid string
3 * @author Ian Christian Myers
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../rules/valid-typeof"),
13 RuleTester = require("../RuleTester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.run("valid-typeof", rule, {
22 valid: [
23 // Original test cases.
24 "typeof foo === 'string'",
25 "typeof foo === 'object'",
26 "typeof foo === 'function'",
27 "typeof foo === 'undefined'",
28 "typeof foo === 'boolean'",
29 "typeof foo === 'number'",
30 "'string' === typeof foo",
31 "'object' === typeof foo",
32 "'function' === typeof foo",
33 "'undefined' === typeof foo",
34 "'boolean' === typeof foo",
35 "'number' === typeof foo",
36 "typeof foo === typeof bar",
37 "typeof foo === baz",
38 "typeof foo !== someType",
39 "typeof bar != someType",
40 "someType === typeof bar",
41 "someType == typeof bar",
42 "typeof foo == 'string'",
43 "typeof(foo) === 'string'",
44 "typeof(foo) !== 'string'",
45 "typeof(foo) == 'string'",
46 "typeof(foo) != 'string'",
47 "var oddUse = typeof foo + 'thing'",
48 {
49 code: "typeof foo === 'number'",
50 options: [{ requireStringLiterals: true }]
51 },
52 {
53 code: "typeof foo === \"number\"",
54 options: [{ requireStringLiterals: true }]
55 },
56 {
57 code: "var baz = typeof foo + 'thing'",
58 options: [{ requireStringLiterals: true }]
59 },
60 {
61 code: "typeof foo === typeof bar",
62 options: [{ requireStringLiterals: true }]
63 },
64 {
65 code: "typeof foo === `string`",
66 options: [{ requireStringLiterals: true }],
67 parserOptions: { ecmaVersion: 6 }
68 },
69 {
70 code: "`object` === typeof foo",
71 options: [{ requireStringLiterals: true }],
72 parserOptions: { ecmaVersion: 6 }
73 },
74 {
75 code: "typeof foo === `str${somethingElse}`",
76 parserOptions: { ecmaVersion: 6 }
77 },
78
79 // Babel-specific test cases.
80 {
81 code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'"
82 },
83 {
84 code: "'bigint' === typeof BigInt(Number.MAX_SAFE_INTEGER)"
85 },
86 {
87 code: "typeof BigInt(Number.MAX_SAFE_INTEGER) === 'bigint'",
88 options: [{ requireStringLiterals: true }]
89 },
90 ],
91 invalid: [
92 {
93 code: "typeof foo === 'strnig'",
94 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
95 },
96 {
97 code: "'strnig' === typeof foo",
98 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
99 },
100 {
101 code: "if (typeof bar === 'umdefined') {}",
102 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
103 },
104 {
105 code: "typeof foo !== 'strnig'",
106 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
107 },
108 {
109 code: "'strnig' !== typeof foo",
110 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
111 },
112 {
113 code: "if (typeof bar !== 'umdefined') {}",
114 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
115 },
116 {
117 code: "typeof foo != 'strnig'",
118 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
119 },
120 {
121 code: "'strnig' != typeof foo",
122 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
123 },
124 {
125 code: "if (typeof bar != 'umdefined') {}",
126 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
127 },
128 {
129 code: "typeof foo == 'strnig'",
130 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
131 },
132 {
133 code: "'strnig' == typeof foo",
134 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
135 },
136 {
137 code: "if (typeof bar == 'umdefined') {}",
138 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
139 },
140 {
141 code: "if (typeof bar === `umdefined`) {}",
142 parserOptions: { ecmaVersion: 6 },
143 errors: [{ message: "Invalid typeof comparison value.", type: "TemplateLiteral" }]
144 },
145 {
146 code: "typeof foo == 'invalid string'",
147 options: [{ requireStringLiterals: true }],
148 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
149 },
150 {
151 code: "typeof foo == Object",
152 options: [{ requireStringLiterals: true }],
153 errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
154 },
155 {
156 code: "typeof foo === undefined",
157 options: [{ requireStringLiterals: true }],
158 errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
159 },
160 {
161 code: "undefined === typeof foo",
162 options: [{ requireStringLiterals: true }],
163 errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
164 },
165 {
166 code: "undefined == typeof foo",
167 options: [{ requireStringLiterals: true }],
168 errors: [{ message: "Typeof comparisons should be to string literals.", type: "Identifier" }]
169 },
170 {
171 code: "typeof foo === `undefined${foo}`",
172 options: [{ requireStringLiterals: true }],
173 parserOptions: { ecmaVersion: 6 },
174 errors: [{ message: "Typeof comparisons should be to string literals.", type: "TemplateLiteral" }]
175 },
176 {
177 code: "typeof foo === `${string}`",
178 options: [{ requireStringLiterals: true }],
179 parserOptions: { ecmaVersion: 6 },
180 errors: [{ message: "Typeof comparisons should be to string literals.", type: "TemplateLiteral" }]
181 },
182
183 // Babel-specific test cases.
184 {
185 code: "typeof foo === 'bgiint'",
186 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
187 },
188 {
189 code: "'bignit' === typeof foo",
190 errors: [{ message: "Invalid typeof comparison value.", type: "Literal" }]
191 },
192 ]
193});