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 | function nodeContains(node, token) {
|
9 | var currentNode = token.parentElement;
|
10 | while (currentNode) {
|
11 | if (currentNode === node) {
|
12 | return true;
|
13 | }
|
14 | currentNode = currentNode.parentElement;
|
15 | }
|
16 | return false;
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | exports.categorizeOpenParen = function(token) {
|
27 | assert(token.value === '(', 'Input token must be a parenthesis');
|
28 | var node = token.parentElement;
|
29 | var nodeType = node.type;
|
30 | var prevToken = token.getPreviousCodeToken();
|
31 |
|
32 |
|
33 | if (!prevToken) {
|
34 | return 'ParenthesizedExpression';
|
35 | }
|
36 |
|
37 |
|
38 | if (prevToken.type === 'Keyword' && PAREN_KEYWORD_TYPE_RE.test(nodeType) &&
|
39 | !NO_PAREN_KEYWORD_TYPE_RE.test(nodeType)) {
|
40 |
|
41 | return 'Statement';
|
42 | }
|
43 |
|
44 |
|
45 | if (FUNCTION_TYPE_RE.test(nodeType) &&
|
46 |
|
47 |
|
48 | (prevToken.type === 'Identifier' || prevToken.value === 'function')) {
|
49 |
|
50 | return 'Function';
|
51 | }
|
52 |
|
53 |
|
54 | var prevNode = prevToken.parentElement;
|
55 | if ((nodeType === 'CallExpression' || nodeType === 'NewExpression') &&
|
56 |
|
57 |
|
58 | prevToken.value !== ',' && prevToken.value !== '(' &&
|
59 |
|
60 |
|
61 |
|
62 | (prevNode === node || prevToken === node.callee.getLastToken())) {
|
63 |
|
64 | return 'CallExpression';
|
65 | }
|
66 |
|
67 |
|
68 | return 'ParenthesizedExpression';
|
69 | };
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | exports.categorizeCloseParen = function(token) {
|
79 | assert(token.value === ')', 'Input token must be a parenthesis');
|
80 | var node = token.parentElement;
|
81 | var nodeType = node.type;
|
82 | var nextToken = token.getNextCodeToken();
|
83 |
|
84 |
|
85 | if (nextToken.type === 'EOF') {
|
86 | switch (nodeType) {
|
87 | case 'DoWhileStatement':
|
88 | return 'Statement';
|
89 | case 'CallExpression':
|
90 | case 'NewExpression':
|
91 | return 'CallExpression';
|
92 | default:
|
93 | return 'ParenthesizedExpression';
|
94 | }
|
95 | }
|
96 |
|
97 |
|
98 | if (PAREN_KEYWORD_TYPE_RE.test(nodeType) && !NO_PAREN_KEYWORD_TYPE_RE.test(nodeType)) {
|
99 |
|
100 |
|
101 | if (nextToken.value === '{' || nextToken.value === '}') {
|
102 | return 'Statement';
|
103 | }
|
104 |
|
105 |
|
106 | var nextNode = nextToken.parentElement;
|
107 | while (!nodeContains(nextNode, token)) {
|
108 | if (QUASI_STATEMENT_TYPE_RE.test(nextNode.type)) {
|
109 | return 'Statement';
|
110 | }
|
111 | nextNode = nextNode.parentElement;
|
112 | }
|
113 | }
|
114 |
|
115 |
|
116 | if (nextToken.value === '{' && FUNCTION_TYPE_RE.test(nodeType)) {
|
117 | return 'Function';
|
118 | }
|
119 |
|
120 |
|
121 | if ((nodeType === 'CallExpression' || nodeType === 'NewExpression')) {
|
122 | var openParen = node.callee.getNextToken();
|
123 | if (openParen.parentElement === node && node.lastChild === token) {
|
124 | return 'CallExpression';
|
125 | }
|
126 | }
|
127 |
|
128 |
|
129 | return 'ParenthesizedExpression';
|
130 | };
|