forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsx-boolean-value.js
69 lines (64 loc) · 2.41 KB
/
jsx-boolean-value.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
/**
* @fileoverview Enforce boolean attributes notation in JSX
* @author Yannick Croissant
*/
'use strict';
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
var rule = require('../../../lib/rules/jsx-boolean-value');
var RuleTester = require('eslint').RuleTester;
var parserOptions = {
ecmaVersion: 8,
sourceType: 'module',
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true
}
};
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
var ruleTester = new RuleTester({parserOptions});
ruleTester.run('jsx-boolean-value', rule, {
valid: [
{code: '<App foo />;', options: ['never']},
{code: '<App foo bar={true} />;', options: ['always', {never: ['foo']}]},
{code: '<App foo />;'},
{code: '<App foo={true} />;', options: ['always']},
{code: '<App foo={true} bar />;', options: ['never', {always: ['foo']}]}
],
invalid: [{
code: '<App foo={true} />;',
output: '<App foo />;',
options: ['never'],
errors: [{message: 'Value must be omitted for boolean attributes'}]
}, {
code: '<App foo={true} bar={true} baz={true} />;',
output: '<App foo bar baz={true} />;',
options: ['always', {never: ['foo', 'bar']}],
errors: [
{message: 'Value must be omitted for boolean attributes for the following props: `foo`, `bar`'},
{message: 'Value must be omitted for boolean attributes for the following props: `foo`, `bar`'}
]
}, {
code: '<App foo={true} />;',
output: '<App foo />;',
errors: [{message: 'Value must be omitted for boolean attributes'}]
}, {
code: '<App foo = {true} />;',
output: '<App foo />;',
errors: [{message: 'Value must be omitted for boolean attributes'}]
}, {
code: '<App foo />;', output: '<App foo={true} />;', options: ['always'],
errors: [{message: 'Value must be set for boolean attributes'}]
}, {
code: '<App foo bar baz />;',
output: '<App foo={true} bar={true} baz />;',
options: ['never', {always: ['foo', 'bar']}],
errors: [
{message: 'Value must be set for boolean attributes for the following props: `foo`, `bar`'},
{message: 'Value must be set for boolean attributes for the following props: `foo`, `bar`'}
]
}]
});