ruleMessages.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. /**
  3. * Given an object of problem messages, return another
  4. * that provides the same messages postfixed with the rule
  5. * that has been violated.
  6. *
  7. * @template {import('stylelint').RuleMessages} T
  8. * @template {{[K in keyof T]: T[K]}} R
  9. * @param {string} ruleName
  10. * @param {T} messages - Object whose keys are message identifiers
  11. * and values are either message strings or functions that return message strings
  12. * @returns {R} New message object, whose messages will be marked with the rule name
  13. */
  14. function ruleMessages(ruleName, messages) {
  15. /** @typedef {keyof T} K */
  16. const newMessages = /** @type {R} */ ({});
  17. for (const [messageId, messageText] of /** @type {[K, T[K]][]} */ (Object.entries(messages))) {
  18. if (typeof messageText === 'string') {
  19. newMessages[messageId] = /** @type {R[K]} */ (`${messageText} (${ruleName})`);
  20. } else {
  21. newMessages[messageId] = /** @type {R[K]} */ (
  22. (...args) => `${messageText(...args)} (${ruleName})`
  23. );
  24. }
  25. }
  26. return newMessages;
  27. }
  28. module.exports = /** @type {typeof import('stylelint').utils.ruleMessages} */ (ruleMessages);