1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 'use strict';
- const atRuleParamIndex = require('../../utils/atRuleParamIndex');
- const isCustomMediaQuery = require('../../utils/isCustomMediaQuery');
- const isRangeContextMediaFeature = require('../../utils/isRangeContextMediaFeature');
- const isStandardSyntaxMediaFeatureName = require('../../utils/isStandardSyntaxMediaFeatureName');
- const keywordSets = require('../../reference/keywordSets');
- const mediaParser = require('postcss-media-query-parser').default;
- const optionsMatches = require('../../utils/optionsMatches');
- const rangeContextNodeParser = require('../rangeContextNodeParser');
- const report = require('../../utils/report');
- const ruleMessages = require('../../utils/ruleMessages');
- const validateOptions = require('../../utils/validateOptions');
- const vendor = require('../../utils/vendor');
- const { isRegExp, isString } = require('../../utils/validateTypes');
- const ruleName = 'media-feature-name-no-unknown';
- const messages = ruleMessages(ruleName, {
- rejected: (mediaFeatureName) => `Unexpected unknown media feature name "${mediaFeatureName}"`,
- });
- const meta = {
- url: 'https://stylelint.io/user-guide/rules/list/media-feature-name-no-unknown',
- };
- /** @type {import('stylelint').Rule} */
- const rule = (primary, secondaryOptions) => {
- return (root, result) => {
- const validOptions = validateOptions(
- result,
- ruleName,
- { actual: primary },
- {
- actual: secondaryOptions,
- possible: {
- ignoreMediaFeatureNames: [isString, isRegExp],
- },
- optional: true,
- },
- );
- if (!validOptions) {
- return;
- }
- root.walkAtRules(/^media$/i, (atRule) => {
- mediaParser(atRule.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
- const parent = mediaFeatureNode.parent;
- const mediaFeatureRangeContext = isRangeContextMediaFeature(parent.value);
- let value;
- let sourceIndex;
- if (mediaFeatureRangeContext) {
- const parsedRangeContext = rangeContextNodeParser(mediaFeatureNode);
- value = parsedRangeContext.name.value;
- sourceIndex = parsedRangeContext.name.sourceIndex;
- } else {
- value = mediaFeatureNode.value;
- sourceIndex = mediaFeatureNode.sourceIndex;
- }
- if (!isStandardSyntaxMediaFeatureName(value) || isCustomMediaQuery(value)) {
- return;
- }
- if (optionsMatches(secondaryOptions, 'ignoreMediaFeatureNames', value)) {
- return;
- }
- if (vendor.prefix(value) || keywordSets.mediaFeatureNames.has(value.toLowerCase())) {
- return;
- }
- report({
- index: atRuleParamIndex(atRule) + sourceIndex,
- message: messages.rejected(value),
- node: atRule,
- ruleName,
- result,
- });
- });
- });
- };
- };
- rule.ruleName = ruleName;
- rule.messages = messages;
- rule.meta = meta;
- module.exports = rule;
|