index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
  8. var _stylelint = require("stylelint");
  9. var _utils = require("../../utils");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  11. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  12. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  13. 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); }
  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. function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
  16. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  17. var interpolationPrefix = /^#{\s*/m;
  18. var rules = {
  19. red: "color",
  20. blue: "color",
  21. green: "color",
  22. mix: "color",
  23. hue: "color",
  24. saturation: "color",
  25. lightness: "color",
  26. complement: "color",
  27. "adjust-color": "color",
  28. "scale-color": "color",
  29. "change-color": "color",
  30. "ie-hex-str": "color",
  31. "map-get": "map",
  32. "map-merge": "map",
  33. "map-remove": "map",
  34. "map-keys": "map",
  35. "map-values": "map",
  36. "map-has-key": "map",
  37. unquote: "string",
  38. quote: "string",
  39. "str-length": "string",
  40. "str-insert": "string",
  41. "str-index": "string",
  42. "str-slice": "string",
  43. "to-upper-case": "string",
  44. "to-lower-case": "string",
  45. "unique-id": "string",
  46. percentage: "math",
  47. round: "math",
  48. ceil: "math",
  49. floor: "math",
  50. abs: "math",
  51. random: "math",
  52. unit: "math",
  53. unitless: "math",
  54. comparable: "math",
  55. length: "list",
  56. nth: "list",
  57. "set-nth": "list",
  58. join: "list",
  59. append: "list",
  60. zip: "list",
  61. index: "list",
  62. "list-separator": "list",
  63. "feature-exists": "meta",
  64. "variable-exists": "meta",
  65. "global-variable-exists": "meta",
  66. "function-exists": "meta",
  67. "mixin-exists": "meta",
  68. inspect: "meta",
  69. "get-function": "meta",
  70. "type-of": "meta",
  71. call: "meta",
  72. "content-exists": "meta",
  73. keywords: "meta",
  74. "selector-nest": "selector",
  75. "selector-append": "selector",
  76. "selector-replace": "selector",
  77. "selector-unify": "selector",
  78. "is-superselector": "selector",
  79. "simple-selectors": "selector",
  80. "selector-parse": "selector",
  81. "selector-extend": "selector",
  82. lighten: "color",
  83. "adjust-hue": "color",
  84. darken: "color",
  85. desaturate: "color",
  86. opacify: "color",
  87. transparentize: "color"
  88. };
  89. var new_rule_names = {
  90. "adjust-color": "adjust",
  91. "scale-color": "scale",
  92. "change-color": "change",
  93. "map-get": "get",
  94. "map-merge": "merge",
  95. "map-remove": "remove",
  96. "map-keys": "keys",
  97. "map-values": "values",
  98. "map-has-key": "has-key",
  99. "str-length": "length",
  100. "str-insert": "insert",
  101. "str-index": "index",
  102. "str-slice": "slice",
  103. unitless: "is-unitless",
  104. comparable: "compatible",
  105. "list-separator": "separator",
  106. "selector-nest": "nest",
  107. "selector-append": "append",
  108. "selector-replace": "replace",
  109. "selector-unify": "unify",
  110. "selector-parse": "parse",
  111. "selector-extend": "extend",
  112. lighten: "adjust",
  113. "adjust-hue": "adjust",
  114. darken: "adjust",
  115. desaturate: "adjust",
  116. opacify: "adjust",
  117. saturate: "adjust",
  118. transparentize: "adjust"
  119. };
  120. var rule_mapping = {
  121. lighten: ["lighten($color, $amount)", "adjust($color, $lightness: $amount)"],
  122. "adjust-hue": ["adjust-hue($color, $amount)", "adjust($color, $hue: $amount)"],
  123. darken: ["darken($color, $amount)", "adjust($color, $lightness: -$amount)"],
  124. desaturate: ["desaturate($color, $amount)", "adjust($color, $saturation: -$amount)"],
  125. opacify: ["opacify($color, $amount)", "adjust($color, $alpha: -$amount)"],
  126. saturate: ["saturate($color, $amount)", "adjust($color, $saturation: $amount)"],
  127. transparentize: ["transparentize($color, $amount)", "adjust($color, $alpha: -$amount)"]
  128. };
  129. var ruleName = (0, _utils.namespace)("no-global-function-names");
  130. exports.ruleName = ruleName;
  131. var messages = _stylelint.utils.ruleMessages(ruleName, {
  132. rejectedFullMessage: function rejectedFullMessage(string) {
  133. return string;
  134. },
  135. rejected: function rejected(name) {
  136. return errorMessage(name);
  137. }
  138. });
  139. exports.messages = messages;
  140. var meta = {
  141. url: (0, _utils.ruleUrl)(ruleName)
  142. };
  143. exports.meta = meta;
  144. function errorMessage(name) {
  145. var sass_package = rules[name];
  146. var rename = new_rule_names[name];
  147. var map_rule = rule_mapping[name];
  148. if (rename) {
  149. if (map_rule) {
  150. var _map_rule = _slicedToArray(map_rule, 2),
  151. old_rule = _map_rule[0],
  152. new_rule = _map_rule[1];
  153. return "Expected ".concat(sass_package, ".").concat(new_rule, " instead of ").concat(old_rule);
  154. }
  155. return "Expected ".concat(sass_package, ".").concat(rename, " instead of ").concat(name);
  156. } else {
  157. return "Expected ".concat(sass_package, ".").concat(name, " instead of ").concat(name);
  158. }
  159. }
  160. function rule(value) {
  161. return function (root, result) {
  162. var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
  163. actual: value
  164. });
  165. if (!validOptions) {
  166. return;
  167. }
  168. root.walkDecls(function (decl) {
  169. (0, _postcssValueParser["default"])(decl.value).walk(function (node) {
  170. var cleanValue = node.value.replace(interpolationPrefix, "");
  171. // Verify that we're only looking at functions.
  172. if (node.type !== "function" || cleanValue === "") {
  173. return;
  174. }
  175. if (Object.keys(rules).includes(cleanValue)) {
  176. _stylelint.utils.report({
  177. message: messages.rejected(cleanValue),
  178. node: decl,
  179. index: (0, _utils.declarationValueIndex)(decl) + node.sourceIndex,
  180. result: result,
  181. ruleName: ruleName
  182. });
  183. }
  184. });
  185. });
  186. };
  187. }
  188. rule.ruleName = ruleName;
  189. rule.messages = messages;
  190. rule.meta = meta;