Sha256: 0aed9b7ec0e837dd32597dd2e4a4dcec96db3e9bd0eca0606555c5053d157a47

Contents?: true

Size: 1.76 KB

Versions: 23

Compression:

Stored size: 1.76 KB

Contents

'use strict';

const _ = require('lodash');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const ruleName = 'selector-max-empty-lines';

const messages = ruleMessages(ruleName, {
	expected: (max) => `Expected no more than ${max} empty ${max === 1 ? 'line' : 'lines'}`,
});

function rule(max, options, context) {
	const maxAdjacentNewlines = max + 1;

	return (root, result) => {
		const validOptions = validateOptions(result, ruleName, {
			actual: max,
			possible: _.isNumber,
		});

		if (!validOptions) {
			return;
		}

		const violatedCRLFNewLinesRegex = new RegExp(`(?:\r\n){${maxAdjacentNewlines + 1},}`);
		const violatedLFNewLinesRegex = new RegExp(`\n{${maxAdjacentNewlines + 1},}`);
		const allowedLFNewLinesString = context.fix ? '\n'.repeat(maxAdjacentNewlines) : '';
		const allowedCRLFNewLinesString = context.fix ? '\r\n'.repeat(maxAdjacentNewlines) : '';

		root.walkRules((rule) => {
			const selector = rule.raws.selector ? rule.raws.selector.raw : rule.selector;

			if (context.fix) {
				const newSelectorString = selector
					.replace(new RegExp(violatedLFNewLinesRegex, 'gm'), allowedLFNewLinesString)
					.replace(new RegExp(violatedCRLFNewLinesRegex, 'gm'), allowedCRLFNewLinesString);

				if (rule.raws.selector) {
					rule.raws.selector.raw = newSelectorString;
				} else {
					rule.selector = newSelectorString;
				}
			} else if (
				violatedLFNewLinesRegex.test(selector) ||
				violatedCRLFNewLinesRegex.test(selector)
			) {
				report({
					message: messages.expected(max),
					node: rule,
					index: 0,
					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/selector-max-empty-lines/index.js
trusty-cms-5.0.6 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-5.0.5 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-5.0.4 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-5.0.3 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-5.0.2 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-5.0.1 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.3.5 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-5.0.0 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.3.4 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.3.3 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.3.2 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.3.1 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.3 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.2.3 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.2.2 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.2.1 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.2 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.1.9 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js
trusty-cms-4.1.8 node_modules/stylelint/lib/rules/selector-max-empty-lines/index.js