index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
  3. const hasEmptyLine = require('../../utils/hasEmptyLine');
  4. const isAfterComment = require('../../utils/isAfterComment');
  5. const isFirstNested = require('../../utils/isFirstNested');
  6. const isFirstNodeOfRoot = require('../../utils/isFirstNodeOfRoot');
  7. const isSharedLineComment = require('../../utils/isSharedLineComment');
  8. const isStandardSyntaxComment = require('../../utils/isStandardSyntaxComment');
  9. const optionsMatches = require('../../utils/optionsMatches');
  10. const removeEmptyLinesBefore = require('../../utils/removeEmptyLinesBefore');
  11. const report = require('../../utils/report');
  12. const ruleMessages = require('../../utils/ruleMessages');
  13. const validateOptions = require('../../utils/validateOptions');
  14. const { isRegExp, isString } = require('../../utils/validateTypes');
  15. const ruleName = 'comment-empty-line-before';
  16. const messages = ruleMessages(ruleName, {
  17. expected: 'Expected empty line before comment',
  18. rejected: 'Unexpected empty line before comment',
  19. });
  20. const meta = {
  21. url: 'https://stylelint.io/user-guide/rules/list/comment-empty-line-before',
  22. };
  23. const stylelintCommandPrefix = 'stylelint-';
  24. /** @type {import('stylelint').Rule} */
  25. const rule = (primary, secondaryOptions, context) => {
  26. return (root, result) => {
  27. const validOptions = validateOptions(
  28. result,
  29. ruleName,
  30. {
  31. actual: primary,
  32. possible: ['always', 'never'],
  33. },
  34. {
  35. actual: secondaryOptions,
  36. possible: {
  37. except: ['first-nested'],
  38. ignore: ['stylelint-commands', 'after-comment'],
  39. ignoreComments: [isString, isRegExp],
  40. },
  41. optional: true,
  42. },
  43. );
  44. if (!validOptions) {
  45. return;
  46. }
  47. root.walkComments((comment) => {
  48. // Ignore the first node
  49. if (isFirstNodeOfRoot(comment)) {
  50. return;
  51. }
  52. // Optionally ignore stylelint commands
  53. if (
  54. comment.text.startsWith(stylelintCommandPrefix) &&
  55. optionsMatches(secondaryOptions, 'ignore', 'stylelint-commands')
  56. ) {
  57. return;
  58. }
  59. // Optionally ignore newlines between comments
  60. if (optionsMatches(secondaryOptions, 'ignore', 'after-comment') && isAfterComment(comment)) {
  61. return;
  62. }
  63. // Ignore comments matching the ignoreComments option.
  64. if (optionsMatches(secondaryOptions, 'ignoreComments', comment.text)) {
  65. return;
  66. }
  67. // Ignore shared-line comments
  68. if (isSharedLineComment(comment)) {
  69. return;
  70. }
  71. // Ignore non-standard comments
  72. if (!isStandardSyntaxComment(comment)) {
  73. return;
  74. }
  75. const expectEmptyLineBefore = (() => {
  76. if (optionsMatches(secondaryOptions, 'except', 'first-nested') && isFirstNested(comment)) {
  77. return false;
  78. }
  79. return primary === 'always';
  80. })();
  81. const before = comment.raws.before || '';
  82. const hasEmptyLineBefore = hasEmptyLine(before);
  83. // Return if the expectation is met
  84. if (expectEmptyLineBefore === hasEmptyLineBefore) {
  85. return;
  86. }
  87. // Fix
  88. if (context.fix) {
  89. if (typeof context.newline !== 'string') return;
  90. if (expectEmptyLineBefore) {
  91. addEmptyLineBefore(comment, context.newline);
  92. } else {
  93. removeEmptyLinesBefore(comment, context.newline);
  94. }
  95. return;
  96. }
  97. const message = expectEmptyLineBefore ? messages.expected : messages.rejected;
  98. report({
  99. message,
  100. node: comment,
  101. result,
  102. ruleName,
  103. });
  104. });
  105. };
  106. };
  107. rule.ruleName = ruleName;
  108. rule.messages = messages;
  109. rule.meta = meta;
  110. module.exports = rule;