index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/list/declaration-block-semicolon-newline-after',
  18. };
  19. /** @type {import('stylelint').Rule} */
  20. const rule = (primary, _secondaryOptions, context) => {
  21. const checker = whitespaceChecker('newline', primary, messages);
  22. return (root, result) => {
  23. const validOptions = validateOptions(result, ruleName, {
  24. actual: primary,
  25. possible: ['always', 'always-multi-line', 'never-multi-line'],
  26. });
  27. if (!validOptions) {
  28. return;
  29. }
  30. root.walkDecls((decl) => {
  31. // Ignore last declaration if there's no trailing semicolon
  32. const parentRule = decl.parent;
  33. if (!parentRule) throw new Error('A parent node must be present');
  34. if (!isAtRule(parentRule) && !isRule(parentRule)) {
  35. return;
  36. }
  37. if (!parentRule.raws.semicolon && parentRule.last === decl) {
  38. return;
  39. }
  40. const nextNode = decl.next();
  41. if (!nextNode) {
  42. return;
  43. }
  44. // Allow end-of-line comment
  45. const nodeToCheck = nextNonCommentNode(nextNode);
  46. if (!nodeToCheck) {
  47. return;
  48. }
  49. checker.afterOneOnly({
  50. source: rawNodeString(nodeToCheck),
  51. index: -1,
  52. lineCheckStr: blockString(parentRule),
  53. err: (m) => {
  54. if (context.fix) {
  55. if (primary.startsWith('always')) {
  56. const index = nodeToCheck.raws.before.search(/\r?\n/);
  57. nodeToCheck.raws.before =
  58. index >= 0
  59. ? nodeToCheck.raws.before.slice(index)
  60. : context.newline + nodeToCheck.raws.before;
  61. return;
  62. }
  63. if (primary === 'never-multi-line') {
  64. nodeToCheck.raws.before = '';
  65. return;
  66. }
  67. }
  68. report({
  69. message: m,
  70. node: decl,
  71. index: decl.toString().length + 1,
  72. result,
  73. ruleName,
  74. });
  75. },
  76. });
  77. });
  78. };
  79. };
  80. rule.ruleName = ruleName;
  81. rule.messages = messages;
  82. rule.meta = meta;
  83. module.exports = rule;