index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. };
  21. /** @type {import('stylelint').Rule} */
  22. const rule = (primary, _secondaryOptions, context) => {
  23. const checker = whitespaceChecker('newline', primary, messages);
  24. return (root, result) => {
  25. const validOptions = validateOptions(result, ruleName, {
  26. actual: primary,
  27. possible: [
  28. 'always',
  29. 'always-single-line',
  30. 'never-single-line',
  31. 'always-multi-line',
  32. 'never-multi-line',
  33. ],
  34. });
  35. if (!validOptions) {
  36. return;
  37. }
  38. // Check both kinds of statement: rules and at-rules
  39. root.walkRules(check);
  40. root.walkAtRules(check);
  41. /**
  42. * @param {import('postcss').Rule | import('postcss').AtRule} statement
  43. */
  44. function check(statement) {
  45. // Return early if blockless or has an empty block
  46. if (!hasBlock(statement) || hasEmptyBlock(statement)) {
  47. return;
  48. }
  49. const source = beforeBlockString(statement);
  50. const beforeBraceNoRaw = beforeBlockString(statement, {
  51. noRawBefore: true,
  52. });
  53. let index = beforeBraceNoRaw.length - 1;
  54. if (beforeBraceNoRaw[index - 1] === '\r') {
  55. index -= 1;
  56. }
  57. checker.beforeAllowingIndentation({
  58. lineCheckStr: blockString(statement),
  59. source,
  60. index: source.length,
  61. err: (m) => {
  62. if (context.fix) {
  63. const statementRaws = statement.raws;
  64. if (typeof statementRaws.between !== 'string') return;
  65. if (primary.startsWith('always')) {
  66. const spaceIndex = statementRaws.between.search(/\s+$/);
  67. if (spaceIndex >= 0) {
  68. statement.raws.between =
  69. statementRaws.between.slice(0, spaceIndex) +
  70. context.newline +
  71. statementRaws.between.slice(spaceIndex);
  72. } else {
  73. statementRaws.between += context.newline;
  74. }
  75. return;
  76. }
  77. if (primary.startsWith('never')) {
  78. statementRaws.between = statementRaws.between.replace(/\s*$/, '');
  79. return;
  80. }
  81. }
  82. report({
  83. message: m,
  84. node: statement,
  85. index,
  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;