index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. const blockString = require('../../utils/blockString');
  3. const nextNonCommentNode = require('../../utils/nextNonCommentNode');
  4. const rawNodeString = require('../../utils/rawNodeString');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const whitespaceChecker = require('../../utils/whitespaceChecker');
  9. const { isAtRule, isRule } = require('../../utils/typeGuards');
  10. const ruleName = 'declaration-block-semicolon-newline-after';
  11. const messages = ruleMessages(ruleName, {
  12. expectedAfter: () => 'Expected newline after ";"',
  13. expectedAfterMultiLine: () => 'Expected newline after ";" in a multi-line declaration block',
  14. rejectedAfterMultiLine: () => 'Unexpected newline after ";" in a multi-line declaration block',
  15. });
  16. const meta = {
  17. url: 'https://stylelint.io/user-guide/rules/declaration-block-semicolon-newline-after',
  18. fixable: true,
  19. };
  20. /** @type {import('stylelint').Rule} */
  21. const rule = (primary, _secondaryOptions, context) => {
  22. const checker = whitespaceChecker('newline', primary, messages);
  23. return (root, result) => {
  24. const validOptions = validateOptions(result, ruleName, {
  25. actual: primary,
  26. possible: ['always', 'always-multi-line', 'never-multi-line'],
  27. });
  28. if (!validOptions) {
  29. return;
  30. }
  31. root.walkDecls((decl) => {
  32. // Ignore last declaration if there's no trailing semicolon
  33. const parentRule = decl.parent;
  34. if (!parentRule) throw new Error('A parent node must be present');
  35. if (!isAtRule(parentRule) && !isRule(parentRule)) {
  36. return;
  37. }
  38. if (!parentRule.raws.semicolon && parentRule.last === decl) {
  39. return;
  40. }
  41. const nextNode = decl.next();
  42. if (!nextNode) {
  43. return;
  44. }
  45. // Allow end-of-line comment
  46. const nodeToCheck = nextNonCommentNode(nextNode);
  47. if (!nodeToCheck) {
  48. return;
  49. }
  50. checker.afterOneOnly({
  51. source: rawNodeString(nodeToCheck),
  52. index: -1,
  53. lineCheckStr: blockString(parentRule),
  54. err: (m) => {
  55. if (context.fix) {
  56. if (primary.startsWith('always')) {
  57. const index = nodeToCheck.raws.before.search(/\r?\n/);
  58. nodeToCheck.raws.before =
  59. index >= 0
  60. ? nodeToCheck.raws.before.slice(index)
  61. : context.newline + nodeToCheck.raws.before;
  62. return;
  63. }
  64. if (primary === 'never-multi-line') {
  65. nodeToCheck.raws.before = '';
  66. return;
  67. }
  68. }
  69. report({
  70. message: m,
  71. node: decl,
  72. index: decl.toString().length + 1,
  73. result,
  74. ruleName,
  75. });
  76. },
  77. });
  78. });
  79. };
  80. };
  81. rule.ruleName = ruleName;
  82. rule.messages = messages;
  83. rule.meta = meta;
  84. module.exports = rule;