24dd75ae325988b0363a96b4593c48ce8ce007ab
[platform/framework/web/crosswalk-tizen.git] /
1 "use strict";
2
3 // Important: Params is a "virtual" node type, not part of the AST spec.
4 // this hook is actually called by FunctionDeclaration and FunctionExpression
5 // hooks. It's mainly a way to share the common logic between both hooks.
6
7 var _ws = require('rocambole-whitespace');
8 var _tk = require('rocambole-token');
9 var _limit = require('../limit');
10
11
12 exports.format = function Params(node) {
13   var params = node.params;
14   var defaults = node.defaults;
15   var opening = node.startToken.value === '(' ?
16     node.startToken :
17     _tk.findNext(node.startToken, '(');
18   var closing = _tk.findPrev(node.body.startToken, ')');
19
20   if (params.length) {
21     _ws.limitBefore(_tk.findNextNonEmpty(opening), 'ParameterList');
22     params.forEach(function(param, i) {
23       // if only one param or last one there are no commas to look for
24       if (i === params.length - 1) return;
25
26       _ws.limit(_tk.findNext(param.startToken, ','), 'ParameterComma');
27     });
28     defaults.forEach(function(init) {
29       if (init) {
30         _limit.around(_tk.findPrev(init.startToken, '='), 'ParameterDefault');
31       }
32     });
33     _ws.limitAfter(_tk.findPrevNonEmpty(closing), 'ParameterList');
34   } else {
35     _limit.after(opening, 0);
36   }
37 };
38
39 exports.getIndentEdges = function(node, opts) {
40   var params = node.params;
41   if (params.length && opts.ParameterList) {
42     // get/set on ObjectEpression affect drastically the FunctionExpression
43     // structure so we need to handle it differently
44     var start = node.parent.type === 'Property' ?
45       node.parent.startToken :
46       node.startToken;
47     return {
48       // we check if start is equal to "(" because of arrow functions
49       startToken: start.value === '(' ? start : _tk.findNext(start, '('),
50       endToken: _tk.findPrev(node.body.startToken, ')'),
51       level: opts.ParameterList
52     };
53   }
54   return null;
55 };