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-5.0.7 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-5.0.6 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-5.0.5 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-5.0.4 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-5.0.3 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-5.0.2 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-5.0.1 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.3.5 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-5.0.0 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.3.4 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.3.3 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.3.2 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.3.1 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.3 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.2.3 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.2.2 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.2.1 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.2 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.1.9 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js
trusty-cms-4.1.8 node_modules/stylelint/lib/rules/no-duplicate-at-import-rules/index.js