index.js 3.7 KB

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