index.js 3.7 KB

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