ident.js
2.73 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
/*!
* Stylus - Ident
* Copyright (c) Automattic <developer.wordpress.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var Node = require('./node')
, nodes = require('./');
/**
* Initialize a new `Ident` by `name` with the given `val` node.
*
* @param {String} name
* @param {Node} val
* @api public
*/
var Ident = module.exports = function Ident(name, val, mixin){
Node.call(this);
this.name = name;
this.string = name;
this.val = val || nodes.null;
this.mixin = !!mixin;
};
/**
* Check if the variable has a value.
*
* @return {Boolean}
* @api public
*/
Ident.prototype.__defineGetter__('isEmpty', function(){
return undefined == this.val;
});
/**
* Return hash.
*
* @return {String}
* @api public
*/
Ident.prototype.__defineGetter__('hash', function(){
return this.name;
});
/**
* Inherit from `Node.prototype`.
*/
Ident.prototype.__proto__ = Node.prototype;
/**
* Return a clone of this node.
*
* @return {Node}
* @api public
*/
Ident.prototype.clone = function(parent){
var clone = new Ident(this.name);
clone.val = this.val.clone(parent, clone);
clone.mixin = this.mixin;
clone.lineno = this.lineno;
clone.column = this.column;
clone.filename = this.filename;
clone.property = this.property;
clone.rest = this.rest;
return clone;
};
/**
* Return a JSON representation of this node.
*
* @return {Object}
* @api public
*/
Ident.prototype.toJSON = function(){
return {
__type: 'Ident',
name: this.name,
val: this.val,
mixin: this.mixin,
property: this.property,
rest: this.rest,
lineno: this.lineno,
column: this.column,
filename: this.filename
};
};
/**
* Return <name>.
*
* @return {String}
* @api public
*/
Ident.prototype.toString = function(){
return this.name;
};
/**
* Coerce `other` to an ident.
*
* @param {Node} other
* @return {String}
* @api public
*/
Ident.prototype.coerce = function(other){
switch (other.nodeName) {
case 'ident':
case 'string':
case 'literal':
return new Ident(other.string);
case 'unit':
return new Ident(other.toString());
default:
return Node.prototype.coerce.call(this, other);
}
};
/**
* Operate on `right` with the given `op`.
*
* @param {String} op
* @param {Node} right
* @return {Node}
* @api public
*/
Ident.prototype.operate = function(op, right){
var val = right.first;
switch (op) {
case '-':
if ('unit' == val.nodeName) {
var expr = new nodes.Expression;
val = val.clone();
val.val = -val.val;
expr.push(this);
expr.push(val);
return expr;
}
case '+':
return new nodes.Ident(this.string + this.coerce(val).string);
}
return Node.prototype.operate.call(this, op, right);
};