forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsx-embed-condition.js
72 lines (65 loc) · 2.15 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
64
65
66
67
68
69
70
71
72
/**
* @fileoverview Prevents usage of && condition in JSX Embeds.
* @author Anees Iqbal <i@steelbrain.me>
*/
'use strict';
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const RuleTester = require('eslint').RuleTester;
const version = require('eslint/package.json').version;
const semver = require('semver');
const rule = require('../../../lib/rules/jsx-embed-condition');
const parsers = require('../../helpers/parsers');
const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
};
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
const ruleTester = new RuleTester({parserOptions});
ruleTester.run('jsx-embed-condition', rule, {
valid: [].concat({
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>'
}, semver.satisfies(version, '<= 5') ? [] : [{
code: '<App test>{x ?? <div />}</App>',
parserOptions: {
ecmaVersion: 2020
}
}, {
code: '<App test>{x ?? <div />}</App>',
parser: parsers.TYPESCRIPT_ESLINT
}, {
code: '<App test>{x ?? <div />}</App>',
parser: parsers['@TYPEDCRIPT_ESLINT']
}]),
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'}
]
}]
});