index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const ruleName = 'at-rule-name-case';
  7. const messages = ruleMessages(ruleName, {
  8. expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
  9. });
  10. const meta = {
  11. url: 'https://stylelint.io/user-guide/rules/at-rule-name-case',
  12. fixable: true,
  13. };
  14. /** @type {import('stylelint').Rule} */
  15. const rule = (primary, _secondary, context) => {
  16. return (root, result) => {
  17. const validOptions = validateOptions(result, ruleName, {
  18. actual: primary,
  19. possible: ['lower', 'upper'],
  20. });
  21. if (!validOptions) {
  22. return;
  23. }
  24. /** @type {'lower' | 'upper'} */
  25. const expectation = primary;
  26. root.walkAtRules((atRule) => {
  27. if (!isStandardSyntaxAtRule(atRule)) {
  28. return;
  29. }
  30. const name = atRule.name;
  31. const expectedName = expectation === 'lower' ? name.toLowerCase() : name.toUpperCase();
  32. if (name === expectedName) {
  33. return;
  34. }
  35. if (context.fix) {
  36. atRule.name = expectedName;
  37. return;
  38. }
  39. report({
  40. message: messages.expected(name, expectedName),
  41. node: atRule,
  42. ruleName,
  43. result,
  44. });
  45. });
  46. };
  47. };
  48. rule.ruleName = ruleName;
  49. rule.messages = messages;
  50. rule.meta = meta;
  51. module.exports = rule;