Skip to content
This repository

Support for labeled blocks and lonely blocks #603

Closed
wants to merge 7 commits into from

2 participants

Carl Ekerot Anton Kovalyov
Carl Ekerot

Added support for labeled blocks and lonely blocks, which are valid according to ECMA-262.
The following cases are tested and supported:

aLonelyBlock:
{
    // Labeled block
}
{
    // Unlabeled block
}

This fix also prevents the parser from halting when errors are present in statements after which a block is expected (f.ex. if-statements).
For example, the following will report error, but not halt parsing with Expected '(end)' and instead saw '}'. which it did before, due to the first { not being taken in to account, and the bracket-matching would fail:

if (a < b); {
    // Bad if-statement
}
CarlEkerot and others added some commits
Carl Ekerot CarlEkerot Fix for issue #565. Makes blocks a special case of statements. This f…
…ix also prevents jshint from crashing due to unmatched curly-brackets in many cases (such as 'expected {, was (end)' in tests/unit/fixtures/comma.js)
7550336
Carl Ekerot CarlEkerot Removed commented addError-calls in parser.js. 03c8ee6
CarlEkerot Fix for issue #565. Makes blocks a special case of statements. This f…
…ix also prevents jshint from crashing due to unmatched curly-brackets in many cases (such as 'expected {, was (end)' in tests/unit/fixtures/comma.js)
e608f03
CarlEkerot Removed commented addError-calls in parser.js. 8d4f985
CarlEkerot Merge branch 'master' of https://github.com/CarlEkerot/jshint 2d65d8a
CarlEkerot Added assertions to ensure that code execution procedes after lonely …
…blocks, and that start-bracket errors doesn't halt the parsing.
f5ea569
CarlEkerot Merge remote branch 'upstream/master' d211d34
Anton Kovalyov valueof closed this pull request from a commit
Carl Ekerot Add support for labeled and lonely blocks.
This patch adds support for labeled and lonely blocks which
are valid according to ECMA-262. The following cases are
tested and supported:

    aLabeledBlock:
    {
        // Labeled block
    }

    {
        // Unlabeled block
    }

This fix also prevents the parser from halting when errors
are present in statements after which a block is expected (e.g.
if-statements).

For example, the following will report error, but not halt parsing
with `Expected '(end)' and instead saw '}'.` which it did before,
due to the first { not being taken in to account, and the
bracket-matching would fail:

    if (a < b); {
        // Bad if-statement
    }

Issues:

Closes GH-603.

Squashed commit of the following:

commit 2eb909c
Merge: 38fe2c9 d211d34
Author: Anton Kovalyov <anton@kovalyov.net>
Date:   Thu Aug 16 21:32:42 2012 -0700

    Merge branch 'master' of git://github.com/CarlEkerot/jshint into CarlEkerot-master

commit d211d34
Merge: f5ea569 a1d7a7d
Author: CarlEkerot <carl.ekerot@lunicore.se>
Date:   Mon Aug 13 10:16:10 2012 +0200

    Merge remote branch 'upstream/master'

commit f5ea569
Author: CarlEkerot <carl.ekerot@lunicore.se>
Date:   Thu Aug 2 08:41:44 2012 +0200

    Added assertions to ensure that code execution procedes after lonely blocks, and that start-bracket errors doesn't halt the parsing.

commit 2d65d8a
Merge: 8d4f985 03c8ee6
Author: CarlEkerot <carl.ekerot@lunicore.se>
Date:   Wed Aug 1 11:53:39 2012 +0200

    Merge branch 'master' of https://github.com/CarlEkerot/jshint

commit 8d4f985e334cfdf41b840cacf20ab90b255fa1dc
Author: CarlEkerot <carl.ekerot@lunicore.se>
Date:   Wed Aug 1 11:13:21 2012 +0200

    Removed commented addError-calls in parser.js.

commit e608f03c7173e57fc3d2d07645977ad70e44505e
Author: CarlEkerot <carl.ekerot@lunicore.se>
Date:   Wed Aug 1 11:04:19 2012 +0200

    Fix for issue #565. Makes blocks a special case of statements. 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)

commit 03c8ee690828985510498575f07397344658d693
Author: Carl Ekerot <kalle@implode.se>
Date:   Wed Aug 1 11:13:21 2012 +0200

    Removed commented addError-calls in parser.js.

commit 7550336e5fdcedecd5cdbc3f78d053b634871b6e
Author: Carl Ekerot <kalle@implode.se>
Date:   Wed Aug 1 11:04:19 2012 +0200

    Fix for issue #565. Makes blocks a special case of statements. 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)
9ec3e3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 7 unique commits by 2 authors.

Aug 01, 2012
Carl Ekerot CarlEkerot Fix for issue #565. Makes blocks a special case of statements. This f…
…ix also prevents jshint from crashing due to unmatched curly-brackets in many cases (such as 'expected {, was (end)' in tests/unit/fixtures/comma.js)
7550336
Carl Ekerot CarlEkerot Removed commented addError-calls in parser.js. 03c8ee6
CarlEkerot Fix for issue #565. Makes blocks a special case of statements. This f…
…ix also prevents jshint from crashing due to unmatched curly-brackets in many cases (such as 'expected {, was (end)' in tests/unit/fixtures/comma.js)
e608f03
CarlEkerot Removed commented addError-calls in parser.js. 8d4f985
CarlEkerot Merge branch 'master' of https://github.com/CarlEkerot/jshint 2d65d8a
Aug 02, 2012
CarlEkerot Added assertions to ensure that code execution procedes after lonely …
…blocks, and that start-bracket errors doesn't halt the parsing.
f5ea569
Aug 13, 2012
CarlEkerot Merge remote branch 'upstream/master' d211d34
This page is out of date. Refresh to see the latest.
8 jshint.js
@@ -2508,7 +2508,7 @@ loop: for (;;) {
2508 2508 advance(":");
2509 2509 scope = Object.create(s);
2510 2510 addlabel(t.value, "label");
2511   - if (!nexttoken.labelled) {
  2511 + if (!nexttoken.labelled && nexttoken.value !== "{") {
2512 2512 warning("Label '{a}' on {b} statement.",
2513 2513 nexttoken, t.value, nexttoken.value);
2514 2514 }
@@ -2520,6 +2520,12 @@ loop: for (;;) {
2520 2520 t = nexttoken;
2521 2521 }
2522 2522
  2523 +// Is it a lonely block?
  2524 + if (t.id === "{") {
  2525 + block(true, true);
  2526 + return;
  2527 + }
  2528 +
2523 2529 // Parse the statement.
2524 2530
2525 2531 if (!noindent) {
31 tests/unit/fixtures/blocks.js
... ... @@ -0,0 +1,31 @@
  1 +var a, b, c;
  2 +a = 1;
  3 +{
  4 + b = 2;
  5 + {
  6 + c = 7;
  7 + }
  8 + set_to_three: {
  9 + a = 3;
  10 + }
  11 + count:
  12 + for (var i = 1; i <= 3; ++i) {
  13 + a += i;
  14 + }
  15 + switchStmt:
  16 + switch (a) {
  17 + case 0: b = 1; break;
  18 + case 1: b = 0; break;
  19 + default: b = 2; break;
  20 + }
  21 +}
  22 +
  23 +c = 3;
  24 +
  25 +labeledBlock: {
  26 + c += a + b;
  27 +}
  28 +
  29 +badBlock: {
  30 + a = 0;
  31 + {
22 tests/unit/parser.js
@@ -333,10 +333,19 @@ exports.comma = function () {
333 333 .addError(6, 'Expected an identifier and instead saw \')\'.')
334 334 .addError(6, 'Expected an assignment or function call and instead saw an expression.')
335 335 .addError(6, 'Missing semicolon.')
336   - .addError(6, 'Expected to see a statement and instead saw a block.')
337 336 .addError(6, 'Expected an assignment or function call and instead saw an expression.')
338 337 .addError(6, 'Missing semicolon.')
339   - .addError(8, 'Expected \'(end)\' and instead saw \'}\'.')
  338 + .addError(15, 'Expected an assignment or function call and instead saw an expression.')
  339 + .addError(15, 'Missing semicolon.')
  340 + .addError(20, 'Expected \')\' to match \'(\' from line 20 and instead saw \',\'.')
  341 + .addError(20, 'Expected an assignment or function call and instead saw an expression.')
  342 + .addError(20, 'Missing semicolon.')
  343 + .addError(20, 'Expected an identifier and instead saw \')\'.')
  344 + .addError(30, 'Expected \')\' to match \'(\' from line 30 and instead saw \',\'.')
  345 + .addError(30, 'Expected \')\' and instead saw \'args\'.')
  346 + .addError(30, 'Expected an assignment or function call and instead saw an expression.')
  347 + .addError(30, 'Missing semicolon.')
  348 + .addError(30, 'Expected an identifier and instead saw \')\'.')
340 349 .test(src);
341 350 };
342 351
@@ -361,6 +370,15 @@ exports.withStatement = function () {
361 370 .test(src, {white: true, withstmt: true});
362 371 };
363 372
  373 +exports.blocks = function () {
  374 + var src = fs.readFileSync(__dirname + "/fixtures/blocks.js", "utf8");
  375 +
  376 + TestRun()
  377 + .addError(29, 'Unmatched \'{\'.')
  378 + .addError(31, 'Unmatched \'{\'.')
  379 + .test(src);
  380 +};
  381 +
364 382 exports.functionCharaterLocation = function () {
365 383 var i;
366 384 var src = fs.readFileSync(__dirname + "/fixtures/nestedFunctions.js", "utf8");

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.