themed-custom-property-suffix.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import stylelint from 'stylelint'
  2. const ruleName = 'overleaf/themed-custom-property-suffix'
  3. const messages = stylelint.utils.ruleMessages(ruleName, {
  4. rejected: prop =>
  5. `Custom property "${prop}" must be suffixed with "-themed" (e.g. --bg-secondary-themed), ` +
  6. 'since this file defines theme-dependent variables.',
  7. })
  8. /** @type {import('stylelint').Rule} */
  9. const rule = primary => {
  10. return (root, result) => {
  11. const validOptions = stylelint.utils.validateOptions(result, ruleName, {
  12. actual: primary,
  13. possible: [true, false],
  14. })
  15. if (!validOptions || !primary) {
  16. return
  17. }
  18. // Only the declared property name is checked, never var() usages in the
  19. // value, so referencing non-themed tokens (e.g. var(--bg-dark-primary)) is fine.
  20. root.walkDecls(decl => {
  21. if (!decl.prop.startsWith('--') || decl.prop.endsWith('-themed')) {
  22. return
  23. }
  24. stylelint.utils.report({
  25. result,
  26. ruleName,
  27. node: decl,
  28. word: decl.prop,
  29. message: messages.rejected(decl.prop),
  30. })
  31. })
  32. }
  33. }
  34. rule.ruleName = ruleName
  35. rule.messages = messages
  36. export default stylelint.createPlugin(ruleName, rule)