index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. fixable: true,
  18. };
  19. /** @type {import('stylelint').Rule} */
  20. const rule = (primary, _secondaryOptions, context) => {
  21. const checker = whitespaceChecker('newline', primary, messages);
  22. return (root, result) => {
  23. const validOptions = validateOptions(result, ruleName, {
  24. actual: primary,
  25. possible: ['always', 'always-multi-line', 'never-multi-line'],
  26. });
  27. if (!validOptions) {
  28. return;
  29. }
  30. /** @type {Map<import('postcss').Declaration, number[]> | undefined} */
  31. let fixData;
  32. valueListCommaWhitespaceChecker({
  33. root,
  34. result,
  35. locationChecker: checker.afterOneOnly,
  36. checkedRuleName: ruleName,
  37. fix: context.fix
  38. ? (declNode, index) => {
  39. const valueIndex = declarationValueIndex(declNode);
  40. if (index <= valueIndex) {
  41. return false;
  42. }
  43. fixData = fixData || new Map();
  44. const commaIndices = fixData.get(declNode) || [];
  45. commaIndices.push(index);
  46. fixData.set(declNode, commaIndices);
  47. return true;
  48. }
  49. : null,
  50. determineIndex: (declString, match) => {
  51. const nextChars = declString.substring(match.endIndex, declString.length);
  52. // If there's a // comment, that means there has to be a newline
  53. // ending the comment so we're fine
  54. if (/^[ \t]*\/\//.test(nextChars)) {
  55. return false;
  56. }
  57. // If there are spaces and then a comment begins, look for the newline
  58. return /^[ \t]*\/\*/.test(nextChars)
  59. ? declString.indexOf('*/', match.endIndex) + 1
  60. : match.startIndex;
  61. },
  62. });
  63. if (fixData) {
  64. for (const [decl, commaIndices] of fixData.entries()) {
  65. for (const index of commaIndices.sort((a, b) => a - b).reverse()) {
  66. const value = getDeclarationValue(decl);
  67. const valueIndex = index - declarationValueIndex(decl);
  68. const beforeValue = value.slice(0, valueIndex + 1);
  69. let afterValue = value.slice(valueIndex + 1);
  70. if (primary.startsWith('always')) {
  71. afterValue = context.newline + afterValue;
  72. } else if (primary.startsWith('never-multi-line')) {
  73. afterValue = afterValue.replace(/^\s*/, '');
  74. }
  75. setDeclarationValue(decl, beforeValue + afterValue);
  76. }
  77. }
  78. }
  79. };
  80. };
  81. rule.ruleName = ruleName;
  82. rule.messages = messages;
  83. rule.meta = meta;
  84. module.exports = rule;