@terrazzo/parser
Version:
Parser/validator for the Design Tokens Community Group (DTCG) standard.
65 lines • 2.33 kB
JavaScript
import { getObjMember, getObjMembers } from '@terrazzo/json-schema-tools';
import { docsLink } from '../lib/docs.js';
export const VALID_FONT_FAMILY = 'core/valid-font-family';
const ERROR = 'ERROR';
const rule = {
meta: {
messages: {
[ERROR]: 'Must be a string, or array of strings.',
},
docs: {
description: 'Require fontFamily tokens to follow the format.',
url: docsLink(VALID_FONT_FAMILY),
},
},
defaultOptions: {},
create({ tokens, report }) {
for (const t of Object.values(tokens)) {
if (t.aliasOf || !t.originalValue) {
continue;
}
switch (t.$type) {
case 'fontFamily': {
validateFontFamily(t.originalValue.$value, {
node: getObjMember(t.source.node, '$value'),
filename: t.source.filename,
report,
});
break;
}
case 'typography': {
if (typeof t.originalValue.$value === 'object' && t.originalValue.$value.fontFamily) {
if (t.partialAliasOf?.fontFamily) {
continue;
}
const $value = getObjMember(t.source.node, '$value');
const properties = getObjMembers($value);
validateFontFamily(t.originalValue.$value.fontFamily, {
node: properties.fontFamily,
filename: t.source.filename,
report,
});
}
break;
}
}
}
},
};
function validateFontFamily(value, { node, filename, report, }) {
if (typeof value === 'string') {
if (!value) {
report({ filename, messageId: ERROR, node });
}
}
else if (Array.isArray(value)) {
if (!value.every((element) => element && typeof element === 'string')) {
report({ filename, messageId: ERROR, node });
}
}
else {
report({ filename, messageId: ERROR, node });
}
}
export default rule;
//# sourceMappingURL=valid-font-family.js.map