|
| 1 | +//----------------------------------------------------------------------------- |
| 2 | +// Helpers |
| 3 | +//----------------------------------------------------------------------------- |
| 4 | + |
| 5 | +const propertiesReplacements = new Map([ |
| 6 | + ["bottom", "inset-block-end"], |
| 7 | + ["border-bottom", "border-block-end"], |
| 8 | + ["border-bottom-color", "border-block-end-color"], |
| 9 | + ["border-bottom-left-radius", "border-end-start-radius"], |
| 10 | + ["border-bottom-right-radius", "border-end-end-radius"], |
| 11 | + ["border-bottom-style", "border-block-end-style"], |
| 12 | + ["border-bottom-width", "border-block-end-width"], |
| 13 | + ["border-left", "border-inline-start"], |
| 14 | + ["border-left-color", "border-inline-start-color"], |
| 15 | + ["border-left-style", "border-inline-start-style"], |
| 16 | + ["border-left-width", "border-inline-start-width"], |
| 17 | + ["border-right", "border-inline-end"], |
| 18 | + ["border-right-color", "border-inline-end-color"], |
| 19 | + ["border-right-style", "border-inline-end-style"], |
| 20 | + ["border-right-width", "border-inline-end-width"], |
| 21 | + ["border-top", "border-block-start"], |
| 22 | + ["border-top-color", "border-block-start-color"], |
| 23 | + ["border-top-left-radius", "border-start-start-radius"], |
| 24 | + ["border-top-right-radius", "border-start-end-radius"], |
| 25 | + ["border-top-style", "border-block-start-style"], |
| 26 | + ["border-top-width", "border-block-start-width"], |
| 27 | + ["contain-intrinsic-height", "contain-intrinsic-block-size"], |
| 28 | + ["contain-intrinsic-width", "contain-intrinsic-inline-size"], |
| 29 | + ["height", "block-size"], |
| 30 | + ["left", "inset-inline-start"], |
| 31 | + ["margin-bottom", "margin-block-end"], |
| 32 | + ["margin-left", "margin-inline-start"], |
| 33 | + ["margin-right", "margin-inline-end"], |
| 34 | + ["margin-top", "margin-block-start"], |
| 35 | + ["max-height", "max-block-size"], |
| 36 | + ["max-width", "max-inline-size"], |
| 37 | + ["min-height", "min-block-size"], |
| 38 | + ["min-width", "min-inline-size"], |
| 39 | + ["overflow-x", "overflow-inline"], |
| 40 | + ["overflow-y", "overflow-block"], |
| 41 | + ["overscroll-behavior-x", "overscroll-behavior-inline"], |
| 42 | + ["overscroll-behavior-y", "overscroll-behavior-block"], |
| 43 | + ["padding-bottom", "padding-block-end"], |
| 44 | + ["padding-left", "padding-inline-start"], |
| 45 | + ["padding-right", "padding-inline-end"], |
| 46 | + ["padding-top", "padding-block-start"], |
| 47 | + ["right", "inset-inline-end"], |
| 48 | + ["scroll-margin-bottom", "scroll-margin-block-end"], |
| 49 | + ["scroll-margin-left", "scroll-margin-inline-start"], |
| 50 | + ["scroll-margin-right", "scroll-margin-inline-end"], |
| 51 | + ["scroll-margin-top", "scroll-margin-block-start"], |
| 52 | + ["scroll-padding-bottom", "scroll-padding-block-end"], |
| 53 | + ["scroll-padding-left", "scroll-padding-inline-start"], |
| 54 | + ["scroll-padding-right", "scroll-padding-inline-end"], |
| 55 | + ["scroll-padding-top", "scroll-padding-block-start"], |
| 56 | + ["top", "inset-block-start"], |
| 57 | + ["width", "inline-size"], |
| 58 | +]); |
| 59 | + |
| 60 | +const propertyValuesReplacements = new Map([ |
| 61 | + [ |
| 62 | + "text-align", |
| 63 | + { |
| 64 | + left: "start", |
| 65 | + right: "end", |
| 66 | + }, |
| 67 | + ], |
| 68 | + [ |
| 69 | + "resize", |
| 70 | + { |
| 71 | + horizontal: "inline", |
| 72 | + vertical: "block", |
| 73 | + }, |
| 74 | + ], |
| 75 | + [ |
| 76 | + "caption-side", |
| 77 | + { |
| 78 | + left: "inline-start", |
| 79 | + right: "inline-end", |
| 80 | + }, |
| 81 | + ], |
| 82 | + [ |
| 83 | + "box-orient", |
| 84 | + { |
| 85 | + horizontal: "inline-axis", |
| 86 | + vertical: "block-axis", |
| 87 | + }, |
| 88 | + ], |
| 89 | + [ |
| 90 | + "float", |
| 91 | + { |
| 92 | + left: "inline-start", |
| 93 | + right: "inline-end", |
| 94 | + }, |
| 95 | + ], |
| 96 | + [ |
| 97 | + "clear", |
| 98 | + { |
| 99 | + left: "inline-start", |
| 100 | + right: "inline-end", |
| 101 | + }, |
| 102 | + ], |
| 103 | +]); |
| 104 | + |
| 105 | +const unitReplacements = new Map([ |
| 106 | + ["cqh", "cqb"], |
| 107 | + ["cqw", "cqi"], |
| 108 | + ["dvh", "dvb"], |
| 109 | + ["dvw", "dvi"], |
| 110 | + ["lvh", "lvb"], |
| 111 | + ["lvw", "lvi"], |
| 112 | + ["svh", "svb"], |
| 113 | + ["svw", "svi"], |
| 114 | + ["vh", "vb"], |
| 115 | + ["vw", "vi"], |
| 116 | +]); |
| 117 | + |
| 118 | +//----------------------------------------------------------------------------- |
| 119 | +// Rule Definition |
| 120 | +//----------------------------------------------------------------------------- |
| 121 | +export default { |
| 122 | + meta: { |
| 123 | + type: /** @type {const} */ ("problem"), |
| 124 | + |
| 125 | + fixable: /** @type {const} */ ("code"), |
| 126 | + |
| 127 | + docs: { |
| 128 | + description: "Enforce the use of logical properties", |
| 129 | + url: "https://github.com/eslint/css/blob/main/docs/rules/prefer-logical-properties.md", |
| 130 | + }, |
| 131 | + |
| 132 | + schema: [ |
| 133 | + { |
| 134 | + type: "object", |
| 135 | + properties: { |
| 136 | + allowProperties: { |
| 137 | + type: "array", |
| 138 | + items: { |
| 139 | + type: "string", |
| 140 | + }, |
| 141 | + }, |
| 142 | + allowUnits: { |
| 143 | + type: "array", |
| 144 | + items: { |
| 145 | + type: "string", |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + additionalProperties: false, |
| 150 | + }, |
| 151 | + ], |
| 152 | + |
| 153 | + defaultOptions: [ |
| 154 | + { |
| 155 | + allowProperties: [], |
| 156 | + allowUnits: [], |
| 157 | + }, |
| 158 | + ], |
| 159 | + |
| 160 | + messages: { |
| 161 | + notLogicalProperty: |
| 162 | + "Expected logical property '{{replacement}}' instead of '{{property}}'.", |
| 163 | + notLogicalValue: |
| 164 | + "Expected logical value '{{replacement}}' instead of '{{value}}'.", |
| 165 | + notLogicalUnit: |
| 166 | + "Expected logical unit '{{replacement}}' instead of '{{unit}}'.", |
| 167 | + }, |
| 168 | + }, |
| 169 | + |
| 170 | + create(context) { |
| 171 | + return { |
| 172 | + Declaration(node) { |
| 173 | + const parent = context.sourceCode.getParent(node); |
| 174 | + if (parent.type === "SupportsDeclaration") { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + const allowProperties = context.options[0].allowProperties; |
| 179 | + if ( |
| 180 | + propertiesReplacements.get(node.property) && |
| 181 | + !allowProperties.includes(node.property) |
| 182 | + ) { |
| 183 | + context.report({ |
| 184 | + loc: node.loc, |
| 185 | + messageId: "notLogicalProperty", |
| 186 | + data: { |
| 187 | + property: node.property, |
| 188 | + replacement: propertiesReplacements.get( |
| 189 | + node.property, |
| 190 | + ), |
| 191 | + }, |
| 192 | + }); |
| 193 | + } |
| 194 | + |
| 195 | + if ( |
| 196 | + propertyValuesReplacements.get(node.property) && |
| 197 | + node.value.children[0].type === "Identifier" |
| 198 | + ) { |
| 199 | + const nodeValue = node.value.children[0].name; |
| 200 | + if ( |
| 201 | + propertyValuesReplacements.get(node.property)[nodeValue] |
| 202 | + ) { |
| 203 | + const replacement = propertyValuesReplacements.get( |
| 204 | + node.property, |
| 205 | + )[nodeValue]; |
| 206 | + if (replacement) { |
| 207 | + context.report({ |
| 208 | + loc: node.value.children[0].loc, |
| 209 | + messageId: "notLogicalValue", |
| 210 | + data: { |
| 211 | + value: nodeValue, |
| 212 | + replacement, |
| 213 | + }, |
| 214 | + }); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + }, |
| 219 | + Dimension(node) { |
| 220 | + const allowUnits = context.options[0].allowUnits; |
| 221 | + if ( |
| 222 | + unitReplacements.get(node.unit) && |
| 223 | + !allowUnits.includes(node.unit) |
| 224 | + ) { |
| 225 | + context.report({ |
| 226 | + loc: node.loc, |
| 227 | + messageId: "notLogicalUnit", |
| 228 | + data: { |
| 229 | + unit: node.unit, |
| 230 | + replacement: unitReplacements.get(node.unit), |
| 231 | + }, |
| 232 | + }); |
| 233 | + } |
| 234 | + }, |
| 235 | + }; |
| 236 | + }, |
| 237 | +}; |
0 commit comments