index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
  3. const isCustomProperty = require('../../utils/isCustomProperty');
  4. const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const ruleName = 'declaration-block-no-duplicate-custom-properties';
  9. const messages = ruleMessages(ruleName, {
  10. rejected: (property) => `Unexpected duplicate "${property}"`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/declaration-block-no-duplicate-custom-properties',
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary) => {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, { actual: primary });
  19. if (!validOptions) {
  20. return;
  21. }
  22. eachDeclarationBlock(root, (eachDecl) => {
  23. const decls = new Set();
  24. eachDecl((decl) => {
  25. const prop = decl.prop;
  26. if (!isStandardSyntaxProperty(prop)) {
  27. return;
  28. }
  29. if (!isCustomProperty(prop)) {
  30. return;
  31. }
  32. const isDuplicate = decls.has(prop);
  33. if (isDuplicate) {
  34. report({
  35. message: messages.rejected(prop),
  36. node: decl,
  37. result,
  38. ruleName,
  39. word: prop,
  40. });
  41. return;
  42. }
  43. decls.add(prop);
  44. });
  45. });
  46. };
  47. };
  48. rule.ruleName = ruleName;
  49. rule.messages = messages;
  50. rule.meta = meta;
  51. module.exports = rule;