index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
  4. const optionsMatches = require('../../utils/optionsMatches');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
  8. const validateOptions = require('../../utils/validateOptions');
  9. const { isString, isRegExp } = require('../../utils/validateTypes');
  10. const vendor = require('../../utils/vendor');
  11. const ruleName = 'declaration-property-value-disallowed-list';
  12. const messages = ruleMessages(ruleName, {
  13. rejected: (property, value) => `Unexpected value "${value}" for property "${property}"`,
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/declaration-property-value-disallowed-list',
  17. };
  18. /** @type {import('stylelint').Rule<Record<string, string | RegExp | Array<string | RegExp>>>} */
  19. const rule = (primary) => {
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: primary,
  23. possible: [validateObjectWithArrayProps(isString, isRegExp)],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. root.walkDecls((decl) => {
  29. const prop = decl.prop;
  30. const value = decl.value;
  31. const unprefixedProp = vendor.unprefixed(prop);
  32. const propKey = Object.keys(primary).find((propIdentifier) =>
  33. matchesStringOrRegExp(unprefixedProp, propIdentifier),
  34. );
  35. if (!propKey) {
  36. return;
  37. }
  38. if (!optionsMatches(primary, propKey, value)) {
  39. return;
  40. }
  41. report({
  42. message: messages.rejected(prop, value),
  43. node: decl,
  44. index: declarationValueIndex(decl),
  45. result,
  46. ruleName,
  47. });
  48. });
  49. };
  50. };
  51. rule.ruleName = ruleName;
  52. rule.messages = messages;
  53. rule.meta = meta;
  54. module.exports = rule;