index.js 4.0 KB

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