index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/block-opening-brace-space-after',
  22. fixable: true,
  23. };
  24. /** @type {import('stylelint').Rule} */
  25. const rule = (primary, secondaryOptions, context) => {
  26. const checker = whitespaceChecker('space', primary, messages);
  27. return (root, result) => {
  28. const validOptions = validateOptions(
  29. result,
  30. ruleName,
  31. {
  32. actual: primary,
  33. possible: [
  34. 'always',
  35. 'never',
  36. 'always-single-line',
  37. 'never-single-line',
  38. 'always-multi-line',
  39. 'never-multi-line',
  40. ],
  41. },
  42. {
  43. actual: secondaryOptions,
  44. possible: {
  45. ignore: ['at-rules'],
  46. },
  47. optional: true,
  48. },
  49. );
  50. if (!validOptions) {
  51. return;
  52. }
  53. // Check both kinds of statements: rules and at-rules
  54. root.walkRules(check);
  55. if (!optionsMatches(secondaryOptions, 'ignore', 'at-rules')) {
  56. root.walkAtRules(check);
  57. }
  58. /**
  59. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  60. */
  61. function check(statement) {
  62. // Return early if blockless or has an empty block
  63. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  64. return;
  65. }
  66. checker.after({
  67. source: blockString(statement),
  68. index: 0,
  69. err: (m) => {
  70. if (context.fix) {
  71. const statementFirst = statement.first;
  72. if (statementFirst == null) return;
  73. if (primary.startsWith('always')) {
  74. statementFirst.raws.before = ' ';
  75. return;
  76. }
  77. if (primary.startsWith('never')) {
  78. statementFirst.raws.before = '';
  79. return;
  80. }
  81. }
  82. report({
  83. message: m,
  84. node: statement,
  85. index: beforeBlockString(statement, { noRawBefore: true }).length + 1,
  86. result,
  87. ruleName,
  88. });
  89. },
  90. });
  91. }
  92. };
  93. };
  94. rule.ruleName = ruleName;
  95. rule.messages = messages;
  96. rule.meta = meta;
  97. module.exports = rule;