index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
  3. const { atKeywords } = require('../../reference/atKeywords');
  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/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) || atKeywords.has(name.toLowerCase())) {
  45. return;
  46. }
  47. const atName = `@${name}`;
  48. report({
  49. message: messages.rejected(atName),
  50. node: atRule,
  51. ruleName,
  52. result,
  53. word: atName,
  54. });
  55. });
  56. };
  57. };
  58. rule.ruleName = ruleName;
  59. rule.messages = messages;
  60. rule.meta = meta;
  61. module.exports = rule;