index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. const beforeBlockString = require('../../utils/beforeBlockString');
  3. const blockString = require('../../utils/blockString');
  4. const hasBlock = require('../../utils/hasBlock');
  5. const hasEmptyBlock = require('../../utils/hasEmptyBlock');
  6. const optionsMatches = require('../../utils/optionsMatches');
  7. const rawNodeString = require('../../utils/rawNodeString');
  8. const report = require('../../utils/report');
  9. const ruleMessages = require('../../utils/ruleMessages');
  10. const validateOptions = require('../../utils/validateOptions');
  11. const whitespaceChecker = require('../../utils/whitespaceChecker');
  12. const ruleName = 'block-opening-brace-newline-after';
  13. const messages = ruleMessages(ruleName, {
  14. expectedAfter: () => 'Expected newline after "{"',
  15. expectedAfterMultiLine: () => 'Expected newline after "{" of a multi-line block',
  16. rejectedAfterMultiLine: () => 'Unexpected whitespace after "{" of a multi-line block',
  17. });
  18. const meta = {
  19. url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-newline-after',
  20. };
  21. /** @type {import('stylelint').Rule} */
  22. const rule = (primary, secondaryOptions, context) => {
  23. const checker = whitespaceChecker('newline', primary, messages);
  24. return (root, result) => {
  25. const validOptions = validateOptions(
  26. result,
  27. ruleName,
  28. {
  29. actual: primary,
  30. possible: ['always', 'rules', 'always-multi-line', 'never-multi-line'],
  31. },
  32. {
  33. actual: secondaryOptions,
  34. possible: {
  35. ignore: ['rules'],
  36. },
  37. optional: true,
  38. },
  39. );
  40. if (!validOptions) {
  41. return;
  42. }
  43. // Check both kinds of statement: rules and at-rules
  44. if (!optionsMatches(secondaryOptions, 'ignore', 'rules')) {
  45. root.walkRules(check);
  46. }
  47. root.walkAtRules(check);
  48. /**
  49. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  50. */
  51. function check(statement) {
  52. // Return early if blockless or has an empty block
  53. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  54. return;
  55. }
  56. const backupCommentNextBefores = new Map();
  57. /**
  58. * next node with checking newlines after comment
  59. *
  60. * @param {import('postcss').ChildNode | undefined} startNode
  61. * @returns {import('postcss').ChildNode | undefined}
  62. */
  63. function nextNode(startNode) {
  64. if (!startNode || !startNode.next) return;
  65. if (startNode.type === 'comment') {
  66. const reNewLine = /\r?\n/;
  67. const newLineMatch = reNewLine.test(startNode.raws.before || '');
  68. const next = startNode.next();
  69. if (next && newLineMatch && !reNewLine.test(next.raws.before || '')) {
  70. backupCommentNextBefores.set(next, next.raws.before);
  71. next.raws.before = startNode.raws.before;
  72. }
  73. return nextNode(next);
  74. }
  75. return startNode;
  76. }
  77. // Allow an end-of-line comment
  78. const nodeToCheck = nextNode(statement.first);
  79. if (!nodeToCheck) {
  80. return;
  81. }
  82. checker.afterOneOnly({
  83. source: rawNodeString(nodeToCheck),
  84. index: -1,
  85. lineCheckStr: blockString(statement),
  86. err: (m) => {
  87. if (context.fix) {
  88. const nodeToCheckRaws = nodeToCheck.raws;
  89. if (typeof nodeToCheckRaws.before !== 'string') return;
  90. if (primary.startsWith('always')) {
  91. const index = nodeToCheckRaws.before.search(/\r?\n/);
  92. nodeToCheckRaws.before =
  93. index >= 0
  94. ? nodeToCheckRaws.before.slice(index)
  95. : context.newline + nodeToCheckRaws.before;
  96. backupCommentNextBefores.delete(nodeToCheck);
  97. return;
  98. }
  99. if (primary === 'never-multi-line') {
  100. // Restore the `before` of the node next to the comment node.
  101. for (const [node, before] of backupCommentNextBefores.entries()) {
  102. node.raws.before = before;
  103. }
  104. backupCommentNextBefores.clear();
  105. // Fix
  106. const reNewLine = /\r?\n/;
  107. let fixTarget = statement.first;
  108. while (fixTarget) {
  109. const fixTargetRaws = fixTarget.raws;
  110. if (typeof fixTargetRaws.before !== 'string') continue;
  111. if (reNewLine.test(fixTargetRaws.before || '')) {
  112. fixTargetRaws.before = fixTargetRaws.before.replace(/\r?\n/g, '');
  113. }
  114. if (fixTarget.type !== 'comment') {
  115. break;
  116. }
  117. fixTarget = fixTarget.next();
  118. }
  119. nodeToCheckRaws.before = '';
  120. return;
  121. }
  122. }
  123. report({
  124. message: m,
  125. node: statement,
  126. index: beforeBlockString(statement, { noRawBefore: true }).length + 1,
  127. result,
  128. ruleName,
  129. });
  130. },
  131. });
  132. // Restore the `before` of the node next to the comment node.
  133. for (const [node, before] of backupCommentNextBefores.entries()) {
  134. node.raws.before = before;
  135. }
  136. }
  137. };
  138. };
  139. rule.ruleName = ruleName;
  140. rule.messages = messages;
  141. rule.meta = meta;
  142. module.exports = rule;