index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 { mediaFeatureNames } = require('../../reference/mediaFeatures');
  7. const mediaParser = require('postcss-media-query-parser').default;
  8. const optionsMatches = require('../../utils/optionsMatches');
  9. const rangeContextNodeParser = require('../rangeContextNodeParser');
  10. const report = require('../../utils/report');
  11. const ruleMessages = require('../../utils/ruleMessages');
  12. const validateOptions = require('../../utils/validateOptions');
  13. const vendor = require('../../utils/vendor');
  14. const { isRegExp, isString } = require('../../utils/validateTypes');
  15. const ruleName = 'media-feature-name-no-unknown';
  16. const messages = ruleMessages(ruleName, {
  17. rejected: (mediaFeatureName) => `Unexpected unknown media feature name "${mediaFeatureName}"`,
  18. });
  19. const meta = {
  20. url: 'https://stylelint.io/user-guide/rules/media-feature-name-no-unknown',
  21. };
  22. /** @type {import('stylelint').Rule} */
  23. const rule = (primary, secondaryOptions) => {
  24. return (root, result) => {
  25. const validOptions = validateOptions(
  26. result,
  27. ruleName,
  28. { actual: primary },
  29. {
  30. actual: secondaryOptions,
  31. possible: {
  32. ignoreMediaFeatureNames: [isString, isRegExp],
  33. },
  34. optional: true,
  35. },
  36. );
  37. if (!validOptions) {
  38. return;
  39. }
  40. root.walkAtRules(/^media$/i, (atRule) => {
  41. mediaParser(atRule.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
  42. const parent = mediaFeatureNode.parent;
  43. const mediaFeatureRangeContext = isRangeContextMediaFeature(parent.value);
  44. let value;
  45. let sourceIndex;
  46. if (mediaFeatureRangeContext) {
  47. const parsedRangeContext = rangeContextNodeParser(mediaFeatureNode);
  48. value = parsedRangeContext.name.value;
  49. sourceIndex = parsedRangeContext.name.sourceIndex;
  50. } else {
  51. value = mediaFeatureNode.value;
  52. sourceIndex = mediaFeatureNode.sourceIndex;
  53. }
  54. if (!isStandardSyntaxMediaFeatureName(value) || isCustomMediaQuery(value)) {
  55. return;
  56. }
  57. if (optionsMatches(secondaryOptions, 'ignoreMediaFeatureNames', value)) {
  58. return;
  59. }
  60. if (vendor.prefix(value) || mediaFeatureNames.has(value.toLowerCase())) {
  61. return;
  62. }
  63. const index = atRuleParamIndex(atRule) + sourceIndex;
  64. const endIndex = index + value.length;
  65. report({
  66. index,
  67. endIndex,
  68. message: messages.rejected(value),
  69. node: atRule,
  70. ruleName,
  71. result,
  72. });
  73. });
  74. });
  75. };
  76. };
  77. rule.ruleName = ruleName;
  78. rule.messages = messages;
  79. rule.meta = meta;
  80. module.exports = rule;