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