index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  3. const isCustomMediaQuery = require('../../utils/isCustomMediaQuery');
  4. const isRangeContextMediaFeature = require('../../utils/isRangeContextMediaFeature');
  5. const isStandardSyntaxMediaFeatureName = require('../../utils/isStandardSyntaxMediaFeatureName');
  6. const mediaParser = require('postcss-media-query-parser').default;
  7. const rangeContextNodeParser = require('../rangeContextNodeParser');
  8. const report = require('../../utils/report');
  9. const ruleMessages = require('../../utils/ruleMessages');
  10. const validateOptions = require('../../utils/validateOptions');
  11. const ruleName = 'media-feature-name-case';
  12. const messages = ruleMessages(ruleName, {
  13. expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/media-feature-name-case',
  17. };
  18. /** @type {import('stylelint').Rule} */
  19. const rule = (primary, _secondaryOptions, context) => {
  20. return (root, result) => {
  21. const validOptions = validateOptions(result, ruleName, {
  22. actual: primary,
  23. possible: ['lower', 'upper'],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. root.walkAtRules(/^media$/i, (atRule) => {
  29. let hasComments = atRule.raws.params && atRule.raws.params.raw;
  30. const mediaRule = hasComments ? hasComments : atRule.params;
  31. mediaParser(mediaRule).walk(/^media-feature$/i, (mediaFeatureNode) => {
  32. const parent = mediaFeatureNode.parent;
  33. const mediaFeatureRangeContext = isRangeContextMediaFeature(parent.value);
  34. let value;
  35. let sourceIndex;
  36. if (mediaFeatureRangeContext) {
  37. const parsedRangeContext = rangeContextNodeParser(mediaFeatureNode);
  38. value = parsedRangeContext.name.value;
  39. sourceIndex = parsedRangeContext.name.sourceIndex;
  40. } else {
  41. value = mediaFeatureNode.value;
  42. sourceIndex = mediaFeatureNode.sourceIndex;
  43. }
  44. if (!isStandardSyntaxMediaFeatureName(value) || isCustomMediaQuery(value)) {
  45. return;
  46. }
  47. const expectedFeatureName = primary === 'lower' ? value.toLowerCase() : value.toUpperCase();
  48. if (value === expectedFeatureName) {
  49. return;
  50. }
  51. if (context.fix) {
  52. if (hasComments) {
  53. hasComments =
  54. hasComments.slice(0, sourceIndex) +
  55. expectedFeatureName +
  56. hasComments.slice(sourceIndex + expectedFeatureName.length);
  57. if (atRule.raws.params == null) {
  58. throw new Error('The `AtRuleRaws` node must have a `params` property');
  59. }
  60. atRule.raws.params.raw = hasComments;
  61. } else {
  62. atRule.params =
  63. atRule.params.slice(0, sourceIndex) +
  64. expectedFeatureName +
  65. atRule.params.slice(sourceIndex + expectedFeatureName.length);
  66. }
  67. return;
  68. }
  69. report({
  70. index: atRuleParamIndex(atRule) + sourceIndex,
  71. message: messages.expected(value, expectedFeatureName),
  72. node: atRule,
  73. ruleName,
  74. result,
  75. });
  76. });
  77. });
  78. };
  79. };
  80. rule.ruleName = ruleName;
  81. rule.messages = messages;
  82. rule.meta = meta;
  83. module.exports = rule;