index.js 3.4 KB

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