index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 _utils = require("../../utils");
  8. var _stylelint = require("stylelint");
  9. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  10. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  11. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  12. var ruleName = (0, _utils.namespace)("dollar-variable-first-in-block");
  13. exports.ruleName = ruleName;
  14. var messages = _stylelint.utils.ruleMessages(ruleName, {
  15. expected: "Expected $-variable to be first in block"
  16. });
  17. exports.messages = messages;
  18. var meta = {
  19. url: (0, _utils.ruleUrl)(ruleName)
  20. };
  21. exports.meta = meta;
  22. function rule(primary, options) {
  23. return function (root, result) {
  24. var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
  25. actual: primary
  26. }, {
  27. actual: options,
  28. possible: {
  29. ignore: ["comments", "imports"],
  30. except: ["root", "at-rule", "function", "mixin", "if-else", "loops"]
  31. },
  32. optional: true
  33. });
  34. if (!validOptions) {
  35. return;
  36. }
  37. var isDollarVar = function isDollarVar(node) {
  38. return node.prop && node.prop[0] === "$";
  39. };
  40. root.walkDecls(function (decl) {
  41. // Ignore declarations that aren't variables.
  42. // ------------------------------------------
  43. if (!isDollarVar(decl)) {
  44. return;
  45. }
  46. // If selected, ignore declarations in root.
  47. // -----------------------------------------
  48. if ((0, _utils.optionsHaveException)(options, "root") && decl.parent === root) {
  49. return;
  50. }
  51. // If selected, ignore declarations in different types of at-rules.
  52. // ----------------------------------------------------------------
  53. if (decl.parent.type === "atrule") {
  54. if ((0, _utils.optionsHaveException)(options, "at-rule") || (0, _utils.optionsHaveException)(options, "function") && decl.parent.name === "function" || (0, _utils.optionsHaveException)(options, "mixin") && decl.parent.name === "mixin" || (0, _utils.optionsHaveException)(options, "if-else") && (decl.parent.name === "if" || decl.parent.name === "else") || (0, _utils.optionsHaveException)(options, "loops") && (decl.parent.name === "each" || decl.parent.name === "for" || decl.parent.name === "while")) {
  55. return;
  56. }
  57. }
  58. var previous = decl.prev();
  59. // If first or preceded by another variable.
  60. // -----------------------------------------
  61. if (!previous || isDollarVar(previous)) {
  62. return;
  63. }
  64. // Check if preceded only by allowed types.
  65. // ----------------------------------------
  66. var precededOnlyByAllowed = true;
  67. var allowComments = (0, _utils.optionsHaveIgnored)(options, "comments");
  68. var allowImports = (0, _utils.optionsHaveIgnored)(options, "imports");
  69. var importAtRules = ["import", "use", "forward"];
  70. var _iterator = _createForOfIteratorHelper(decl.parent.nodes),
  71. _step;
  72. try {
  73. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  74. var sibling = _step.value;
  75. if (sibling === decl) {
  76. break;
  77. } else if (!isDollarVar(sibling) && !(allowComments && sibling.type === "comment" || allowImports && sibling.type === "atrule" && importAtRules.includes(sibling.name))) {
  78. precededOnlyByAllowed = false;
  79. }
  80. }
  81. } catch (err) {
  82. _iterator.e(err);
  83. } finally {
  84. _iterator.f();
  85. }
  86. if (precededOnlyByAllowed) {
  87. return;
  88. }
  89. _stylelint.utils.report({
  90. message: messages.expected,
  91. node: decl,
  92. result: result,
  93. ruleName: ruleName
  94. });
  95. });
  96. };
  97. }
  98. rule.ruleName = ruleName;
  99. rule.messages = messages;
  100. rule.meta = meta;