index.js 3.2 KB

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