UNPKG

3.57 kBJavaScriptView Raw
1/**
2 * @fileoverview Enforce boolean attributes notation in JSX
3 * @author Yannick Croissant
4 */
5
6'use strict';
7
8const docsUrl = require('../util/docsUrl');
9
10// ------------------------------------------------------------------------------
11// Rule Definition
12// ------------------------------------------------------------------------------
13
14const exceptionsSchema = {
15 type: 'array',
16 items: {type: 'string', minLength: 1},
17 uniqueItems: true
18};
19
20const ALWAYS = 'always';
21const NEVER = 'never';
22
23const errorData = new WeakMap();
24function getErrorData(exceptions) {
25 if (!errorData.has(exceptions)) {
26 const exceptionProps = Array.from(exceptions, (name) => `\`${name}\``).join(', ');
27 const exceptionsMessage = exceptions.size > 0 ? ` for the following props: ${exceptionProps}` : '';
28 errorData.set(exceptions, {exceptionsMessage});
29 }
30 return errorData.get(exceptions);
31}
32
33function isAlways(configuration, exceptions, propName) {
34 const isException = exceptions.has(propName);
35 if (configuration === ALWAYS) {
36 return !isException;
37 }
38 return isException;
39}
40
41function isNever(configuration, exceptions, propName) {
42 const isException = exceptions.has(propName);
43 if (configuration === NEVER) {
44 return !isException;
45 }
46 return isException;
47}
48
49module.exports = {
50 meta: {
51 docs: {
52 description: 'Enforce boolean attributes notation in JSX',
53 category: 'Stylistic Issues',
54 recommended: false,
55 url: docsUrl('jsx-boolean-value')
56 },
57 fixable: 'code',
58
59 schema: {
60 anyOf: [{
61 type: 'array',
62 items: [{enum: [ALWAYS, NEVER]}],
63 additionalItems: false
64 }, {
65 type: 'array',
66 items: [{
67 enum: [ALWAYS]
68 }, {
69 type: 'object',
70 additionalProperties: false,
71 properties: {
72 [NEVER]: exceptionsSchema
73 }
74 }],
75 additionalItems: false
76 }, {
77 type: 'array',
78 items: [{
79 enum: [NEVER]
80 }, {
81 type: 'object',
82 additionalProperties: false,
83 properties: {
84 [ALWAYS]: exceptionsSchema
85 }
86 }],
87 additionalItems: false
88 }]
89 }
90 },
91
92 create(context) {
93 const configuration = context.options[0] || NEVER;
94 const configObject = context.options[1] || {};
95 const exceptions = new Set((configuration === ALWAYS ? configObject[NEVER] : configObject[ALWAYS]) || []);
96
97 const NEVER_MESSAGE = 'Value must be omitted for boolean attributes{{exceptionsMessage}}';
98 const ALWAYS_MESSAGE = 'Value must be set for boolean attributes{{exceptionsMessage}}';
99
100 return {
101 JSXAttribute(node) {
102 const propName = node.name && node.name.name;
103 const value = node.value;
104
105 if (isAlways(configuration, exceptions, propName) && value === null) {
106 const data = getErrorData(exceptions);
107 context.report({
108 node,
109 message: ALWAYS_MESSAGE,
110 data,
111 fix(fixer) {
112 return fixer.insertTextAfter(node, '={true}');
113 }
114 });
115 }
116 if (isNever(configuration, exceptions, propName) && value && value.type === 'JSXExpressionContainer' && value.expression.value === true) {
117 const data = getErrorData(exceptions);
118 context.report({
119 node,
120 message: NEVER_MESSAGE,
121 data,
122 fix(fixer) {
123 return fixer.removeRange([node.name.range[1], value.range[1]]);
124 }
125 });
126 }
127 }
128 };
129 }
130};