Source code
Revision control
Copy as Markdown
Other Tools
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
import stylelint from "stylelint";
import valueParser from "postcss-value-parser";
import {
createTokenNamesArray,
createAllowList,
createRawValuesObject,
getLocalCustomProperties,
isValidTokenUsage,
namespace,
} from "../helpers.mjs";
const {
utils: { report, ruleMessages, validateOptions },
} = stylelint;
const ruleName = namespace("use-font-weight-tokens");
const messages = ruleMessages(ruleName, {
rejected: value => `${value} should use a font-weight design token.`,
});
const meta = {
fixable: true,
};
const PROPERTY_NAME = "font-weight";
const tokenCSS = createTokenNamesArray([PROPERTY_NAME]);
const ALLOW_LIST = createAllowList();
const tokenFixMap = createRawValuesObject([PROPERTY_NAME]);
const ruleFunction = primaryOption => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primaryOption,
possible: [true],
});
if (!validOptions) {
return;
}
const cssCustomProperties = getLocalCustomProperties(root);
root.walkDecls(declarations => {
// ignore properties other than font-weight
if (declarations.prop !== PROPERTY_NAME) {
return;
}
if (
isValidTokenUsage(
declarations.value,
tokenCSS,
cssCustomProperties,
ALLOW_LIST
)
) {
return;
}
report({
message: messages.rejected(declarations.value),
node: declarations,
result,
ruleName,
fix: () => {
const val = valueParser(declarations.value);
let hasFixes = false;
val.walk(node => {
if (node.type === "word") {
const token = tokenFixMap[node.value.trim()];
if (token) {
hasFixes = true;
node.value = token;
}
}
});
if (hasFixes) {
declarations.value = val.toString();
}
},
});
});
};
};
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = meta;
export default ruleFunction;