|
| 1 | +/** |
| 2 | + * @fileoverview Rule to prevent invalid named grid areas in CSS grid templates. |
| 3 | + * @author xbinaryx |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Type Definitions |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +/** |
| 11 | + * @import { CSSRuleDefinition } from "../types.js" |
| 12 | + * @typedef {"emptyGridArea" | "unevenGridArea" | "nonRectangularGridArea"} NoInvalidNamedGridAreasMessageIds |
| 13 | + * @typedef {CSSRuleDefinition<{ RuleOptions: [], MessageIds: NoInvalidNamedGridAreasMessageIds }>} NoInvalidNamedGridAreasRuleDefinition |
| 14 | + */ |
| 15 | + |
| 16 | +//----------------------------------------------------------------------------- |
| 17 | +// Helpers |
| 18 | +//----------------------------------------------------------------------------- |
| 19 | + |
| 20 | +/** |
| 21 | + * Regular expression to match null cell tokens (sequences of one or more dots) |
| 22 | + */ |
| 23 | +const nullCellToken = /^\.+$/u; |
| 24 | + |
| 25 | +/** |
| 26 | + * Finds non-rectangular grid areas in a 2D grid |
| 27 | + * @param {string[][]} grid 2D array representing the grid areas |
| 28 | + * @returns {Array<{name: string, row: number}>} Array of errors found |
| 29 | + */ |
| 30 | +function findNonRectangularAreas(grid) { |
| 31 | + const errors = []; |
| 32 | + const reported = new Set(); |
| 33 | + const names = [...new Set(grid.flat())].filter( |
| 34 | + name => !nullCellToken.test(name), |
| 35 | + ); |
| 36 | + |
| 37 | + for (const name of names) { |
| 38 | + const indicesByRow = grid.map(row => { |
| 39 | + const indices = []; |
| 40 | + let idx = row.indexOf(name); |
| 41 | + |
| 42 | + while (idx !== -1) { |
| 43 | + indices.push(idx); |
| 44 | + idx = row.indexOf(name, idx + 1); |
| 45 | + } |
| 46 | + |
| 47 | + return indices; |
| 48 | + }); |
| 49 | + |
| 50 | + for (let i = 0; i < indicesByRow.length; i++) { |
| 51 | + for (let j = i + 1; j < indicesByRow.length; j++) { |
| 52 | + const row1 = indicesByRow[i]; |
| 53 | + const row2 = indicesByRow[j]; |
| 54 | + |
| 55 | + if (row1.length === 0 || row2.length === 0) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if ( |
| 60 | + row1.length !== row2.length || |
| 61 | + !row1.every((val, idx) => val === row2[idx]) |
| 62 | + ) { |
| 63 | + const key = `${name}|${j}`; |
| 64 | + if (!reported.has(key)) { |
| 65 | + errors.push({ name, row: j }); |
| 66 | + reported.add(key); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return errors; |
| 74 | +} |
| 75 | + |
| 76 | +const validProps = new Set(["grid-template-areas", "grid-template", "grid"]); |
| 77 | + |
| 78 | +//----------------------------------------------------------------------------- |
| 79 | +// Rule Definition |
| 80 | +//----------------------------------------------------------------------------- |
| 81 | + |
| 82 | +/** @type {NoInvalidNamedGridAreasRuleDefinition} */ |
| 83 | +export default { |
| 84 | + meta: { |
| 85 | + type: "problem", |
| 86 | + |
| 87 | + docs: { |
| 88 | + description: "Disallow invalid named grid areas", |
| 89 | + recommended: true, |
| 90 | + url: "https://github.com/eslint/css/blob/main/docs/rules/no-invalid-named-grid-areas.md", |
| 91 | + }, |
| 92 | + |
| 93 | + messages: { |
| 94 | + emptyGridArea: "Grid area must contain at least one cell token.", |
| 95 | + unevenGridArea: |
| 96 | + "Grid area strings must have the same number of cell tokens.", |
| 97 | + nonRectangularGridArea: |
| 98 | + "Cell tokens with name '{{name}}' must form a rectangle.", |
| 99 | + }, |
| 100 | + }, |
| 101 | + |
| 102 | + create(context) { |
| 103 | + return { |
| 104 | + Declaration(node) { |
| 105 | + const propName = node.property.toLowerCase(); |
| 106 | + |
| 107 | + if ( |
| 108 | + validProps.has(propName) && |
| 109 | + node.value.type === "Value" && |
| 110 | + node.value.children.length > 0 |
| 111 | + ) { |
| 112 | + const stringNodes = node.value.children.filter( |
| 113 | + child => child.type === "String", |
| 114 | + ); |
| 115 | + |
| 116 | + if (stringNodes.length === 0) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + const grid = []; |
| 121 | + const emptyNodes = []; |
| 122 | + const unevenNodes = []; |
| 123 | + let firstRowLen = null; |
| 124 | + |
| 125 | + for (const stringNode of stringNodes) { |
| 126 | + const trimmedValue = stringNode.value.trim(); |
| 127 | + |
| 128 | + if (trimmedValue === "") { |
| 129 | + emptyNodes.push(stringNode); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + const row = trimmedValue.split(" ").filter(Boolean); |
| 134 | + grid.push(row); |
| 135 | + |
| 136 | + if (firstRowLen === null) { |
| 137 | + firstRowLen = row.length; |
| 138 | + } else if (row.length !== firstRowLen) { |
| 139 | + unevenNodes.push(stringNode); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (emptyNodes.length > 0) { |
| 144 | + emptyNodes.forEach(emptyNode => |
| 145 | + context.report({ |
| 146 | + node: emptyNode, |
| 147 | + messageId: "emptyGridArea", |
| 148 | + }), |
| 149 | + ); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + if (unevenNodes.length > 0) { |
| 154 | + unevenNodes.forEach(unevenNode => |
| 155 | + context.report({ |
| 156 | + node: unevenNode, |
| 157 | + messageId: "unevenGridArea", |
| 158 | + }), |
| 159 | + ); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + const nonRectErrors = findNonRectangularAreas(grid); |
| 164 | + nonRectErrors.forEach(({ name, row }) => { |
| 165 | + const stringNode = stringNodes[row]; |
| 166 | + context.report({ |
| 167 | + node: stringNode, |
| 168 | + messageId: "nonRectangularGridArea", |
| 169 | + data: { |
| 170 | + name, |
| 171 | + }, |
| 172 | + }); |
| 173 | + }); |
| 174 | + } |
| 175 | + }, |
| 176 | + }; |
| 177 | + }, |
| 178 | +}; |
0 commit comments