index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. const fs = require('fs');
  3. const valueParser = require('postcss-value-parser');
  4. const functionsListPath = require('css-functions-list');
  5. const declarationValueIndex = require('../../utils/declarationValueIndex');
  6. const optionsMatches = require('../../utils/optionsMatches');
  7. const report = require('../../utils/report');
  8. const ruleMessages = require('../../utils/ruleMessages');
  9. const validateOptions = require('../../utils/validateOptions');
  10. const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
  11. const isCustomFunction = require('../../utils/isCustomFunction');
  12. const { isRegExp, isString } = require('../../utils/validateTypes');
  13. const ruleName = 'function-no-unknown';
  14. const messages = ruleMessages(ruleName, {
  15. rejected: (name) => `Unexpected unknown function "${name}"`,
  16. });
  17. const meta = {
  18. url: 'https://stylelint.io/user-guide/rules/list/function-no-unknown',
  19. };
  20. /** @type {import('stylelint').Rule} */
  21. const rule = (primary, secondaryOptions) => {
  22. return (root, result) => {
  23. const validOptions = validateOptions(
  24. result,
  25. ruleName,
  26. { actual: primary },
  27. {
  28. actual: secondaryOptions,
  29. possible: {
  30. ignoreFunctions: [isString, isRegExp],
  31. },
  32. optional: true,
  33. },
  34. );
  35. if (!validOptions) {
  36. return;
  37. }
  38. const functionsList = JSON.parse(fs.readFileSync(functionsListPath.toString(), 'utf8'));
  39. root.walkDecls((decl) => {
  40. const { value } = decl;
  41. valueParser(value).walk((node) => {
  42. if (node.type !== 'function') {
  43. return;
  44. }
  45. if (!isStandardSyntaxFunction(node)) {
  46. return;
  47. }
  48. if (isCustomFunction(node.value)) {
  49. return;
  50. }
  51. if (optionsMatches(secondaryOptions, 'ignoreFunctions', node.value)) {
  52. return;
  53. }
  54. if (functionsList.includes(node.value.toLowerCase())) {
  55. return;
  56. }
  57. report({
  58. message: messages.rejected(node.value),
  59. node: decl,
  60. index: declarationValueIndex(decl) + node.sourceIndex,
  61. result,
  62. ruleName,
  63. });
  64. });
  65. });
  66. };
  67. };
  68. rule.ruleName = ruleName;
  69. rule.messages = messages;
  70. rule.meta = meta;
  71. module.exports = rule;