index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/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. const name = node.value;
  43. if (node.type !== 'function') {
  44. return;
  45. }
  46. if (!isStandardSyntaxFunction(node)) {
  47. return;
  48. }
  49. if (isCustomFunction(name)) {
  50. return;
  51. }
  52. if (optionsMatches(secondaryOptions, 'ignoreFunctions', name)) {
  53. return;
  54. }
  55. if (functionsList.includes(name.toLowerCase())) {
  56. return;
  57. }
  58. report({
  59. message: messages.rejected(name),
  60. node: decl,
  61. index: declarationValueIndex(decl) + node.sourceIndex,
  62. result,
  63. ruleName,
  64. word: name,
  65. });
  66. });
  67. });
  68. };
  69. };
  70. rule.ruleName = ruleName;
  71. rule.messages = messages;
  72. rule.meta = meta;
  73. module.exports = rule;