UNPKG

@sketch-hq/sketch-assistant-utils

Version:

Utility functions and types for Sketch Assistants.

64 lines (63 loc) 2.49 kB
import Ajv from 'ajv'; import { ViolationSeverity, ReservedRuleOptionNames, } from '@sketch-hq/sketch-assistant-types'; import { helpers, buildRuleOptionSchema } from '../rule-option-schemas'; /** * Get rule configuration from an assistant config. */ const getRuleConfig = (config, ruleName) => config.rules[ruleName]; /** * Determine if the rule has been mentioned in a given config. */ const isRuleConfigured = (config, ruleName) => !!getRuleConfig(config, ruleName); /** * Get the value of a specific rule option. */ const getRuleOption = (config, ruleName, optionKey) => { const ruleConfig = getRuleConfig(config, ruleName); return ruleConfig ? (optionKey in ruleConfig ? ruleConfig[optionKey] : null) : null; }; /** * Validate a rule's options in a config object according to the schema defined * on the rule module. */ const isRuleConfigValid = (config, rule) => { if (typeof rule.getOptions === 'undefined') { // If the rule hasn't defined an options schema we can't validate it return true; } const schema = buildRuleOptionSchema(rule.getOptions(helpers)); const ruleConfig = getRuleConfig(config, rule.name); const ajv = new Ajv(); ajv.validate(schema, ruleConfig); return ajv.errors || true; }; /** * Determine if a rule is active. An active rule must both be mentioned in the * config and have its `active` option set to `true`. */ const isRuleActive = (config, ruleName) => { const active = getRuleOption(config, ruleName, ReservedRuleOptionNames.active); return typeof active === 'boolean' ? active : false; }; /** * Determine a rule's severity, falling back to default values if not specified. */ const getRuleSeverity = (config, ruleName) => { const severity = getRuleOption(config, ruleName, ReservedRuleOptionNames.severity); switch (severity) { case ViolationSeverity.info: case ViolationSeverity.warn: case ViolationSeverity.error: return severity; default: return config.defaultSeverity || ViolationSeverity.error; } }; /** * Return the custom title for a rule if its been defined at configuration-time. */ const getRuleTitle = (config, ruleName) => { const ruleTitle = getRuleOption(config, ruleName, ReservedRuleOptionNames.ruleTitle); return typeof ruleTitle === 'string' ? ruleTitle : null; }; export { getRuleConfig, getRuleOption, isRuleConfigured, isRuleActive, getRuleSeverity, isRuleConfigValid, getRuleTitle, };