Skip to content

Commit

Permalink
Fix for issue jshint#565. Makes blocks a special case of statements. …
Browse files Browse the repository at this point in the history
…This fix also prevents jshint from crashing due to unmatched curly-brackets in many cases (such as 'expected {, was (end)' in tests/unit/fixtures/comma.js)
  • Loading branch information
CarlEkerot committed Aug 1, 2012
1 parent e07b48a commit e608f03
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
8 changes: 7 additions & 1 deletion jshint.js
Expand Up @@ -2531,7 +2531,7 @@ loop: for (;;) {
advance(":");
scope = Object.create(s);
addlabel(t.value, "label");
if (!nexttoken.labelled) {
if (!nexttoken.labelled && nexttoken.value !== "{") {
warning("Label '{a}' on {b} statement.",
nexttoken, t.value, nexttoken.value);
}
Expand All @@ -2543,6 +2543,12 @@ loop: for (;;) {
t = nexttoken;
}

// Is it a lonely block?
if (t.id === "{") {
block(true, true);
return;
}

// Parse the statement.

if (!noindent) {
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/fixtures/blocks.js
@@ -0,0 +1,26 @@
var a, b, c;
a = 1;
{
b = 2;
{
c = 7;
}
set_to_three: {
a = 3;
}
count:
for (var i = 1; i <= 3; ++i) {
a += i;
}
switchStmt:
switch (a) {
case 0: b = 1; break;
case 1: b = 0; break;
default: b = 2; break;
}
}
c = 3;

labeledBlock: {
c += a + b;
}
21 changes: 19 additions & 2 deletions tests/unit/parser.js
Expand Up @@ -333,10 +333,21 @@ exports.comma = function () {
.addError(6, 'Expected an identifier and instead saw \')\'.')
.addError(6, 'Expected an assignment or function call and instead saw an expression.')
.addError(6, 'Missing semicolon.')
.addError(6, 'Expected to see a statement and instead saw a block.')
//.addError(6, 'Expected to see a statement and instead saw a block.')
.addError(6, 'Expected an assignment or function call and instead saw an expression.')
.addError(6, 'Missing semicolon.')
.addError(8, 'Expected \'(end)\' and instead saw \'}\'.')
//.addError(8, 'Expected \'(end)\' and instead saw \'}\'.')
.addError(15, 'Expected an assignment or function call and instead saw an expression.')
.addError(15, 'Missing semicolon.')
.addError(20, 'Expected \')\' to match \'(\' from line 20 and instead saw \',\'.')
.addError(20, 'Expected an assignment or function call and instead saw an expression.')
.addError(20, 'Missing semicolon.')
.addError(20, 'Expected an identifier and instead saw \')\'.')
.addError(30, 'Expected \')\' to match \'(\' from line 30 and instead saw \',\'.')
.addError(30, 'Expected \')\' and instead saw \'args\'.')
.addError(30, 'Expected an assignment or function call and instead saw an expression.')
.addError(30, 'Missing semicolon.')
.addError(30, 'Expected an identifier and instead saw \')\'.')
.test(src);
};

Expand All @@ -361,6 +372,12 @@ exports.withStatement = function () {
.test(src, {white: true, withstmt: true});
};

exports.blocks = function () {
var src = fs.readFileSync(__dirname + "/fixtures/blocks.js", "utf8");

TestRun().test(src);
};

exports.functionCharaterLocation = function () {
var i;
var src = fs.readFileSync(__dirname + "/fixtures/nestedFunctions.js", "utf8");
Expand Down

0 comments on commit e608f03

Please sign in to comment.