index.mjs 1.7 KB

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