Skip to content

Commit 9bc0386

Browse files
kresimir-cokobryceosterhaus
authored andcommitted
feat(eslint-plugin): test the no-conditional-object-keys rule
1 parent 36c91c5 commit 9bc0386

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
const MultiTester = require('../../../../../scripts/MultiTester');
7+
const rule = require('../../../lib/rules/no-conditional-object-keys');
8+
9+
const parserOptions = {
10+
parserOptions: {
11+
ecmaVersion: 6,
12+
sourceType: 'module',
13+
},
14+
};
15+
16+
const ruleTester = new MultiTester(parserOptions);
17+
18+
const message = 'Object.keys({}) always evaluates to truthy';
19+
const negationMessage = '!Object.keys({}) always evaluates to false';
20+
const doubleNegationMessage = '!!Object.keys({}) always evaluates to true';
21+
22+
ruleTester.run('no-conditional-object-keys', rule, {
23+
invalid: [
24+
{
25+
code: `
26+
if (Object.keys({foo: 'bar'})) {}
27+
`,
28+
errors: [
29+
{
30+
message,
31+
type: 'CallExpression',
32+
},
33+
],
34+
},
35+
{
36+
code: `
37+
if (!Object.keys({foo: 'bar'})) {}
38+
`,
39+
errors: [
40+
{
41+
message: negationMessage,
42+
type: 'CallExpression',
43+
},
44+
],
45+
},
46+
{
47+
code: `
48+
if (!!Object.keys({foo: 'bar'})) {}
49+
`,
50+
errors: [
51+
{
52+
message: doubleNegationMessage,
53+
type: 'CallExpression',
54+
},
55+
],
56+
},
57+
{
58+
code: `
59+
!Object.keys({foo: 'bar'})
60+
`,
61+
errors: [
62+
{
63+
message: negationMessage,
64+
type: 'CallExpression',
65+
},
66+
],
67+
},
68+
{
69+
code: `
70+
!!Object.keys({foo: 'bar'})
71+
`,
72+
errors: [
73+
{
74+
message: doubleNegationMessage,
75+
type: 'CallExpression',
76+
},
77+
],
78+
},
79+
{
80+
code: `
81+
Object.keys({foo: 'bar'}) && 'test'
82+
`,
83+
errors: [
84+
{
85+
message,
86+
type: 'CallExpression',
87+
},
88+
],
89+
},
90+
{
91+
code: `
92+
!Object.keys({foo: 'bar'}) && 'test'
93+
`,
94+
errors: [
95+
{
96+
message: negationMessage,
97+
type: 'CallExpression',
98+
},
99+
],
100+
},
101+
{
102+
code: `
103+
!!Object.keys({foo: 'bar'}) && 'test'
104+
`,
105+
errors: [
106+
{
107+
message: doubleNegationMessage,
108+
type: 'CallExpression',
109+
},
110+
],
111+
},
112+
{
113+
code: `
114+
const keysNegated = !Object.keys({foo: 'bar'});
115+
`,
116+
errors: [
117+
{
118+
message: negationMessage,
119+
type: 'CallExpression',
120+
},
121+
],
122+
},
123+
{
124+
code: `
125+
const keysDoubleNegated = !!Object.keys({foo: 'bar'});
126+
`,
127+
errors: [
128+
{
129+
message: doubleNegationMessage,
130+
type: 'CallExpression',
131+
},
132+
],
133+
},
134+
],
135+
valid: [
136+
{
137+
code: `
138+
if (Object.keys({foo: 'bar'}).length) {}
139+
`,
140+
},
141+
{
142+
code: `
143+
if (!Object.keys({foo: 'bar'}).length) {}
144+
`,
145+
},
146+
{
147+
code: `
148+
!Object.keys({foo: 'bar'}).length;
149+
`,
150+
},
151+
{
152+
code: `
153+
!!Object.keys({foo: 'bar'}).length;
154+
`,
155+
},
156+
{
157+
code: `
158+
Object.keys({foo: 'bar'}).length && 'test';
159+
`,
160+
},
161+
{
162+
code: `
163+
!Object.keys({foo: 'bar'}).length && 'test';
164+
`,
165+
},
166+
{
167+
code: `
168+
!!Object.keys({foo: 'bar'}).length && 'test';
169+
`,
170+
},
171+
{
172+
code: `
173+
typeof Object.keys({foo: 'bar'});
174+
`,
175+
},
176+
{
177+
code: `
178+
Object.keys({foo: 'bar'}).find(i => true);
179+
`,
180+
},
181+
{
182+
code: `
183+
if (Object.keys({foo: 'bar'}).find(i => true)) {};
184+
`,
185+
},
186+
{
187+
code: `
188+
const keys = Object.keys({foo: 'bar'});
189+
`,
190+
},
191+
],
192+
});

0 commit comments

Comments
 (0)