index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. const beforeBlockString = require('../../utils/beforeBlockString');
  3. const blockString = require('../../utils/blockString');
  4. const hasBlock = require('../../utils/hasBlock');
  5. const hasEmptyBlock = require('../../utils/hasEmptyBlock');
  6. const optionsMatches = require('../../utils/optionsMatches');
  7. const report = require('../../utils/report');
  8. const ruleMessages = require('../../utils/ruleMessages');
  9. const validateOptions = require('../../utils/validateOptions');
  10. const whitespaceChecker = require('../../utils/whitespaceChecker');
  11. const ruleName = 'block-opening-brace-space-after';
  12. const messages = ruleMessages(ruleName, {
  13. expectedAfter: () => 'Expected single space after "{"',
  14. rejectedAfter: () => 'Unexpected whitespace after "{"',
  15. expectedAfterSingleLine: () => 'Expected single space after "{" of a single-line block',
  16. rejectedAfterSingleLine: () => 'Unexpected whitespace after "{" of a single-line block',
  17. expectedAfterMultiLine: () => 'Expected single space after "{" of a multi-line block',
  18. rejectedAfterMultiLine: () => 'Unexpected whitespace after "{" of a multi-line block',
  19. });
  20. const meta = {
  21. url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-space-after',
  22. };
  23. /** @type {import('stylelint').Rule} */
  24. const rule = (primary, secondaryOptions, context) => {
  25. const checker = whitespaceChecker('space', primary, messages);
  26. return (root, result) => {
  27. const validOptions = validateOptions(
  28. result,
  29. ruleName,
  30. {
  31. actual: primary,
  32. possible: [
  33. 'always',
  34. 'never',
  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. ignore: ['at-rules'],
  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. if (!optionsMatches(secondaryOptions, 'ignore', 'at-rules')) {
  55. root.walkAtRules(check);
  56. }
  57. /**
  58. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  59. */
  60. function check(statement) {
  61. // Return early if blockless or has an empty block
  62. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  63. return;
  64. }
  65. checker.after({
  66. source: blockString(statement),
  67. index: 0,
  68. err: (m) => {
  69. if (context.fix) {
  70. const statementFirst = statement.first;
  71. if (statementFirst == null) return;
  72. if (primary.startsWith('always')) {
  73. statementFirst.raws.before = ' ';
  74. return;
  75. }
  76. if (primary.startsWith('never')) {
  77. statementFirst.raws.before = '';
  78. return;
  79. }
  80. }
  81. report({
  82. message: m,
  83. node: statement,
  84. index: beforeBlockString(statement, { noRawBefore: true }).length + 1,
  85. result,
  86. ruleName,
  87. });
  88. },
  89. });
  90. }
  91. };
  92. };
  93. rule.ruleName = ruleName;
  94. rule.messages = messages;
  95. rule.meta = meta;
  96. module.exports = rule;