index.js 3.7 KB

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