index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // @ts-nocheck
  2. 'use strict';
  3. const declarationValueIndex = require('../../utils/declarationValueIndex');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const valueListCommaWhitespaceChecker = require('../valueListCommaWhitespaceChecker');
  7. const whitespaceChecker = require('../../utils/whitespaceChecker');
  8. const ruleName = 'value-list-comma-newline-after';
  9. const messages = ruleMessages(ruleName, {
  10. expectedAfter: () => 'Expected newline after ","',
  11. expectedAfterMultiLine: () => 'Expected newline after "," in a multi-line list',
  12. rejectedAfterMultiLine: () => 'Unexpected whitespace after "," in a multi-line list',
  13. });
  14. function rule(expectation, options, context) {
  15. const checker = whitespaceChecker('newline', expectation, messages);
  16. return (root, result) => {
  17. const validOptions = validateOptions(result, ruleName, {
  18. actual: expectation,
  19. possible: ['always', 'always-multi-line', 'never-multi-line'],
  20. });
  21. if (!validOptions) {
  22. return;
  23. }
  24. let fixData;
  25. valueListCommaWhitespaceChecker({
  26. root,
  27. result,
  28. locationChecker: checker.afterOneOnly,
  29. checkedRuleName: ruleName,
  30. fix: context.fix
  31. ? (declNode, index) => {
  32. const valueIndex = declarationValueIndex(declNode);
  33. if (index <= valueIndex) {
  34. return false;
  35. }
  36. fixData = fixData || new Map();
  37. const commaIndices = fixData.get(declNode) || [];
  38. commaIndices.push(index);
  39. fixData.set(declNode, commaIndices);
  40. return true;
  41. }
  42. : null,
  43. determineIndex: (declString, match) => {
  44. const nextChars = declString.substr(match.endIndex, declString.length - match.endIndex);
  45. // If there's a // comment, that means there has to be a newline
  46. // ending the comment so we're fine
  47. if (/^[ \t]*\/\//.test(nextChars)) {
  48. return false;
  49. }
  50. // If there are spaces and then a comment begins, look for the newline
  51. return /^[ \t]*\/\*/.test(nextChars)
  52. ? declString.indexOf('*/', match.endIndex) + 1
  53. : match.startIndex;
  54. },
  55. });
  56. if (fixData) {
  57. fixData.forEach((commaIndices, decl) => {
  58. commaIndices
  59. .sort((a, b) => a - b)
  60. .reverse()
  61. .forEach((index) => {
  62. const value = decl.raws.value ? decl.raws.value.raw : decl.value;
  63. const valueIndex = index - declarationValueIndex(decl);
  64. const beforeValue = value.slice(0, valueIndex + 1);
  65. let afterValue = value.slice(valueIndex + 1);
  66. if (expectation.startsWith('always')) {
  67. afterValue = context.newline + afterValue;
  68. } else if (expectation.startsWith('never-multi-line')) {
  69. afterValue = afterValue.replace(/^\s*/, '');
  70. }
  71. if (decl.raws.value) {
  72. decl.raws.value.raw = beforeValue + afterValue;
  73. } else {
  74. decl.value = beforeValue + afterValue;
  75. }
  76. });
  77. });
  78. }
  79. };
  80. }
  81. rule.ruleName = ruleName;
  82. rule.messages = messages;
  83. module.exports = rule;