Loading ...
Sorry, an error occurred while loading the content.

Nested switch statements

Expand Messages
  • Chris
    Hello, JSLint reports errors in switch statements caused by omitting a break statement. I recently encountered such an error that JSLint did not catch. It
    Message 1 of 2 , Jun 21, 2008
    View Source
    • 0 Attachment
      Hello,

      JSLint reports errors in switch statements caused by omitting a "break" statement.

      I recently encountered such an error that JSLint did not catch.  It looks like nested switch statements may be ill advised?

      function test(item) {
          var x;

          switch (item.type) {
          case 'a':
              alert('type a');
              for (x = 0; x < item.length; x = x + 1) {
                  if (item[x].value === 'oh no!') {
                      break;
                  }
              }
              // JSLint reports a problem here because there
              // is no break statement before the next case.

          case 'b':
              switch (item.value) {
              case '1':
                  alert('type b.1');
                  break;

              case '2':
                  alert('type b.2');
                  break;

              case '3':
                  alert('type b.3');
                  break;
              }

              // Expected JSLint to complain about the lack
              // of break | return | throw here.

          case 'c':
              alert('type c');
          }

      }

    • Douglas Crockford
      ... JSlint should now correctly identify fallthrough in nested switch statements. Thank you for the report.
      Message 2 of 2 , Jun 21, 2008
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Chris" <Nielsen.Chris@...> wrote:
        > I recently encountered such an error that JSLint did not catch.

        JSlint should now correctly identify fallthrough in nested switch
        statements. Thank you for the report.
      Your message has been successfully submitted and would be delivered to recipients shortly.