forked from Lilly-Protocol/lily-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailwindcss-class-consistency.js
More file actions
48 lines (45 loc) · 1.36 KB
/
Copy pathtailwindcss-class-consistency.js
File metadata and controls
48 lines (45 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Enforce consistent Tailwind CSS class usage and prevent arbitrary values",
recommended: true,
},
schema: [],
messages: {
arbitraryValue:
"Avoid arbitrary Tailwind values like '{{value}}'. Use design tokens or predefined classes instead.",
},
},
create(context) {
return {
JSXAttribute(node) {
if (node.name.type !== "JSXIdentifier" || node.name.name !== "className") {
return;
}
let value = null;
if (node.value && node.value.type === "Literal" && typeof node.value.value === "string") {
value = node.value.value;
} else if (
node.value &&
node.value.type === "JSXExpressionContainer" &&
node.value.expression.type === "TemplateLiteral"
) {
value = node.value.expression.quasis.map((q) => q.value.raw).join("");
}
if (!value) return;
const classes = value.split(/\s+/);
for (const cls of classes) {
if (/\[.+\]/.test(cls)) {
context.report({
node,
messageId: "arbitraryValue",
data: { value: cls },
});
}
}
},
};
},
};