1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 'use strict';
- const declarationValueIndex = require('../../utils/declarationValueIndex');
- const getDeclarationValue = require('../../utils/getDeclarationValue');
- const ruleMessages = require('../../utils/ruleMessages');
- const setDeclarationValue = require('../../utils/setDeclarationValue');
- const validateOptions = require('../../utils/validateOptions');
- const valueListCommaWhitespaceChecker = require('../valueListCommaWhitespaceChecker');
- const whitespaceChecker = require('../../utils/whitespaceChecker');
- const ruleName = 'value-list-comma-space-after';
- const messages = ruleMessages(ruleName, {
- expectedAfter: () => 'Expected single space after ","',
- rejectedAfter: () => 'Unexpected whitespace after ","',
- expectedAfterSingleLine: () => 'Expected single space after "," in a single-line list',
- rejectedAfterSingleLine: () => 'Unexpected whitespace after "," in a single-line list',
- });
- const meta = {
- url: 'https://stylelint.io/user-guide/rules/list/value-list-comma-space-after',
- };
- /** @type {import('stylelint').Rule} */
- const rule = (primary, _secondaryOptions, context) => {
- const checker = whitespaceChecker('space', primary, messages);
- return (root, result) => {
- const validOptions = validateOptions(result, ruleName, {
- actual: primary,
- possible: ['always', 'never', 'always-single-line', 'never-single-line'],
- });
- if (!validOptions) {
- return;
- }
- /** @type {Map<import('postcss').Declaration, number[]> | undefined} */
- let fixData;
- valueListCommaWhitespaceChecker({
- root,
- result,
- locationChecker: checker.after,
- checkedRuleName: ruleName,
- fix: context.fix
- ? (declNode, index) => {
- const valueIndex = declarationValueIndex(declNode);
- if (index <= valueIndex) {
- return false;
- }
- fixData = fixData || new Map();
- const commaIndices = fixData.get(declNode) || [];
- commaIndices.push(index);
- fixData.set(declNode, commaIndices);
- return true;
- }
- : null,
- });
- if (fixData) {
- for (const [decl, commaIndices] of fixData.entries()) {
- for (const index of commaIndices.sort((a, b) => b - a)) {
- const value = getDeclarationValue(decl);
- const valueIndex = index - declarationValueIndex(decl);
- const beforeValue = value.slice(0, valueIndex + 1);
- let afterValue = value.slice(valueIndex + 1);
- if (primary.startsWith('always')) {
- afterValue = afterValue.replace(/^\s*/, ' ');
- } else if (primary.startsWith('never')) {
- afterValue = afterValue.replace(/^\s*/, '');
- }
- setDeclarationValue(decl, beforeValue + afterValue);
- }
- }
- }
- };
- };
- rule.ruleName = ruleName;
- rule.messages = messages;
- rule.meta = meta;
- module.exports = rule;
|