Skip to content

Commit

Permalink
[[FIX]] Close synthetic scope for labeled blocks
Browse files Browse the repository at this point in the history
JSHint creates a synthetic block scope in order to track labeled
statements prior to parsing the statements themselves. Previously, this
scope was not closed (or "unstacked") for labeled Block statements.

Ensure that the synthetic scope is closed in these cases as well. Assert
correct behavior via various parsing/linting mechanisms that are aware
of scope.
  • Loading branch information
jugglinmike authored and rwaldron committed Apr 25, 2017
1 parent 70f9ca2 commit 5f0f789
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/jshint.js
Expand Up @@ -1622,6 +1622,11 @@ var JSHINT = (function() {
// }
var iscase = (state.funct["(verb)"] === "case" && state.tokens.curr.value === ":");
block(true, true, false, false, iscase);

if (hasOwnScope) {
state.funct["(scope)"].unstack();
}

return;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/core.js
Expand Up @@ -866,6 +866,14 @@ exports.testES6Modules = function (test) {
TestRun(test)
.test(src2, {});

// See gh-3055 "Labels Break JSHint"
TestRun(test, "following labeled block")
.test([
"label: {}",
"export function afterLabelExported() {}",
"import afterLabelImported from 'elsewhere';"
], { esversion: 6 });

test.done();
};

Expand Down Expand Up @@ -1756,6 +1764,14 @@ exports.labelsOutOfScope = function (test) {
.addError(24, "'baz' is not a statement label.")
.test(src);

// See gh-3055 "Labels Break JSHint"
TestRun(test, "following labeled block")
.addError(2, "'x' is not a statement label.")
.test([
"x: {}",
"break x;"
]);

test.done();
};

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/options.js
Expand Up @@ -794,6 +794,16 @@ exports.undef = function (test) {

test.strictEqual(JSHINT.data().implieds, undefined);

// See gh-3055 "Labels Break JSHint"
TestRun(test, "following labeled block")
.addError(4, "'x' is not defined.")
.test([
"label: {",
" let x;",
"}",
"void x;"
], { esversion: 6, undef: true });

test.done();
};

Expand Down

0 comments on commit 5f0f789

Please sign in to comment.