index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/list/at-rule-name-case',
  12. };
  13. /** @type {import('stylelint').Rule} */
  14. const rule = (primary, _secondary, context) => {
  15. return (root, result) => {
  16. const validOptions = validateOptions(result, ruleName, {
  17. actual: primary,
  18. possible: ['lower', 'upper'],
  19. });
  20. if (!validOptions) {
  21. return;
  22. }
  23. /** @type {'lower' | 'upper'} */
  24. const expectation = primary;
  25. root.walkAtRules((atRule) => {
  26. if (!isStandardSyntaxAtRule(atRule)) {
  27. return;
  28. }
  29. const name = atRule.name;
  30. const expectedName = expectation === 'lower' ? name.toLowerCase() : name.toUpperCase();
  31. if (name === expectedName) {
  32. return;
  33. }
  34. if (context.fix) {
  35. atRule.name = expectedName;
  36. return;
  37. }
  38. report({
  39. message: messages.expected(name, expectedName),
  40. node: atRule,
  41. ruleName,
  42. result,
  43. });
  44. });
  45. };
  46. };
  47. rule.ruleName = ruleName;
  48. rule.messages = messages;
  49. rule.meta = meta;
  50. module.exports = rule;