1 | var assert = require('assert');
|
2 |
|
3 | var FUNCTION_TYPE_RE = /Function/;
|
4 | var PAREN_KEYWORD_TYPE_RE = /Statement$|^CatchClause$/;
|
5 | var NO_PAREN_KEYWORD_TYPE_RE = /^ExpressionStatement$|^ReturnStatement$|^ThrowStatement$/;
|
6 | var QUASI_STATEMENT_TYPE_RE = /Statement$|Declaration$/;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | exports.categorizeOpenParen = function(token, file) {
|
17 | assert(token.value === '(', 'Input token must be a parenthesis');
|
18 | var node = file.getNodeByRange(token.range[0]);
|
19 | var nodeType = node.type;
|
20 | var prevToken = file.getPrevToken(token);
|
21 |
|
22 |
|
23 | if (!prevToken) {
|
24 | return 'ParenthesizedExpression';
|
25 | }
|
26 |
|
27 |
|
28 | if (prevToken.type === 'Keyword' && PAREN_KEYWORD_TYPE_RE.test(nodeType) &&
|
29 | !NO_PAREN_KEYWORD_TYPE_RE.test(nodeType)) {
|
30 |
|
31 | return 'Statement';
|
32 | }
|
33 |
|
34 |
|
35 | if (FUNCTION_TYPE_RE.test(nodeType) &&
|
36 |
|
37 |
|
38 | (prevToken.type === 'Identifier' || prevToken.value === 'function')) {
|
39 |
|
40 | return 'Function';
|
41 | }
|
42 |
|
43 |
|
44 | var prevNode = file.getNodeByRange(prevToken.range[0]);
|
45 | if ((nodeType === 'CallExpression' || nodeType === 'NewExpression') &&
|
46 |
|
47 |
|
48 | prevToken.value !== ',' && prevToken.value !== '(' &&
|
49 |
|
50 |
|
51 |
|
52 | (prevNode === node || prevToken === file.getLastNodeToken(node.callee))) {
|
53 |
|
54 | return 'CallExpression';
|
55 | }
|
56 |
|
57 |
|
58 | return 'ParenthesizedExpression';
|
59 | };
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | exports.categorizeCloseParen = function(token, file) {
|
70 | assert(token.value === ')', 'Input token must be a parenthesis');
|
71 | var node = file.getNodeByRange(token.range[0]);
|
72 | var nodeType = node.type;
|
73 | var nextToken = file.getNextToken(token);
|
74 |
|
75 |
|
76 | if (nextToken.type === 'EOF') {
|
77 | switch (nodeType) {
|
78 | case 'DoWhileStatement':
|
79 | return 'Statement';
|
80 | case 'CallExpression':
|
81 | case 'NewExpression':
|
82 | return 'CallExpression';
|
83 | default:
|
84 | return 'ParenthesizedExpression';
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 | if (PAREN_KEYWORD_TYPE_RE.test(nodeType) && !NO_PAREN_KEYWORD_TYPE_RE.test(nodeType)) {
|
90 |
|
91 |
|
92 | if (nextToken.value === '{' || nextToken.value === '}') {
|
93 | return 'Statement';
|
94 | }
|
95 |
|
96 |
|
97 | var nextNode = file.getNodeByRange(nextToken.range[0]);
|
98 | while (nextNode.range[0] >= token.range[1]) {
|
99 | if (QUASI_STATEMENT_TYPE_RE.test(nextNode.type)) {
|
100 | return 'Statement';
|
101 | }
|
102 | nextNode = nextNode.parentNode;
|
103 | }
|
104 | }
|
105 |
|
106 |
|
107 | if (nextToken.value === '{' && FUNCTION_TYPE_RE.test(nodeType)) {
|
108 | return 'Function';
|
109 | }
|
110 |
|
111 |
|
112 | if ((nodeType === 'CallExpression' || nodeType === 'NewExpression') &&
|
113 | nextToken.range[0] >= node.range[1]) {
|
114 |
|
115 | return 'CallExpression';
|
116 | }
|
117 |
|
118 |
|
119 | return 'ParenthesizedExpression';
|
120 | };
|