space-after-colon.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var gonzales = require('gonzales-pe');
  2. module.exports = {
  3. name: 'space-after-colon',
  4. runBefore: 'block-indent',
  5. syntax: ['css', 'less', 'sass', 'scss'],
  6. accepts: {
  7. number: true,
  8. string: /^[ \t\n]*$/
  9. },
  10. /**
  11. * Processes tree node.
  12. *
  13. * @param {node} node
  14. */
  15. process: function(node) {
  16. if (!node.is('declaration')) return;
  17. var value = this.getValue('space-after-colon');
  18. for (var i = node.length; i--;) {
  19. if (!node.get(i).is('propertyDelimiter')) continue;
  20. if (this.getSyntax() === 'sass' && !node.get(i - 1)) break;
  21. // Remove any spaces after colon:
  22. if (node.get(i + 1).is('space')) node.remove(i + 1);
  23. // If the value set in config is not empty, add spaces:
  24. var space = gonzales.createNode({ type: 'space', content: value });
  25. if (value !== '') node.insert(i + 1, space);
  26. break;
  27. }
  28. },
  29. /**
  30. * Detects the value of an option at the tree node.
  31. *
  32. * @param {node} node
  33. */
  34. detect: function(node) {
  35. if (!node.is('declaration')) return;
  36. for (var i = node.length; i--;) {
  37. if (!node.get(i).is('propertyDelimiter')) continue;
  38. if (node.get(i + 1).is('space')) {
  39. return node.get(i + 1).content;
  40. } else {
  41. return '';
  42. }
  43. }
  44. }
  45. };