eslint-plugin-gamut

2.4.32.4.4-alpha.212edd.0
dist/no-raw-z-index.js
+dist/no-raw-z-index.jsNew file
+55
Index: package/dist/no-raw-z-index.js
===================================================================
--- package/dist/no-raw-z-index.js
+++ package/dist/no-raw-z-index.js
@@ -0,0 +1,55 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+const utils_1 = require("@typescript-eslint/utils");
+const createRule_1 = require("./createRule");
+/**
+ * True for a numeric literal, including a negated one like `-1`.
+ */
+const isNumericLiteral = (node) => {
+    if (!node)
+        return false;
+    if (node.type === utils_1.AST_NODE_TYPES.Literal && typeof node.value === 'number') {
+        return true;
+    }
+    return (node.type === utils_1.AST_NODE_TYPES.UnaryExpression &&
+        (node.operator === '-' || node.operator === '+') &&
+        isNumericLiteral(node.argument));
+};
+const isZIndexKey = (key) => (key.type === utils_1.AST_NODE_TYPES.Identifier && key.name === 'zIndex') ||
+    (key.type === utils_1.AST_NODE_TYPES.Literal && key.value === 'zIndex') ||
+    (key.type === utils_1.AST_NODE_TYPES.Literal && key.value === 'z-index');
+exports.default = (0, createRule_1.createRule)({
+    create(context) {
+        return {
+            // Style objects: `{ zIndex: 1 }` / `{ 'z-index': 1 }`
+            Property(node) {
+                if (isZIndexKey(node.key) && isNumericLiteral(node.value)) {
+                    context.report({ messageId: 'noRawZIndex', node: node.value });
+                }
+            },
+            // JSX props: `<Box zIndex={1} />`
+            JSXAttribute(node) {
+                if (node.name.type === utils_1.AST_NODE_TYPES.JSXIdentifier &&
+                    node.name.name === 'zIndex' &&
+                    node.value?.type === utils_1.AST_NODE_TYPES.JSXExpressionContainer &&
+                    isNumericLiteral(node.value.expression)) {
+                    context.report({ messageId: 'noRawZIndex', node: node.value });
+                }
+            },
+        };
+    },
+    defaultOptions: [],
+    meta: {
+        docs: {
+            description: 'Discourage raw numeric z-index values which can lead to z-index stacking issues and encourage usage of semantic tokens from the `zIndices` scale.',
+            recommended: 'error',
+        },
+        messages: {
+            noRawZIndex: 'Semantic tokens from the `zIndices` scale (e.g. `zIndices.modal`) are recommendedinstead of a raw z-index number. For a deliberate in-between value, disable this rule inline with a justifying comment.',
+        },
+        type: 'suggestion',
+        schema: [],
+    },
+    name: 'no-raw-z-index',
+});
+//# sourceMappingURL=no-raw-z-index.js.map
\ No newline at end of file