index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
  3. const keywordSets = require('../../reference/keywordSets');
  4. const optionsMatches = require('../../utils/optionsMatches');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const vendor = require('../../utils/vendor');
  9. const { isRegExp, isString } = require('../../utils/validateTypes');
  10. const ruleName = 'at-rule-no-unknown';
  11. const messages = ruleMessages(ruleName, {
  12. rejected: (atRule) => `Unexpected unknown at-rule "${atRule}"`,
  13. });
  14. const meta = {
  15. url: 'https://stylelint.io/user-guide/rules/list/at-rule-no-unknown',
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary, secondaryOptions) => {
  19. return (root, result) => {
  20. const validOptions = validateOptions(
  21. result,
  22. ruleName,
  23. { actual: primary },
  24. {
  25. actual: secondaryOptions,
  26. possible: {
  27. ignoreAtRules: [isString, isRegExp],
  28. },
  29. optional: true,
  30. },
  31. );
  32. if (!validOptions) {
  33. return;
  34. }
  35. root.walkAtRules((atRule) => {
  36. if (!isStandardSyntaxAtRule(atRule)) {
  37. return;
  38. }
  39. const name = atRule.name;
  40. // Return early if at-rule is to be ignored
  41. if (optionsMatches(secondaryOptions, 'ignoreAtRules', atRule.name)) {
  42. return;
  43. }
  44. if (vendor.prefix(name) || keywordSets.atRules.has(name.toLowerCase())) {
  45. return;
  46. }
  47. report({
  48. message: messages.rejected(`@${name}`),
  49. node: atRule,
  50. ruleName,
  51. result,
  52. });
  53. });
  54. };
  55. };
  56. rule.ruleName = ruleName;
  57. rule.messages = messages;
  58. rule.meta = meta;
  59. module.exports = rule;