mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-01 08:54:47 +08:00
49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
import stylelint from "stylelint";
|
|
|
|
const blocked = [
|
|
/--primary(-.*)?/,
|
|
/--secondary(-.*)?/,
|
|
/--tertiary(-.*)?/,
|
|
/--quarternary(-.*)?/,
|
|
];
|
|
|
|
const varRegex = /var\(\s*(--[^,)]+)/g;
|
|
|
|
export default stylelint.createPlugin(
|
|
"discourse/no-core-color-variables",
|
|
(primaryOption) => {
|
|
return (root, result) => {
|
|
if (!primaryOption) {
|
|
return;
|
|
}
|
|
|
|
root.walkDecls((decl) => {
|
|
const value = decl.value;
|
|
if (!value.includes("var(")) {
|
|
return;
|
|
}
|
|
|
|
let match;
|
|
while ((match = varRegex.exec(value)) !== null) {
|
|
const varName = match[1].trim();
|
|
|
|
for (const pattern of blocked) {
|
|
if (pattern.test(varName)) {
|
|
stylelint.utils.report({
|
|
message:
|
|
"Do not use core color variables directly. Use var(--token-*) design tokens instead",
|
|
node: decl,
|
|
result,
|
|
ruleName: "discourse/no-core-color-variables",
|
|
word: varName,
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
varRegex.lastIndex = 0;
|
|
});
|
|
};
|
|
}
|
|
);
|