forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsx-no-length-truthiness.js
81 lines (73 loc) · 2.4 KB
/
jsx-no-length-truthiness.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @fileoverview Tests for jsx-no-length-truthiness
* @author Carl Mäsak & David Waller
*/
'use strict';
// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------
const rule = require('../../../lib/rules/jsx-no-length-truthiness');
const RuleTester = require('eslint').RuleTester;
const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
};
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
const ruleTester = new RuleTester({parserOptions});
const expectedError = {
message: 'Don\'t check .length truthiness in JSX, might render 0'
};
ruleTester.run('jsx-no-length-truthiness', rule, {
valid: [
{code: 'arr.length > 0 && <Foo />'},
{code: 'arr.length > 1 && <Foo />'},
{code: 'arr.length && "foobar"'}, // ok since we can't tell if this is a JSX expression at all
{code: 'arr.longth && <Foo />'},
{code: '(arr.length || flag) && <Foo />'},
{code: '<Foo>Number of posts: {arr.length}</Foo>'},
{code: '<Foo>Number of posts: {flag && arr.length}</Foo>'},
{code: '<Foo>You have {arr.length || "no"} posts</Foo>'}
],
invalid: [
{
code: 'arr.length && <Foo />',
errors: [expectedError],
output: 'arr.length > 0 && <Foo />'
},
{
code: 'arr.foo.length && <Foo />',
errors: [expectedError],
output: 'arr.foo.length > 0 && <Foo />'
},
{
code: 'arr && arr.length && <Foo />',
errors: [expectedError],
output: 'arr && arr.length > 0 && <Foo />'
},
{
code: 'arr && arr.length && somethingElse && <Foo />',
errors: [expectedError],
output: 'arr && arr.length > 0 && somethingElse && <Foo />'
},
{
code: 'flag || arr.length && <Foo />',
errors: [expectedError],
output: 'flag || arr.length > 0 && <Foo />'
},
{
code: '<Foo><Bar/>{arr.length && "you have posts!"}</Foo>',
errors: [expectedError],
output: '<Foo><Bar/>{arr.length > 0 && "you have posts!"}</Foo>'
},
{
code: '<Foo><Bar/>{flag && arr.length && "you have posts!"}</Foo>',
errors: [expectedError],
output: '<Foo><Bar/>{flag && arr.length > 0 && "you have posts!"}</Foo>'
}
]
});