index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const getDeclarationValue = require('../../utils/getDeclarationValue');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const setDeclarationValue = require('../../utils/setDeclarationValue');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const valueListCommaWhitespaceChecker = require('../valueListCommaWhitespaceChecker');
  8. const whitespaceChecker = require('../../utils/whitespaceChecker');
  9. const ruleName = 'value-list-comma-newline-after';
  10. const messages = ruleMessages(ruleName, {
  11. expectedAfter: () => 'Expected newline after ","',
  12. expectedAfterMultiLine: () => 'Expected newline after "," in a multi-line list',
  13. rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line list',
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/value-list-comma-newline-after',
  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. /** @type {Map<import('postcss').Declaration, number[]> | undefined} */
  30. let fixData;
  31. valueListCommaWhitespaceChecker({
  32. root,
  33. result,
  34. locationChecker: checker.afterOneOnly,
  35. checkedRuleName: ruleName,
  36. fix: context.fix
  37. ? (declNode, index) => {
  38. const valueIndex = declarationValueIndex(declNode);
  39. if (index <= valueIndex) {
  40. return false;
  41. }
  42. fixData = fixData || new Map();
  43. const commaIndices = fixData.get(declNode) || [];
  44. commaIndices.push(index);
  45. fixData.set(declNode, commaIndices);
  46. return true;
  47. }
  48. : null,
  49. determineIndex: (declString, match) => {
  50. const nextChars = declString.substring(match.endIndex, declString.length);
  51. // If there's a // comment, that means there has to be a newline
  52. // ending the comment so we're fine
  53. if (/^[ \t]*\/\//.test(nextChars)) {
  54. return false;
  55. }
  56. // If there are spaces and then a comment begins, look for the newline
  57. return /^[ \t]*\/\*/.test(nextChars)
  58. ? declString.indexOf('*/', match.endIndex) + 1
  59. : match.startIndex;
  60. },
  61. });
  62. if (fixData) {
  63. for (const [decl, commaIndices] of fixData.entries()) {
  64. for (const index of commaIndices.sort((a, b) => a - b).reverse()) {
  65. const value = getDeclarationValue(decl);
  66. const valueIndex = index - declarationValueIndex(decl);
  67. const beforeValue = value.slice(0, valueIndex + 1);
  68. let afterValue = value.slice(valueIndex + 1);
  69. if (primary.startsWith('always')) {
  70. afterValue = context.newline + afterValue;
  71. } else if (primary.startsWith('never-multi-line')) {
  72. afterValue = afterValue.replace(/^\s*/, '');
  73. }
  74. setDeclarationValue(decl, beforeValue + afterValue);
  75. }
  76. }
  77. }
  78. };
  79. };
  80. rule.ruleName = ruleName;
  81. rule.messages = messages;
  82. rule.meta = meta;
  83. module.exports = rule;