index.js 4.7 KB

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