File tree Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -252,4 +252,11 @@ Object {
252252}
253253` ;
254254
255+ exports [` const uses block scoping instead of function scoping 1` ] = `
256+ Object {
257+ " status" : " finished" ,
258+ " value" : true ,
259+ }
260+ ` ;
261+
255262exports [` parseError for missing semicolon 1` ] = ` "Line 1: Missing semicolon at the end of statement"` ;
Original file line number Diff line number Diff line change @@ -131,3 +131,26 @@ test(
131131 } ,
132132 30000
133133)
134+
135+ // This is bad practice. Don't do this!
136+ test ( 'const uses block scoping instead of function scoping' , ( ) => {
137+ const code = `
138+ function test(){
139+ const x = true;
140+ if(true) {
141+ const x = false;
142+ } else {
143+ const x = false;
144+ }
145+ return x;
146+ }
147+ test();
148+ ` ;
149+ const context = mockContext ( )
150+ const promise = runInContext ( code , context , { scheduler : 'preemptive' } )
151+ return promise . then ( obj => {
152+ expect ( obj ) . toMatchSnapshot ( )
153+ expect ( obj . status ) . toBe ( 'finished' )
154+ expect ( ( obj as Finished ) . value ) . toBe ( true )
155+ } )
156+ } )
Original file line number Diff line number Diff line change @@ -43,6 +43,20 @@ const createFrame = (
4343 return frame
4444}
4545
46+ const createBlockFrame = (
47+ context : Context ,
48+ vars : es . Identifier [ ] ,
49+ args : Value [ ] ,
50+ ) : Frame => {
51+ const frame : Frame = {
52+ name : 'ifStatementBlock' ,
53+ parent : currentFrame ( context ) ,
54+ environment : { } ,
55+ thisContext : context
56+ }
57+ return frame
58+ }
59+
4660const handleError = ( context : Context , error : SourceError ) => {
4761 context . errors . push ( error )
4862 if ( error . severity === ErrorSeverity . ERROR ) {
@@ -369,10 +383,18 @@ export const evaluators: { [nodeType: string]: Evaluator<es.Node> } = {
369383 return undefined
370384 }
371385
386+ // Create a new frame (block scoping)
387+ const frame = createBlockFrame ( context , [ ] , [ ] )
388+ pushFrame ( context , frame )
389+
372390 if ( test ) {
373- return yield * evaluate ( node . consequent , context )
391+ const result = yield * evaluate ( node . consequent , context )
392+ popFrame ( context )
393+ return result
374394 } else if ( node . alternate ) {
375- return yield * evaluate ( node . alternate , context )
395+ const result = yield * evaluate ( node . alternate , context )
396+ popFrame ( context )
397+ return result
376398 } else {
377399 return undefined
378400 }
You can’t perform that action at this time.
0 commit comments