Sha256: fe9ff690217136be9060477f3c32bdd7503be56235d6dc80716c683889afad52

Contents?: true

Size: 1.57 KB

Versions: 23

Compression:

Stored size: 1.57 KB

Contents

'use strict';

const mediaParser = require('postcss-media-query-parser').default;
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');

const ruleName = 'no-duplicate-at-import-rules';

const messages = ruleMessages(ruleName, {
	rejected: (atImport) => `Unexpected duplicate @import rule ${atImport}`,
});

function rule(actual) {
	return (root, result) => {
		const validOptions = validateOptions(result, ruleName, { actual });

		if (!validOptions) {
			return;
		}

		const imports = {};

		root.walkAtRules(/^import$/i, (atRule) => {
			const params = valueParser(atRule.params).nodes;

			if (!params.length) {
				return;
			}

			// extract uri from url() if exists
			const uri =
				params[0].type === 'function' && params[0].value === 'url'
					? params[0].nodes[0].value
					: params[0].value;
			// extract media queries if any
			const media = mediaParser(valueParser.stringify(params.slice(1)))
				.nodes.map((n) => n.value.replace(/\s/g, ''))
				.filter((n) => n.length);

			const isDuplicate = media.length
				? imports[uri] && media.some((q) => imports[uri].includes(q))
				: imports[uri];

			if (isDuplicate) {
				report({
					message: messages.rejected(uri),
					node: atRule,
					result,
					ruleName,
				});

				return;
			}

			if (!imports[uri]) imports[uri] = [];

			imports[uri] = imports[uri].concat(media);
		});
	};
}

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

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-4.1.7 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.1.6 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.1.5 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js