index.js 2.8 KB

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