Skip to content

Commit

Permalink
fix: the body of switch statement is a block scope (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
LungZeno authored Sep 2, 2022
1 parent ff46a53 commit 8de559f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function isScope(node) {
return node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression' || node.type === 'Program';
}
function isBlockScope(node) {
return node.type === 'BlockStatement' || isScope(node);
// The body of switch statement is a block.
return node.type === 'BlockStatement' || node.type === 'SwitchStatement' || isScope(node);
}

function declaresArguments(node) {
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/switch-statement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
switch (3) {
case 3:
let a;
}

a;
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ test('return_hash.js - named argument / parameter', function () {
test('right_hand.js - globals on the right-hand of assignment', function () {
assert.deepEqual(detect(read('right_hand.js')).map(nameOf), [ 'exports', '__dirname', '__filename' ].sort());
});
test('switch-statement.js - id in outer scope of switch is a global', function () {
assert.deepEqual(detect(read('switch-statement.js')).map(nameOf), ['a']);
});
test('try_catch.js - the exception in a try catch block is a local', function () {
assert.deepEqual(detect(read('try_catch.js')), []);
});
Expand Down

0 comments on commit 8de559f

Please sign in to comment.