Sha256: 8d2e61884bf92588168f87cdeccf24093c02cf1df5d704afd7d04d26595181bb

Contents?: true

Size: 1.93 KB

Versions: 23

Compression:

Stored size: 1.93 KB

Contents

'use strict';

const _ = require('lodash');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const keywordSets = require('../../reference/keywordSets');
const postcss = require('postcss');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');

const ruleName = 'time-min-milliseconds';

const messages = ruleMessages(ruleName, {
	expected: (time) => `Expected a minimum of ${time} milliseconds`,
});

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

		if (!validOptions) {
			return;
		}

		root.walkDecls((decl) => {
			const propertyName = postcss.vendor.unprefixed(decl.prop.toLowerCase());

			if (keywordSets.longhandTimeProperties.has(propertyName) && !isAcceptableTime(decl.value)) {
				complain(decl);
			}

			if (keywordSets.shorthandTimeProperties.has(propertyName)) {
				const valueList = postcss.list.space(decl.value);

				for (const value of valueList) {
					if (!isAcceptableTime(value)) {
						complain(decl, decl.value.indexOf(value));
					}
				}
			}
		});

		function isAcceptableTime(time) {
			const parsedTime = valueParser.unit(time);

			if (!parsedTime) return true;

			if (parsedTime.number <= 0) {
				return true;
			}

			if (parsedTime.unit.toLowerCase() === 'ms' && parsedTime.number < minimum) {
				return false;
			}

			if (parsedTime.unit.toLowerCase() === 's' && parsedTime.number * 1000 < minimum) {
				return false;
			}

			return true;
		}

		function complain(decl, offset = 0) {
			report({
				result,
				ruleName,
				message: messages.expected(minimum),
				index: declarationValueIndex(decl) + offset,
				node: decl,
			});
		}
	};
}

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