index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: (pattern) => `Expected custom property name to match pattern "${pattern}"`,
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/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.length, decl);
  50. });
  51. if (check(prop)) return;
  52. complain(0, prop.length, decl);
  53. });
  54. /**
  55. * @param {number} index
  56. * @param {number} length
  57. * @param {import('postcss').Declaration} decl
  58. */
  59. function complain(index, length, decl) {
  60. report({
  61. result,
  62. ruleName,
  63. message: messages.expected(primary),
  64. node: decl,
  65. index,
  66. endIndex: index + length,
  67. });
  68. }
  69. };
  70. };
  71. rule.ruleName = ruleName;
  72. rule.messages = messages;
  73. rule.meta = meta;
  74. module.exports = rule;