index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const valueParser = require('postcss-value-parser');
  3. const isCustomProperty = require('../../utils/isCustomProperty');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const declarationValueIndex = require('../../utils/declarationValueIndex');
  8. const { isRegExp, isString } = require('../../utils/validateTypes');
  9. const { isValueFunction } = require('../../utils/typeGuards');
  10. const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
  11. const ruleName = 'custom-property-pattern';
  12. const messages = ruleMessages(ruleName, {
  13. expected: (propName, pattern) => `Expected "${propName}" to match pattern "${pattern}"`,
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/custom-property-pattern',
  17. };
  18. /** @type {import('stylelint').Rule} */
  19. const rule = (primary) => {
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: primary,
  23. possible: [isRegExp, isString],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. const regexpPattern = isString(primary) ? new RegExp(primary) : primary;
  29. /**
  30. * @param {string} property
  31. * @returns {boolean}
  32. */
  33. function check(property) {
  34. return (
  35. !isStandardSyntaxProperty(property) ||
  36. !isCustomProperty(property) ||
  37. regexpPattern.test(property.slice(2))
  38. );
  39. }
  40. root.walkDecls((decl) => {
  41. const { prop, value } = decl;
  42. const parsedValue = valueParser(value);
  43. parsedValue.walk((node) => {
  44. if (!isValueFunction(node)) return;
  45. if (node.value.toLowerCase() !== 'var') return;
  46. const { nodes } = node;
  47. const firstNode = nodes[0];
  48. if (!firstNode || check(firstNode.value)) return;
  49. complain(declarationValueIndex(decl) + firstNode.sourceIndex, firstNode.value, decl);
  50. });
  51. if (check(prop)) return;
  52. complain(0, prop, decl);
  53. });
  54. /**
  55. * @param {number} index
  56. * @param {string} propName
  57. * @param {import('postcss').Declaration} decl
  58. */
  59. function complain(index, propName, decl) {
  60. report({
  61. result,
  62. ruleName,
  63. message: messages.expected,
  64. messageArgs: [propName, primary],
  65. node: decl,
  66. index,
  67. endIndex: index + propName.length,
  68. });
  69. }
  70. };
  71. };
  72. rule.ruleName = ruleName;
  73. rule.messages = messages;
  74. rule.meta = meta;
  75. module.exports = rule;