index.js 2.7 KB

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