|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { AST_NODE_TYPES } from "@typescript-eslint/utils"; |
| 20 | + |
| 21 | +export const remNamespace = "rem"; |
| 22 | + |
| 23 | +/** |
| 24 | + * Check if a value contains rem units |
| 25 | + * @param {string} value |
| 26 | + * @returns {boolean} |
| 27 | + */ |
| 28 | +const containsRem = (value) => /\d+\.?\d*rem/u.test(value); |
| 29 | + |
| 30 | +/** |
| 31 | + * Convert rem value to pixels (1rem = 16px) |
| 32 | + * @param {string} value |
| 33 | + * @returns {string} |
| 34 | + */ |
| 35 | +const convertRemToPixels = (value) => |
| 36 | + value.replaceAll(/(?<temp1>\d+\.?\d*)rem/gu, (_, number) => { |
| 37 | + if (typeof number === "string") { |
| 38 | + const pixels = parseFloat(number) * 16; |
| 39 | + |
| 40 | + return `${pixels}px`; |
| 41 | + } |
| 42 | + |
| 43 | + return value; |
| 44 | + }); |
| 45 | + |
| 46 | +export const remPlugin = { |
| 47 | + rules: { |
| 48 | + "no-rem-in-props": { |
| 49 | + /** @param {import('@typescript-eslint/utils').TSESLint.RuleContext<'noRemInProps', []>} context */ |
| 50 | + create(context) { |
| 51 | + /** @param {import('@typescript-eslint/utils').TSESTree.JSXOpeningElement} node */ |
| 52 | + const checkAttributes = (node) => { |
| 53 | + // Check all attributes for rem values in size, width, height props (but not style) |
| 54 | + node.attributes.forEach((attr) => { |
| 55 | + if (attr.type !== AST_NODE_TYPES.JSXAttribute || !attr.value) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const attrName = attr.name.name; |
| 60 | + |
| 61 | + // Skip style attributes - rem is allowed there |
| 62 | + if (attrName === "style") { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Only check size, width, height attributes |
| 67 | + if (attrName !== "height" && attrName !== "size" && attrName !== "width") { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + let attrValue = undefined; |
| 72 | + |
| 73 | + // Handle different attribute value types |
| 74 | + if (attr.value.type === AST_NODE_TYPES.Literal) { |
| 75 | + attrValue = attr.value.value; |
| 76 | + } else if ( |
| 77 | + attr.value.type === AST_NODE_TYPES.JSXExpressionContainer && |
| 78 | + attr.value.expression.type === AST_NODE_TYPES.Literal |
| 79 | + ) { |
| 80 | + attrValue = attr.value.expression.value; |
| 81 | + } |
| 82 | + |
| 83 | + // Check for rem values |
| 84 | + if (typeof attrValue === "string" && containsRem(attrValue)) { |
| 85 | + const fixedValue = convertRemToPixels(attrValue); |
| 86 | + |
| 87 | + context.report({ |
| 88 | + data: { |
| 89 | + attribute: attrName, |
| 90 | + fixedValue, |
| 91 | + value: attrValue, |
| 92 | + }, |
| 93 | + fix(fixer) { |
| 94 | + // For string literals, replace the entire value |
| 95 | + if (attr.value !== null && attr.value.type === AST_NODE_TYPES.Literal) { |
| 96 | + return fixer.replaceText(attr.value, `{${fixedValue}}`); |
| 97 | + } |
| 98 | + // For JSX expressions with literal values, replace just the literal |
| 99 | + if ( |
| 100 | + attr.value !== null && |
| 101 | + attr.value.type === AST_NODE_TYPES.JSXExpressionContainer && |
| 102 | + attr.value.expression.type === AST_NODE_TYPES.Literal |
| 103 | + ) { |
| 104 | + return fixer.replaceText(attr.value.expression, fixedValue); |
| 105 | + } |
| 106 | + |
| 107 | + // eslint-disable-next-line unicorn/no-null |
| 108 | + return null; |
| 109 | + }, |
| 110 | + messageId: "noRemInProps", |
| 111 | + node: attr, |
| 112 | + }); |
| 113 | + } |
| 114 | + }); |
| 115 | + }; |
| 116 | + |
| 117 | + return { |
| 118 | + /** @param {import('@typescript-eslint/utils').TSESTree.JSXOpeningElement} node */ |
| 119 | + JSXOpeningElement(node) { |
| 120 | + checkAttributes(node); |
| 121 | + }, |
| 122 | + }; |
| 123 | + }, |
| 124 | + meta: { |
| 125 | + docs: { |
| 126 | + category: "Best Practices", |
| 127 | + description: "Disallow rem units in size, width, and height attributes (but allow in style)", |
| 128 | + recommended: "error", |
| 129 | + }, |
| 130 | + fixable: "code", |
| 131 | + messages: { |
| 132 | + noRemInProps: |
| 133 | + "Avoid using rem units in {{attribute}} attribute. Use numeric pixel values instead of '{{value}}'. Auto-fix available: {{fixedValue}}", |
| 134 | + }, |
| 135 | + type: "problem", |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | +}; |
| 140 | + |
| 141 | +/** @type {import("@typescript-eslint/utils/ts-eslint").FlatConfig.Config} */ |
| 142 | +export const remRules = { |
| 143 | + files: ["**/*.tsx"], |
| 144 | + plugins: { |
| 145 | + [remNamespace]: remPlugin, |
| 146 | + }, |
| 147 | + rules: { |
| 148 | + [`${remNamespace}/no-rem-in-props`]: "error", |
| 149 | + }, |
| 150 | +}; |
0 commit comments