index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. const blockString = require('../../utils/blockString');
  3. const hasBlock = require('../../utils/hasBlock');
  4. const hasEmptyBlock = require('../../utils/hasEmptyBlock');
  5. const isSingleLineString = require('../../utils/isSingleLineString');
  6. const report = require('../../utils/report');
  7. const ruleMessages = require('../../utils/ruleMessages');
  8. const validateOptions = require('../../utils/validateOptions');
  9. const ruleName = 'block-closing-brace-newline-before';
  10. const messages = ruleMessages(ruleName, {
  11. expectedBefore: 'Expected newline before "}"',
  12. expectedBeforeMultiLine: 'Expected newline before "}" of a multi-line block',
  13. rejectedBeforeMultiLine: 'Unexpected whitespace before "}" of a multi-line block',
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/block-closing-brace-newline-before',
  17. };
  18. /** @type {import('stylelint').Rule} */
  19. const rule = (primary, _secondaryOptions, context) => {
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: primary,
  23. possible: ['always', 'always-multi-line', 'never-multi-line'],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. // Check both kinds of statements: rules and at-rules
  29. root.walkRules(check);
  30. root.walkAtRules(check);
  31. /**
  32. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  33. */
  34. function check(statement) {
  35. // Return early if blockless or has empty block
  36. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  37. return;
  38. }
  39. // Ignore extra semicolon
  40. const after = (statement.raws.after || '').replace(/;+/, '');
  41. if (after === undefined) {
  42. return;
  43. }
  44. const blockIsMultiLine = !isSingleLineString(blockString(statement));
  45. const statementString = statement.toString();
  46. let index = statementString.length - 2;
  47. if (statementString[index - 1] === '\r') {
  48. index -= 1;
  49. }
  50. // We're really just checking whether a
  51. // newline *starts* the block's final space -- between
  52. // the last declaration and the closing brace. We can
  53. // ignore any other whitespace between them, because that
  54. // will be checked by the indentation rule.
  55. if (!after.startsWith('\n') && !after.startsWith('\r\n')) {
  56. if (primary === 'always') {
  57. complain(messages.expectedBefore);
  58. } else if (blockIsMultiLine && primary === 'always-multi-line') {
  59. complain(messages.expectedBeforeMultiLine);
  60. }
  61. }
  62. if (after !== '' && blockIsMultiLine && primary === 'never-multi-line') {
  63. complain(messages.rejectedBeforeMultiLine);
  64. }
  65. /**
  66. * @param {string} message
  67. */
  68. function complain(message) {
  69. if (context.fix) {
  70. const statementRaws = statement.raws;
  71. if (typeof statementRaws.after !== 'string') return;
  72. if (primary.startsWith('always')) {
  73. const firstWhitespaceIndex = statementRaws.after.search(/\s/);
  74. const newlineBefore =
  75. firstWhitespaceIndex >= 0
  76. ? statementRaws.after.slice(0, firstWhitespaceIndex)
  77. : statementRaws.after;
  78. const newlineAfter =
  79. firstWhitespaceIndex >= 0 ? statementRaws.after.slice(firstWhitespaceIndex) : '';
  80. const newlineIndex = newlineAfter.search(/\r?\n/);
  81. statementRaws.after =
  82. newlineIndex >= 0
  83. ? newlineBefore + newlineAfter.slice(newlineIndex)
  84. : newlineBefore + context.newline + newlineAfter;
  85. return;
  86. }
  87. if (primary === 'never-multi-line') {
  88. statementRaws.after = statementRaws.after.replace(/\s/g, '');
  89. return;
  90. }
  91. }
  92. report({
  93. message,
  94. result,
  95. ruleName,
  96. node: statement,
  97. index,
  98. });
  99. }
  100. }
  101. };
  102. };
  103. rule.ruleName = ruleName;
  104. rule.messages = messages;
  105. rule.meta = meta;
  106. module.exports = rule;