index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. const blockString = require('../../utils/blockString');
  3. const hasBlock = require('../../utils/hasBlock');
  4. const optionsMatches = require('../../utils/optionsMatches');
  5. const rawNodeString = require('../../utils/rawNodeString');
  6. const report = require('../../utils/report');
  7. const ruleMessages = require('../../utils/ruleMessages');
  8. const validateOptions = require('../../utils/validateOptions');
  9. const whitespaceChecker = require('../../utils/whitespaceChecker');
  10. const { isString } = require('../../utils/validateTypes');
  11. const ruleName = 'block-closing-brace-newline-after';
  12. const messages = ruleMessages(ruleName, {
  13. expectedAfter: () => 'Expected newline after "}"',
  14. expectedAfterSingleLine: () => 'Expected newline after "}" of a single-line block',
  15. rejectedAfterSingleLine: () => 'Unexpected whitespace after "}" of a single-line block',
  16. expectedAfterMultiLine: () => 'Expected newline after "}" of a multi-line block',
  17. rejectedAfterMultiLine: () => 'Unexpected whitespace after "}" of a multi-line block',
  18. });
  19. const meta = {
  20. url: 'https://stylelint.io/user-guide/rules/list/block-closing-brace-newline-after',
  21. };
  22. /** @type {import('stylelint').Rule} */
  23. const rule = (primary, secondaryOptions, context) => {
  24. const checker = whitespaceChecker('newline', primary, messages);
  25. return (root, result) => {
  26. const validOptions = validateOptions(
  27. result,
  28. ruleName,
  29. {
  30. actual: primary,
  31. possible: [
  32. 'always',
  33. 'always-single-line',
  34. 'never-single-line',
  35. 'always-multi-line',
  36. 'never-multi-line',
  37. ],
  38. },
  39. {
  40. actual: secondaryOptions,
  41. possible: {
  42. ignoreAtRules: [isString],
  43. },
  44. optional: true,
  45. },
  46. );
  47. if (!validOptions) {
  48. return;
  49. }
  50. // Check both kinds of statements: rules and at-rules
  51. root.walkRules(check);
  52. root.walkAtRules(check);
  53. /**
  54. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  55. */
  56. function check(statement) {
  57. if (!hasBlock(statement)) {
  58. return;
  59. }
  60. if (
  61. statement.type === 'atrule' &&
  62. optionsMatches(secondaryOptions, 'ignoreAtRules', statement.name)
  63. ) {
  64. return;
  65. }
  66. const nextNode = statement.next();
  67. if (!nextNode) {
  68. return;
  69. }
  70. // Allow an end-of-line comment x spaces after the brace
  71. const nextNodeIsSingleLineComment =
  72. nextNode.type === 'comment' &&
  73. !/[^ ]/.test(nextNode.raws.before || '') &&
  74. !nextNode.toString().includes('\n');
  75. const nodeToCheck = nextNodeIsSingleLineComment ? nextNode.next() : nextNode;
  76. if (!nodeToCheck) {
  77. return;
  78. }
  79. let reportIndex = statement.toString().length;
  80. let source = rawNodeString(nodeToCheck);
  81. // Skip a semicolon at the beginning, if any
  82. if (source && source.startsWith(';')) {
  83. source = source.slice(1);
  84. reportIndex++;
  85. }
  86. // Only check one after, because there might be other
  87. // spaces handled by the indentation rule
  88. checker.afterOneOnly({
  89. source,
  90. index: -1,
  91. lineCheckStr: blockString(statement),
  92. err: (msg) => {
  93. if (context.fix) {
  94. const nodeToCheckRaws = nodeToCheck.raws;
  95. if (typeof nodeToCheckRaws.before !== 'string') return;
  96. if (primary.startsWith('always')) {
  97. const index = nodeToCheckRaws.before.search(/\r?\n/);
  98. nodeToCheckRaws.before =
  99. index >= 0
  100. ? nodeToCheckRaws.before.slice(index)
  101. : context.newline + nodeToCheckRaws.before;
  102. return;
  103. }
  104. if (primary.startsWith('never')) {
  105. nodeToCheckRaws.before = '';
  106. return;
  107. }
  108. }
  109. report({
  110. message: msg,
  111. node: statement,
  112. index: reportIndex,
  113. result,
  114. ruleName,
  115. });
  116. },
  117. });
  118. }
  119. };
  120. };
  121. rule.ruleName = ruleName;
  122. rule.messages = messages;
  123. rule.meta = meta;
  124. module.exports = rule;