index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @ts-nocheck
  2. 'use strict';
  3. const _ = require('lodash');
  4. const isWhitespace = require('../../utils/isWhitespace');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const ruleName = 'comment-whitespace-inside';
  9. const messages = ruleMessages(ruleName, {
  10. expectedOpening: 'Expected whitespace after "/*"',
  11. rejectedOpening: 'Unexpected whitespace after "/*"',
  12. expectedClosing: 'Expected whitespace before "*/"',
  13. rejectedClosing: 'Unexpected whitespace before "*/"',
  14. });
  15. function rule(expectation, options, context) {
  16. return function (root, result) {
  17. const validOptions = validateOptions(result, ruleName, {
  18. actual: expectation,
  19. possible: ['always', 'never'],
  20. });
  21. if (!validOptions) {
  22. return;
  23. }
  24. root.walkComments((comment) => {
  25. if (comment.raws.inline || comment.inline) {
  26. return;
  27. }
  28. const rawComment = comment.toString();
  29. const firstFourChars = rawComment.substr(0, 4);
  30. // Return early if sourcemap or copyright comment
  31. if (/^\/\*[#!]\s/.test(firstFourChars)) {
  32. return;
  33. }
  34. const leftMatches = rawComment.match(/(^\/\*+)(\s)?/);
  35. const rightMatches = rawComment.match(/(\s)?(\*+\/)$/);
  36. const opener = leftMatches[1];
  37. const leftSpace = leftMatches[2] || '';
  38. const rightSpace = rightMatches[1] || '';
  39. const closer = rightMatches[2];
  40. if (expectation === 'never' && leftSpace !== '') {
  41. complain(messages.rejectedOpening, opener.length);
  42. }
  43. if (expectation === 'always' && !isWhitespace(leftSpace)) {
  44. complain(messages.expectedOpening, opener.length);
  45. }
  46. if (expectation === 'never' && rightSpace !== '') {
  47. complain(messages.rejectedClosing, comment.toString().length - closer.length - 1);
  48. }
  49. if (expectation === 'always' && !isWhitespace(rightSpace)) {
  50. complain(messages.expectedClosing, comment.toString().length - closer.length - 1);
  51. }
  52. function addWhitespaceBefore(comment) {
  53. if (comment.text.startsWith('*')) {
  54. comment.text = comment.text.replace(/^(\*+)/, `$1 `);
  55. } else {
  56. comment.raws.left = ' ';
  57. }
  58. }
  59. function addWhitespaceAfter(comment) {
  60. if (_.last(comment.text) === '*') {
  61. comment.text = comment.text.replace(/(\*+)$/, ` $1`);
  62. } else {
  63. comment.raws.right = ' ';
  64. }
  65. }
  66. function complain(message, index) {
  67. if (context.fix) {
  68. if (expectation === 'never') {
  69. comment.raws.left = '';
  70. comment.raws.right = '';
  71. comment.text = comment.text.replace(/^(\*+)(\s+)?/, '$1').replace(/(\s+)?(\*+)$/, '$2');
  72. } else {
  73. if (!leftSpace) {
  74. addWhitespaceBefore(comment);
  75. }
  76. if (!rightSpace) {
  77. addWhitespaceAfter(comment);
  78. }
  79. }
  80. return;
  81. }
  82. report({
  83. message,
  84. index,
  85. result,
  86. ruleName,
  87. node: comment,
  88. });
  89. }
  90. });
  91. };
  92. }
  93. rule.ruleName = ruleName;
  94. rule.messages = messages;
  95. module.exports = rule;