|
| 1 | +//----------------------------------------------------------------------------- |
| 2 | +// Helpers |
| 3 | +//----------------------------------------------------------------------------- |
| 4 | + |
| 5 | +const propertiesReplacements = { |
| 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 = { |
| 61 | + "text-align": { |
| 62 | + left: "start", |
| 63 | + right: "end", |
| 64 | + }, |
| 65 | + resize: { |
| 66 | + horizontal: "inline", |
| 67 | + vertical: "block", |
| 68 | + }, |
| 69 | + "caption-side": { |
| 70 | + left: "inline-start", |
| 71 | + right: "inline-end", |
| 72 | + }, |
| 73 | + "box-orient": { |
| 74 | + horizontal: "inline-axis", |
| 75 | + vertical: "block-axis", |
| 76 | + }, |
| 77 | + float: { |
| 78 | + left: "inline-start", |
| 79 | + right: "inline-end", |
| 80 | + }, |
| 81 | + clear: { |
| 82 | + left: "inline-start", |
| 83 | + right: "inline-end", |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +const unitReplacements = { |
| 88 | + cqh: "cqb", |
| 89 | + cqw: "cqi", |
| 90 | + dvh: "dvb", |
| 91 | + dvw: "dvi", |
| 92 | + lvh: "lvb", |
| 93 | + lvw: "lvi", |
| 94 | + svh: "svb", |
| 95 | + svw: "svi", |
| 96 | + vh: "vb", |
| 97 | + vw: "vi", |
| 98 | +}; |
| 99 | + |
| 100 | +//----------------------------------------------------------------------------- |
| 101 | +// Rule Definition |
| 102 | +//----------------------------------------------------------------------------- |
| 103 | +export default { |
| 104 | + meta: { |
| 105 | + type: /** @type {const} */ ("problem"), |
| 106 | + |
| 107 | + fixable: "code", |
| 108 | + |
| 109 | + docs: { |
| 110 | + description: "Require use of layers", |
| 111 | + url: "https://github.com/eslint/css/blob/main/docs/rules/prefer-logical-properties.md", |
| 112 | + }, |
| 113 | + |
| 114 | + messages: { |
| 115 | + notLogicalProperty: |
| 116 | + "Expected logical property '{{replacement}}' instead of '{{property}}'.", |
| 117 | + notLogicalValue: |
| 118 | + "Expected logical value '{{replacement}}' instead of '{{value}}'.", |
| 119 | + notLogicalUnit: |
| 120 | + "Expected logical unit '{{replacement}}' instead of '{{unit}}'.", |
| 121 | + }, |
| 122 | + }, |
| 123 | + |
| 124 | + create(context) { |
| 125 | + return { |
| 126 | + Declaration(node) { |
| 127 | + if (propertiesReplacements[node.property]) { |
| 128 | + context.report({ |
| 129 | + loc: node.loc, |
| 130 | + messageId: "notLogicalProperty", |
| 131 | + data: { |
| 132 | + property: node.property, |
| 133 | + replacement: propertiesReplacements[node.property], |
| 134 | + }, |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + if ( |
| 139 | + propertyValuesReplacements[node.property] && |
| 140 | + node.value.children[0].type === "Identifier" |
| 141 | + ) { |
| 142 | + const nodeValue = node.value.children[0].name; |
| 143 | + if (propertyValuesReplacements[node.property][nodeValue]) { |
| 144 | + const replacement = |
| 145 | + propertyValuesReplacements[node.property][ |
| 146 | + nodeValue |
| 147 | + ]; |
| 148 | + if (replacement) { |
| 149 | + context.report({ |
| 150 | + loc: node.value.children[0].loc, |
| 151 | + messageId: "notLogicalValue", |
| 152 | + data: { |
| 153 | + value: nodeValue, |
| 154 | + replacement, |
| 155 | + }, |
| 156 | + }); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + }, |
| 161 | + Dimension(node) { |
| 162 | + if (unitReplacements[node.unit]) { |
| 163 | + context.report({ |
| 164 | + loc: node.loc, |
| 165 | + messageId: "notLogicalUnit", |
| 166 | + data: { |
| 167 | + unit: node.unit, |
| 168 | + replacement: unitReplacements[node.unit], |
| 169 | + }, |
| 170 | + }); |
| 171 | + } |
| 172 | + }, |
| 173 | + }; |
| 174 | + }, |
| 175 | +}; |
0 commit comments