node.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*!
  2. * Stylus - Node
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Evaluator = require('../visitor/evaluator')
  10. , utils = require('../utils')
  11. , nodes = require('./');
  12. /**
  13. * Initialize a new `CoercionError` with the given `msg`.
  14. *
  15. * @param {String} msg
  16. * @api private
  17. */
  18. function CoercionError(msg) {
  19. this.name = 'CoercionError'
  20. this.message = msg
  21. if (Error.captureStackTrace) {
  22. Error.captureStackTrace(this, CoercionError);
  23. }
  24. }
  25. /**
  26. * Inherit from `Error.prototype`.
  27. */
  28. CoercionError.prototype.__proto__ = Error.prototype;
  29. /**
  30. * Node constructor.
  31. *
  32. * @api public
  33. */
  34. var Node = module.exports = function Node(){
  35. this.lineno = nodes.lineno || 1;
  36. this.column = nodes.column || 1;
  37. this.filename = nodes.filename;
  38. };
  39. Node.prototype = {
  40. constructor: Node,
  41. /**
  42. * Return this node.
  43. *
  44. * @return {Node}
  45. * @api public
  46. */
  47. get first() {
  48. return this;
  49. },
  50. /**
  51. * Return hash.
  52. *
  53. * @return {String}
  54. * @api public
  55. */
  56. get hash() {
  57. return this.val;
  58. },
  59. /**
  60. * Return node name.
  61. *
  62. * @return {String}
  63. * @api public
  64. */
  65. get nodeName() {
  66. return this.constructor.name.toLowerCase();
  67. },
  68. /**
  69. * Return this node.
  70. *
  71. * @return {Node}
  72. * @api public
  73. */
  74. clone: function(){
  75. return this;
  76. },
  77. /**
  78. * Return a JSON representation of this node.
  79. *
  80. * @return {Object}
  81. * @api public
  82. */
  83. toJSON: function(){
  84. return {
  85. lineno: this.lineno,
  86. column: this.column,
  87. filename: this.filename
  88. };
  89. },
  90. /**
  91. * Nodes by default evaluate to themselves.
  92. *
  93. * @return {Node}
  94. * @api public
  95. */
  96. eval: function(){
  97. return new Evaluator(this).evaluate();
  98. },
  99. /**
  100. * Return true.
  101. *
  102. * @return {Boolean}
  103. * @api public
  104. */
  105. toBoolean: function(){
  106. return nodes.true;
  107. },
  108. /**
  109. * Return the expression, or wrap this node in an expression.
  110. *
  111. * @return {Expression}
  112. * @api public
  113. */
  114. toExpression: function(){
  115. if ('expression' == this.nodeName) return this;
  116. var expr = new nodes.Expression;
  117. expr.push(this);
  118. return expr;
  119. },
  120. /**
  121. * Return false if `op` is generally not coerced.
  122. *
  123. * @param {String} op
  124. * @return {Boolean}
  125. * @api private
  126. */
  127. shouldCoerce: function(op){
  128. switch (op) {
  129. case 'is a':
  130. case 'in':
  131. case '||':
  132. case '&&':
  133. return false;
  134. default:
  135. return true;
  136. }
  137. },
  138. /**
  139. * Operate on `right` with the given `op`.
  140. *
  141. * @param {String} op
  142. * @param {Node} right
  143. * @return {Node}
  144. * @api public
  145. */
  146. operate: function(op, right){
  147. switch (op) {
  148. case 'is a':
  149. if ('string' == right.first.nodeName) {
  150. return nodes.Boolean(this.nodeName == right.val);
  151. } else {
  152. throw new Error('"is a" expects a string, got ' + right.toString());
  153. }
  154. case '==':
  155. return nodes.Boolean(this.hash == right.hash);
  156. case '!=':
  157. return nodes.Boolean(this.hash != right.hash);
  158. case '>=':
  159. return nodes.Boolean(this.hash >= right.hash);
  160. case '<=':
  161. return nodes.Boolean(this.hash <= right.hash);
  162. case '>':
  163. return nodes.Boolean(this.hash > right.hash);
  164. case '<':
  165. return nodes.Boolean(this.hash < right.hash);
  166. case '||':
  167. return this.toBoolean().isTrue
  168. ? this
  169. : right;
  170. case 'in':
  171. var vals = utils.unwrap(right).nodes
  172. , len = vals && vals.length
  173. , hash = this.hash;
  174. if (!vals) throw new Error('"in" given invalid right-hand operand, expecting an expression');
  175. // 'prop' in obj
  176. if (1 == len && 'object' == vals[0].nodeName) {
  177. return nodes.Boolean(vals[0].has(this.hash));
  178. }
  179. for (var i = 0; i < len; ++i) {
  180. if (hash == vals[i].hash) {
  181. return nodes.true;
  182. }
  183. }
  184. return nodes.false;
  185. case '&&':
  186. var a = this.toBoolean()
  187. , b = right.toBoolean();
  188. return a.isTrue && b.isTrue
  189. ? right
  190. : a.isFalse
  191. ? this
  192. : right;
  193. default:
  194. if ('[]' == op) {
  195. var msg = 'cannot perform '
  196. + this
  197. + '[' + right + ']';
  198. } else {
  199. var msg = 'cannot perform'
  200. + ' ' + this
  201. + ' ' + op
  202. + ' ' + right;
  203. }
  204. throw new Error(msg);
  205. }
  206. },
  207. /**
  208. * Default coercion throws.
  209. *
  210. * @param {Node} other
  211. * @return {Node}
  212. * @api public
  213. */
  214. coerce: function(other){
  215. if (other.nodeName == this.nodeName) return other;
  216. throw new CoercionError('cannot coerce ' + other + ' to ' + this.nodeName);
  217. }
  218. };