index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const valueParser = require('postcss-value-parser');
  3. const declarationValueIndex = require('../../utils/declarationValueIndex');
  4. const flattenArray = require('../../utils/flattenArray');
  5. const getUnitFromValueNode = require('../../utils/getUnitFromValueNode');
  6. const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
  7. const optionsMatches = require('../../utils/optionsMatches');
  8. const report = require('../../utils/report');
  9. const ruleMessages = require('../../utils/ruleMessages');
  10. const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
  11. const validateOptions = require('../../utils/validateOptions');
  12. const { isString } = require('../../utils/validateTypes');
  13. const vendor = require('../../utils/vendor');
  14. const ruleName = 'declaration-property-unit-allowed-list';
  15. const messages = ruleMessages(ruleName, {
  16. rejected: (property, unit) => `Unexpected unit "${unit}" for property "${property}"`,
  17. });
  18. const meta = {
  19. url: 'https://stylelint.io/user-guide/rules/list/declaration-property-unit-allowed-list',
  20. };
  21. /** @type {import('stylelint').Rule<Record<string, string | string[]>>} */
  22. const rule = (primary, secondaryOptions) => {
  23. return (root, result) => {
  24. const validOptions = validateOptions(
  25. result,
  26. ruleName,
  27. {
  28. actual: primary,
  29. possible: [validateObjectWithArrayProps(isString)],
  30. },
  31. {
  32. actual: secondaryOptions,
  33. possible: {
  34. ignore: ['inside-function'],
  35. },
  36. optional: true,
  37. },
  38. );
  39. if (!validOptions) {
  40. return;
  41. }
  42. root.walkDecls((decl) => {
  43. const prop = decl.prop;
  44. const value = decl.value;
  45. const unprefixedProp = vendor.unprefixed(prop);
  46. const propKey = Object.keys(primary).find((propIdentifier) =>
  47. matchesStringOrRegExp(unprefixedProp, propIdentifier),
  48. );
  49. if (!propKey) {
  50. return;
  51. }
  52. const propList = flattenArray(primary[propKey]);
  53. if (!propList) {
  54. return;
  55. }
  56. valueParser(value).walk((node) => {
  57. // Ignore wrong units within `url` function
  58. if (node.type === 'function') {
  59. if (node.value.toLowerCase() === 'url') {
  60. return false;
  61. }
  62. if (optionsMatches(secondaryOptions, 'ignore', 'inside-function')) {
  63. return false;
  64. }
  65. }
  66. if (node.type === 'string') {
  67. return;
  68. }
  69. const unit = getUnitFromValueNode(node);
  70. if (!unit || (unit && propList.includes(unit.toLowerCase()))) {
  71. return;
  72. }
  73. report({
  74. message: messages.rejected(prop, unit),
  75. node: decl,
  76. index: declarationValueIndex(decl) + node.sourceIndex,
  77. result,
  78. ruleName,
  79. });
  80. });
  81. });
  82. };
  83. };
  84. rule.ruleName = ruleName;
  85. rule.messages = messages;
  86. rule.meta = meta;
  87. module.exports = rule;