-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
jsx-embed-condition.js
63 lines (56 loc) · 1.78 KB
/
jsx-embed-condition.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
/**
* @fileoverview Prevents usage of && condition in JSX Embeds.
* @author Anees Iqbal <i@steelbrain.me>
*/
'use strict';
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/jsx-embed-condition');
const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
};
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
const ruleTester = new RuleTester({parserOptions});
ruleTester.run('jsx-embed-condition', rule, {
valid: [{
code: '<App>Test</App>'
}, {
code: '<App test>Another</App>'
}, {
code: '<App foo={e => bar(e)}>Hello World</App>'
}, {
code: '<App>{x ? <div></div> : null}</App>'
}, {
code: '<App>{x ? <div>Hello</div> : null}</App>'
}, {
code: '<App>{x ? <div>{y ? <y /> : <z />}</div> : null}</App>'
}, {
code: '<App x={x && y}>{x ? <div>{y ? <y /> : <z />}</div> : null}</App>'
}, {
code: '<App test>{x ?? <div />}</App>',
parserOptions: {
ecmaVersion: 2020
}
}],
invalid: [{
code: '<div>{x && <div />}</div>',
output: '<div>{x && <div />}</div>',
errors: [
{message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead'}
]
}, {
code: '<div>{x ? <div>{y && <div />}</div> : null}</div>',
output: '<div>{x ? <div>{y && <div />}</div> : null}</div>',
errors: [
{message: 'Using && to condition JSX embeds is forbidden. Convert it to a ternary operation instead'}
]
}]
});