Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for labeled blocks and lonely blocks #603

Closed
wants to merge 7 commits into from

2 participants

@CarlEkerot

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
@CarlEkerot 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
@CarlEkerot 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
@valueof 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 2eb909c1e20f3e581b0cf520c6cf9ede5620adf8
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 8d4f985
Author: CarlEkerot <carl.ekerot@lunicore.se>
Date:   Wed Aug 1 11:13:21 2012 +0200

    Removed commented addError-calls in parser.js.

commit e608f03
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 03c8ee6
Author: Carl Ekerot <kalle@implode.se>
Date:   Wed Aug 1 11:13:21 2012 +0200

    Removed commented addError-calls in parser.js.

commit 7550336
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
@valueof valueof closed this in 9ec3e3a
@jugglinmike jugglinmike referenced this pull request from a commit in jugglinmike/jshint
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 2eb909c1e20f3e581b0cf520c6cf9ede5620adf8
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 8d4f985
Author: CarlEkerot <carl.ekerot@lunicore.se>
Date:   Wed Aug 1 11:13:21 2012 +0200

    Removed commented addError-calls in parser.js.

commit e608f03
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 03c8ee6
Author: Carl Ekerot <kalle@implode.se>
Date:   Wed Aug 1 11:13:21 2012 +0200

    Removed commented addError-calls in parser.js.

commit 7550336
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)
54f17e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 1, 2012
  1. @CarlEkerot

    Fix for issue #565. Makes blocks a special case of statements. This f…

    CarlEkerot authored
    …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)
  2. @CarlEkerot
  3. Fix for issue #565. Makes blocks a special case of statements. This f…

    CarlEkerot authored
    …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)
  4. Removed commented addError-calls in parser.js.

    CarlEkerot authored
Commits on Aug 2, 2012
  1. Added assertions to ensure that code execution procedes after lonely …

    CarlEkerot authored
    …blocks, and that start-bracket errors doesn't halt the parsing.
Commits on Aug 13, 2012
  1. Merge remote branch 'upstream/master'

    CarlEkerot authored
This page is out of date. Refresh to see the latest.
Showing with 58 additions and 3 deletions.
  1. +7 −1 jshint.js
  2. +31 −0 tests/unit/fixtures/blocks.js
  3. +20 −2 tests/unit/parser.js
View
8 jshint.js
@@ -2508,7 +2508,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);
}
@@ -2520,6 +2520,12 @@ loop: for (;;) {
t = nexttoken;
}
+// Is it a lonely block?
+ if (t.id === "{") {
+ block(true, true);
+ return;
+ }
+
// Parse the statement.
if (!noindent) {
View
31 tests/unit/fixtures/blocks.js
@@ -0,0 +1,31 @@
+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;
+}
+
+badBlock: {
+ a = 0;
+ {
View
22 tests/unit/parser.js
@@ -333,10 +333,19 @@ 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 an assignment or function call and instead saw an expression.')
.addError(6, 'Missing semicolon.')
- .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);
};
@@ -361,6 +370,15 @@ exports.withStatement = function () {
.test(src, {white: true, withstmt: true});
};
+exports.blocks = function () {
+ var src = fs.readFileSync(__dirname + "/fixtures/blocks.js", "utf8");
+
+ TestRun()
+ .addError(29, 'Unmatched \'{\'.')
+ .addError(31, 'Unmatched \'{\'.')
+ .test(src);
+};
+
exports.functionCharaterLocation = function () {
var i;
var src = fs.readFileSync(__dirname + "/fixtures/nestedFunctions.js", "utf8");
Something went wrong with that request. Please try again.