index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use strict';
  2. const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
  3. const blockString = require('../../utils/blockString');
  4. const hasEmptyLine = require('../../utils/hasEmptyLine');
  5. const isAfterComment = require('../../utils/isAfterComment');
  6. const isAfterStandardPropertyDeclaration = require('../../utils/isAfterStandardPropertyDeclaration');
  7. const isCustomProperty = require('../../utils/isCustomProperty');
  8. const isFirstNested = require('../../utils/isFirstNested');
  9. const isFirstNodeOfRoot = require('../../utils/isFirstNodeOfRoot');
  10. const isSingleLineString = require('../../utils/isSingleLineString');
  11. const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
  12. const optionsMatches = require('../../utils/optionsMatches');
  13. const removeEmptyLinesBefore = require('../../utils/removeEmptyLinesBefore');
  14. const report = require('../../utils/report');
  15. const ruleMessages = require('../../utils/ruleMessages');
  16. const validateOptions = require('../../utils/validateOptions');
  17. const { isAtRule, isRule, isRoot } = require('../../utils/typeGuards');
  18. const ruleName = 'declaration-empty-line-before';
  19. const messages = ruleMessages(ruleName, {
  20. expected: 'Expected empty line before declaration',
  21. rejected: 'Unexpected empty line before declaration',
  22. });
  23. const meta = {
  24. url: 'https://stylelint.io/user-guide/rules/list/declaration-empty-line-before',
  25. fixable: true,
  26. };
  27. /** @type {import('stylelint').Rule} */
  28. const rule = (primary, secondaryOptions, context) => {
  29. return (root, result) => {
  30. const validOptions = validateOptions(
  31. result,
  32. ruleName,
  33. {
  34. actual: primary,
  35. possible: ['always', 'never'],
  36. },
  37. {
  38. actual: secondaryOptions,
  39. possible: {
  40. except: ['first-nested', 'after-comment', 'after-declaration'],
  41. ignore: [
  42. 'after-comment',
  43. 'after-declaration',
  44. 'first-nested',
  45. 'inside-single-line-block',
  46. ],
  47. },
  48. optional: true,
  49. },
  50. );
  51. if (!validOptions) {
  52. return;
  53. }
  54. root.walkDecls((decl) => {
  55. const prop = decl.prop;
  56. const parent = decl.parent;
  57. if (parent == null) {
  58. return;
  59. }
  60. // Ignore the first node
  61. if (isFirstNodeOfRoot(decl)) {
  62. return;
  63. }
  64. if (!isAtRule(parent) && !isRule(parent) && !isRoot(parent)) {
  65. return;
  66. }
  67. if (!isStandardSyntaxDeclaration(decl)) {
  68. return;
  69. }
  70. if (isCustomProperty(prop)) {
  71. return;
  72. }
  73. // Optionally ignore the node if a comment precedes it
  74. if (optionsMatches(secondaryOptions, 'ignore', 'after-comment') && isAfterComment(decl)) {
  75. return;
  76. }
  77. // Optionally ignore the node if a declaration precedes it
  78. if (
  79. optionsMatches(secondaryOptions, 'ignore', 'after-declaration') &&
  80. isAfterStandardPropertyDeclaration(decl)
  81. ) {
  82. return;
  83. }
  84. // Optionally ignore the node if it is the first nested
  85. if (optionsMatches(secondaryOptions, 'ignore', 'first-nested') && isFirstNested(decl)) {
  86. return;
  87. }
  88. // Optionally ignore nodes inside single-line blocks
  89. if (
  90. optionsMatches(secondaryOptions, 'ignore', 'inside-single-line-block') &&
  91. isSingleLineString(blockString(parent))
  92. ) {
  93. return;
  94. }
  95. let expectEmptyLineBefore = primary === 'always';
  96. // Optionally reverse the expectation if any exceptions apply
  97. if (
  98. (optionsMatches(secondaryOptions, 'except', 'first-nested') && isFirstNested(decl)) ||
  99. (optionsMatches(secondaryOptions, 'except', 'after-comment') && isAfterComment(decl)) ||
  100. (optionsMatches(secondaryOptions, 'except', 'after-declaration') &&
  101. isAfterStandardPropertyDeclaration(decl))
  102. ) {
  103. expectEmptyLineBefore = !expectEmptyLineBefore;
  104. }
  105. // Check for at least one empty line
  106. const hasEmptyLineBefore = hasEmptyLine(decl.raws.before);
  107. // Return if the expectation is met
  108. if (expectEmptyLineBefore === hasEmptyLineBefore) {
  109. return;
  110. }
  111. // Fix
  112. if (context.fix) {
  113. if (context.newline == null) return;
  114. if (expectEmptyLineBefore) {
  115. addEmptyLineBefore(decl, context.newline);
  116. } else {
  117. removeEmptyLinesBefore(decl, context.newline);
  118. }
  119. return;
  120. }
  121. const message = expectEmptyLineBefore ? messages.expected : messages.rejected;
  122. report({ message, node: decl, result, ruleName });
  123. });
  124. };
  125. };
  126. rule.ruleName = ruleName;
  127. rule.messages = messages;
  128. rule.meta = meta;
  129. module.exports = rule;