binop.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*!
  2. * Stylus - BinOp
  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 `BinOp` with `op`, `left` and `right`.
  12. *
  13. * @param {String} op
  14. * @param {Node} left
  15. * @param {Node} right
  16. * @api public
  17. */
  18. var BinOp = module.exports = function BinOp(op, left, right){
  19. Node.call(this);
  20. this.op = op;
  21. this.left = left;
  22. this.right = right;
  23. };
  24. /**
  25. * Inherit from `Node.prototype`.
  26. */
  27. BinOp.prototype.__proto__ = Node.prototype;
  28. /**
  29. * Return a clone of this node.
  30. *
  31. * @return {Node}
  32. * @api public
  33. */
  34. BinOp.prototype.clone = function(parent){
  35. var clone = new BinOp(this.op);
  36. clone.left = this.left.clone(parent, clone);
  37. clone.right = this.right && this.right.clone(parent, clone);
  38. clone.lineno = this.lineno;
  39. clone.column = this.column;
  40. clone.filename = this.filename;
  41. if (this.val) clone.val = this.val.clone(parent, clone);
  42. return clone;
  43. };
  44. /**
  45. * Return <left> <op> <right>
  46. *
  47. * @return {String}
  48. * @api public
  49. */
  50. BinOp.prototype.toString = function() {
  51. return this.left.toString() + ' ' + this.op + ' ' + this.right.toString();
  52. };
  53. /**
  54. * Return a JSON representation of this node.
  55. *
  56. * @return {Object}
  57. * @api public
  58. */
  59. BinOp.prototype.toJSON = function(){
  60. var json = {
  61. __type: 'BinOp',
  62. left: this.left,
  63. right: this.right,
  64. op: this.op,
  65. lineno: this.lineno,
  66. column: this.column,
  67. filename: this.filename
  68. };
  69. if (this.val) json.val = this.val;
  70. return json;
  71. };