index.js 2.7 KB

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