index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. };
  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. ignoreAtRules: [isString, isRegExp],
  46. ignoreSelectors: [isString, isRegExp],
  47. },
  48. optional: true,
  49. },
  50. );
  51. if (!validOptions) {
  52. return;
  53. }
  54. // Check both kinds of statements: rules and at-rules
  55. root.walkRules(check);
  56. root.walkAtRules(check);
  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. // Return early if at-rule is to be ignored
  66. if (
  67. statement.type === 'atrule' &&
  68. optionsMatches(secondaryOptions, 'ignoreAtRules', statement.name)
  69. ) {
  70. return;
  71. }
  72. // Return early if selector is to be ignored
  73. if (
  74. statement.type === 'rule' &&
  75. optionsMatches(secondaryOptions, 'ignoreSelectors', statement.selector)
  76. ) {
  77. return;
  78. }
  79. const source = beforeBlockString(statement);
  80. const beforeBraceNoRaw = beforeBlockString(statement, {
  81. noRawBefore: true,
  82. });
  83. let index = beforeBraceNoRaw.length - 1;
  84. if (beforeBraceNoRaw[index - 1] === '\r') {
  85. index -= 1;
  86. }
  87. checker.before({
  88. source,
  89. index: source.length,
  90. lineCheckStr: blockString(statement),
  91. err: (m) => {
  92. if (context.fix) {
  93. if (primary.startsWith('always')) {
  94. statement.raws.between = ' ';
  95. return;
  96. }
  97. if (primary.startsWith('never')) {
  98. statement.raws.between = '';
  99. return;
  100. }
  101. }
  102. report({
  103. message: m,
  104. node: statement,
  105. index,
  106. result,
  107. ruleName,
  108. });
  109. },
  110. });
  111. }
  112. };
  113. };
  114. rule.ruleName = ruleName;
  115. rule.messages = messages;
  116. rule.meta = meta;
  117. module.exports = rule;