index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/list/declaration-colon-newline-after',
  15. };
  16. /** @type {import('stylelint').Rule} */
  17. const rule = (primary, _secondaryOptions, context) => {
  18. const checker = whitespaceChecker('newline', primary, messages);
  19. return (root, result) => {
  20. const validOptions = validateOptions(result, ruleName, {
  21. actual: primary,
  22. possible: ['always', 'always-multi-line'],
  23. });
  24. if (!validOptions) {
  25. return;
  26. }
  27. root.walkDecls((decl) => {
  28. if (!isStandardSyntaxDeclaration(decl)) {
  29. return;
  30. }
  31. // Get the raw prop, and only the prop
  32. const endOfPropIndex = declarationValueIndex(decl) + (decl.raws.between || '').length - 1;
  33. // The extra characters tacked onto the end ensure that there is a character to check
  34. // after the colon. Otherwise, with `background:pink` the character after the
  35. const propPlusColon = `${decl.toString().slice(0, endOfPropIndex)}xxx`;
  36. for (let i = 0, l = propPlusColon.length; i < l; i++) {
  37. if (propPlusColon[i] !== ':') {
  38. continue;
  39. }
  40. const indexToCheck = /^[^\S\r\n]*\/\*/.test(propPlusColon.slice(i + 1))
  41. ? propPlusColon.indexOf('*/', i) + 1
  42. : i;
  43. checker.afterOneOnly({
  44. source: propPlusColon,
  45. index: indexToCheck,
  46. lineCheckStr: decl.value,
  47. err: (m) => {
  48. if (context.fix) {
  49. const between = decl.raws.between;
  50. if (between == null) throw new Error('`between` must be present');
  51. const betweenStart = declarationValueIndex(decl) - between.length;
  52. const sliceIndex = indexToCheck - betweenStart + 1;
  53. const betweenBefore = between.slice(0, sliceIndex);
  54. const betweenAfter = between.slice(sliceIndex);
  55. decl.raws.between = /^\s*\n/.test(betweenAfter)
  56. ? betweenBefore + betweenAfter.replace(/^[^\S\r\n]*/, '')
  57. : betweenBefore + context.newline + betweenAfter;
  58. return;
  59. }
  60. report({
  61. message: m,
  62. node: decl,
  63. index: indexToCheck,
  64. result,
  65. ruleName,
  66. });
  67. },
  68. });
  69. }
  70. });
  71. };
  72. };
  73. rule.ruleName = ruleName;
  74. rule.messages = messages;
  75. rule.meta = meta;
  76. module.exports = rule;