123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 'use strict';
- const mediaParser = require('postcss-media-query-parser').default;
- const atRuleParamIndex = require('../../utils/atRuleParamIndex');
- const isRangeContextMediaFeature = require('../../utils/isRangeContextMediaFeature');
- const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
- const optionsMatches = require('../../utils/optionsMatches');
- const rangeContextNodeParser = require('../rangeContextNodeParser');
- const report = require('../../utils/report');
- const ruleMessages = require('../../utils/ruleMessages');
- const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
- const validateOptions = require('../../utils/validateOptions');
- const { isString, isRegExp } = require('../../utils/validateTypes');
- const vendor = require('../../utils/vendor');
- const ruleName = 'media-feature-name-value-allowed-list';
- const messages = ruleMessages(ruleName, {
- rejected: (name, value) => `Unexpected value "${value}" for name "${name}"`,
- });
- const meta = {
- url: 'https://stylelint.io/user-guide/rules/list/media-feature-name-value-allowed-list',
- };
- /** @type {import('stylelint').Rule<Record<string, string | RegExp | Array<string | RegExp>>>} */
- const rule = (primary) => {
- return (root, result) => {
- const validOptions = validateOptions(result, ruleName, {
- actual: primary,
- possible: [validateObjectWithArrayProps(isString, isRegExp)],
- });
- if (!validOptions) {
- return;
- }
- root.walkAtRules(/^media$/i, (atRule) => {
- mediaParser(atRule.params).walk(/^media-feature-expression$/i, (node) => {
- if (!node.nodes) return;
- const mediaFeatureRangeContext = isRangeContextMediaFeature(node.parent.value);
- // Ignore boolean
- if (!node.value.includes(':') && !mediaFeatureRangeContext) {
- return;
- }
- const mediaFeatureNode = node.nodes.find((n) => n.type === 'media-feature');
- if (mediaFeatureNode == null) throw new Error('A `media-feature` node must be present');
- let mediaFeatureName;
- let values;
- if (mediaFeatureRangeContext) {
- const parsedRangeContext = rangeContextNodeParser(mediaFeatureNode);
- mediaFeatureName = parsedRangeContext.name.value;
- values = parsedRangeContext.values;
- } else {
- mediaFeatureName = mediaFeatureNode.value;
- const valueNode = node.nodes.find((n) => n.type === 'value');
- if (valueNode == null) throw new Error('A `value` node must be present');
- values = [valueNode];
- }
- for (const valueNode of values) {
- const value = valueNode.value;
- const unprefixedMediaFeatureName = vendor.unprefixed(mediaFeatureName);
- const allowedValuesKey = Object.keys(primary).find((featureName) =>
- matchesStringOrRegExp(unprefixedMediaFeatureName, featureName),
- );
- if (allowedValuesKey == null) {
- return;
- }
- if (optionsMatches(primary, allowedValuesKey, value)) {
- return;
- }
- report({
- index: atRuleParamIndex(atRule) + valueNode.sourceIndex,
- message: messages.rejected(mediaFeatureName, value),
- node: atRule,
- ruleName,
- result,
- });
- }
- });
- });
- };
- };
- rule.ruleName = ruleName;
- rule.messages = messages;
- rule.meta = meta;
- module.exports = rule;
|