index.js 3.7 KB

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