Skip to content

Commit

Permalink
[[FIX]] Allow let and const to be in a block outside of a block
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored and jugglinmike committed Jan 2, 2016
1 parent f155698 commit 3f8bd9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,7 @@ var JSHINT = (function() {

// create a new block scope
state.funct["(scope)"].stack();
state.funct["(noblockscopedvar)"] = false;

line = state.tokens.curr.line;
if (state.tokens.next.id !== "}") {
Expand Down
24 changes: 16 additions & 8 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4970,25 +4970,32 @@ exports["no let not directly within a block"] = function (test) {
" if (true)",
" let x = 1;",
"}",
"if (true) let (x = 1) print(x);",
"for (let x = 0; x < 42; ++x) let a = 1;",
"for (let x in [1, 2, 3, 4] ) let a = 1;",
"for (let x of [1, 2, 3, 4] ) let a = 1;",
"while (true) let a = 1;",
"if (false) let a = 1; else if (true) let a = 1; else let a = 2;"
"if (false) let a = 1; else if (true) let a = 1; else let a = 2;",
"if (true) if (false) let x = 1;",
"if (true) if (false) { let x = 1; }",
"if (true) try { let x = 1; } catch (e) { let x = 1; }"
];

TestRun(test)
var run = TestRun(test)
.addError(1, "Let declaration not directly within block.")
.addError(4, "Let declaration not directly within block.")
.addError(6, "Let declaration not directly within block.")
.addError(7, "Let declaration not directly within block.")
.addError(8, "Let declaration not directly within block.")
.addError(9, "Let declaration not directly within block.")
.addError(10, "Let declaration not directly within block.")
.addError(11, "Let declaration not directly within block.")
.addError(11, "Let declaration not directly within block.")
.addError(11, "Let declaration not directly within block.")
.test(code, {moz: true, predef: ["print"]});
.addError(10, "Let declaration not directly within block.")
.addError(10, "Let declaration not directly within block.")
.addError(11, "Let declaration not directly within block.");
run.test(code, {esversion: 6});
run.test(code, {moz: true});

// Don't warn about let expressions
TestRun(test).test("if (true) let (x = 1) print(x);", {moz: true, predef: ["print"]});

test.done();
};
Expand All @@ -5002,7 +5009,8 @@ exports["no const not directly within a block"] = function (test) {
"}",
"for (let x = 0; x < 42; ++x) const a = 1;",
"while (true) const a = 1;",
"if (false) const a = 1; else if (true) const a = 1; else const a = 2;"
"if (false) const a = 1; else if (true) const a = 1; else const a = 2;",
"if (true) if (false) { const a = 1; }"
];

TestRun(test)
Expand Down

0 comments on commit 3f8bd9e

Please sign in to comment.