index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  3. const mediaQueryListCommaWhitespaceChecker = require('../mediaQueryListCommaWhitespaceChecker');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const whitespaceChecker = require('../../utils/whitespaceChecker');
  7. const ruleName = 'media-query-list-comma-newline-after';
  8. const messages = ruleMessages(ruleName, {
  9. expectedAfter: () => 'Expected newline after ","',
  10. expectedAfterMultiLine: () => 'Expected newline after "," in a multi-line list',
  11. rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line list',
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/media-query-list-comma-newline-after',
  15. fixable: true,
  16. deprecated: true,
  17. };
  18. /** @type {import('stylelint').Rule} */
  19. const rule = (primary, _secondaryOptions, context) => {
  20. const checker = whitespaceChecker('newline', primary, messages);
  21. return (root, result) => {
  22. const validOptions = validateOptions(result, ruleName, {
  23. actual: primary,
  24. possible: ['always', 'always-multi-line', 'never-multi-line'],
  25. });
  26. if (!validOptions) {
  27. return;
  28. }
  29. // Only check for the newline after the comma, while allowing
  30. // arbitrary indentation after the newline
  31. /** @type {Map<import('postcss').AtRule, number[]> | undefined} */
  32. let fixData;
  33. mediaQueryListCommaWhitespaceChecker({
  34. root,
  35. result,
  36. locationChecker: checker.afterOneOnly,
  37. checkedRuleName: ruleName,
  38. allowTrailingComments: primary.startsWith('always'),
  39. fix: context.fix
  40. ? (atRule, index) => {
  41. const paramCommaIndex = index - atRuleParamIndex(atRule);
  42. fixData = fixData || new Map();
  43. const commaIndices = fixData.get(atRule) || [];
  44. commaIndices.push(paramCommaIndex);
  45. fixData.set(atRule, commaIndices);
  46. return true;
  47. }
  48. : null,
  49. });
  50. if (fixData) {
  51. for (const [atRule, commaIndices] of fixData.entries()) {
  52. let params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
  53. for (const index of commaIndices.sort((a, b) => b - a)) {
  54. const beforeComma = params.slice(0, index + 1);
  55. const afterComma = params.slice(index + 1);
  56. if (primary.startsWith('always')) {
  57. params = /^\s*\n/.test(afterComma)
  58. ? beforeComma + afterComma.replace(/^[^\S\r\n]*/, '')
  59. : beforeComma + context.newline + afterComma;
  60. } else if (primary.startsWith('never')) {
  61. params = beforeComma + afterComma.replace(/^\s*/, '');
  62. }
  63. }
  64. if (atRule.raws.params) {
  65. atRule.raws.params.raw = params;
  66. } else {
  67. atRule.params = params;
  68. }
  69. }
  70. }
  71. };
  72. };
  73. rule.ruleName = ruleName;
  74. rule.messages = messages;
  75. rule.meta = meta;
  76. module.exports = rule;