index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. 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)("no-duplicate-dollar-variables");
  13. exports.ruleName = ruleName;
  14. var messages = _stylelint.utils.ruleMessages(ruleName, {
  15. rejected: function rejected(variable) {
  16. return "Unexpected duplicate dollar variable ".concat(variable);
  17. }
  18. });
  19. exports.messages = messages;
  20. var meta = {
  21. url: (0, _utils.ruleUrl)(ruleName)
  22. };
  23. exports.meta = meta;
  24. function rule(value, secondaryOptions) {
  25. return function (root, result) {
  26. var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
  27. actual: value
  28. }, {
  29. actual: secondaryOptions,
  30. possible: {
  31. ignoreInside: ["at-rule", "nested-at-rule"],
  32. ignoreInsideAtRules: [_utils.isString],
  33. ignoreDefaults: [_utils.isBoolean]
  34. },
  35. optional: true
  36. });
  37. if (!validOptions) {
  38. return;
  39. }
  40. var vars = {};
  41. /**
  42. * Traverse the [vars] tree through the path defined by [ancestors], creating nodes as needed.
  43. * @param {*} ancestors
  44. * @returns the tree of the node defined by the last of [ancestors].
  45. */
  46. function getScope(ancestors) {
  47. var scope = vars;
  48. var _iterator = _createForOfIteratorHelper(ancestors),
  49. _step;
  50. try {
  51. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  52. var node = _step.value;
  53. if (!(node in scope)) {
  54. scope[node] = {};
  55. }
  56. scope = scope[node];
  57. }
  58. } catch (err) {
  59. _iterator.e(err);
  60. } finally {
  61. _iterator.f();
  62. }
  63. return scope;
  64. }
  65. /**
  66. * Iterates through the ancestors while checking each scope until the [variable] is found.
  67. * If not found, an object with empty values is returned.
  68. * @param {*} ancestors
  69. * @param {string} variable the variable name.
  70. * @returns The previously declared variable data or an object with empty values.
  71. */
  72. function getVariableData(ancestors, variable) {
  73. var scope = vars;
  74. var _iterator2 = _createForOfIteratorHelper(ancestors),
  75. _step2;
  76. try {
  77. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  78. var node = _step2.value;
  79. scope = scope[node];
  80. if (scope[variable]) {
  81. return scope[variable];
  82. }
  83. }
  84. } catch (err) {
  85. _iterator2.e(err);
  86. } finally {
  87. _iterator2.f();
  88. }
  89. return {
  90. defaultCount: 0,
  91. isDeclared: false
  92. };
  93. }
  94. /**
  95. * Checks whether the given [variableData] is declared.
  96. * @param {{ defaultCount: number; isDeclared: boolean; }} variableData the variable data
  97. * containing default count and if the variable is declared.
  98. * @param {boolean} isDefault if the variable contains the `!default` keyword.
  99. * @param {boolean | number} ignoreDefaults the ignore defaults options.
  100. * @returns true if declared.
  101. */
  102. function isDeclared(variableData, isDefault, ignoreDefaults) {
  103. if (isDefault) {
  104. if (Number.isFinite(ignoreDefaults)) {
  105. return variableData.defaultCount >= ignoreDefaults;
  106. } else if (ignoreDefaults) {
  107. return false;
  108. }
  109. }
  110. return variableData.isDeclared;
  111. }
  112. /**
  113. * Processes the variable data based on the given arguments.
  114. * @param {{ defaultCount: number; isDeclared: boolean; }} variableData the variable data
  115. * containing default count and if the variable is declared.
  116. * @param {boolean} isDefault if the variable contains the `!default` keyword.
  117. * @param {boolean | number} ignoreDefaults the ignore defaults options.
  118. * @returns the updated `variableData`.
  119. */
  120. function processVariableData(variableData, isDefault, ignoreDefaults) {
  121. return {
  122. defaultCount: isDefault ? ++variableData.defaultCount : variableData.defaultCount,
  123. isDeclared: isDefault && ignoreDefaults !== false ? variableData.isDeclared : true
  124. };
  125. }
  126. var ignoreDefaults = secondaryOptions && secondaryOptions.ignoreDefaults !== undefined ? secondaryOptions.ignoreDefaults : 1;
  127. root.walkDecls(function (decl) {
  128. var isVar = decl.prop[0] === "$";
  129. var isInsideIgnoredAtRule = decl.parent.type === "atrule" && secondaryOptions && secondaryOptions.ignoreInside && secondaryOptions.ignoreInside === "at-rule";
  130. var isInsideIgnoredNestedAtRule = decl.parent.type === "atrule" && decl.parent.parent.type !== "root" && secondaryOptions && secondaryOptions.ignoreInside && secondaryOptions.ignoreInside === "nested-at-rule";
  131. var isInsideIgnoredSpecifiedAtRule = decl.parent.type === "atrule" && secondaryOptions && secondaryOptions.ignoreInsideAtRules && secondaryOptions.ignoreInsideAtRules.includes(decl.parent.name);
  132. if (!isVar || isInsideIgnoredAtRule || isInsideIgnoredNestedAtRule || isInsideIgnoredSpecifiedAtRule) {
  133. return;
  134. }
  135. var ancestors = [];
  136. var parent = decl.parent;
  137. while (parent !== null && parent !== undefined) {
  138. var parentKey = parent.toString();
  139. ancestors.unshift(parentKey);
  140. parent = parent.parent;
  141. }
  142. var scope = getScope(ancestors);
  143. var isDefault = /!default/.test(decl.value);
  144. var variableData = getVariableData(ancestors, decl.prop);
  145. if (isDeclared(variableData, isDefault, ignoreDefaults)) {
  146. _stylelint.utils.report({
  147. message: messages.rejected(decl.prop),
  148. node: decl,
  149. result: result,
  150. ruleName: ruleName
  151. });
  152. }
  153. scope[decl.prop] = processVariableData(variableData, isDefault, ignoreDefaults);
  154. });
  155. };
  156. }
  157. rule.ruleName = ruleName;
  158. rule.messages = messages;
  159. rule.meta = meta;