ident.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*!
  2. * Stylus - Ident
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Node = require('./node')
  10. , nodes = require('./');
  11. /**
  12. * Initialize a new `Ident` by `name` with the given `val` node.
  13. *
  14. * @param {String} name
  15. * @param {Node} val
  16. * @api public
  17. */
  18. var Ident = module.exports = function Ident(name, val, mixin){
  19. Node.call(this);
  20. this.name = name;
  21. this.string = name;
  22. this.val = val || nodes.null;
  23. this.mixin = !!mixin;
  24. };
  25. /**
  26. * Check if the variable has a value.
  27. *
  28. * @return {Boolean}
  29. * @api public
  30. */
  31. Ident.prototype.__defineGetter__('isEmpty', function(){
  32. return undefined == this.val;
  33. });
  34. /**
  35. * Return hash.
  36. *
  37. * @return {String}
  38. * @api public
  39. */
  40. Ident.prototype.__defineGetter__('hash', function(){
  41. return this.name;
  42. });
  43. /**
  44. * Inherit from `Node.prototype`.
  45. */
  46. Ident.prototype.__proto__ = Node.prototype;
  47. /**
  48. * Return a clone of this node.
  49. *
  50. * @return {Node}
  51. * @api public
  52. */
  53. Ident.prototype.clone = function(parent){
  54. var clone = new Ident(this.name);
  55. clone.val = this.val.clone(parent, clone);
  56. clone.mixin = this.mixin;
  57. clone.lineno = this.lineno;
  58. clone.column = this.column;
  59. clone.filename = this.filename;
  60. clone.property = this.property;
  61. clone.rest = this.rest;
  62. return clone;
  63. };
  64. /**
  65. * Return a JSON representation of this node.
  66. *
  67. * @return {Object}
  68. * @api public
  69. */
  70. Ident.prototype.toJSON = function(){
  71. return {
  72. __type: 'Ident',
  73. name: this.name,
  74. val: this.val,
  75. mixin: this.mixin,
  76. property: this.property,
  77. rest: this.rest,
  78. lineno: this.lineno,
  79. column: this.column,
  80. filename: this.filename
  81. };
  82. };
  83. /**
  84. * Return <name>.
  85. *
  86. * @return {String}
  87. * @api public
  88. */
  89. Ident.prototype.toString = function(){
  90. return this.name;
  91. };
  92. /**
  93. * Coerce `other` to an ident.
  94. *
  95. * @param {Node} other
  96. * @return {String}
  97. * @api public
  98. */
  99. Ident.prototype.coerce = function(other){
  100. switch (other.nodeName) {
  101. case 'ident':
  102. case 'string':
  103. case 'literal':
  104. return new Ident(other.string);
  105. case 'unit':
  106. return new Ident(other.toString());
  107. default:
  108. return Node.prototype.coerce.call(this, other);
  109. }
  110. };
  111. /**
  112. * Operate on `right` with the given `op`.
  113. *
  114. * @param {String} op
  115. * @param {Node} right
  116. * @return {Node}
  117. * @api public
  118. */
  119. Ident.prototype.operate = function(op, right){
  120. var val = right.first;
  121. switch (op) {
  122. case '-':
  123. if ('unit' == val.nodeName) {
  124. var expr = new nodes.Expression;
  125. val = val.clone();
  126. val.val = -val.val;
  127. expr.push(this);
  128. expr.push(val);
  129. return expr;
  130. }
  131. case '+':
  132. return new nodes.Ident(this.string + this.coerce(val).string);
  133. }
  134. return Node.prototype.operate.call(this, op, right);
  135. };