index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 keywordSets = require('../../reference/keywordSets');
  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/list/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) || keywordSets.mediaFeatureNames.has(value.toLowerCase())) {
  61. return;
  62. }
  63. report({
  64. index: atRuleParamIndex(atRule) + sourceIndex,
  65. message: messages.rejected(value),
  66. node: atRule,
  67. ruleName,
  68. result,
  69. });
  70. });
  71. });
  72. };
  73. };
  74. rule.ruleName = ruleName;
  75. rule.messages = messages;
  76. rule.meta = meta;
  77. module.exports = rule;