index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
  4. const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const valueParser = require('postcss-value-parser');
  9. const vendor = require('../../utils/vendor');
  10. const { isRegExp, isString } = require('../../utils/validateTypes');
  11. const ruleName = 'function-disallowed-list';
  12. const messages = ruleMessages(ruleName, {
  13. rejected: (name) => `Unexpected function "${name}"`,
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/function-disallowed-list',
  17. };
  18. /** @type {import('stylelint').Rule<string | RegExp | Array<string | RegExp>>} */
  19. const rule = (primary) => {
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: primary,
  23. possible: [isString, isRegExp],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. root.walkDecls((decl) => {
  29. const value = decl.value;
  30. valueParser(value).walk((node) => {
  31. if (node.type !== 'function') {
  32. return;
  33. }
  34. if (!isStandardSyntaxFunction(node)) {
  35. return;
  36. }
  37. if (!matchesStringOrRegExp(vendor.unprefixed(node.value), primary)) {
  38. return;
  39. }
  40. report({
  41. message: messages.rejected(node.value),
  42. node: decl,
  43. index: declarationValueIndex(decl) + node.sourceIndex,
  44. result,
  45. ruleName,
  46. });
  47. });
  48. });
  49. };
  50. };
  51. rule.primaryOptionArray = true;
  52. rule.ruleName = ruleName;
  53. rule.messages = messages;
  54. rule.meta = meta;
  55. module.exports = rule;