index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. const mediaParser = require('postcss-media-query-parser').default;
  3. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  4. const isRangeContextMediaFeature = require('../../utils/isRangeContextMediaFeature');
  5. const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
  6. const optionsMatches = require('../../utils/optionsMatches');
  7. const rangeContextNodeParser = require('../rangeContextNodeParser');
  8. const report = require('../../utils/report');
  9. const ruleMessages = require('../../utils/ruleMessages');
  10. const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
  11. const validateOptions = require('../../utils/validateOptions');
  12. const { isString, isRegExp } = require('../../utils/validateTypes');
  13. const vendor = require('../../utils/vendor');
  14. const ruleName = 'media-feature-name-value-allowed-list';
  15. const messages = ruleMessages(ruleName, {
  16. rejected: (name, value) => `Unexpected value "${value}" for name "${name}"`,
  17. });
  18. const meta = {
  19. url: 'https://stylelint.io/user-guide/rules/list/media-feature-name-value-allowed-list',
  20. };
  21. /** @type {import('stylelint').Rule<Record<string, string | RegExp | Array<string | RegExp>>>} */
  22. const rule = (primary) => {
  23. return (root, result) => {
  24. const validOptions = validateOptions(result, ruleName, {
  25. actual: primary,
  26. possible: [validateObjectWithArrayProps(isString, isRegExp)],
  27. });
  28. if (!validOptions) {
  29. return;
  30. }
  31. root.walkAtRules(/^media$/i, (atRule) => {
  32. mediaParser(atRule.params).walk(/^media-feature-expression$/i, (node) => {
  33. if (!node.nodes) return;
  34. const mediaFeatureRangeContext = isRangeContextMediaFeature(node.parent.value);
  35. // Ignore boolean
  36. if (!node.value.includes(':') && !mediaFeatureRangeContext) {
  37. return;
  38. }
  39. const mediaFeatureNode = node.nodes.find((n) => n.type === 'media-feature');
  40. if (mediaFeatureNode == null) throw new Error('A `media-feature` node must be present');
  41. let mediaFeatureName;
  42. let values;
  43. if (mediaFeatureRangeContext) {
  44. const parsedRangeContext = rangeContextNodeParser(mediaFeatureNode);
  45. mediaFeatureName = parsedRangeContext.name.value;
  46. values = parsedRangeContext.values;
  47. } else {
  48. mediaFeatureName = mediaFeatureNode.value;
  49. const valueNode = node.nodes.find((n) => n.type === 'value');
  50. if (valueNode == null) throw new Error('A `value` node must be present');
  51. values = [valueNode];
  52. }
  53. for (const valueNode of values) {
  54. const value = valueNode.value;
  55. const unprefixedMediaFeatureName = vendor.unprefixed(mediaFeatureName);
  56. const allowedValuesKey = Object.keys(primary).find((featureName) =>
  57. matchesStringOrRegExp(unprefixedMediaFeatureName, featureName),
  58. );
  59. if (allowedValuesKey == null) {
  60. return;
  61. }
  62. if (optionsMatches(primary, allowedValuesKey, value)) {
  63. return;
  64. }
  65. report({
  66. index: atRuleParamIndex(atRule) + valueNode.sourceIndex,
  67. message: messages.rejected(mediaFeatureName, value),
  68. node: atRule,
  69. ruleName,
  70. result,
  71. });
  72. }
  73. });
  74. });
  75. };
  76. };
  77. rule.ruleName = ruleName;
  78. rule.messages = messages;
  79. rule.meta = meta;
  80. module.exports = rule;