index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. const valueParser = require('postcss-value-parser');
  3. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  4. const declarationValueIndex = require('../../utils/declarationValueIndex');
  5. const getUnitFromValueNode = require('../../utils/getUnitFromValueNode');
  6. const optionsMatches = require('../../utils/optionsMatches');
  7. const report = require('../../utils/report');
  8. const ruleMessages = require('../../utils/ruleMessages');
  9. const validateOptions = require('../../utils/validateOptions');
  10. const { isNumber, isRegExp, isString } = require('../../utils/validateTypes');
  11. const ruleName = 'number-max-precision';
  12. const messages = ruleMessages(ruleName, {
  13. expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/number-max-precision',
  17. };
  18. /** @type {import('stylelint').Rule} */
  19. const rule = (primary, secondaryOptions) => {
  20. return (root, result) => {
  21. const validOptions = validateOptions(
  22. result,
  23. ruleName,
  24. {
  25. actual: primary,
  26. possible: [isNumber],
  27. },
  28. {
  29. optional: true,
  30. actual: secondaryOptions,
  31. possible: {
  32. ignoreProperties: [isString, isRegExp],
  33. ignoreUnits: [isString, isRegExp],
  34. },
  35. },
  36. );
  37. if (!validOptions) {
  38. return;
  39. }
  40. root.walkAtRules((atRule) => {
  41. if (atRule.name.toLowerCase() === 'import') {
  42. return;
  43. }
  44. check(atRule, atRule.params);
  45. });
  46. root.walkDecls((decl) => check(decl, decl.value));
  47. /**
  48. * @param {import('postcss').AtRule | import('postcss').Declaration} node
  49. * @param {string} value
  50. */
  51. function check(node, value) {
  52. // Get out quickly if there are no periods
  53. if (!value.includes('.')) {
  54. return;
  55. }
  56. const prop = 'prop' in node ? node.prop : undefined;
  57. if (optionsMatches(secondaryOptions, 'ignoreProperties', prop)) {
  58. return;
  59. }
  60. valueParser(value).walk((valueNode) => {
  61. const unit = getUnitFromValueNode(valueNode);
  62. if (optionsMatches(secondaryOptions, 'ignoreUnits', unit)) {
  63. return;
  64. }
  65. // Ignore `url` function
  66. if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
  67. return false;
  68. }
  69. // Ignore strings, comments, etc
  70. if (valueNode.type !== 'word') {
  71. return;
  72. }
  73. const match = /\d*\.(\d+)/.exec(valueNode.value);
  74. if (match === null) {
  75. return;
  76. }
  77. if (match[1].length <= primary) {
  78. return;
  79. }
  80. const baseIndex =
  81. node.type === 'atrule' ? atRuleParamIndex(node) : declarationValueIndex(node);
  82. const actual = Number.parseFloat(match[0]);
  83. report({
  84. result,
  85. ruleName,
  86. node,
  87. index: baseIndex + valueNode.sourceIndex + match.index,
  88. message: messages.expected(actual, actual.toFixed(primary)),
  89. });
  90. });
  91. }
  92. };
  93. };
  94. rule.ruleName = ruleName;
  95. rule.messages = messages;
  96. rule.meta = meta;
  97. module.exports = rule;