index.cjs 4.5 KB

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