@terrazzo/parser
Version:
Parser/validator for the Design Tokens Community Group (DTCG) standard.
46 lines • 1.83 kB
JavaScript
import { docsLink } from '../lib/docs.js';
import { cachedLintMatcher } from '../lib/matchers.js';
export const A11Y_MIN_FONT_SIZE = 'a11y/min-font-size';
export const ERROR_TOO_SMALL = 'TOO_SMALL';
const rule = {
meta: {
messages: {
[ERROR_TOO_SMALL]: '{{ id }} font size too small. Expected minimum of {{ min }}',
},
docs: {
description: 'Enforce font sizes are no smaller than the given value.',
url: docsLink(A11Y_MIN_FONT_SIZE),
},
},
defaultOptions: {},
create({ tokens, options, report }) {
if (!options.minSizePx && !options.minSizeRem) {
throw new Error('Must specify at least one of minSizePx or minSizeRem');
}
const shouldIgnore = options.ignore ? cachedLintMatcher.tokenIDMatch(options.ignore) : null;
for (const t of Object.values(tokens)) {
if (shouldIgnore?.(t.id)) {
continue;
}
// skip aliases
if (t.aliasOf) {
continue;
}
if (t.$type === 'typography' && 'fontSize' in t.$value) {
const fontSize = t.$value.fontSize;
if ((fontSize.unit === 'px' && options.minSizePx && fontSize.value < options.minSizePx) ||
(fontSize.unit === 'rem' && options.minSizeRem && fontSize.value < options.minSizeRem)) {
report({
messageId: ERROR_TOO_SMALL,
data: {
id: t.id,
min: options.minSizePx ? `${options.minSizePx}px` : `${options.minSizeRem}rem`,
},
});
}
}
}
},
};
export default rule;
//# sourceMappingURL=a11y-min-font-size.js.map