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 | (function () {
|
26 | 'use strict';
|
27 |
|
28 | function isExpression(node) {
|
29 | if (node == null) { return false; }
|
30 | switch (node.type) {
|
31 | case 'ArrayExpression':
|
32 | case 'AssignmentExpression':
|
33 | case 'BinaryExpression':
|
34 | case 'CallExpression':
|
35 | case 'ConditionalExpression':
|
36 | case 'FunctionExpression':
|
37 | case 'Identifier':
|
38 | case 'Literal':
|
39 | case 'LogicalExpression':
|
40 | case 'MemberExpression':
|
41 | case 'NewExpression':
|
42 | case 'ObjectExpression':
|
43 | case 'SequenceExpression':
|
44 | case 'ThisExpression':
|
45 | case 'UnaryExpression':
|
46 | case 'UpdateExpression':
|
47 | return true;
|
48 | }
|
49 | return false;
|
50 | }
|
51 |
|
52 | function isIterationStatement(node) {
|
53 | if (node == null) { return false; }
|
54 | switch (node.type) {
|
55 | case 'DoWhileStatement':
|
56 | case 'ForInStatement':
|
57 | case 'ForStatement':
|
58 | case 'WhileStatement':
|
59 | return true;
|
60 | }
|
61 | return false;
|
62 | }
|
63 |
|
64 | function isStatement(node) {
|
65 | if (node == null) { return false; }
|
66 | switch (node.type) {
|
67 | case 'BlockStatement':
|
68 | case 'BreakStatement':
|
69 | case 'ContinueStatement':
|
70 | case 'DebuggerStatement':
|
71 | case 'DoWhileStatement':
|
72 | case 'EmptyStatement':
|
73 | case 'ExpressionStatement':
|
74 | case 'ForInStatement':
|
75 | case 'ForStatement':
|
76 | case 'IfStatement':
|
77 | case 'LabeledStatement':
|
78 | case 'ReturnStatement':
|
79 | case 'SwitchStatement':
|
80 | case 'ThrowStatement':
|
81 | case 'TryStatement':
|
82 | case 'VariableDeclaration':
|
83 | case 'WhileStatement':
|
84 | case 'WithStatement':
|
85 | return true;
|
86 | }
|
87 | return false;
|
88 | }
|
89 |
|
90 | function isSourceElement(node) {
|
91 | return isStatement(node) || node != null && node.type === 'FunctionDeclaration';
|
92 | }
|
93 |
|
94 | function trailingStatement(node) {
|
95 | switch (node.type) {
|
96 | case 'IfStatement':
|
97 | if (node.alternate != null) {
|
98 | return node.alternate;
|
99 | }
|
100 | return node.consequent;
|
101 |
|
102 | case 'LabeledStatement':
|
103 | case 'ForStatement':
|
104 | case 'ForInStatement':
|
105 | case 'WhileStatement':
|
106 | case 'WithStatement':
|
107 | return node.body;
|
108 | }
|
109 | return null;
|
110 | }
|
111 |
|
112 | function isProblematicIfStatement(node) {
|
113 | var current;
|
114 |
|
115 | if (node.type !== 'IfStatement') {
|
116 | return false;
|
117 | }
|
118 | if (node.alternate == null) {
|
119 | return false;
|
120 | }
|
121 | current = node.consequent;
|
122 | do {
|
123 | if (current.type === 'IfStatement') {
|
124 | if (current.alternate == null) {
|
125 | return true;
|
126 | }
|
127 | }
|
128 | current = trailingStatement(current);
|
129 | } while (current);
|
130 |
|
131 | return false;
|
132 | }
|
133 |
|
134 | module.exports = {
|
135 | isExpression: isExpression,
|
136 | isStatement: isStatement,
|
137 | isIterationStatement: isIterationStatement,
|
138 | isSourceElement: isSourceElement,
|
139 | isProblematicIfStatement: isProblematicIfStatement,
|
140 |
|
141 | trailingStatement: trailingStatement
|
142 | };
|
143 | }());
|
144 |
|