index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. 'use strict';
  2. const valueParser = require('postcss-value-parser');
  3. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  4. const declarationValueIndex = require('../../utils/declarationValueIndex');
  5. const getAtRuleParams = require('../../utils/getAtRuleParams');
  6. const getDeclarationValue = require('../../utils/getDeclarationValue');
  7. const isCustomProperty = require('../../utils/isCustomProperty');
  8. const isMathFunction = require('../../utils/isMathFunction');
  9. const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
  10. const { lengthUnits } = require('../../reference/units');
  11. const optionsMatches = require('../../utils/optionsMatches');
  12. const report = require('../../utils/report');
  13. const ruleMessages = require('../../utils/ruleMessages');
  14. const setAtRuleParams = require('../../utils/setAtRuleParams');
  15. const setDeclarationValue = require('../../utils/setDeclarationValue');
  16. const validateOptions = require('../../utils/validateOptions');
  17. const { isRegExp, isString } = require('../../utils/validateTypes');
  18. const ruleName = 'length-zero-no-unit';
  19. const messages = ruleMessages(ruleName, {
  20. rejected: 'Unexpected unit',
  21. });
  22. const meta = {
  23. url: 'https://stylelint.io/user-guide/rules/length-zero-no-unit',
  24. fixable: true,
  25. };
  26. /** @type {import('stylelint').Rule} */
  27. const rule = (primary, secondaryOptions, context) => {
  28. return (root, result) => {
  29. const validOptions = validateOptions(
  30. result,
  31. ruleName,
  32. {
  33. actual: primary,
  34. },
  35. {
  36. actual: secondaryOptions,
  37. possible: {
  38. ignore: ['custom-properties'],
  39. ignoreFunctions: [isString, isRegExp],
  40. },
  41. optional: true,
  42. },
  43. );
  44. if (!validOptions) return;
  45. let needsFix;
  46. /**
  47. * @param {import('postcss').Node} node
  48. * @param {number} nodeIndex
  49. * @param {import('postcss-value-parser').Node} valueNode
  50. */
  51. function check(node, nodeIndex, valueNode) {
  52. const { value, sourceIndex } = valueNode;
  53. if (isMathFunction(valueNode)) return false;
  54. if (isFunction(valueNode) && optionsMatches(secondaryOptions, 'ignoreFunctions', value))
  55. return false;
  56. if (!isWord(valueNode)) return;
  57. const numberUnit = valueParser.unit(value);
  58. if (numberUnit === false) return;
  59. const { number, unit } = numberUnit;
  60. if (unit === '') return;
  61. if (!isLength(unit)) return;
  62. if (isFraction(unit)) return;
  63. if (!isZero(number)) return;
  64. if (context.fix) {
  65. let regularNumber = number;
  66. if (regularNumber.startsWith('.')) {
  67. regularNumber = number.slice(1);
  68. }
  69. valueNode.value = regularNumber;
  70. needsFix = true;
  71. return;
  72. }
  73. const index = nodeIndex + sourceIndex + number.length;
  74. const endIndex = index + unit.length;
  75. report({
  76. index,
  77. endIndex,
  78. message: messages.rejected,
  79. node,
  80. result,
  81. ruleName,
  82. });
  83. }
  84. /**
  85. * @param {import('postcss').AtRule} node
  86. */
  87. function checkAtRule(node) {
  88. if (!isStandardSyntaxAtRule(node)) return;
  89. needsFix = false;
  90. const index = atRuleParamIndex(node);
  91. const parsedValue = valueParser(getAtRuleParams(node));
  92. parsedValue.walk((valueNode) => check(node, index, valueNode));
  93. if (needsFix) {
  94. setAtRuleParams(node, parsedValue.toString());
  95. }
  96. }
  97. /**
  98. * @param {import('postcss').Declaration} node
  99. */
  100. function checkDecl(node) {
  101. needsFix = false;
  102. const { prop } = node;
  103. if (isLineHeight(prop)) return;
  104. if (isFlex(prop)) return;
  105. if (optionsMatches(secondaryOptions, 'ignore', 'custom-properties') && isCustomProperty(prop))
  106. return;
  107. const index = declarationValueIndex(node);
  108. const parsedValue = valueParser(getDeclarationValue(node));
  109. parsedValue.walk((valueNode, valueNodeIndex, valueNodes) => {
  110. if (isLineHeightValue(node, valueNodes, valueNodeIndex)) return;
  111. return check(node, index, valueNode);
  112. });
  113. if (needsFix) {
  114. setDeclarationValue(node, parsedValue.toString());
  115. }
  116. }
  117. root.walkAtRules(checkAtRule);
  118. root.walkDecls(checkDecl);
  119. };
  120. };
  121. /**
  122. * @param {import('postcss').Declaration} decl
  123. * @param {import('postcss-value-parser').Node[]} nodes
  124. * @param {number} index
  125. */
  126. function isLineHeightValue({ prop }, nodes, index) {
  127. const lastNode = nodes[index - 1];
  128. return (
  129. prop.toLowerCase() === 'font' && lastNode && lastNode.type === 'div' && lastNode.value === '/'
  130. );
  131. }
  132. /**
  133. * @param {string} prop
  134. */
  135. function isLineHeight(prop) {
  136. return prop.toLowerCase() === 'line-height';
  137. }
  138. /**
  139. * @param {string} prop
  140. */
  141. function isFlex(prop) {
  142. return prop.toLowerCase() === 'flex';
  143. }
  144. /**
  145. * @param {import('postcss-value-parser').Node} node
  146. */
  147. function isWord({ type }) {
  148. return type === 'word';
  149. }
  150. /**
  151. * @param {string} unit
  152. */
  153. function isLength(unit) {
  154. return lengthUnits.has(unit.toLowerCase());
  155. }
  156. /**
  157. * @param {import('postcss-value-parser').Node} node
  158. */
  159. function isFunction({ type }) {
  160. return type === 'function';
  161. }
  162. /**
  163. * @param {string} unit
  164. */
  165. function isFraction(unit) {
  166. return unit.toLowerCase() === 'fr';
  167. }
  168. /**
  169. * @param {string} number
  170. */
  171. function isZero(number) {
  172. return Number.parseFloat(number) === 0;
  173. }
  174. rule.ruleName = ruleName;
  175. rule.messages = messages;
  176. rule.meta = meta;
  177. module.exports = rule;