index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const blockString = require('../../utils/blockString');
  3. const hasBlock = require('../../utils/hasBlock');
  4. const hasEmptyBlock = require('../../utils/hasEmptyBlock');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const whitespaceChecker = require('../../utils/whitespaceChecker');
  9. const ruleName = 'block-closing-brace-space-before';
  10. const messages = ruleMessages(ruleName, {
  11. expectedBefore: () => 'Expected single space before "}"',
  12. rejectedBefore: () => 'Unexpected whitespace before "}"',
  13. expectedBeforeSingleLine: () => 'Expected single space before "}" of a single-line block',
  14. rejectedBeforeSingleLine: () => 'Unexpected whitespace before "}" of a single-line block',
  15. expectedBeforeMultiLine: () => 'Expected single space before "}" of a multi-line block',
  16. rejectedBeforeMultiLine: () => 'Unexpected whitespace before "}" of a multi-line block',
  17. });
  18. const meta = {
  19. url: 'https://stylelint.io/user-guide/rules/list/block-closing-brace-space-before',
  20. };
  21. /** @type {import('stylelint').Rule} */
  22. const rule = (primary, _secondaryOptions, context) => {
  23. const checker = whitespaceChecker('space', primary, messages);
  24. return (root, result) => {
  25. const validOptions = validateOptions(result, ruleName, {
  26. actual: primary,
  27. possible: [
  28. 'always',
  29. 'never',
  30. 'always-single-line',
  31. 'never-single-line',
  32. 'always-multi-line',
  33. 'never-multi-line',
  34. ],
  35. });
  36. if (!validOptions) {
  37. return;
  38. }
  39. // Check both kinds of statement: rules and at-rules
  40. root.walkRules(check);
  41. root.walkAtRules(check);
  42. /**
  43. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  44. */
  45. function check(statement) {
  46. // Return early if blockless or has empty block
  47. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  48. return;
  49. }
  50. const source = blockString(statement);
  51. const statementString = statement.toString();
  52. let index = statementString.length - 2;
  53. if (statementString[index - 1] === '\r') {
  54. index -= 1;
  55. }
  56. checker.before({
  57. source,
  58. index: source.length - 1,
  59. err: (msg) => {
  60. if (context.fix) {
  61. const statementRaws = statement.raws;
  62. if (typeof statementRaws.after !== 'string') return;
  63. if (primary.startsWith('always')) {
  64. statementRaws.after = statementRaws.after.replace(/\s*$/, ' ');
  65. return;
  66. }
  67. if (primary.startsWith('never')) {
  68. statementRaws.after = statementRaws.after.replace(/\s*$/, '');
  69. return;
  70. }
  71. }
  72. report({
  73. message: msg,
  74. node: statement,
  75. index,
  76. result,
  77. ruleName,
  78. });
  79. },
  80. });
  81. }
  82. };
  83. };
  84. rule.ruleName = ruleName;
  85. rule.messages = messages;
  86. rule.meta = meta;
  87. module.exports = rule;