index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. const isStandardSyntaxComment = require('../../utils/isStandardSyntaxComment');
  3. const isWhitespace = require('../../utils/isWhitespace');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const ruleName = 'comment-whitespace-inside';
  8. const messages = ruleMessages(ruleName, {
  9. expectedOpening: 'Expected whitespace after "/*"',
  10. rejectedOpening: 'Unexpected whitespace after "/*"',
  11. expectedClosing: 'Expected whitespace before "*/"',
  12. rejectedClosing: 'Unexpected whitespace before "*/"',
  13. });
  14. const meta = {
  15. url: 'https://stylelint.io/user-guide/rules/comment-whitespace-inside',
  16. fixable: true,
  17. };
  18. /**
  19. * @param {import('postcss').Comment} comment
  20. */
  21. function addWhitespaceBefore(comment) {
  22. if (comment.text.startsWith('*')) {
  23. comment.text = comment.text.replace(/^(\*+)/, `$1 `);
  24. } else {
  25. comment.raws.left = ' ';
  26. }
  27. }
  28. /**
  29. * @param {import('postcss').Comment} comment
  30. */
  31. function addWhitespaceAfter(comment) {
  32. if (comment.text[comment.text.length - 1] === '*') {
  33. comment.text = comment.text.replace(/(\*+)$/, ` $1`);
  34. } else {
  35. comment.raws.right = ' ';
  36. }
  37. }
  38. /** @type {import('stylelint').Rule} */
  39. const rule = (primary, _secondaryOptions, context) => {
  40. return (root, result) => {
  41. const validOptions = validateOptions(result, ruleName, {
  42. actual: primary,
  43. possible: ['always', 'never'],
  44. });
  45. if (!validOptions) {
  46. return;
  47. }
  48. root.walkComments((comment) => {
  49. if (!isStandardSyntaxComment(comment)) {
  50. return;
  51. }
  52. const rawComment = comment.toString();
  53. const firstFourChars = rawComment.slice(0, 4);
  54. // Return early if sourcemap or copyright comment
  55. if (/^\/\*[#!]\s/.test(firstFourChars)) {
  56. return;
  57. }
  58. const leftMatches = rawComment.match(/(^\/\*+)(\s)?/);
  59. if (leftMatches == null || leftMatches[1] == null) {
  60. throw new Error(`Invalid comment: "${rawComment}"`);
  61. }
  62. const rightMatches = rawComment.match(/(\s)?(\*+\/)$/);
  63. if (rightMatches == null || rightMatches[2] == null) {
  64. throw new Error(`Invalid comment: "${rawComment}"`);
  65. }
  66. const opener = leftMatches[1];
  67. const leftSpace = leftMatches[2] || '';
  68. const rightSpace = rightMatches[1] || '';
  69. const closer = rightMatches[2];
  70. if (primary === 'never' && leftSpace !== '') {
  71. complain(messages.rejectedOpening, opener.length);
  72. }
  73. if (primary === 'always' && !isWhitespace(leftSpace)) {
  74. complain(messages.expectedOpening, opener.length);
  75. }
  76. if (primary === 'never' && rightSpace !== '') {
  77. complain(messages.rejectedClosing, comment.toString().length - closer.length - 1);
  78. }
  79. if (primary === 'always' && !isWhitespace(rightSpace)) {
  80. complain(messages.expectedClosing, comment.toString().length - closer.length - 1);
  81. }
  82. /**
  83. * @param {string} message
  84. * @param {number} index
  85. */
  86. function complain(message, index) {
  87. if (context.fix) {
  88. if (primary === 'never') {
  89. comment.raws.left = '';
  90. comment.raws.right = '';
  91. comment.text = comment.text.replace(/^(\*+)(\s+)?/, '$1').replace(/(\s+)?(\*+)$/, '$2');
  92. } else {
  93. if (!leftSpace) {
  94. addWhitespaceBefore(comment);
  95. }
  96. if (!rightSpace) {
  97. addWhitespaceAfter(comment);
  98. }
  99. }
  100. return;
  101. }
  102. report({
  103. message,
  104. index,
  105. result,
  106. ruleName,
  107. node: comment,
  108. });
  109. }
  110. });
  111. };
  112. };
  113. rule.ruleName = ruleName;
  114. rule.messages = messages;
  115. rule.meta = meta;
  116. module.exports = rule;