-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathjsx-boolean-value.js
138 lines (123 loc) · 3.9 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @fileoverview Enforce boolean attributes notation in JSX
* @author Yannick Croissant
*/
'use strict';
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const exceptionsSchema = {
type: 'array',
items: { type: 'string', minLength: 1 },
uniqueItems: true,
};
const ALWAYS = 'always';
const NEVER = 'never';
const errorData = new WeakMap();
function getErrorData(exceptions) {
if (!errorData.has(exceptions)) {
const exceptionProps = Array.from(exceptions, (name) => `\`${name}\``).join(', ');
const exceptionsMessage = exceptions.size > 0 ? ` for the following props: ${exceptionProps}` : '';
errorData.set(exceptions, { exceptionsMessage });
}
return errorData.get(exceptions);
}
function isAlways(configuration, exceptions, propName) {
const isException = exceptions.has(propName);
if (configuration === ALWAYS) {
return !isException;
}
return isException;
}
function isNever(configuration, exceptions, propName) {
const isException = exceptions.has(propName);
if (configuration === NEVER) {
return !isException;
}
return isException;
}
const messages = {
omitBoolean: 'Value must be omitted for boolean attributes{{exceptionsMessage}}',
omitBoolean_noMessage: 'Value must be omitted for boolean attributes',
setBoolean: 'Value must be set for boolean attributes{{exceptionsMessage}}',
setBoolean_noMessage: 'Value must be set for boolean attributes',
};
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce boolean attributes notation in JSX',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('jsx-boolean-value'),
},
fixable: 'code',
messages,
schema: {
anyOf: [{
type: 'array',
items: [{ enum: [ALWAYS, NEVER] }],
additionalItems: false,
}, {
type: 'array',
items: [{
enum: [ALWAYS],
}, {
type: 'object',
additionalProperties: false,
properties: {
[NEVER]: exceptionsSchema,
},
}],
additionalItems: false,
}, {
type: 'array',
items: [{
enum: [NEVER],
}, {
type: 'object',
additionalProperties: false,
properties: {
[ALWAYS]: exceptionsSchema,
},
}],
additionalItems: false,
}],
},
},
create(context) {
const configuration = context.options[0] || NEVER;
const configObject = context.options[1] || {};
const exceptions = new Set((configuration === ALWAYS ? configObject[NEVER] : configObject[ALWAYS]) || []);
return {
JSXAttribute(node) {
const propName = node.name && node.name.name;
const value = node.value;
if (isAlways(configuration, exceptions, propName) && value === null) {
const data = getErrorData(exceptions);
const messageId = data.exceptionsMessage ? 'setBoolean' : 'setBoolean_noMessage';
report(context, messages[messageId], messageId, {
node,
data,
fix(fixer) {
return fixer.insertTextAfter(node, '={true}');
},
});
}
if (isNever(configuration, exceptions, propName) && value && value.type === 'JSXExpressionContainer' && value.expression.value === true) {
const data = getErrorData(exceptions);
const messageId = data.exceptionsMessage ? 'omitBoolean' : 'omitBoolean_noMessage';
report(context, messages[messageId], messageId, {
node,
data,
fix(fixer) {
return fixer.removeRange([node.name.range[1], value.range[1]]);
},
});
}
},
};
},
};