|
| 1 | +/** |
| 2 | + * @typedef { import("@html-eslint/types").Tag } Tag |
| 3 | + * @typedef { import("@html-eslint/types").StyleTag } StyleTag |
| 4 | + * @typedef { import("@html-eslint/types").ScriptTag } ScriptTag |
| 5 | + * @typedef { import("@html-eslint/types").AttributeValue } AttributeValue |
| 6 | + * @typedef { import("../types").RuleModule<[]> } RuleModule |
| 7 | + * @typedef {Object} ClassInfo |
| 8 | + * @property {string} name |
| 9 | + * @property {import("@html-eslint/types").AnyNode['loc']} loc |
| 10 | + * @property {import("@html-eslint/types").AnyNode['range']} range |
| 11 | + */ |
| 12 | + |
| 13 | +const { NodeTypes } = require("es-html-parser"); |
| 14 | +const { RULE_CATEGORY } = require("../constants"); |
| 15 | +const { createVisitors } = require("./utils/visitors"); |
| 16 | + |
| 17 | +const MESSAGE_IDS = { |
| 18 | + DUPLICATE_CLASS: "duplicateClass", |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * @type {RuleModule} |
| 23 | + */ |
| 24 | +module.exports = { |
| 25 | + meta: { |
| 26 | + type: "code", |
| 27 | + docs: { |
| 28 | + description: "Disallow to use duplicate class", |
| 29 | + category: RULE_CATEGORY.BEST_PRACTICE, |
| 30 | + recommended: false, |
| 31 | + }, |
| 32 | + fixable: "code", |
| 33 | + schema: [], |
| 34 | + messages: { |
| 35 | + [MESSAGE_IDS.DUPLICATE_CLASS]: "The class '{{class}}' is duplicated.", |
| 36 | + }, |
| 37 | + }, |
| 38 | + |
| 39 | + create(context) { |
| 40 | + /** |
| 41 | + * @param {AttributeValue} value |
| 42 | + * @returns {{value: string, pos: number}[]} |
| 43 | + */ |
| 44 | + function splitClassAndSpaces(value) { |
| 45 | + /** |
| 46 | + * @type {{value: string, pos: number}[]} |
| 47 | + */ |
| 48 | + const result = []; |
| 49 | + const regex = /(\s+|\S+)/g; |
| 50 | + /** |
| 51 | + * @type {RegExpExecArray | null} |
| 52 | + */ |
| 53 | + let match = null; |
| 54 | + |
| 55 | + while ((match = regex.exec(value.value)) !== null) { |
| 56 | + result.push({ |
| 57 | + value: match[0], |
| 58 | + pos: match.index, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + return createVisitors(context, { |
| 66 | + Attribute(node) { |
| 67 | + if (node.key.value.toLowerCase() !== "class") { |
| 68 | + return; |
| 69 | + } |
| 70 | + const attributeValue = node.value; |
| 71 | + if ( |
| 72 | + !attributeValue || |
| 73 | + !attributeValue.value || |
| 74 | + attributeValue.parts.some((part) => part.type === NodeTypes.Template) |
| 75 | + ) { |
| 76 | + return; |
| 77 | + } |
| 78 | + const classesAndSpaces = splitClassAndSpaces(attributeValue); |
| 79 | + const classSet = new Set(); |
| 80 | + classesAndSpaces.forEach(({ value, pos }, index) => { |
| 81 | + const className = value.trim(); |
| 82 | + |
| 83 | + if (className.length && classSet.has(className)) { |
| 84 | + context.report({ |
| 85 | + loc: { |
| 86 | + start: { |
| 87 | + line: attributeValue.loc.start.line, |
| 88 | + column: attributeValue.loc.start.column + pos, |
| 89 | + }, |
| 90 | + end: { |
| 91 | + line: attributeValue.loc.start.line, |
| 92 | + column: |
| 93 | + attributeValue.loc.start.column + pos + className.length, |
| 94 | + }, |
| 95 | + }, |
| 96 | + data: { |
| 97 | + class: className, |
| 98 | + }, |
| 99 | + messageId: MESSAGE_IDS.DUPLICATE_CLASS, |
| 100 | + fix(fixer) { |
| 101 | + if (!node.value) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + const before = classesAndSpaces[index - 1]; |
| 105 | + const after = classesAndSpaces[index + 1]; |
| 106 | + const hasSpacesBefore = |
| 107 | + !!before && before.value.trim().length === 0; |
| 108 | + const hasSpacesAfter = |
| 109 | + !!after && after.value.trim().length === 0; |
| 110 | + const hasClassBefore = !!classesAndSpaces[index - 2]; |
| 111 | + const hasClassAfter = !!classesAndSpaces[index + 2]; |
| 112 | + |
| 113 | + const startRange = hasSpacesBefore |
| 114 | + ? attributeValue.range[0] + before.pos |
| 115 | + : attributeValue.range[0] + pos; |
| 116 | + |
| 117 | + const endRange = hasSpacesAfter |
| 118 | + ? attributeValue.range[0] + |
| 119 | + pos + |
| 120 | + value.length + |
| 121 | + after.value.length |
| 122 | + : attributeValue.range[0] + pos + value.length; |
| 123 | + |
| 124 | + return fixer.replaceTextRange( |
| 125 | + [startRange, endRange], |
| 126 | + hasClassBefore && hasClassAfter ? " " : "" |
| 127 | + ); |
| 128 | + }, |
| 129 | + }); |
| 130 | + } else { |
| 131 | + classSet.add(className); |
| 132 | + } |
| 133 | + }); |
| 134 | + }, |
| 135 | + }); |
| 136 | + }, |
| 137 | +}; |
0 commit comments