Skip to content

Commit

Permalink
[[FIX]] block scope vars dont redefine in blocks
Browse files Browse the repository at this point in the history
Do give a redefinition error when a block scope declaration is inside a
block.
Fixes #2438
  • Loading branch information
lukeapage authored and jugglinmike committed Jul 9, 2015
1 parent 81fe3fa commit 9e74025
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3350,7 +3350,7 @@ var JSHINT = (function() {
for (var t in tokens) {
if (tokens.hasOwnProperty(t)) {
t = tokens[t];
if (state.funct["(global)"]) {
if (state.funct["(scope)"].block.isGlobal()) {
if (predefined[t.id] === false) {
warning("W079", t.token, t.id);
}
Expand Down Expand Up @@ -4384,7 +4384,7 @@ var JSHINT = (function() {
ok = false;
}

if (!state.funct["(global)"] || !state.funct["(scope)"].atTop()) {
if (!state.funct["(scope)"].block.isGlobal()) {
error("E053", state.tokens.curr);
ok = false;
}
Expand Down
13 changes: 9 additions & 4 deletions src/scope-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,6 @@ var scopeManager = function(state, predefined, exported, declared) {
return null;
},

atTop: function() {
return _scopeStack.length === 1;
},

/**
* for the exported options, indicating a variable is used outside the file
*/
Expand Down Expand Up @@ -641,6 +637,15 @@ var scopeManager = function(state, predefined, exported, declared) {
},

block: {

/**
* is the current block global?
* @returns Boolean
*/
isGlobal: function() {
return _current["(global)"];
},

use: function(labelName, token) {

// if resolves to current function params, then do not store usage just resolve
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2760,6 +2760,20 @@ exports["make sure variables may shadow globals in functions after they are refe
test.done();
};

exports["test block scope redefines globals only outside of blocks"] = function (test) {
var code = [
"{",
" let Map = true;",
"}",
"let Map = false;"
];

TestRun(test)
.addError(4, "Redefinition of 'Map'.")
.test(code, { esnext: true, browser: true });
test.done();
};

exports["test destructuring function as moz"] = function (test) {
// Example from https://developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7
var code = [
Expand Down

0 comments on commit 9e74025

Please sign in to comment.