index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, sourceIndex } = node;
  47. const firstNode = nodes[0];
  48. if (!firstNode || check(firstNode.value)) return;
  49. complain(declarationValueIndex(decl) + sourceIndex, decl);
  50. });
  51. if (check(prop)) return;
  52. complain(0, decl);
  53. });
  54. /**
  55. * @param {number} index
  56. * @param {import('postcss').Declaration} decl
  57. */
  58. function complain(index, decl) {
  59. report({
  60. result,
  61. ruleName,
  62. message: messages.expected(primary),
  63. node: decl,
  64. index,
  65. });
  66. }
  67. };
  68. };
  69. rule.ruleName = ruleName;
  70. rule.messages = messages;
  71. rule.meta = meta;
  72. module.exports = rule;