index.js 2.7 KB

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