index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
  3. const blockString = require('../../utils/blockString');
  4. const getPreviousNonSharedLineCommentNode = require('../../utils/getPreviousNonSharedLineCommentNode');
  5. const hasEmptyLine = require('../../utils/hasEmptyLine');
  6. const isAfterComment = require('../../utils/isAfterComment');
  7. const isCustomProperty = require('../../utils/isCustomProperty');
  8. const isFirstNested = require('../../utils/isFirstNested');
  9. const isSingleLineString = require('../../utils/isSingleLineString');
  10. const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
  11. const optionsMatches = require('../../utils/optionsMatches');
  12. const removeEmptyLinesBefore = require('../../utils/removeEmptyLinesBefore');
  13. const report = require('../../utils/report');
  14. const ruleMessages = require('../../utils/ruleMessages');
  15. const validateOptions = require('../../utils/validateOptions');
  16. const { isAtRule, isDeclaration, isRule } = require('../../utils/typeGuards');
  17. const ruleName = 'custom-property-empty-line-before';
  18. const messages = ruleMessages(ruleName, {
  19. expected: 'Expected empty line before custom property',
  20. rejected: 'Unexpected empty line before custom property',
  21. });
  22. const meta = {
  23. url: 'https://stylelint.io/user-guide/rules/list/custom-property-empty-line-before',
  24. };
  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', 'after-comment', 'after-custom-property'],
  39. ignore: ['after-comment', 'first-nested', 'inside-single-line-block'],
  40. },
  41. optional: true,
  42. },
  43. );
  44. if (!validOptions) {
  45. return;
  46. }
  47. root.walkDecls((decl) => {
  48. const prop = decl.prop;
  49. const parent = decl.parent;
  50. if (!isStandardSyntaxDeclaration(decl)) {
  51. return;
  52. }
  53. if (!isCustomProperty(prop)) {
  54. return;
  55. }
  56. // Optionally ignore the node if a comment precedes it
  57. if (optionsMatches(secondaryOptions, 'ignore', 'after-comment') && isAfterComment(decl)) {
  58. return;
  59. }
  60. // Optionally ignore the node if it is the first nested
  61. if (optionsMatches(secondaryOptions, 'ignore', 'first-nested') && isFirstNested(decl)) {
  62. return;
  63. }
  64. // Optionally ignore nodes inside single-line blocks
  65. if (
  66. optionsMatches(secondaryOptions, 'ignore', 'inside-single-line-block') &&
  67. parent != null &&
  68. (isAtRule(parent) || isRule(parent)) &&
  69. isSingleLineString(blockString(parent))
  70. ) {
  71. return;
  72. }
  73. let expectEmptyLineBefore = primary === 'always';
  74. // Optionally reverse the expectation if any exceptions apply
  75. if (
  76. (optionsMatches(secondaryOptions, 'except', 'first-nested') && isFirstNested(decl)) ||
  77. (optionsMatches(secondaryOptions, 'except', 'after-comment') && isAfterComment(decl)) ||
  78. (optionsMatches(secondaryOptions, 'except', 'after-custom-property') &&
  79. isAfterCustomProperty(decl))
  80. ) {
  81. expectEmptyLineBefore = !expectEmptyLineBefore;
  82. }
  83. const hasEmptyLineBefore = hasEmptyLine(decl.raws.before);
  84. // Return if the expectation is met
  85. if (expectEmptyLineBefore === hasEmptyLineBefore) {
  86. return;
  87. }
  88. // Fix
  89. if (context.fix) {
  90. if (context.newline == null) return;
  91. if (expectEmptyLineBefore) {
  92. addEmptyLineBefore(decl, context.newline);
  93. } else {
  94. removeEmptyLinesBefore(decl, context.newline);
  95. }
  96. return;
  97. }
  98. const message = expectEmptyLineBefore ? messages.expected : messages.rejected;
  99. report({
  100. message,
  101. node: decl,
  102. result,
  103. ruleName,
  104. });
  105. });
  106. };
  107. };
  108. /**
  109. * @param {import('postcss').Declaration} decl
  110. */
  111. function isAfterCustomProperty(decl) {
  112. const prevNode = getPreviousNonSharedLineCommentNode(decl);
  113. return prevNode != null && isDeclaration(prevNode) && isCustomProperty(prevNode.prop);
  114. }
  115. rule.ruleName = ruleName;
  116. rule.messages = messages;
  117. rule.meta = meta;
  118. module.exports = rule;