index.js 2.9 KB

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