|
| 1 | +/** |
| 2 | + * Copyright (C) 2023 Green Code Initiative |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +"use strict"; |
| 18 | + |
| 19 | +/** @type {import("eslint").Rule.RuleModule} */ |
| 20 | +module.exports = { |
| 21 | + meta: { |
| 22 | + type: "suggestion", |
| 23 | + docs: { |
| 24 | + description: "Disallow multiple style changes at once.", |
| 25 | + category: "eco-design", |
| 26 | + recommended: "warn", |
| 27 | + }, |
| 28 | + messages: { |
| 29 | + UseClassInstead: |
| 30 | + "There are more than two style assignations for '{{elementName}}'. Use a class instead.", |
| 31 | + }, |
| 32 | + schema: [], |
| 33 | + }, |
| 34 | + create: function (context) { |
| 35 | + function isNodeUseStyleProperty(node) { |
| 36 | + return ( |
| 37 | + node != null && |
| 38 | + node.object != null && |
| 39 | + node.object.property != null && |
| 40 | + node.object.property.name === "style" |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + return { |
| 45 | + AssignmentExpression(node) { |
| 46 | + // Are we checking an assignation on a style property |
| 47 | + if (isNodeUseStyleProperty(node.left)) { |
| 48 | + const domElementName = node.left.object.object.name; |
| 49 | + const currentRangestart = node.left.object.object.range[0]; |
| 50 | + |
| 51 | + /** We get the parent AST to check if there is more than one assignation on |
| 52 | + the style of the same domElement */ |
| 53 | + const currentScopeASTBody = |
| 54 | + context.getScope().block.body.length != null |
| 55 | + ? context.getScope().block.body |
| 56 | + : context.getScope().block.body.body; |
| 57 | + |
| 58 | + const filtered = currentScopeASTBody.filter( |
| 59 | + (e) => |
| 60 | + e.type === "ExpressionStatement" && |
| 61 | + e.expression.type === "AssignmentExpression" && |
| 62 | + isNodeUseStyleProperty(e.expression.left) && |
| 63 | + e.expression.left.object.object.name === domElementName |
| 64 | + ); |
| 65 | + |
| 66 | + // De-duplication, prevents multiple alerts for each line involved |
| 67 | + const isCurrentNodeTheFirstAssignation = |
| 68 | + currentRangestart <= |
| 69 | + filtered[0].expression.left.object.object.range[0]; |
| 70 | + |
| 71 | + if (filtered.length > 1 && isCurrentNodeTheFirstAssignation) { |
| 72 | + context.report({ |
| 73 | + node, |
| 74 | + messageId: "UseClassInstead", |
| 75 | + data: { elementName: domElementName }, |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + }, |
| 80 | + }; |
| 81 | + }, |
| 82 | +}; |
0 commit comments