index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. const valueParser = require('postcss-value-parser');
  3. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  4. const declarationValueIndex = require('../../utils/declarationValueIndex');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const ruleName = 'number-no-trailing-zeros';
  9. const messages = ruleMessages(ruleName, {
  10. rejected: 'Unexpected trailing zero(s)',
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/list/number-no-trailing-zeros',
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary, _secondaryOptions, context) => {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, { actual: primary });
  19. if (!validOptions) {
  20. return;
  21. }
  22. root.walkAtRules((atRule) => {
  23. if (atRule.name.toLowerCase() === 'import') {
  24. return;
  25. }
  26. check(atRule, atRule.params);
  27. });
  28. root.walkDecls((decl) => check(decl, decl.value));
  29. /**
  30. * @param {import('postcss').AtRule | import('postcss').Declaration} node
  31. * @param {string} value
  32. */
  33. function check(node, value) {
  34. /** @type {Array<{ startIndex: number, endIndex: number }>} */
  35. const fixPositions = [];
  36. // Get out quickly if there are no periods
  37. if (!value.includes('.')) {
  38. return;
  39. }
  40. valueParser(value).walk((valueNode) => {
  41. // Ignore `url` function
  42. if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
  43. return false;
  44. }
  45. // Ignore strings, comments, etc
  46. if (valueNode.type !== 'word') {
  47. return;
  48. }
  49. const match = /\.(\d{0,100}?)(0+)(?:\D|$)/.exec(valueNode.value);
  50. // match[1] is any numbers between the decimal and our trailing zero, could be empty
  51. // match[2] is our trailing zero(s)
  52. if (match === null) {
  53. return;
  54. }
  55. // our index is:
  56. // the index of our valueNode +
  57. // the index of our match +
  58. // 1 for our decimal +
  59. // the length of our potential non-zero number match (match[1])
  60. const index = valueNode.sourceIndex + match.index + 1 + match[1].length;
  61. // our startIndex is identical to our index except when we have only
  62. // trailing zeros after our decimal. in that case we don't need the decimal
  63. // either so we move our index back by 1.
  64. const startIndex = match[1].length > 0 ? index : index - 1;
  65. // our end index is our original index + the length of our trailing zeros
  66. const endIndex = index + match[2].length;
  67. if (context.fix) {
  68. fixPositions.unshift({
  69. startIndex,
  70. endIndex,
  71. });
  72. return;
  73. }
  74. const baseIndex =
  75. node.type === 'atrule' ? atRuleParamIndex(node) : declarationValueIndex(node);
  76. report({
  77. message: messages.rejected,
  78. node,
  79. // this is the index of the _first_ trailing zero
  80. index: baseIndex + index,
  81. result,
  82. ruleName,
  83. });
  84. });
  85. if (fixPositions.length) {
  86. for (const fixPosition of fixPositions) {
  87. const startIndex = fixPosition.startIndex;
  88. const endIndex = fixPosition.endIndex;
  89. if (node.type === 'atrule') {
  90. node.params = removeTrailingZeros(node.params, startIndex, endIndex);
  91. } else {
  92. node.value = removeTrailingZeros(node.value, startIndex, endIndex);
  93. }
  94. }
  95. }
  96. }
  97. };
  98. };
  99. /**
  100. * @param {string} input
  101. * @param {number} startIndex
  102. * @param {number} endIndex
  103. * @returns {string}
  104. */
  105. function removeTrailingZeros(input, startIndex, endIndex) {
  106. return input.slice(0, startIndex) + input.slice(endIndex);
  107. }
  108. rule.ruleName = ruleName;
  109. rule.messages = messages;
  110. rule.meta = meta;
  111. module.exports = rule;