123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 'use strict';
- const atRuleParamIndex = require('../../utils/atRuleParamIndex');
- const mediaQueryListCommaWhitespaceChecker = require('../mediaQueryListCommaWhitespaceChecker');
- const ruleMessages = require('../../utils/ruleMessages');
- const validateOptions = require('../../utils/validateOptions');
- const whitespaceChecker = require('../../utils/whitespaceChecker');
- const ruleName = 'media-query-list-comma-newline-after';
- const messages = ruleMessages(ruleName, {
- expectedAfter: () => 'Expected newline after ","',
- expectedAfterMultiLine: () => 'Expected newline after "," in a multi-line list',
- rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line list',
- });
- function rule(expectation, options, context) {
- const checker = whitespaceChecker('newline', expectation, messages);
- return (root, result) => {
- const validOptions = validateOptions(result, ruleName, {
- actual: expectation,
- possible: ['always', 'always-multi-line', 'never-multi-line'],
- });
- if (!validOptions) {
- return;
- }
-
-
- let fixData;
- mediaQueryListCommaWhitespaceChecker({
- root,
- result,
- locationChecker: checker.afterOneOnly,
- checkedRuleName: ruleName,
- allowTrailingComments: expectation.startsWith('always'),
- fix: context.fix
- ? (atRule, index) => {
- const paramCommaIndex = index - atRuleParamIndex(atRule);
- fixData = fixData || new Map();
- const commaIndices = fixData.get(atRule) || [];
- commaIndices.push(paramCommaIndex);
- fixData.set(atRule, commaIndices);
- return true;
- }
- : null,
- });
- if (fixData) {
- fixData.forEach((commaIndices, atRule) => {
- let params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
- commaIndices
- .sort((a, b) => b - a)
- .forEach((index) => {
- const beforeComma = params.slice(0, index + 1);
- const afterComma = params.slice(index + 1);
- if (expectation.startsWith('always')) {
- if (/^\s*\r?\n/.test(afterComma)) {
- params = beforeComma + afterComma.replace(/^[^\S\r\n]*/, '');
- } else {
- params = beforeComma + context.newline + afterComma;
- }
- } else if (expectation.startsWith('never')) {
- params = beforeComma + afterComma.replace(/^\s*/, '');
- }
- });
- if (atRule.raws.params) {
- atRule.raws.params.raw = params;
- } else {
- atRule.params = params;
- }
- });
- }
- };
- }
- rule.ruleName = ruleName;
- rule.messages = messages;
- module.exports = rule;
|