index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. const hasBlock = require('../../utils/hasBlock');
  3. const optionsMatches = require('../../utils/optionsMatches');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const { isAtRule } = require('../../utils/typeGuards');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const ruleName = 'declaration-block-trailing-semicolon';
  9. const messages = ruleMessages(ruleName, {
  10. expected: 'Expected a trailing semicolon',
  11. rejected: 'Unexpected trailing semicolon',
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/declaration-block-trailing-semicolon',
  15. fixable: true,
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary, secondaryOptions, context) => {
  19. return (root, result) => {
  20. const validOptions = validateOptions(
  21. result,
  22. ruleName,
  23. {
  24. actual: primary,
  25. possible: ['always', 'never'],
  26. },
  27. {
  28. actual: secondaryOptions,
  29. possible: {
  30. ignore: ['single-declaration'],
  31. },
  32. optional: true,
  33. },
  34. );
  35. if (!validOptions) {
  36. return;
  37. }
  38. root.walkAtRules((atRule) => {
  39. if (!atRule.parent) throw new Error('A parent node must be present');
  40. if (atRule.parent === root) {
  41. return;
  42. }
  43. if (atRule !== atRule.parent.last) {
  44. return;
  45. }
  46. if (hasBlock(atRule)) {
  47. return;
  48. }
  49. checkLastNode(atRule);
  50. });
  51. root.walkDecls((decl) => {
  52. if (!decl.parent) throw new Error('A parent node must be present');
  53. if (decl.parent.type === 'object') {
  54. return;
  55. }
  56. if (decl !== decl.parent.last) {
  57. return;
  58. }
  59. checkLastNode(decl);
  60. });
  61. /**
  62. * @param {import('postcss').Node} node
  63. */
  64. function checkLastNode(node) {
  65. if (!node.parent) throw new Error('A parent node must be present');
  66. const hasSemicolon = node.parent.raws.semicolon;
  67. const ignoreSingleDeclaration = optionsMatches(
  68. secondaryOptions,
  69. 'ignore',
  70. 'single-declaration',
  71. );
  72. if (ignoreSingleDeclaration && node.parent.first === node) {
  73. return;
  74. }
  75. let message;
  76. if (primary === 'always') {
  77. if (hasSemicolon) {
  78. return;
  79. }
  80. // auto-fix
  81. if (context.fix) {
  82. node.parent.raws.semicolon = true;
  83. if (isAtRule(node)) {
  84. node.raws.between = '';
  85. node.parent.raws.after = ' ';
  86. }
  87. return;
  88. }
  89. message = messages.expected;
  90. } else if (primary === 'never') {
  91. if (!hasSemicolon) {
  92. return;
  93. }
  94. // auto-fix
  95. if (context.fix) {
  96. node.parent.raws.semicolon = false;
  97. return;
  98. }
  99. message = messages.rejected;
  100. } else {
  101. throw new Error(`Unexpected primary option: "${primary}"`);
  102. }
  103. report({
  104. message,
  105. node,
  106. index: node.toString().trim().length - 1,
  107. result,
  108. ruleName,
  109. });
  110. }
  111. };
  112. };
  113. rule.ruleName = ruleName;
  114. rule.messages = messages;
  115. rule.meta = meta;
  116. module.exports = rule;