index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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)("dollar-variable-empty-line-after");
  10. exports.ruleName = ruleName;
  11. var messages = _stylelint.utils.ruleMessages(ruleName, {
  12. expected: "Expected an empty line after $-variable",
  13. rejected: "Unexpected empty line after $-variable"
  14. });
  15. exports.messages = messages;
  16. var meta = {
  17. url: (0, _utils.ruleUrl)(ruleName)
  18. };
  19. exports.meta = meta;
  20. function rule(expectation, options, context) {
  21. return function (root, result) {
  22. var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
  23. actual: expectation,
  24. possible: ["always", "never"]
  25. }, {
  26. actual: options,
  27. possible: {
  28. except: ["last-nested", "before-comment", "before-dollar-variable"],
  29. ignore: ["before-comment", "inside-single-line-block"],
  30. disableFix: _utils.isBoolean
  31. },
  32. optional: true
  33. });
  34. if (!validOptions) {
  35. return;
  36. }
  37. var fixNext = function fixNext(decl, match, replace) {
  38. decl.raws.before = decl.raws.before.replace(new RegExp("^".concat(match)), replace);
  39. };
  40. var fixParent = function fixParent(decl, match, replace) {
  41. decl.parent.raws.after = decl.parent.raws.after.replace(new RegExp("^".concat(match)), replace);
  42. };
  43. var hasNewline = function hasNewline(str) {
  44. return str.indexOf(context.newline) > -1;
  45. };
  46. var isDollarVar = function isDollarVar(node) {
  47. return node.prop && node.prop[0] === "$";
  48. };
  49. root.walkDecls(function (decl) {
  50. var expectEmptyLineAfter = expectation === "always";
  51. var exceptLastNested = (0, _utils.optionsHaveException)(options, "last-nested");
  52. var exceptBeforeComment = (0, _utils.optionsHaveException)(options, "before-comment");
  53. var exceptBeforeVariable = (0, _utils.optionsHaveException)(options, "before-dollar-variable");
  54. var ignoreInsideSingleLineBlock = (0, _utils.optionsHaveIgnored)(options, "inside-single-line-block");
  55. var ignoreBeforeComment = (0, _utils.optionsHaveIgnored)(options, "before-comment");
  56. var isSingleLineDeclaration = (0, _utils.isSingleLineString)((0, _utils.blockString)(decl.parent));
  57. // Ignore declarations that aren't variables.
  58. // ------------------------------------------
  59. if (!isDollarVar(decl)) {
  60. return;
  61. }
  62. // Ignore declaration if it's the last line in a file.
  63. // ---------------------------------------------------
  64. if (decl === root.last) {
  65. return;
  66. }
  67. // Ignore single line blocks (if chosen as an option).
  68. // ---------------------------------------------------
  69. if (ignoreInsideSingleLineBlock && decl.parent.type !== "root" && isSingleLineDeclaration) {
  70. return;
  71. }
  72. var next = decl.next();
  73. // The declaration is the last in a block.
  74. // ---------------------------------------
  75. if (!next) {
  76. var hasEmptyLineAfter = (0, _utils.hasEmptyLine)(decl.parent.raws.after);
  77. if (expectEmptyLineAfter && hasEmptyLineAfter && !exceptLastNested || !expectEmptyLineAfter && !hasEmptyLineAfter && !exceptLastNested || expectEmptyLineAfter && !hasEmptyLineAfter && exceptLastNested || !expectEmptyLineAfter && hasEmptyLineAfter && exceptLastNested) {
  78. return;
  79. }
  80. }
  81. // The declaration is NOT the last in a block.
  82. // -------------------------------------------
  83. else {
  84. var _hasEmptyLineAfter = (0, _utils.hasEmptyLine)(next.raws.before);
  85. var nextIsComment = next.type === "comment";
  86. var nextIsVariable = isDollarVar(next);
  87. if (nextIsComment) {
  88. if (ignoreBeforeComment || expectEmptyLineAfter && _hasEmptyLineAfter && !exceptBeforeComment || !expectEmptyLineAfter && !_hasEmptyLineAfter && !exceptBeforeComment || expectEmptyLineAfter && !_hasEmptyLineAfter && exceptBeforeComment || !expectEmptyLineAfter && _hasEmptyLineAfter && exceptBeforeComment) {
  89. return;
  90. }
  91. } else if (nextIsVariable) {
  92. if (expectEmptyLineAfter && _hasEmptyLineAfter && !exceptBeforeVariable || !expectEmptyLineAfter && !_hasEmptyLineAfter && !exceptBeforeVariable || expectEmptyLineAfter && !_hasEmptyLineAfter && exceptBeforeVariable || !expectEmptyLineAfter && _hasEmptyLineAfter && exceptBeforeVariable || expectEmptyLineAfter && _hasEmptyLineAfter && exceptBeforeVariable) {
  93. return;
  94. }
  95. } else if (expectEmptyLineAfter === _hasEmptyLineAfter) {
  96. return;
  97. }
  98. }
  99. var isFixDisabled = options && options.disableFix === true;
  100. if (context.fix && !isFixDisabled) {
  101. if (next) {
  102. var nextBefore = next.raws.before;
  103. var _hasEmptyLineAfter2 = (0, _utils.hasEmptyLine)(nextBefore);
  104. var _nextIsComment = next.type === "comment";
  105. var _nextIsVariable = isDollarVar(next);
  106. if (expectEmptyLineAfter && !_hasEmptyLineAfter2) {
  107. fixNext(next, context.newline, context.newline + context.newline);
  108. if (exceptLastNested && !hasNewline(nextBefore)) {
  109. fixNext(next, "\\s+", context.newline + context.newline);
  110. }
  111. return;
  112. } else if (expectEmptyLineAfter && exceptBeforeComment && _nextIsComment && _hasEmptyLineAfter2 || expectEmptyLineAfter && exceptBeforeVariable && _nextIsVariable && _hasEmptyLineAfter2 || !expectEmptyLineAfter && _hasEmptyLineAfter2) {
  113. fixNext(decl, "\\n\\r\\n", "\r\n");
  114. fixNext(next, context.newline + context.newline, context.newline);
  115. return;
  116. } else if (!expectEmptyLineAfter && exceptBeforeComment && _nextIsComment && !_hasEmptyLineAfter2 || !expectEmptyLineAfter && exceptBeforeVariable && _nextIsVariable && !_hasEmptyLineAfter2) {
  117. fixNext(next, context.newline, context.newline + context.newline);
  118. return;
  119. }
  120. } else {
  121. var _hasEmptyLineAfter3 = (0, _utils.hasEmptyLine)(decl.parent.raws.after);
  122. expectEmptyLineAfter = exceptLastNested ? !expectEmptyLineAfter : expectEmptyLineAfter;
  123. if (expectEmptyLineAfter && !_hasEmptyLineAfter3) {
  124. fixParent(decl, context.newline, context.newline + context.newline);
  125. return;
  126. } else if (!expectEmptyLineAfter && _hasEmptyLineAfter3) {
  127. fixParent(decl, "\\n\\r\\n", "\r\n");
  128. fixParent(decl, context.newline + context.newline, context.newline);
  129. return;
  130. }
  131. }
  132. }
  133. _stylelint.utils.report({
  134. message: expectEmptyLineAfter ? messages.expected : messages.rejected,
  135. node: decl,
  136. result: result,
  137. ruleName: ruleName
  138. });
  139. });
  140. };
  141. }
  142. rule.ruleName = ruleName;
  143. rule.messages = messages;
  144. rule.meta = meta;