@@ -46,9 +46,10 @@ let createSymbol = null;
46
46
const getSymbol = function ( node , globals , scope , opt ) {
47
47
const opts = opt || { } ;
48
48
let block = scope ;
49
- if ( node . type === 'Identifier' ) {
49
+ switch ( node . type ) {
50
+ case 'Identifier' : {
50
51
return getIdentifier ( node , globals , scope , opts ) ;
51
- } else if ( node . type === 'MemberExpression' ) {
52
+ } case 'MemberExpression' : {
52
53
const obj = getSymbol ( node . object , globals , scope , opts ) ;
53
54
const propertySymbol = getSymbol ( node . property , globals , scope , { simpleIdentifier : ! node . computed } ) ;
54
55
const propertyValue = getSymbolValue ( propertySymbol ) ;
@@ -57,26 +58,26 @@ const getSymbol = function (node, globals, scope, opt) {
57
58
block = obj . props [ propertyValue ] ;
58
59
59
60
return block ;
60
- } else if ( opts . createMissingProps && propertyValue ) {
61
+ }
62
+ if ( opts . createMissingProps && propertyValue ) {
61
63
obj . props [ propertyValue ] = createNode ( ) ;
62
64
63
65
return obj . props [ propertyValue ] ;
64
- } else {
65
- debug ( 'MemberExpression: Missing property ' + node . property . name ) ;
66
-
67
- return null ;
68
66
}
69
- } else if ( node . type === 'FunctionExpression' || node . type === 'FunctionDeclaration' || node . type === 'ArrowFunctionExpression' ) {
67
+ debug ( 'MemberExpression: Missing property ' + node . property . name ) ;
68
+
69
+ return null ;
70
+ } case 'FunctionExpression' : case 'FunctionDeclaration' : case 'ArrowFunctionExpression' : {
70
71
const val = createNode ( ) ;
71
72
val . props . prototype = createNode ( ) ;
72
73
val . props . prototype . type = 'object' ;
73
74
val . type = 'object' ;
74
75
val . value = node ;
75
76
76
77
return val ;
77
- } else if ( node . type === 'AssignmentExpression' ) {
78
+ } case 'AssignmentExpression' : {
78
79
return createSymbol ( node . left , globals , node . right , scope , opts ) ;
79
- } else if ( node . type === 'ClassBody' ) {
80
+ } case 'ClassBody' : {
80
81
const val = createNode ( ) ;
81
82
node . body . forEach ( ( method ) => {
82
83
val . props [ method . key . name ] = createNode ( ) ;
@@ -87,7 +88,7 @@ const getSymbol = function (node, globals, scope, opt) {
87
88
val . value = node ;
88
89
89
90
return val ;
90
- } else if ( node . type === 'ObjectExpression' ) {
91
+ } case 'ObjectExpression' : {
91
92
const val = createNode ( ) ;
92
93
val . type = 'object' ;
93
94
node . properties . forEach ( ( prop ) => {
@@ -98,36 +99,38 @@ const getSymbol = function (node, globals, scope, opt) {
98
99
} ) ;
99
100
100
101
return val ;
101
- } else if ( node . type === 'Literal' ) {
102
+ } case 'Literal' : {
102
103
const val = createNode ( ) ;
103
104
val . type = 'literal' ;
104
105
val . value = node ;
105
106
106
107
return val ;
107
108
}
109
+ }
108
110
109
111
return null ;
110
112
} ;
111
113
112
114
createSymbol = function ( node , globals , value , scope ) {
113
115
const block = scope || globals ;
114
116
let symbol ;
115
- if ( node . type === 'Identifier' ) {
117
+ switch ( node . type ) {
118
+ case 'Identifier' : {
116
119
if ( value ) {
117
120
const valueSymbol = getSymbol ( value , globals , block ) ;
118
121
if ( valueSymbol ) {
119
122
block . props [ node . name ] = valueSymbol ;
120
123
121
124
return block . props [ node . name ] ;
122
- } else {
123
- debug ( 'Identifier: Missing value symbol for %s' , node . name ) ;
124
125
}
126
+ debug ( 'Identifier: Missing value symbol for %s' , node . name ) ;
125
127
} else {
126
128
block . props [ node . name ] = createNode ( ) ;
127
129
128
130
return block . props [ node . name ] ;
129
131
}
130
- } else if ( node . type === 'MemberExpression' ) {
132
+ break ;
133
+ } case 'MemberExpression' : {
131
134
symbol = getSymbol ( node . object , globals , block ) ;
132
135
133
136
const propertySymbol = getSymbol ( node . property , globals , block , { simpleIdentifier : ! node . computed } ) ;
@@ -136,27 +139,32 @@ createSymbol = function (node, globals, value, scope) {
136
139
symbol . props [ propertyValue ] = getSymbol ( value , globals , block ) ;
137
140
138
141
return symbol . props [ propertyValue ] ;
139
- } else {
140
- debug ( 'MemberExpression: Missing symbol: %s' , node . property . name ) ;
141
142
}
142
- } else if ( node . type === 'FunctionDeclaration' ) {
143
+ debug ( 'MemberExpression: Missing symbol: %s' , node . property . name ) ;
144
+ break ;
145
+ } case 'FunctionDeclaration' : {
143
146
if ( node . id . type === 'Identifier' ) {
144
147
return createSymbol ( node . id , globals , node , globals ) ;
145
148
}
149
+ break ;
150
+ }
146
151
}
147
152
148
153
return null ;
149
154
} ;
150
155
151
156
// Creates variables from variable definitions
152
157
const initVariables = function ( node , globals ) {
153
- if ( node . type === 'Program' ) {
158
+ switch ( node . type ) {
159
+ case 'Program' : {
154
160
node . body . forEach ( ( childNode ) => {
155
161
initVariables ( childNode , globals ) ;
156
162
} ) ;
157
- } else if ( node . type === 'ExpressionStatement' ) {
163
+ break ;
164
+ } case 'ExpressionStatement' : {
158
165
initVariables ( node . expression , globals ) ;
159
- } else if ( node . type === 'VariableDeclaration' ) {
166
+ break ;
167
+ } case 'VariableDeclaration' : {
160
168
node . declarations . forEach ( ( declaration ) => {
161
169
// let and const
162
170
const symbol = createSymbol ( declaration . id , globals , null , globals ) ;
@@ -165,43 +173,56 @@ const initVariables = function (node, globals) {
165
173
globals . props . window . props [ declaration . id . name ] = symbol ;
166
174
}
167
175
} ) ;
176
+ break ;
177
+ }
168
178
}
169
179
} ;
170
180
171
181
// Populates variable maps using AST
172
182
const mapVariables = function ( node , globals ) {
173
- if ( node . type === 'Program' ) {
183
+ switch ( node . type ) {
184
+ case 'Program' : {
174
185
node . body . forEach ( ( childNode ) => {
175
186
mapVariables ( childNode , globals ) ;
176
187
} ) ;
177
- } else if ( node . type === 'ExpressionStatement' ) {
188
+ break ;
189
+ } case 'ExpressionStatement' : {
178
190
mapVariables ( node . expression , globals ) ;
179
- } else if ( node . type === 'AssignmentExpression' ) {
191
+ break ;
192
+ } case 'AssignmentExpression' : {
180
193
createSymbol ( node . left , globals , node . right ) ;
181
- } else if ( node . type === 'VariableDeclaration' ) {
194
+ break ;
195
+ } case 'VariableDeclaration' : {
182
196
node . declarations . forEach ( ( declaration ) => {
183
197
createSymbol ( declaration . id , globals , declaration . init ) ;
184
198
} ) ;
185
- } else if ( node . type === 'FunctionDeclaration' ) {
199
+ break ;
200
+ } case 'FunctionDeclaration' : {
186
201
if ( node . id . type === 'Identifier' ) {
187
202
createSymbol ( node . id , globals , node , globals ) ;
188
203
}
189
- } else if ( node . type === 'ExportDefaultDeclaration' ) {
204
+ break ;
205
+ } case 'ExportDefaultDeclaration' : {
190
206
const symbol = createSymbol ( node . declaration , globals , node . declaration ) ;
191
207
symbol . exported = true ;
192
- } else if ( node . type === 'ExportNamedDeclaration' ) {
208
+ break ;
209
+ } case 'ExportNamedDeclaration' : {
193
210
if ( node . declaration ) {
194
211
const symbol = createSymbol ( node . declaration , globals , node . declaration ) ;
195
212
symbol . exported = true ;
196
213
}
197
214
node . specifiers . forEach ( ( specifier ) => {
198
215
mapVariables ( specifier , globals ) ;
199
216
} ) ;
200
- } else if ( node . type === 'ExportSpecifier' ) {
217
+ break ;
218
+ } case 'ExportSpecifier' : {
201
219
const symbol = getSymbol ( node . local , globals , globals ) ;
202
220
symbol . exported = true ;
203
- } else if ( node . type === 'ClassDeclaration' ) {
221
+ break ;
222
+ } case 'ClassDeclaration' : {
204
223
createSymbol ( node . id , globals , node . body , globals ) ;
224
+ break ;
225
+ }
205
226
}
206
227
} ;
207
228
0 commit comments