feature.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*!
  2. * Stylus - Feature
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Node = require('./node');
  10. /**
  11. * Initialize a new `Feature` with the given `segs`.
  12. *
  13. * @param {Array} segs
  14. * @api public
  15. */
  16. var Feature = module.exports = function Feature(segs){
  17. Node.call(this);
  18. this.segments = segs;
  19. this.expr = null;
  20. };
  21. /**
  22. * Inherit from `Node.prototype`.
  23. */
  24. Feature.prototype.__proto__ = Node.prototype;
  25. /**
  26. * Return a clone of this node.
  27. *
  28. * @return {Node}
  29. * @api public
  30. */
  31. Feature.prototype.clone = function(parent){
  32. var clone = new Feature;
  33. clone.segments = this.segments.map(function(node){ return node.clone(parent, clone); });
  34. if (this.expr) clone.expr = this.expr.clone(parent, clone);
  35. if (this.name) clone.name = this.name;
  36. clone.lineno = this.lineno;
  37. clone.column = this.column;
  38. clone.filename = this.filename;
  39. return clone;
  40. };
  41. /**
  42. * Return "<ident>" or "(<ident>: <expr>)"
  43. *
  44. * @return {String}
  45. * @api public
  46. */
  47. Feature.prototype.toString = function(){
  48. if (this.expr) {
  49. return '(' + this.segments.join('') + ': ' + this.expr.toString() + ')';
  50. } else {
  51. return this.segments.join('');
  52. }
  53. };
  54. /**
  55. * Return a JSON representation of this node.
  56. *
  57. * @return {Object}
  58. * @api public
  59. */
  60. Feature.prototype.toJSON = function(){
  61. var json = {
  62. __type: 'Feature',
  63. segments: this.segments,
  64. lineno: this.lineno,
  65. column: this.column,
  66. filename: this.filename
  67. };
  68. if (this.expr) json.expr = this.expr;
  69. if (this.name) json.name = this.name;
  70. return json;
  71. };