|
| 1 | +import { |
| 2 | + isMethodCall, |
| 3 | + isMemberExpression, |
| 4 | + isStringLiteral, |
| 5 | + isCallExpression, |
| 6 | + isExpressionStatement, |
| 7 | +} from './ast/index.js'; |
| 8 | +import { |
| 9 | + replaceMemberExpressionProperty, |
| 10 | + fixSpaceAroundKeyword, |
| 11 | +} from './fix/index.js'; |
| 12 | +import { |
| 13 | + isSameReference, |
| 14 | + isParenthesized, |
| 15 | + getParenthesizedText, |
| 16 | + shouldAddParenthesesToUnaryExpressionArgument, |
| 17 | + needsSemicolon, |
| 18 | +} from './utils/index.js'; |
| 19 | + |
| 20 | +const MESSAGE_ID_ERROR = 'prefer-classlist-toggle/error'; |
| 21 | +const MESSAGE_ID_SUGGESTION = 'prefer-classlist-toggle/suggestion'; |
| 22 | +const messages = { |
| 23 | + [MESSAGE_ID_ERROR]: 'Prefer using `Element#classList.toggle()` to toggle class names.', |
| 24 | + [MESSAGE_ID_SUGGESTION]: 'Replace with `Element#classList.toggle()`.', |
| 25 | +}; |
| 26 | + |
| 27 | +const isClassList = node => isMemberExpression(node, { |
| 28 | + property: 'classList', |
| 29 | + computed: false, |
| 30 | +}); |
| 31 | + |
| 32 | +const getProblem = (valueNode, fix, reportNode) => { |
| 33 | + const problem = { |
| 34 | + node: reportNode ?? valueNode, |
| 35 | + messageId: MESSAGE_ID_ERROR, |
| 36 | + }; |
| 37 | + |
| 38 | + const shouldUseSuggestion = valueNode.type === 'IfStatement' |
| 39 | + ? false |
| 40 | + : !(isExpressionStatement(valueNode) || isExpressionStatement(valueNode.parent)); |
| 41 | + |
| 42 | + if (shouldUseSuggestion) { |
| 43 | + problem.suggest = [ |
| 44 | + { |
| 45 | + messageId: MESSAGE_ID_SUGGESTION, |
| 46 | + fix, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + } else { |
| 50 | + problem.fix = fix; |
| 51 | + } |
| 52 | + |
| 53 | + return problem; |
| 54 | +}; |
| 55 | + |
| 56 | +const getConditionText = (node, sourceCode, isNegative) => { |
| 57 | + let text = getParenthesizedText(node, sourceCode); |
| 58 | + |
| 59 | + if (isNegative) { |
| 60 | + if ( |
| 61 | + !isParenthesized(node, sourceCode) |
| 62 | + && shouldAddParenthesesToUnaryExpressionArgument(node, '!') |
| 63 | + ) { |
| 64 | + text = `(${text})`; |
| 65 | + } |
| 66 | + |
| 67 | + text = `!${text}`; |
| 68 | + return text; |
| 69 | + } |
| 70 | + |
| 71 | + if ( |
| 72 | + !isParenthesized(node, sourceCode) |
| 73 | + && node.type === 'SequenceExpression' |
| 74 | + ) { |
| 75 | + text = `(${text})`; |
| 76 | + } |
| 77 | + |
| 78 | + return text; |
| 79 | +}; |
| 80 | + |
| 81 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 82 | +const create = context => { |
| 83 | + const {sourceCode} = context; |
| 84 | + |
| 85 | + /* |
| 86 | + ```js |
| 87 | + if (condition) { |
| 88 | + element.classList.add('className'); |
| 89 | + } else { |
| 90 | + element.classList.remove('className'); |
| 91 | + } |
| 92 | + ``` |
| 93 | +
|
| 94 | + ```js |
| 95 | + condition |
| 96 | + ? element.classList.add('className'); |
| 97 | + : element.classList.remove('className'); |
| 98 | + ``` |
| 99 | + */ |
| 100 | + context.on(['IfStatement', 'ConditionalExpression'], node => { |
| 101 | + const clauses = [node.consequent, node.alternate] |
| 102 | + .map(node => { |
| 103 | + if (!node) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (node.type === 'BlockStatement' && node.body.length === 1) { |
| 108 | + node = node.body[0]; |
| 109 | + } |
| 110 | + |
| 111 | + if (node.type === 'ExpressionStatement') { |
| 112 | + node = node.expression; |
| 113 | + } |
| 114 | + |
| 115 | + if (node.type === 'ChainExpression') { |
| 116 | + node = node.expression; |
| 117 | + } |
| 118 | + |
| 119 | + return node; |
| 120 | + }); |
| 121 | + |
| 122 | + // `element.classList.add('className');` |
| 123 | + // `element.classList.remove('className');` |
| 124 | + if (!clauses.every(node => |
| 125 | + isMethodCall(node, { |
| 126 | + methods: ['add', 'remove'], |
| 127 | + argumentsLength: 1, |
| 128 | + optionalCall: false, |
| 129 | + optionalMember: false, |
| 130 | + }) |
| 131 | + && isClassList(node.callee.object), |
| 132 | + )) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + const [consequent, alternate] = clauses; |
| 137 | + if ( |
| 138 | + (consequent.callee.property.name === alternate.callee.property.name) |
| 139 | + || !isSameReference(consequent.callee.object, alternate.callee.object) |
| 140 | + || !isSameReference(consequent.arguments[0], alternate.arguments[0]) |
| 141 | + ) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 146 | + function * fix(fixer) { |
| 147 | + const isOptional = consequent.callee.object.optional || alternate.callee.object.optional; |
| 148 | + const elementText = getParenthesizedText(consequent.callee.object.object, sourceCode); |
| 149 | + const classNameText = getParenthesizedText(consequent.arguments[0], sourceCode); |
| 150 | + const isExpression = node.type === 'ConditionalExpression'; |
| 151 | + const isNegative = consequent.callee.property.name === 'remove'; |
| 152 | + const conditionText = getConditionText(node.test, sourceCode, isNegative); |
| 153 | + |
| 154 | + let text = `${elementText}${isOptional ? '?' : ''}.classList.toggle(${classNameText}, ${conditionText})`; |
| 155 | + |
| 156 | + if (!isExpression) { |
| 157 | + text = `${text};`; |
| 158 | + } |
| 159 | + |
| 160 | + if (needsSemicolon(sourceCode.getTokenBefore(node), sourceCode, text)) { |
| 161 | + text = `;${text}`; |
| 162 | + } |
| 163 | + |
| 164 | + yield fixer.replaceText(node, text); |
| 165 | + |
| 166 | + if (isExpression) { |
| 167 | + yield * fixSpaceAroundKeyword(fixer, node, sourceCode); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return getProblem(node, fix); |
| 172 | + }); |
| 173 | + |
| 174 | + // `element.classList[condition ? 'add' : 'remove']('className')` |
| 175 | + context.on('ConditionalExpression', conditionalExpression => { |
| 176 | + const clauses = [conditionalExpression.consequent, conditionalExpression.alternate]; |
| 177 | + |
| 178 | + if (!( |
| 179 | + clauses.every(node => isStringLiteral(node) && (node.value === 'add' || node.value === 'remove')) |
| 180 | + && clauses[0].value !== clauses[1].value |
| 181 | + && conditionalExpression.parent.type === 'MemberExpression' |
| 182 | + && conditionalExpression.parent.computed |
| 183 | + && !conditionalExpression.parent.optional |
| 184 | + && conditionalExpression.parent.property === conditionalExpression |
| 185 | + && isClassList(conditionalExpression.parent.object) |
| 186 | + && isCallExpression(conditionalExpression.parent.parent, {optional: false, argumentsLength: 1}) |
| 187 | + && conditionalExpression.parent.parent.callee === conditionalExpression.parent |
| 188 | + )) { |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + const classListMethod = conditionalExpression.parent; |
| 193 | + const callExpression = classListMethod.parent; |
| 194 | + |
| 195 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 196 | + function * fix(fixer) { |
| 197 | + const isNegative = conditionalExpression.consequent.value === 'remove'; |
| 198 | + const conditionText = getConditionText(conditionalExpression.test, sourceCode, isNegative); |
| 199 | + |
| 200 | + yield fixer.insertTextAfter(callExpression.arguments[0], `, ${conditionText}`); |
| 201 | + yield replaceMemberExpressionProperty(fixer, classListMethod, sourceCode, '.toggle'); |
| 202 | + } |
| 203 | + |
| 204 | + return getProblem(callExpression, fix, conditionalExpression); |
| 205 | + }); |
| 206 | +}; |
| 207 | + |
| 208 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 209 | +const config = { |
| 210 | + create, |
| 211 | + meta: { |
| 212 | + type: 'suggestion', |
| 213 | + docs: { |
| 214 | + description: 'Prefer using `Element#classList.toggle()` to toggle class names.', |
| 215 | + recommended: true, |
| 216 | + }, |
| 217 | + fixable: 'code', |
| 218 | + hasSuggestions: true, |
| 219 | + messages, |
| 220 | + }, |
| 221 | +}; |
| 222 | + |
| 223 | +export default config; |
0 commit comments