a24c0d1c5bd9d41aaa81cdfa407cae6d724b1835
[platform/framework/web/crosswalk-tizen.git] /
1 "use strict";
2
3 var _br = require('rocambole-linebreak');
4 var _tk = require('rocambole-token');
5 var _ws = require('rocambole-whitespace');
6 var _limit = require('../limit');
7
8
9 exports.format = function IfStatement(node) {
10
11   var startBody = node.consequent.startToken;
12   var endBody = node.consequent.endToken;
13
14   var conditionalStart = _tk.findPrev(node.test.startToken, '(');
15   var conditionalEnd = _tk.findNext(node.test.endToken, ')');
16
17   _ws.limit(conditionalStart, 'IfStatementConditionalOpening');
18   _ws.limit(conditionalEnd, 'IfStatementConditionalClosing');
19
20   var alt = node.alternate;
21   if (alt) {
22     var elseKeyword = _tk.findPrev(alt.startToken, 'else');
23
24     if (alt.type === 'IfStatement') {
25       // ElseIfStatement
26       _br.limitBefore(alt.startToken, 0);
27       _ws.limitBefore(alt.startToken, 1);
28
29       if (alt.consequent.type === 'BlockStatement') {
30         _br.limitBefore(alt.consequent.startToken, 'ElseIfStatementOpeningBrace');
31         _br.limitBefore(alt.consequent.endToken, 'ElseIfStatementClosingBrace');
32       }
33
34       _br.limitBefore(elseKeyword, 'ElseIfStatement');
35       if (! alt.alternate) {
36         // we only limit the line breaks after the ElseIfStatement if it is not
37         // followed by an ElseStatement, otherwise it would add line breaks
38         // that it shouldn't
39         _br.limitAfter(alt.consequent.endToken, 'ElseIfStatement');
40       }
41
42     } else if (alt.type === 'BlockStatement') {
43       // ElseStatement
44
45       _limit.around(alt.startToken, 'ElseStatementOpeningBrace');
46
47       _br.limitBefore(elseKeyword, 'ElseStatement');
48       _br.limitAfter(alt.endToken, 'ElseStatement');
49
50       _ws.limitBefore(elseKeyword, 1);
51
52       _limit.around(alt.endToken, 'ElseStatementClosingBrace');
53     } else {
54       // ElseStatement without curly braces
55       _ws.limitAfter(elseKeyword, 1);
56     }
57   }
58
59   // only handle braces if block statement
60   if (node.consequent.type === 'BlockStatement') {
61     _limit.around(startBody, 'IfStatementOpeningBrace');
62     if (!alt) {
63       _br.limit(endBody, 'IfStatementClosingBrace');
64     } else {
65       _br.limitBefore(endBody, 'IfStatementClosingBrace');
66     }
67     _ws.limit(endBody, 'IfStatementClosingBrace');
68   }
69
70 };
71
72
73 exports.getIndentEdges = function(node, opts) {
74   var edges = [];
75
76   var test = node.test;
77   var consequent = node.consequent;
78   var alt = node.alternate;
79
80   // test (IfStatementConditional)
81   edges.push({
82     level: opts.IfStatementConditional,
83     startToken: _tk.findNext(node.startToken, '('),
84     endToken: _tk.findPrev(consequent.startToken, ')'),
85   });
86
87   function isExecutable(token) {
88     return _tk.isNotEmpty(token) && !_tk.isComment(token);
89   }
90
91   // consequent (body)
92   edges.push({
93     startToken: (
94       consequent.type === 'BlockStatement' ?
95       consequent.startToken :
96       test.endToken.next
97     ),
98     // we have some special rules for comments just before the `else` statement
99     // because of jQuery style guide. maybe in the future we will add
100     // a setting to toggle this behavior (if someone asks for it)
101     endToken: (
102       alt && _tk.isComment(_tk.findPrevNonEmpty(consequent.endToken)) ?
103       _tk.findPrev(consequent.endToken, isExecutable).next :
104       consequent.endToken
105     )
106   });
107
108   // alt (else)
109   if (alt && alt.type !== 'IfStatement') {
110     // it the alternate is IfStatement it will already take care of indentation
111     edges.push({
112       startToken: (
113         alt.type === 'BlockStatement' ?
114         alt.startToken :
115         _tk.findPrevNonEmpty(alt.startToken).next
116       ),
117       endToken: alt.endToken
118     });
119   }
120
121   return edges;
122 };