Skip to content

Commit

Permalink
Merge pull request #1498 from lukastaegert/master
Browse files Browse the repository at this point in the history
Correctly handle variables introduced in switch scopes
  • Loading branch information
Rich-Harris committed Aug 11, 2017
2 parents 2656dbc + f9dc502 commit bfeea43
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/ast/nodes/SwitchStatement.js
@@ -0,0 +1,14 @@
import Scope from '../scopes/Scope.js';
import Statement from './shared/Statement.js';

export default class SwitchStatement extends Statement {
initialise ( scope ) {
this.scope = new Scope( {
parent: scope,
isBlockScope: true,
isLexicalBoundary: false
} );

super.initialise( this.scope );
}
}
3 changes: 2 additions & 1 deletion src/ast/nodes/index.js
Expand Up @@ -28,6 +28,7 @@ import NewExpression from './NewExpression.js';
import ObjectExpression from './ObjectExpression.js';
import ReturnStatement from './ReturnStatement.js';
import Statement from './shared/Statement.js';
import SwitchStatement from './SwitchStatement.js';
import TemplateLiteral from './TemplateLiteral.js';
import ThisExpression from './ThisExpression.js';
import ThrowStatement from './ThrowStatement.js';
Expand Down Expand Up @@ -67,7 +68,7 @@ export default {
NewExpression,
ObjectExpression,
ReturnStatement,
SwitchStatement: Statement,
SwitchStatement,
TemplateLiteral,
ThisExpression,
ThrowStatement,
Expand Down
6 changes: 6 additions & 0 deletions test/form/switch-scopes/_config.js
@@ -0,0 +1,6 @@
module.exports = {
description: 'correctly handles switch scopes',
options: {
moduleName: 'myBundle'
}
};
13 changes: 13 additions & 0 deletions test/form/switch-scopes/_expected/amd.js
@@ -0,0 +1,13 @@
define(function () { 'use strict';

const x = globalFunction;
switch ( anotherGlobal ) {
case 2:
x();
}

switch ( globalFunction() ) {
case 4:
}

});
11 changes: 11 additions & 0 deletions test/form/switch-scopes/_expected/cjs.js
@@ -0,0 +1,11 @@
'use strict';

const x = globalFunction;
switch ( anotherGlobal ) {
case 2:
x();
}

switch ( globalFunction() ) {
case 4:
}
9 changes: 9 additions & 0 deletions test/form/switch-scopes/_expected/es.js
@@ -0,0 +1,9 @@
const x = globalFunction;
switch ( anotherGlobal ) {
case 2:
x();
}

switch ( globalFunction() ) {
case 4:
}
14 changes: 14 additions & 0 deletions test/form/switch-scopes/_expected/iife.js
@@ -0,0 +1,14 @@
(function () {
'use strict';

const x = globalFunction;
switch ( anotherGlobal ) {
case 2:
x();
}

switch ( globalFunction() ) {
case 4:
}

}());
17 changes: 17 additions & 0 deletions test/form/switch-scopes/_expected/umd.js
@@ -0,0 +1,17 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';

const x = globalFunction;
switch ( anotherGlobal ) {
case 2:
x();
}

switch ( globalFunction() ) {
case 4:
}

})));
23 changes: 23 additions & 0 deletions test/form/switch-scopes/main.js
@@ -0,0 +1,23 @@
const x = globalFunction;
function y () {}

switch ( anotherGlobal ) {
case 1:
const x = function () {};
x();
}

switch ( anotherGlobal ) {
case 2:
x();
}

switch ( anotherGlobal ) {
case 3:
const y = globalFunction;
}
y();

switch ( globalFunction() ) {
case 4:
}

0 comments on commit bfeea43

Please sign in to comment.