index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. const declarationValueIndex = require('../../utils/declarationValueIndex');
  3. const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const whitespaceChecker = require('../../utils/whitespaceChecker');
  8. const ruleName = 'declaration-colon-newline-after';
  9. const messages = ruleMessages(ruleName, {
  10. expectedAfter: () => 'Expected newline after ":"',
  11. expectedAfterMultiLine: () => 'Expected newline after ":" with a multi-line declaration',
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/declaration-colon-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'],
  24. });
  25. if (!validOptions) {
  26. return;
  27. }
  28. root.walkDecls((decl) => {
  29. if (!isStandardSyntaxDeclaration(decl)) {
  30. return;
  31. }
  32. // Get the raw prop, and only the prop
  33. const endOfPropIndex = declarationValueIndex(decl) + (decl.raws.between || '').length - 1;
  34. // The extra characters tacked onto the end ensure that there is a character to check
  35. // after the colon. Otherwise, with `background:pink` the character after the
  36. const propPlusColon = `${decl.toString().slice(0, endOfPropIndex)}xxx`;
  37. for (let i = 0, l = propPlusColon.length; i < l; i++) {
  38. if (propPlusColon[i] !== ':') {
  39. continue;
  40. }
  41. const indexToCheck = /^[^\S\r\n]*\/\*/.test(propPlusColon.slice(i + 1))
  42. ? propPlusColon.indexOf('*/', i) + 1
  43. : i;
  44. checker.afterOneOnly({
  45. source: propPlusColon,
  46. index: indexToCheck,
  47. lineCheckStr: decl.value,
  48. err: (m) => {
  49. if (context.fix) {
  50. const between = decl.raws.between;
  51. if (between == null) throw new Error('`between` must be present');
  52. const betweenStart = declarationValueIndex(decl) - between.length;
  53. const sliceIndex = indexToCheck - betweenStart + 1;
  54. const betweenBefore = between.slice(0, sliceIndex);
  55. const betweenAfter = between.slice(sliceIndex);
  56. decl.raws.between = /^\s*\n/.test(betweenAfter)
  57. ? betweenBefore + betweenAfter.replace(/^[^\S\r\n]*/, '')
  58. : betweenBefore + context.newline + betweenAfter;
  59. return;
  60. }
  61. report({
  62. message: m,
  63. node: decl,
  64. index: indexToCheck,
  65. result,
  66. ruleName,
  67. });
  68. },
  69. });
  70. }
  71. });
  72. };
  73. };
  74. rule.ruleName = ruleName;
  75. rule.messages = messages;
  76. rule.meta = meta;
  77. module.exports = rule;