index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  10. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  11. 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); }
  12. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  13. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  14. 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; }
  15. var ruleName = (0, _utils.namespace)("at-mixin-named-arguments");
  16. exports.ruleName = ruleName;
  17. var messages = _stylelint.utils.ruleMessages(ruleName, {
  18. expected: "Expected a named parameter to be used in at-include call",
  19. rejected: "Unexpected a named parameter in at-include call"
  20. });
  21. exports.messages = messages;
  22. var meta = {
  23. url: (0, _utils.ruleUrl)(ruleName)
  24. };
  25. exports.meta = meta;
  26. var hasArgumentsRegExp = /\((.*)\)$/;
  27. var isScssVarRegExp = /^\$\S*/;
  28. function rule(expectation, options) {
  29. return function (root, result) {
  30. var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
  31. actual: expectation,
  32. possible: ["always", "never"]
  33. }, {
  34. actual: options,
  35. possible: {
  36. ignore: ["single-argument"]
  37. },
  38. optional: true
  39. });
  40. if (!validOptions) {
  41. return;
  42. }
  43. var shouldIgnoreSingleArgument = (0, _utils.optionsHaveIgnored)(options, "single-argument");
  44. root.walkAtRules("include", function (atRule) {
  45. var argsString = atRule.params.replace(/\n/g, " ").match(hasArgumentsRegExp);
  46. // Ignore @include that does not contain arguments.
  47. if (!argsString || argsString.index === -1 || argsString[0].length === 2) {
  48. return;
  49. }
  50. var args = argsString[1]
  51. // Create array of arguments.
  52. .split(",")
  53. // Create a key-value array for every argument.
  54. .map(function (argsString) {
  55. return argsString.split(":").map(function (argsKeyValuePair) {
  56. return argsKeyValuePair.trim();
  57. });
  58. }).reduce(function (resultArray, keyValuePair) {
  59. var pair = {
  60. value: keyValuePair[1] || keyValuePair[0]
  61. };
  62. if (keyValuePair[1]) {
  63. pair.key = keyValuePair[0];
  64. }
  65. return [].concat(_toConsumableArray(resultArray), [pair]);
  66. }, []);
  67. var isSingleArgument = args.length === 1;
  68. if (isSingleArgument && shouldIgnoreSingleArgument) {
  69. return;
  70. }
  71. args.forEach(function (arg) {
  72. switch (expectation) {
  73. case "never":
  74. {
  75. if (!arg.key) {
  76. return;
  77. }
  78. _stylelint.utils.report({
  79. message: messages.rejected,
  80. node: atRule,
  81. result: result,
  82. ruleName: ruleName
  83. });
  84. break;
  85. }
  86. case "always":
  87. {
  88. if (arg.key && isScssVarRegExp.test(arg.key)) {
  89. return;
  90. }
  91. _stylelint.utils.report({
  92. message: messages.expected,
  93. node: atRule,
  94. result: result,
  95. ruleName: ruleName
  96. });
  97. break;
  98. }
  99. }
  100. });
  101. });
  102. };
  103. }
  104. rule.ruleName = ruleName;
  105. rule.messages = messages;
  106. rule.meta = meta;