Sha256: dd1f8ad8eb4d6ed68343c34bd03992ce41431fb95bb52afbdad8d5a47c0c1411

Contents?: true

Size: 1.9 KB

Versions: 23

Compression:

Stored size: 1.9 KB

Contents

'use strict';

const _ = require('lodash');
const requireRule = require('./requireRule');

// Rule settings can take a number of forms, e.g.
// a. "rule-name": null
// b. "rule-name": [null, ...]
// c. "rule-name": primaryOption
// d. "rule-name": [primaryOption]
// e. "rule-name": [primaryOption, secondaryOption]
// Where primaryOption can be anything: primitive, Object, or Array.

/**
 * This function normalizes all the possibilities into the
 * standard form: [primaryOption, secondaryOption]
 * Except in the cases with null, a & b, in which case
 * null is returned
 * @param {import('stylelint').StylelintConfigRuleSettings} rawSettings
 * @param {string} ruleName
 * @param {boolean} [primaryOptionArray] If primaryOptionArray is not provided, we try to get it from the, rules themselves, which will not work for plugins
 * @return {[any, Object] | Array<any | [any, Object]> | null}
 */
module.exports = function (
	rawSettings,
	ruleName,
	// If primaryOptionArray is not provided, we try to get it from the
	// rules themselves, which will not work for plugins
	primaryOptionArray,
) {
	if (rawSettings === null) {
		return null;
	}

	if (!Array.isArray(rawSettings)) {
		return [rawSettings];
	}
	// Everything below is an array ...

	if (rawSettings[0] === null) {
		return null;
	}

	if (primaryOptionArray === undefined) {
		const rule = requireRule(ruleName);

		primaryOptionArray = _.get(rule, 'primaryOptionArray');
	}

	if (!primaryOptionArray) {
		return rawSettings;
	}
	// Everything below is a rule that CAN have an array for a primary option ...
	// (they might also have something else, e.g. rule-properties-order can
	// have the string "alphabetical")

	if (rawSettings.length === 1 && Array.isArray(rawSettings[0])) {
		return rawSettings;
	}

	if (
		rawSettings.length === 2 &&
		!_.isPlainObject(rawSettings[0]) &&
		_.isPlainObject(rawSettings[1])
	) {
		return rawSettings;
	}

	return [rawSettings];
};

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-5.0.6 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-5.0.5 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-5.0.4 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-5.0.3 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-5.0.2 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-5.0.1 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.3.5 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-5.0.0 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.3.4 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.3.3 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.3.2 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.3.1 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.3 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.2.3 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.2.2 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.2.1 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.2 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.1.9 node_modules/stylelint/lib/normalizeRuleSettings.js
trusty-cms-4.1.8 node_modules/stylelint/lib/normalizeRuleSettings.js