index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 report = require('../../utils/report');
  7. const ruleMessages = require('../../utils/ruleMessages');
  8. const validateOptions = require('../../utils/validateOptions');
  9. const whitespaceChecker = require('../../utils/whitespaceChecker');
  10. const ruleName = 'block-opening-brace-newline-before';
  11. const messages = ruleMessages(ruleName, {
  12. expectedBefore: () => 'Expected newline before "{"',
  13. expectedBeforeSingleLine: () => 'Expected newline before "{" of a single-line block',
  14. rejectedBeforeSingleLine: () => 'Unexpected whitespace before "{" of a single-line block',
  15. expectedBeforeMultiLine: () => 'Expected newline before "{" of a multi-line block',
  16. rejectedBeforeMultiLine: () => 'Unexpected whitespace before "{" of a multi-line block',
  17. });
  18. const meta = {
  19. url: 'https://stylelint.io/user-guide/rules/list/block-opening-brace-newline-before',
  20. fixable: true,
  21. };
  22. /** @type {import('stylelint').Rule} */
  23. const rule = (primary, _secondaryOptions, context) => {
  24. const checker = whitespaceChecker('newline', primary, messages);
  25. return (root, result) => {
  26. const validOptions = validateOptions(result, ruleName, {
  27. actual: primary,
  28. possible: [
  29. 'always',
  30. 'always-single-line',
  31. 'never-single-line',
  32. 'always-multi-line',
  33. 'never-multi-line',
  34. ],
  35. });
  36. if (!validOptions) {
  37. return;
  38. }
  39. // Check both kinds of statement: rules and at-rules
  40. root.walkRules(check);
  41. root.walkAtRules(check);
  42. /**
  43. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  44. */
  45. function check(statement) {
  46. // Return early if blockless or has an empty block
  47. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  48. return;
  49. }
  50. const source = beforeBlockString(statement);
  51. const beforeBraceNoRaw = beforeBlockString(statement, {
  52. noRawBefore: true,
  53. });
  54. let index = beforeBraceNoRaw.length - 1;
  55. if (beforeBraceNoRaw[index - 1] === '\r') {
  56. index -= 1;
  57. }
  58. checker.beforeAllowingIndentation({
  59. lineCheckStr: blockString(statement),
  60. source,
  61. index: source.length,
  62. err: (m) => {
  63. if (context.fix) {
  64. const statementRaws = statement.raws;
  65. if (typeof statementRaws.between !== 'string') return;
  66. if (primary.startsWith('always')) {
  67. const spaceIndex = statementRaws.between.search(/\s+$/);
  68. if (spaceIndex >= 0) {
  69. statement.raws.between =
  70. statementRaws.between.slice(0, spaceIndex) +
  71. context.newline +
  72. statementRaws.between.slice(spaceIndex);
  73. } else {
  74. statementRaws.between += context.newline;
  75. }
  76. return;
  77. }
  78. if (primary.startsWith('never')) {
  79. statementRaws.between = statementRaws.between.replace(/\s*$/, '');
  80. return;
  81. }
  82. }
  83. report({
  84. message: m,
  85. node: statement,
  86. index,
  87. result,
  88. ruleName,
  89. });
  90. },
  91. });
  92. }
  93. };
  94. };
  95. rule.ruleName = ruleName;
  96. rule.messages = messages;
  97. rule.meta = meta;
  98. module.exports = rule;