@@ -98,7 +98,7 @@ const evaluate_map: EvaluateMap = {
98
98
}
99
99
}
100
100
101
- let new_scope = scope . invasived ? scope : scope . $child ( "block" ) ;
101
+ let new_scope = scope . invasive ? scope : scope . $child ( "block" ) ;
102
102
let _result ;
103
103
for ( const node of block . body ) {
104
104
const result = ( _result = evaluate ( path . $child ( node , new_scope ) ) ) ;
@@ -136,16 +136,15 @@ const evaluate_map: EvaluateMap = {
136
136
VariableDeclaration ( path ) {
137
137
const { node, scope } = path ;
138
138
const kind = node . kind ;
139
+
139
140
for ( const declartor of node . declarations ) {
141
+ const varKeyValueMap : string [ ] = [ ] ;
142
+ let varName : string ;
143
+ let varValue : any ;
140
144
if ( types . isIdentifier ( declartor . id ) ) {
141
- const { name } = declartor . id ;
142
- const value = declartor . init
145
+ varKeyValueMap [ declartor . id . name ] = declartor . init
143
146
? evaluate ( path . $child ( declartor . init ) )
144
147
: undefined ;
145
- if ( ! scope . $declar ( kind , name , value ) ) {
146
- throw new ErrDuplicateDeclard ( name ) ;
147
- }
148
- return value ;
149
148
} else if ( types . isObjectPattern ( declartor . id ) ) {
150
149
// @es 2015 destrucuring
151
150
const vars : { key : string ; alias : string } [ ] = [ ] ;
@@ -161,7 +160,7 @@ const evaluate_map: EvaluateMap = {
161
160
162
161
for ( let $var of vars ) {
163
162
if ( $var . key in obj ) {
164
- scope . $declar ( kind , $ var. alias , obj [ $var . key ] ) ;
163
+ varKeyValueMap [ $ var. alias ] = obj [ $var . key ] ;
165
164
}
166
165
}
167
166
} else if ( types . isArrayPattern ( declartor . id ) ) {
@@ -176,12 +175,10 @@ const evaluate_map: EvaluateMap = {
176
175
if ( types . isArrayExpression ( declartor . init ) ) {
177
176
const el = declartor . init . elements [ i ] ;
178
177
if ( ! el ) {
179
- scope . $declar ( kind , $varName , undefined ) ;
180
- return undefined ;
178
+ varKeyValueMap [ $varName ] = undefined ;
181
179
} else {
182
180
const result = evaluate ( path . $child ( el ) ) ;
183
- scope . $declar ( kind , $varName , result ) ;
184
- return result ;
181
+ varKeyValueMap [ $varName ] = result ;
185
182
}
186
183
} else {
187
184
throw node ;
@@ -191,6 +188,18 @@ const evaluate_map: EvaluateMap = {
191
188
} else {
192
189
throw node ;
193
190
}
191
+
192
+ for ( let varName in varKeyValueMap ) {
193
+ if ( scope . invasive && kind === "var" ) {
194
+ if ( scope . parent ) {
195
+ scope . parent . $declar ( kind , varName , varKeyValueMap [ varName ] ) ;
196
+ } else {
197
+ scope . $declar ( kind , varName , varKeyValueMap [ varName ] ) ;
198
+ }
199
+ } else {
200
+ scope . $declar ( kind , varName , varKeyValueMap [ varName ] ) ;
201
+ }
202
+ }
194
203
}
195
204
} ,
196
205
VariableDeclarator : path => {
@@ -347,7 +356,7 @@ const evaluate_map: EvaluateMap = {
347
356
348
357
for ( const value in evaluate ( path . $child ( node . right ) ) ) {
349
358
const new_scope = scope . $child ( "loop" ) ;
350
- new_scope . invasived = true ;
359
+ new_scope . invasive = true ;
351
360
352
361
new_scope . $declar ( kind , name , value ) ;
353
362
@@ -363,9 +372,10 @@ const evaluate_map: EvaluateMap = {
363
372
} ,
364
373
DoWhileStatement ( path ) {
365
374
const { node, scope } = path ;
375
+ // do while don't have his own scope
366
376
do {
367
377
const new_scope = scope . $child ( "loop" ) ;
368
- new_scope . invasived = true ;
378
+ new_scope . invasive = true ; // do while循环具有侵入性,定义var的时候,是覆盖父级变量
369
379
const result = evaluate ( path . $child ( node . body , new_scope ) ) ; // 先把do的执行一遍
370
380
if ( result === BREAK_SINGAL ) {
371
381
break ;
@@ -380,7 +390,7 @@ const evaluate_map: EvaluateMap = {
380
390
const { node, scope } = path ;
381
391
while ( evaluate ( path . $child ( node . test ) ) ) {
382
392
const new_scope = scope . $child ( "loop" ) ;
383
- new_scope . invasived = true ;
393
+ new_scope . invasive = true ;
384
394
const result = evaluate ( path . $child ( node . body , new_scope ) ) ;
385
395
386
396
if ( result === BREAK_SINGAL ) {
@@ -407,7 +417,7 @@ const evaluate_map: EvaluateMap = {
407
417
if ( node . handler ) {
408
418
const param = < types . Identifier > node . handler . param ;
409
419
const new_scope = scope . $child ( "block" ) ;
410
- new_scope . invasived = true ; // 标记为侵入式Scope,不用再多构造啦
420
+ new_scope . invasive = true ; // 标记为侵入式Scope,不用再多构造啦
411
421
new_scope . $const ( param . name , err ) ;
412
422
return evaluate ( path . $child ( node . handler , new_scope ) ) ;
413
423
} else {
@@ -621,7 +631,7 @@ const evaluate_map: EvaluateMap = {
621
631
const { node, scope } = path ;
622
632
const func = function functionDeclaration ( ..._arguments ) {
623
633
const newScope = scope . $child ( "function" ) ;
624
- newScope . invasived = true ;
634
+ newScope . invasive = true ;
625
635
for ( let i = 0 ; i < node . params . length ; i ++ ) {
626
636
const param = node . params [ i ] ;
627
637
if ( types . isIdentifier ( param ) ) {
@@ -840,7 +850,6 @@ const evaluate_map: EvaluateMap = {
840
850
const { node, scope } = path ;
841
851
const func = function ( ...args ) {
842
852
const new_scope = scope . $child ( "function" ) ;
843
- new_scope . invasived = true ;
844
853
for ( let i = 0 ; i < node . params . length ; i ++ ) {
845
854
const { name } = < types . Identifier > node . params [ i ] ;
846
855
new_scope . $const ( name , args [ i ] ) ;
0 commit comments