index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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-before");
  10. exports.ruleName = ruleName;
  11. var messages = _stylelint.utils.ruleMessages(ruleName, {
  12. expected: "Expected an empty line before $-variable",
  13. rejected: "Unexpected empty line before $-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: ["first-nested", "after-comment", "after-dollar-variable"],
  29. ignore: ["after-comment", "inside-single-line-block", "after-dollar-variable"],
  30. disableFix: _utils.isBoolean
  31. },
  32. optional: true
  33. });
  34. if (!validOptions) {
  35. return;
  36. }
  37. var fix = function fix(decl, match, replace) {
  38. decl.raws.before = decl.raws.before.replace(new RegExp("^".concat(match)), replace);
  39. };
  40. var hasNewline = function hasNewline(str) {
  41. return str.includes(context.newline);
  42. };
  43. root.walkDecls(function (decl) {
  44. if (!isDollarVar(decl)) {
  45. return;
  46. }
  47. // Always ignore the first $var in a stylesheet
  48. if (decl === root.first) {
  49. return;
  50. }
  51. // If ignoring vars after comments is set
  52. if ((0, _utils.optionsHaveIgnored)(options, "after-comment") && decl.prev() && decl.prev().type === "comment") {
  53. return;
  54. }
  55. // If ignoring single-line blocks
  56. if ((0, _utils.optionsHaveIgnored)(options, "inside-single-line-block") && decl.parent.type !== "root" && (0, _utils.isSingleLineString)((0, _utils.blockString)(decl.parent))) {
  57. return;
  58. }
  59. // if ignoring after another $-variable
  60. if ((0, _utils.optionsHaveIgnored)(options, "after-dollar-variable") && decl.prev() && isDollarVar(decl.prev())) {
  61. return;
  62. }
  63. var expectHasEmptyLineBefore = expectation === "always";
  64. // Reverse for a variable that is a first child of its parent
  65. if ((0, _utils.optionsHaveException)(options, "first-nested") && decl === decl.parent.first) {
  66. expectHasEmptyLineBefore = !expectHasEmptyLineBefore;
  67. }
  68. // Reverse if after a comment
  69. if ((0, _utils.optionsHaveException)(options, "after-comment") && decl.prev() && decl.prev().type === "comment") {
  70. expectHasEmptyLineBefore = !expectHasEmptyLineBefore;
  71. }
  72. // Reverse if after another $-variable
  73. if ((0, _utils.optionsHaveException)(options, "after-dollar-variable") && decl.prev() && isDollarVar(decl.prev())) {
  74. expectHasEmptyLineBefore = !expectHasEmptyLineBefore;
  75. }
  76. var before = decl.raws.before;
  77. if (expectHasEmptyLineBefore === (0, _utils.hasEmptyLine)(before)) {
  78. return;
  79. }
  80. var isFixDisabled = options && options.disableFix === true;
  81. if (context.fix && !isFixDisabled) {
  82. if (expectHasEmptyLineBefore && !(0, _utils.hasEmptyLine)(before)) {
  83. fix(decl, context.newline, context.newline + context.newline);
  84. if ((0, _utils.optionsHaveException)(options, "first-nested") && !hasNewline(before)) {
  85. fix(decl, "\\s+", context.newline + context.newline);
  86. }
  87. return;
  88. }
  89. if (!expectHasEmptyLineBefore && (0, _utils.hasEmptyLine)(before)) {
  90. fix(decl, "\\n\\r\\n", "\r\n");
  91. fix(decl, context.newline + context.newline, context.newline);
  92. return;
  93. }
  94. }
  95. _stylelint.utils.report({
  96. message: expectHasEmptyLineBefore ? messages.expected : messages.rejected,
  97. node: decl,
  98. result: result,
  99. ruleName: ruleName
  100. });
  101. });
  102. };
  103. }
  104. rule.ruleName = ruleName;
  105. rule.messages = messages;
  106. rule.meta = meta;
  107. function isDollarVar(node) {
  108. return node.prop && node.prop[0] === "$";
  109. }