You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.5 KiB
86 lines
2.5 KiB
// @ts-nocheck |
|
|
|
'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', |
|
}); |
|
|
|
function rule(expectation, options, context) { |
|
const checker = whitespaceChecker('space', expectation, messages); |
|
|
|
return (root, result) => { |
|
const validOptions = validateOptions(result, ruleName, { |
|
actual: expectation, |
|
possible: ['always', 'never', 'always-single-line', 'never-single-line'], |
|
}); |
|
|
|
if (!validOptions) { |
|
return; |
|
} |
|
|
|
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) { |
|
fixData.forEach((commaIndices, decl) => { |
|
commaIndices |
|
.sort((a, b) => b - a) |
|
.forEach((index) => { |
|
const value = getDeclarationValue(decl); |
|
const valueIndex = index - declarationValueIndex(decl); |
|
const beforeValue = value.slice(0, valueIndex + 1); |
|
let afterValue = value.slice(valueIndex + 1); |
|
|
|
if (expectation.startsWith('always')) { |
|
afterValue = afterValue.replace(/^\s*/, ' '); |
|
} else if (expectation.startsWith('never')) { |
|
afterValue = afterValue.replace(/^\s*/, ''); |
|
} |
|
|
|
setDeclarationValue(decl, beforeValue + afterValue); |
|
}); |
|
}); |
|
} |
|
}; |
|
} |
|
|
|
rule.ruleName = ruleName; |
|
rule.messages = messages; |
|
module.exports = rule;
|
|
|