[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Enforce boolean attributes notation in JSX
|
---|
| 3 | * @author Yannick Croissant
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | 'use strict';
|
---|
| 7 |
|
---|
| 8 | const docsUrl = require('../util/docsUrl');
|
---|
| 9 | const report = require('../util/report');
|
---|
| 10 |
|
---|
| 11 | // ------------------------------------------------------------------------------
|
---|
| 12 | // Rule Definition
|
---|
| 13 | // ------------------------------------------------------------------------------
|
---|
| 14 |
|
---|
| 15 | const exceptionsSchema = {
|
---|
| 16 | type: 'array',
|
---|
| 17 | items: { type: 'string', minLength: 1 },
|
---|
| 18 | uniqueItems: true,
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 | const ALWAYS = 'always';
|
---|
| 22 | const NEVER = 'never';
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * @param {string} configuration
|
---|
| 26 | * @param {Set<string>} exceptions
|
---|
| 27 | * @param {string} propName
|
---|
| 28 | * @returns {boolean} propName
|
---|
| 29 | */
|
---|
| 30 | function isAlways(configuration, exceptions, propName) {
|
---|
| 31 | const isException = exceptions.has(propName);
|
---|
| 32 | if (configuration === ALWAYS) {
|
---|
| 33 | return !isException;
|
---|
| 34 | }
|
---|
| 35 | return isException;
|
---|
| 36 | }
|
---|
| 37 | /**
|
---|
| 38 | * @param {string} configuration
|
---|
| 39 | * @param {Set<string>} exceptions
|
---|
| 40 | * @param {string} propName
|
---|
| 41 | * @returns {boolean} propName
|
---|
| 42 | */
|
---|
| 43 | function isNever(configuration, exceptions, propName) {
|
---|
| 44 | const isException = exceptions.has(propName);
|
---|
| 45 | if (configuration === NEVER) {
|
---|
| 46 | return !isException;
|
---|
| 47 | }
|
---|
| 48 | return isException;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | const messages = {
|
---|
| 52 | omitBoolean: 'Value must be omitted for boolean attribute `{{propName}}`',
|
---|
| 53 | setBoolean: 'Value must be set for boolean attribute `{{propName}}`',
|
---|
| 54 | omitPropAndBoolean: 'Value must be omitted for `false` attribute: `{{propName}}`',
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
| 58 | module.exports = {
|
---|
| 59 | meta: {
|
---|
| 60 | docs: {
|
---|
| 61 | description: 'Enforce boolean attributes notation in JSX',
|
---|
| 62 | category: 'Stylistic Issues',
|
---|
| 63 | recommended: false,
|
---|
| 64 | url: docsUrl('jsx-boolean-value'),
|
---|
| 65 | },
|
---|
| 66 | fixable: 'code',
|
---|
| 67 |
|
---|
| 68 | messages,
|
---|
| 69 |
|
---|
| 70 | schema: {
|
---|
| 71 | anyOf: [{
|
---|
| 72 | type: 'array',
|
---|
| 73 | items: [{ enum: [ALWAYS, NEVER] }],
|
---|
| 74 | additionalItems: false,
|
---|
| 75 | }, {
|
---|
| 76 | type: 'array',
|
---|
| 77 | items: [{
|
---|
| 78 | enum: [ALWAYS],
|
---|
| 79 | }, {
|
---|
| 80 | type: 'object',
|
---|
| 81 | additionalProperties: false,
|
---|
| 82 | properties: {
|
---|
| 83 | [NEVER]: exceptionsSchema,
|
---|
| 84 | assumeUndefinedIsFalse: {
|
---|
| 85 | type: 'boolean',
|
---|
| 86 | },
|
---|
| 87 | },
|
---|
| 88 | }],
|
---|
| 89 | additionalItems: false,
|
---|
| 90 | }, {
|
---|
| 91 | type: 'array',
|
---|
| 92 | items: [{
|
---|
| 93 | enum: [NEVER],
|
---|
| 94 | }, {
|
---|
| 95 | type: 'object',
|
---|
| 96 | additionalProperties: false,
|
---|
| 97 | properties: {
|
---|
| 98 | [ALWAYS]: exceptionsSchema,
|
---|
| 99 | assumeUndefinedIsFalse: {
|
---|
| 100 | type: 'boolean',
|
---|
| 101 | },
|
---|
| 102 | },
|
---|
| 103 | }],
|
---|
| 104 | additionalItems: false,
|
---|
| 105 | }],
|
---|
| 106 | },
|
---|
| 107 | },
|
---|
| 108 |
|
---|
| 109 | create(context) {
|
---|
| 110 | const configuration = context.options[0] || NEVER;
|
---|
| 111 | const configObject = context.options[1] || {};
|
---|
| 112 | const exceptions = new Set((configuration === ALWAYS ? configObject[NEVER] : configObject[ALWAYS]) || []);
|
---|
| 113 |
|
---|
| 114 | return {
|
---|
| 115 | JSXAttribute(node) {
|
---|
| 116 | const propName = node.name && node.name.name;
|
---|
| 117 | const value = node.value;
|
---|
| 118 |
|
---|
| 119 | if (
|
---|
| 120 | isAlways(configuration, exceptions, propName)
|
---|
| 121 | && value === null
|
---|
| 122 | ) {
|
---|
| 123 | const messageId = 'setBoolean';
|
---|
| 124 | const data = { propName };
|
---|
| 125 | report(context, messages[messageId], messageId, {
|
---|
| 126 | node,
|
---|
| 127 | data,
|
---|
| 128 | fix(fixer) {
|
---|
| 129 | return fixer.insertTextAfter(node, '={true}');
|
---|
| 130 | },
|
---|
| 131 | });
|
---|
| 132 | }
|
---|
| 133 | if (
|
---|
| 134 | isNever(configuration, exceptions, propName)
|
---|
| 135 | && value
|
---|
| 136 | && value.type === 'JSXExpressionContainer'
|
---|
| 137 | && value.expression.value === true
|
---|
| 138 | ) {
|
---|
| 139 | const messageId = 'omitBoolean';
|
---|
| 140 | const data = { propName };
|
---|
| 141 | report(context, messages[messageId], messageId, {
|
---|
| 142 | node,
|
---|
| 143 | data,
|
---|
| 144 | fix(fixer) {
|
---|
| 145 | return fixer.removeRange([node.name.range[1], value.range[1]]);
|
---|
| 146 | },
|
---|
| 147 | });
|
---|
| 148 | }
|
---|
| 149 | if (
|
---|
| 150 | isNever(configuration, exceptions, propName)
|
---|
| 151 | && configObject.assumeUndefinedIsFalse
|
---|
| 152 | && value
|
---|
| 153 | && value.type === 'JSXExpressionContainer'
|
---|
| 154 | && value.expression.value === false
|
---|
| 155 | ) {
|
---|
| 156 | const messageId = 'omitPropAndBoolean';
|
---|
| 157 | const data = { propName };
|
---|
| 158 | report(context, messages[messageId], messageId, {
|
---|
| 159 | node,
|
---|
| 160 | data,
|
---|
| 161 | fix(fixer) {
|
---|
| 162 | return fixer.removeRange([node.name.range[0], value.range[1]]);
|
---|
| 163 | },
|
---|
| 164 | });
|
---|
| 165 | }
|
---|
| 166 | },
|
---|
| 167 | };
|
---|
| 168 | },
|
---|
| 169 | };
|
---|