index.js 4.3 KB

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