index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/block-opening-brace-space-before',
  23. fixable: true,
  24. deprecated: true,
  25. };
  26. /** @type {import('stylelint').Rule} */
  27. const rule = (primary, secondaryOptions, context) => {
  28. const checker = whitespaceChecker('space', primary, messages);
  29. return (root, result) => {
  30. const validOptions = validateOptions(
  31. result,
  32. ruleName,
  33. {
  34. actual: primary,
  35. possible: [
  36. 'always',
  37. 'never',
  38. 'always-single-line',
  39. 'never-single-line',
  40. 'always-multi-line',
  41. 'never-multi-line',
  42. ],
  43. },
  44. {
  45. actual: secondaryOptions,
  46. possible: {
  47. ignoreAtRules: [isString, isRegExp],
  48. ignoreSelectors: [isString, isRegExp],
  49. },
  50. optional: true,
  51. },
  52. );
  53. if (!validOptions) {
  54. return;
  55. }
  56. // Check both kinds of statements: rules and at-rules
  57. root.walkRules(check);
  58. root.walkAtRules(check);
  59. /**
  60. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  61. */
  62. function check(statement) {
  63. // Return early if blockless or has an empty block
  64. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  65. return;
  66. }
  67. // Return early if at-rule is to be ignored
  68. if (
  69. statement.type === 'atrule' &&
  70. optionsMatches(secondaryOptions, 'ignoreAtRules', statement.name)
  71. ) {
  72. return;
  73. }
  74. // Return early if selector is to be ignored
  75. if (
  76. statement.type === 'rule' &&
  77. optionsMatches(secondaryOptions, 'ignoreSelectors', statement.selector)
  78. ) {
  79. return;
  80. }
  81. const source = beforeBlockString(statement);
  82. const beforeBraceNoRaw = beforeBlockString(statement, {
  83. noRawBefore: true,
  84. });
  85. let index = beforeBraceNoRaw.length - 1;
  86. if (beforeBraceNoRaw[index - 1] === '\r') {
  87. index -= 1;
  88. }
  89. checker.before({
  90. source,
  91. index: source.length,
  92. lineCheckStr: blockString(statement),
  93. err: (m) => {
  94. if (context.fix) {
  95. if (primary.startsWith('always')) {
  96. statement.raws.between = ' ';
  97. return;
  98. }
  99. if (primary.startsWith('never')) {
  100. statement.raws.between = '';
  101. return;
  102. }
  103. }
  104. report({
  105. message: m,
  106. node: statement,
  107. index,
  108. result,
  109. ruleName,
  110. });
  111. },
  112. });
  113. }
  114. };
  115. };
  116. rule.ruleName = ruleName;
  117. rule.messages = messages;
  118. rule.meta = meta;
  119. module.exports = rule;