1 |
|
---|
2 | /*!
|
---|
3 | * Stylus - Function
|
---|
4 | * Copyright (c) Automattic <developer.wordpress.com>
|
---|
5 | * MIT Licensed
|
---|
6 | */
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Module dependencies.
|
---|
10 | */
|
---|
11 |
|
---|
12 | var Node = require('./node');
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Initialize a new `Function` with `name`, `params`, and `body`.
|
---|
16 | *
|
---|
17 | * @param {String} name
|
---|
18 | * @param {Params|Function} params
|
---|
19 | * @param {Block} body
|
---|
20 | * @api public
|
---|
21 | */
|
---|
22 |
|
---|
23 | var Function = module.exports = function Function(name, params, body){
|
---|
24 | Node.call(this);
|
---|
25 | this.name = name;
|
---|
26 | this.params = params;
|
---|
27 | this.block = body;
|
---|
28 | if ('function' == typeof params) this.fn = params;
|
---|
29 | };
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Check function arity.
|
---|
33 | *
|
---|
34 | * @return {Boolean}
|
---|
35 | * @api public
|
---|
36 | */
|
---|
37 |
|
---|
38 | Function.prototype.__defineGetter__('arity', function(){
|
---|
39 | return this.params.length;
|
---|
40 | });
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Inherit from `Node.prototype`.
|
---|
44 | */
|
---|
45 |
|
---|
46 | Function.prototype.__proto__ = Node.prototype;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Return hash.
|
---|
50 | *
|
---|
51 | * @return {String}
|
---|
52 | * @api public
|
---|
53 | */
|
---|
54 |
|
---|
55 | Function.prototype.__defineGetter__('hash', function(){
|
---|
56 | return 'function ' + this.name;
|
---|
57 | });
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Return a clone of this node.
|
---|
61 | *
|
---|
62 | * @return {Node}
|
---|
63 | * @api public
|
---|
64 | */
|
---|
65 |
|
---|
66 | Function.prototype.clone = function(parent){
|
---|
67 | if (this.fn) {
|
---|
68 | var clone = new Function(
|
---|
69 | this.name
|
---|
70 | , this.fn);
|
---|
71 | } else {
|
---|
72 | var clone = new Function(this.name);
|
---|
73 | clone.params = this.params.clone(parent, clone);
|
---|
74 | clone.block = this.block.clone(parent, clone);
|
---|
75 | }
|
---|
76 | clone.lineno = this.lineno;
|
---|
77 | clone.column = this.column;
|
---|
78 | clone.filename = this.filename;
|
---|
79 | return clone;
|
---|
80 | };
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Return <name>(param1, param2, ...).
|
---|
84 | *
|
---|
85 | * @return {String}
|
---|
86 | * @api public
|
---|
87 | */
|
---|
88 |
|
---|
89 | Function.prototype.toString = function(){
|
---|
90 | if (this.fn) {
|
---|
91 | return this.name
|
---|
92 | + '('
|
---|
93 | + this.fn.toString()
|
---|
94 | .match(/^function *\w*\((.*?)\)/)
|
---|
95 | .slice(1)
|
---|
96 | .join(', ')
|
---|
97 | + ')';
|
---|
98 | } else {
|
---|
99 | return this.name
|
---|
100 | + '('
|
---|
101 | + this.params.nodes.join(', ')
|
---|
102 | + ')';
|
---|
103 | }
|
---|
104 | };
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Return a JSON representation of this node.
|
---|
108 | *
|
---|
109 | * @return {Object}
|
---|
110 | * @api public
|
---|
111 | */
|
---|
112 |
|
---|
113 | Function.prototype.toJSON = function(){
|
---|
114 | var json = {
|
---|
115 | __type: 'Function',
|
---|
116 | name: this.name,
|
---|
117 | lineno: this.lineno,
|
---|
118 | column: this.column,
|
---|
119 | filename: this.filename
|
---|
120 | };
|
---|
121 | if (this.fn) {
|
---|
122 | json.fn = this.fn;
|
---|
123 | } else {
|
---|
124 | json.params = this.params;
|
---|
125 | json.block = this.block;
|
---|
126 | }
|
---|
127 | return json;
|
---|
128 | };
|
---|