declarationColonSpaceChecker.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. const declarationValueIndex = require('../utils/declarationValueIndex');
  3. const isStandardSyntaxDeclaration = require('../utils/isStandardSyntaxDeclaration');
  4. const report = require('../utils/report');
  5. /** @typedef {(args: { source: string, index: number, lineCheckStr: string, err: (message: string) => void }) => void} LocationChecker */
  6. /**
  7. * @param {{
  8. * root: import('postcss').Root,
  9. * locationChecker: LocationChecker,
  10. * fix: ((decl: import('postcss').Declaration, index: number) => boolean) | null,
  11. * result: import('stylelint').PostcssResult,
  12. * checkedRuleName: string,
  13. * }} opts
  14. */
  15. module.exports = function declarationColonSpaceChecker(opts) {
  16. opts.root.walkDecls((decl) => {
  17. if (!isStandardSyntaxDeclaration(decl)) {
  18. return;
  19. }
  20. // Get the raw prop, and only the prop
  21. const endOfPropIndex = declarationValueIndex(decl) + (decl.raws.between || '').length - 1;
  22. // The extra characters tacked onto the end ensure that there is a character to check
  23. // after the colon. Otherwise, with `background:pink` the character after the
  24. const propPlusColon = `${decl.toString().slice(0, endOfPropIndex)}xxx`;
  25. for (let i = 0, l = propPlusColon.length; i < l; i++) {
  26. if (propPlusColon[i] !== ':') {
  27. continue;
  28. }
  29. opts.locationChecker({
  30. source: propPlusColon,
  31. index: i,
  32. lineCheckStr: decl.value,
  33. err: (message) => {
  34. if (opts.fix && opts.fix(decl, i)) {
  35. return;
  36. }
  37. report({
  38. message,
  39. node: decl,
  40. index: decl.prop.toString().length + 1,
  41. result: opts.result,
  42. ruleName: opts.checkedRuleName,
  43. });
  44. },
  45. });
  46. break;
  47. }
  48. });
  49. };