Sha256: 23b8390e4031f0ac41193212082e5a31aa93c37906c8d460109de854ea236f82

Contents?: true

Size: 1.72 KB

Versions: 23

Compression:

Stored size: 1.72 KB

Contents

'use strict';

const _ = require('lodash');
const beforeBlockString = require('../../utils/beforeBlockString');
const hasBlock = require('../../utils/hasBlock');
const hasEmptyBlock = require('../../utils/hasEmptyBlock');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const ruleName = 'block-no-empty';

const messages = ruleMessages(ruleName, {
	rejected: 'Unexpected empty block',
});

function rule(primary, options = {}) {
	return (root, result) => {
		const validOptions = validateOptions(
			result,
			ruleName,
			{
				actual: primary,
				possible: _.isBoolean,
			},
			{
				actual: options,
				possible: {
					ignore: ['comments'],
				},
				optional: true,
			},
		);

		if (!validOptions) {
			return;
		}

		const ignoreComments = optionsMatches(options, 'ignore', 'comments');

		// Check both kinds of statements: rules and at-rules
		root.walkRules(check);
		root.walkAtRules(check);

		function check(statement) {
			if (!hasEmptyBlock(statement) && !ignoreComments) {
				return;
			}

			if (!hasBlock(statement)) {
				return;
			}

			const hasCommentsOnly = statement.nodes.every((node) => node.type === 'comment');

			if (!hasCommentsOnly) {
				return;
			}

			let index = beforeBlockString(statement, { noRawBefore: true }).length;

			// For empty blocks when using SugarSS parser
			if (statement.raws.between === undefined) {
				index--;
			}

			report({
				message: messages.rejected,
				node: statement,
				index,
				result,
				ruleName,
			});
		}
	};
}

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