index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = rule;
  6. exports.ruleName = exports.meta = exports.messages = void 0;
  7. var _stylelint = require("stylelint");
  8. var _utils = require("../../utils");
  9. var ruleName = (0, _utils.namespace)("double-slash-comment-empty-line-before");
  10. exports.ruleName = ruleName;
  11. var messages = _stylelint.utils.ruleMessages(ruleName, {
  12. expected: "Expected empty line before comment",
  13. rejected: "Unexpected empty line before comment"
  14. });
  15. exports.messages = messages;
  16. var meta = {
  17. url: (0, _utils.ruleUrl)(ruleName)
  18. };
  19. exports.meta = meta;
  20. var stylelintCommandPrefix = "stylelint-";
  21. function rule(expectation, options, context) {
  22. return function (root, result) {
  23. var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
  24. actual: expectation,
  25. possible: ["always", "never"]
  26. }, {
  27. actual: options,
  28. possible: {
  29. except: ["first-nested", "inside-block"],
  30. ignore: ["stylelint-commands", "between-comments", "inside-block"]
  31. },
  32. optional: true
  33. });
  34. if (!validOptions) {
  35. return;
  36. }
  37. root.walkComments(function (comment) {
  38. // Only process // comments
  39. if (!comment.raws.inline && !comment.inline) {
  40. return;
  41. }
  42. if ((0, _utils.isInlineComment)(comment)) {
  43. return;
  44. }
  45. // Ignore the first node
  46. if (comment === root.first) {
  47. return;
  48. }
  49. // Optionally ignore stylelint commands
  50. if (comment.text.indexOf(stylelintCommandPrefix) === 0 && (0, _utils.optionsHaveIgnored)(options, "stylelint-commands")) {
  51. return;
  52. }
  53. // Optionally ignore comments inside blocks
  54. if (comment.parent !== root && (0, _utils.optionsHaveIgnored)(options, "inside-block")) {
  55. return;
  56. }
  57. // Optionally ignore newlines between comments
  58. var prev = comment.prev();
  59. if (prev && prev.type === "comment" && (0, _utils.optionsHaveIgnored)(options, "between-comments")) {
  60. return;
  61. }
  62. var before = comment.raw("before");
  63. var expectEmptyLineBefore = function () {
  64. if ((0, _utils.optionsHaveException)(options, "first-nested") && comment.parent !== root && comment === comment.parent.first) {
  65. return false;
  66. }
  67. // Reverse expectation for comments inside blocks
  68. if (comment.parent !== root && (0, _utils.optionsHaveException)(options, "inside-block")) {
  69. return expectation === "never";
  70. }
  71. return expectation === "always";
  72. }();
  73. var hasEmptyLineBefore = before.search(/\n\s*?\n/) !== -1;
  74. // Return if the expectation is met
  75. if (expectEmptyLineBefore === hasEmptyLineBefore) {
  76. return;
  77. }
  78. if (context.fix) {
  79. if (expectEmptyLineBefore && !hasEmptyLineBefore) {
  80. (0, _utils.addEmptyLineBefore)(comment, context.newline);
  81. return;
  82. }
  83. if (!expectEmptyLineBefore && hasEmptyLineBefore) {
  84. (0, _utils.removeEmptyLinesBefore)(comment, context.newline);
  85. return;
  86. }
  87. }
  88. var message = expectEmptyLineBefore ? messages.expected : messages.rejected;
  89. _stylelint.utils.report({
  90. message: message,
  91. node: comment,
  92. result: result,
  93. ruleName: ruleName
  94. });
  95. });
  96. };
  97. }
  98. rule.ruleName = ruleName;
  99. rule.messages = messages;
  100. rule.meta = meta;