Sha256: 666e74b7d3c87f821854953908a04801d729502f9edee56422ad3798bdfbd69d

Contents?: true

Size: 1.91 KB

Versions: 23

Compression:

Stored size: 1.91 KB

Contents

'use strict';

const declarationValueIndex = require('../../utils/declarationValueIndex');
const findFontFamily = require('../../utils/findFontFamily');
const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const _ = require('lodash');

const ruleName = 'font-family-no-missing-generic-family-keyword';

const messages = ruleMessages(ruleName, {
	rejected: 'Unexpected missing generic font family',
});

const isFamilyNameKeyword = (node) =>
	!node.quote && keywordSets.fontFamilyKeywords.has(node.value.toLowerCase());

function rule(actual, options) {
	return (root, result) => {
		const validOptions = validateOptions(
			result,
			ruleName,
			{ actual },
			{
				actual: options,
				possible: {
					ignoreFontFamilies: [_.isString, _.isRegExp],
				},
				optional: true,
			},
		);

		if (!validOptions) {
			return;
		}

		root.walkDecls(/^font(-family)?$/i, (decl) => {
			// Ignore @font-face
			if (
				decl.parent &&
				decl.parent.type === 'atrule' &&
				decl.parent.name.toLowerCase() === 'font-face'
			) {
				return;
			}

			if (decl.prop === 'font' && keywordSets.systemFontValues.has(decl.value.toLowerCase())) {
				return;
			}

			const fontFamilies = findFontFamily(decl.value);

			if (fontFamilies.length === 0) {
				return;
			}

			if (fontFamilies.some(isFamilyNameKeyword)) {
				return;
			}

			if (fontFamilies.some((node) => optionsMatches(options, 'ignoreFontFamilies', node.value))) {
				return;
			}

			report({
				result,
				ruleName,
				message: messages.rejected,
				node: decl,
				index: declarationValueIndex(decl) + fontFamilies[fontFamilies.length - 1].sourceIndex,
			});
		});
	};
}

rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-5.0.6 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-5.0.5 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-5.0.4 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-5.0.3 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-5.0.2 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-5.0.1 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.3.5 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-5.0.0 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.3.4 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.3.3 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.3.2 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.3.1 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.3 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.2.3 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.2.2 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.2.1 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.2 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.1.9 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js
trusty-cms-4.1.8 node_modules/stylelint/lib/rules/font-family-no-missing-generic-family-keyword/index.js