index.js 3.1 KB

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