From f9dc50205c54392f250150eac8817cefc8c8ac4d Mon Sep 17 00:00:00 2001 From: Lukas Taegert Date: Mon, 24 Jul 2017 08:00:53 +0200 Subject: [PATCH] Correctly handle variables introduced in switch scopes --- src/ast/nodes/SwitchStatement.js | 14 ++++++++++++++ src/ast/nodes/index.js | 3 ++- test/form/switch-scopes/_config.js | 6 ++++++ test/form/switch-scopes/_expected/amd.js | 13 +++++++++++++ test/form/switch-scopes/_expected/cjs.js | 11 +++++++++++ test/form/switch-scopes/_expected/es.js | 9 +++++++++ test/form/switch-scopes/_expected/iife.js | 14 ++++++++++++++ test/form/switch-scopes/_expected/umd.js | 17 +++++++++++++++++ test/form/switch-scopes/main.js | 23 +++++++++++++++++++++++ 9 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/ast/nodes/SwitchStatement.js create mode 100644 test/form/switch-scopes/_config.js create mode 100644 test/form/switch-scopes/_expected/amd.js create mode 100644 test/form/switch-scopes/_expected/cjs.js create mode 100644 test/form/switch-scopes/_expected/es.js create mode 100644 test/form/switch-scopes/_expected/iife.js create mode 100644 test/form/switch-scopes/_expected/umd.js create mode 100644 test/form/switch-scopes/main.js diff --git a/src/ast/nodes/SwitchStatement.js b/src/ast/nodes/SwitchStatement.js new file mode 100644 index 00000000000..ce5ae9e29fe --- /dev/null +++ b/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 ); + } +} diff --git a/src/ast/nodes/index.js b/src/ast/nodes/index.js index bfda415448a..0d851bb78ed 100644 --- a/src/ast/nodes/index.js +++ b/src/ast/nodes/index.js @@ -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'; @@ -67,7 +68,7 @@ export default { NewExpression, ObjectExpression, ReturnStatement, - SwitchStatement: Statement, + SwitchStatement, TemplateLiteral, ThisExpression, ThrowStatement, diff --git a/test/form/switch-scopes/_config.js b/test/form/switch-scopes/_config.js new file mode 100644 index 00000000000..47676bff0d9 --- /dev/null +++ b/test/form/switch-scopes/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'correctly handles switch scopes', + options: { + moduleName: 'myBundle' + } +}; diff --git a/test/form/switch-scopes/_expected/amd.js b/test/form/switch-scopes/_expected/amd.js new file mode 100644 index 00000000000..f8af6ec51dd --- /dev/null +++ b/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: + } + +}); diff --git a/test/form/switch-scopes/_expected/cjs.js b/test/form/switch-scopes/_expected/cjs.js new file mode 100644 index 00000000000..ee73f2e70d7 --- /dev/null +++ b/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: +} diff --git a/test/form/switch-scopes/_expected/es.js b/test/form/switch-scopes/_expected/es.js new file mode 100644 index 00000000000..f5cd80e94e6 --- /dev/null +++ b/test/form/switch-scopes/_expected/es.js @@ -0,0 +1,9 @@ +const x = globalFunction; +switch ( anotherGlobal ) { + case 2: + x(); +} + +switch ( globalFunction() ) { + case 4: +} diff --git a/test/form/switch-scopes/_expected/iife.js b/test/form/switch-scopes/_expected/iife.js new file mode 100644 index 00000000000..f52899caedb --- /dev/null +++ b/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: + } + +}()); diff --git a/test/form/switch-scopes/_expected/umd.js b/test/form/switch-scopes/_expected/umd.js new file mode 100644 index 00000000000..bed407c91ef --- /dev/null +++ b/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: + } + +}))); diff --git a/test/form/switch-scopes/main.js b/test/form/switch-scopes/main.js new file mode 100644 index 00000000000..092ec07c8b2 --- /dev/null +++ b/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: +}