UNPKG

@terrazzo/parser

Version:

Parser/validator for the Design Tokens Community Group (DTCG) standard.

97 lines 4.13 kB
import { getObjMember } from '@terrazzo/json-schema-tools'; import { isAlias, STROKE_STYLE_LINE_CAP_VALUES, STROKE_STYLE_OBJECT_REQUIRED_PROPERTIES, STROKE_STYLE_STRING_VALUES, TRANSITION_REQUIRED_PROPERTIES, } from '@terrazzo/token-tools'; import { docsLink } from '../lib/docs.js'; export const VALID_STROKE_STYLE = 'core/valid-stroke-style'; const ERROR_STR = 'ERROR_STR'; const ERROR_OBJ = 'ERROR_OBJ'; const ERROR_LINE_CAP = 'ERROR_LINE_CAP'; const ERROR_INVALID_PROP = 'ERROR_INVALID_PROP'; const rule = { meta: { messages: { [ERROR_STR]: `Value most be one of ${new Intl.ListFormat('en-us', { type: 'disjunction' }).format(STROKE_STYLE_STRING_VALUES)}.`, [ERROR_OBJ]: `Missing required properties: ${new Intl.ListFormat('en-us', { type: 'conjunction' }).format(TRANSITION_REQUIRED_PROPERTIES)}.`, [ERROR_LINE_CAP]: `lineCap must be one of ${new Intl.ListFormat('en-us', { type: 'disjunction' }).format(STROKE_STYLE_LINE_CAP_VALUES)}.`, [ERROR_INVALID_PROP]: 'Unknown property: {{ key }}.', }, docs: { description: 'Require strokeStyle tokens to follow the format.', url: docsLink(VALID_STROKE_STYLE), }, }, defaultOptions: {}, create({ tokens, report }) { for (const t of Object.values(tokens)) { if (t.aliasOf || !t.originalValue) { continue; } switch (t.$type) { case 'strokeStyle': { validateStrokeStyle(t.originalValue.$value, { node: getObjMember(t.source.node, '$value'), filename: t.source.filename, report, }); break; } case 'border': { if (t.originalValue.$value && typeof t.originalValue.$value === 'object') { const $valueNode = getObjMember(t.source.node, '$value'); if (t.originalValue.$value.style) { validateStrokeStyle(t.originalValue.$value.style, { node: getObjMember($valueNode, 'style'), filename: t.source.filename, report, }); } } break; } } } }, }; // Note: we validate sub-properties using other checks like valid-dimension, valid-font-family, etc. // The only thing remaining is to check that all properties exist (since missing properties won’t appear as invalid) function validateStrokeStyle(value, { node, filename, report, }) { if (typeof value === 'string') { if (!isAlias(value) && !STROKE_STYLE_STRING_VALUES.includes(value)) { report({ filename, messageId: ERROR_STR, node }); } } else if (value && typeof value === 'object') { if (!STROKE_STYLE_OBJECT_REQUIRED_PROPERTIES.every((property) => property in value)) { report({ filename, messageId: ERROR_OBJ, node }); } if (!Array.isArray(value.dashArray)) { report({ filename, messageId: ERROR_OBJ, node: getObjMember(node, 'dashArray'), }); } if (!STROKE_STYLE_LINE_CAP_VALUES.includes(value.lineCap)) { report({ filename, messageId: ERROR_OBJ, node: getObjMember(node, 'lineCap'), }); } for (const key of Object.keys(value)) { if (!['dashArray', 'lineCap'].includes(key)) { report({ data: { key: JSON.stringify(key) }, filename, messageId: ERROR_INVALID_PROP, node: getObjMember(node, key), }); } } } else { report({ filename, messageId: ERROR_OBJ, node }); } } export default rule; //# sourceMappingURL=valid-stroke-style.js.map