Blame view

node_modules/stylus/lib/nodes/function.js 2.2 KB
ce4c83ff   wxy   初始提交
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
  
  /*!
   * Stylus - Function
   * Copyright (c) Automattic <developer.wordpress.com>
   * MIT Licensed
   */
  
  /**
   * Module dependencies.
   */
  
  var Node = require('./node');
  
  /**
   * Initialize a new `Function` with `name`, `params`, and `body`.
   *
   * @param {String} name
   * @param {Params|Function} params
   * @param {Block} body
   * @api public
   */
  
  var Function = module.exports = function Function(name, params, body){
    Node.call(this);
    this.name = name;
    this.params = params;
    this.block = body;
    if ('function' == typeof params) this.fn = params;
  };
  
  /**
   * Check function arity.
   *
   * @return {Boolean}
   * @api public
   */
  
  Function.prototype.__defineGetter__('arity', function(){
    return this.params.length;
  });
  
  /**
   * Inherit from `Node.prototype`.
   */
  
  Function.prototype.__proto__ = Node.prototype;
  
  /**
   * Return hash.
   *
   * @return {String}
   * @api public
   */
  
  Function.prototype.__defineGetter__('hash', function(){
    return 'function ' + this.name;
  });
  
  /**
   * Return a clone of this node.
   * 
   * @return {Node}
   * @api public
   */
  
  Function.prototype.clone = function(parent){
    if (this.fn) {
      var clone = new Function(
          this.name
        , this.fn);
    } else {
      var clone = new Function(this.name);
      clone.params = this.params.clone(parent, clone);
      clone.block = this.block.clone(parent, clone);
    }
    clone.lineno = this.lineno;
    clone.column = this.column;
    clone.filename = this.filename;
    return clone;
  };
  
  /**
   * Return <name>(param1, param2, ...).
   *
   * @return {String}
   * @api public
   */
  
  Function.prototype.toString = function(){
    if (this.fn) {
      return this.name
        + '('
        + this.fn.toString()
          .match(/^function *\w*\((.*?)\)/)
          .slice(1)
          .join(', ')
        + ')';
    } else {
      return this.name
        + '('
        + this.params.nodes.join(', ')
        + ')';
    }
  };
  
  /**
   * Return a JSON representation of this node.
   *
   * @return {Object}
   * @api public
   */
  
  Function.prototype.toJSON = function(){
    var json = {
      __type: 'Function',
      name: this.name,
      lineno: this.lineno,
      column: this.column,
      filename: this.filename
    };
    if (this.fn) {
      json.fn = this.fn;
    } else {
      json.params = this.params;
      json.block = this.block;
    }
    return json;
  };