index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { isRegExp, isString } = require('../../utils/validateTypes');
  12. const ruleName = 'block-opening-brace-space-before';
  13. const messages = ruleMessages(ruleName, {
  14. expectedBefore: () => 'Expected single space before "{"',
  15. rejectedBefore: () => 'Unexpected whitespace before "{"',
  16. expectedBeforeSingleLine: () => 'Expected single space before "{" of a single-line block',
  17. rejectedBeforeSingleLine: () => 'Unexpected whitespace before "{" of a single-line block',
  18. expectedBeforeMultiLine: () => 'Expected single space before "{" of a multi-line block',
  19. rejectedBeforeMultiLine: () => 'Unexpected whitespace before "{" of a multi-line block',
  20. });
  21. const meta = {
  22. url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-space-before',
  23. fixable: true,
  24. };
  25. /** @type {import('stylelint').Rule} */
  26. const rule = (primary, secondaryOptions, context) => {
  27. const checker = whitespaceChecker('space', primary, messages);
  28. return (root, result) => {
  29. const validOptions = validateOptions(
  30. result,
  31. ruleName,
  32. {
  33. actual: primary,
  34. possible: [
  35. 'always',
  36. 'never',
  37. 'always-single-line',
  38. 'never-single-line',
  39. 'always-multi-line',
  40. 'never-multi-line',
  41. ],
  42. },
  43. {
  44. actual: secondaryOptions,
  45. possible: {
  46. ignoreAtRules: [isString, isRegExp],
  47. ignoreSelectors: [isString, isRegExp],
  48. },
  49. optional: true,
  50. },
  51. );
  52. if (!validOptions) {
  53. return;
  54. }
  55. // Check both kinds of statements: rules and at-rules
  56. root.walkRules(check);
  57. root.walkAtRules(check);
  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. // Return early if at-rule is to be ignored
  67. if (
  68. statement.type === 'atrule' &&
  69. optionsMatches(secondaryOptions, 'ignoreAtRules', statement.name)
  70. ) {
  71. return;
  72. }
  73. // Return early if selector is to be ignored
  74. if (
  75. statement.type === 'rule' &&
  76. optionsMatches(secondaryOptions, 'ignoreSelectors', statement.selector)
  77. ) {
  78. return;
  79. }
  80. const source = beforeBlockString(statement);
  81. const beforeBraceNoRaw = beforeBlockString(statement, {
  82. noRawBefore: true,
  83. });
  84. let index = beforeBraceNoRaw.length - 1;
  85. if (beforeBraceNoRaw[index - 1] === '\r') {
  86. index -= 1;
  87. }
  88. checker.before({
  89. source,
  90. index: source.length,
  91. lineCheckStr: blockString(statement),
  92. err: (m) => {
  93. if (context.fix) {
  94. if (primary.startsWith('always')) {
  95. statement.raws.between = ' ';
  96. return;
  97. }
  98. if (primary.startsWith('never')) {
  99. statement.raws.between = '';
  100. return;
  101. }
  102. }
  103. report({
  104. message: m,
  105. node: statement,
  106. index,
  107. result,
  108. ruleName,
  109. });
  110. },
  111. });
  112. }
  113. };
  114. };
  115. rule.ruleName = ruleName;
  116. rule.messages = messages;
  117. rule.meta = meta;
  118. module.exports = rule;