conversation-design-system
Version:
> 💁♀️ This repository contains an Assistant to help designers adhere to the conventions in the > Conversation Design System. It defines the following rules...
107 lines (106 loc) • 6.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.overrideNaming = void 0;
const sketch_assistant_types_1 = require("@sketch-hq/sketch-assistant-types");
// const IGNORE = ['artboard', 'page', 'symbolMaster']
// type ForeignStyleMap = Map<StyleId, LibraryName>
//Override naming rule
exports.overrideNaming = {
rule: (context) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f;
const { utils } = context;
//Assert options are strings or arrays of strings
function assertOption(value) {
if (typeof value !== 'string') {
throw new Error('Option array element is not a string');
}
}
// Get configuration options named
const fillOverride = utils.getOption('fillOverride');
const imageOverride = utils.getOption('imageOverride');
const iconOverride = utils.getOption('iconOverride');
const textOverride = utils.getOption('textOverride');
assertOption(fillOverride);
assertOption(imageOverride);
assertOption(iconOverride);
assertOption(textOverride);
//Make a list of all icon symobls (following sunco "icon/..." or garden "<size>px-icons/..." format)
var foreignMasters = Array.from(utils.foreignObjects.symbolMaster);
foreignMasters = foreignMasters.filter((master) => {
return new RegExp('(?:/|^)(?:ddpx-)?icon(?!-).+', 'i').test(master.name);
});
//TODO: Exempt overrides that have been toggled off.
const layers = Array.from(utils.objects.anyLayer);
// Iterate all symbol
const masters = Array.from(utils.objects.symbolMaster);
for (const master of masters) {
const overrides = Array.from(master.overrideProperties);
for (const override of overrides) {
//seperate override properties into the id of the layer the override and the type
const overrideDetail = override.overrideName.match(new RegExp('^.+(?=_)|_.+', 'g'));
if (override.canOverride && overrideDetail) {
const layer = layers.find((layer) => layer.do_objectID === overrideDetail[0]);
if (layer) {
switch (overrideDetail[1]) {
case '_image':
//check image format
if (!new RegExp('^↪?' + imageOverride + '\\S.+$').test(layer.name)) {
utils.report(`Image layer ${layer.name} should be prefixed with "${imageOverride}" (followed by no space).`, layer);
}
break;
case '_stringValue':
// check text format
if (!new RegExp('^↪?' + textOverride + '\\S.+$').test(layer.name)) {
utils.report(`Text layer ${layer.name} should be prefixed with "${textOverride}" (followed by no space).`, layer);
}
break;
case '_layerStyle':
//check colors
if ((_b = (_a = layer.style) === null || _a === void 0 ? void 0 : _a.borders) === null || _b === void 0 ? void 0 : _b.length)
continue; //Ignore styles with borders
if ((_d = (_c = layer.style) === null || _c === void 0 ? void 0 : _c.shadows) === null || _d === void 0 ? void 0 : _d.length)
continue; //Ignore styles with shadows
//We also account for styles that provide an image fill, but have the image override turned off
if ((_f = (_e = layer.style) === null || _e === void 0 ? void 0 : _e.fills) === null || _f === void 0 ? void 0 : _f.find((fill) => fill.image)) {
//check image format
if (!new RegExp('^↪?' + imageOverride + '\\S.+$').test(layer.name)) {
utils.report(`Image layer ${layer.name} should be prefixed with "${imageOverride}" (followed by no space).`, layer);
}
}
else if (!new RegExp('^↪?' + fillOverride + '\\S.+$').test(layer.name)) {
//...otherwise we assume it is a color fill
utils.report(`Fill layer ${layer.name} should be prefixed with "${fillOverride}" (followed by no space).`, layer);
}
break;
case '_symbolID':
//check icons
if (layer._class === sketch_assistant_types_1.FileFormat.ClassValue.SymbolInstance &&
foreignMasters.some((master) => {
return master.symbolID === layer.symbolID;
})) {
if (!new RegExp('^↪?' + iconOverride + '\\S.+$').test(layer.name)) {
utils.report(`Icon layer ${layer.name} should be prefixed with "${iconOverride}" (followed by no space).`, layer);
}
}
break;
default:
//do nothing
}
}
}
}
}
}),
name: 'conversation-design-system/name-pattern-overrides',
title: 'Override not named correctly',
description: 'Checks that overrides includes specified prefixes',
};