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

Possible Bug - break before case when always returning

Expand Messages
  • Luke Page
    Hello, One piece of code which jslint fails on is something like the following switch(a) { case 2: return case 2 ; case 3: if (b) { return case 3b ; } else {
    Message 1 of 4 , Oct 6, 2010
    • 0 Attachment
      Hello,

      One piece of code which jslint fails on is something like the following

      switch(a) {
      case 2:
      return "case 2";
      case 3:
      if (b) {
      return "case 3b";
      } else {
      return "case 3";
      }
      case 4:
      return "case 4";
      }

      Problem at line 9 character 9: Expected a 'break' statement before 'case'.

      Is this correct? I think adding the break statement in makes it look like it
      doesn't always return.

      What do people think?

      Regards,

      Luke


      [Non-text portions of this message have been removed]
    • seank_com
      Rewriting it like this works and is arguably more correct. switch(a) { case 2: return case 2 ; case 3: if (b) { return case 3b ; } return case 3 ; case 4:
      Message 2 of 4 , Oct 6, 2010
      • 0 Attachment
        Rewriting it like this works and is arguably more correct.

        switch(a) {
        case 2:
        return "case 2";
        case 3:
        if (b) {
        return "case 3b";
        }
        return "case 3";
        case 4:
        return "case 4";
        }


        --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
        >
        > Hello,
        >
        > One piece of code which jslint fails on is something like the following
        >
        > switch(a) {
        > case 2:
        > return "case 2";
        > case 3:
        > if (b) {
        > return "case 3b";
        > } else {
        > return "case 3";
        > }
        > case 4:
        > return "case 4";
        > }
        >
        > Problem at line 9 character 9: Expected a 'break' statement before 'case'.
        >
        > Is this correct? I think adding the break statement in makes it look like it
        > doesn't always return.
        >
        > What do people think?
        >
        > Regards,
        >
        > Luke
        >
        >
        > [Non-text portions of this message have been removed]
        >
      • Douglas Crockford
        ... switch(a) { case 2: return case 2 ; case 3: return b ? case 3b : case 3 ; case 4: return case 4 ; }
        Message 3 of 4 , Oct 7, 2010
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
          switch(a) {
          case 2:
          return "case 2";
          case 3:
          return b ? "case 3b" : "case 3";
          case 4:
          return "case 4";
          }
        • Luke Page
          Yes, I think I had a quite serious moment of stupidity when I wrote that message. Thank you. ... [Non-text portions of this message have been removed]
          Message 4 of 4 , Oct 7, 2010
          • 0 Attachment
            Yes, I think I had a quite serious moment of stupidity when I wrote that
            message.

            Thank you.

            On 7 October 2010 08:43, Douglas Crockford <douglas@...> wrote:

            >
            >
            > --- In jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>, Luke
            > Page <luke.a.page@...> wrote:
            > switch(a) {
            > case 2:
            > return "case 2";
            > case 3:
            > return b ? "case 3b" : "case 3";
            >
            > case 4:
            > return "case 4";
            > }
            >
            >
            >


            [Non-text portions of this message have been removed]
          Your message has been successfully submitted and would be delivered to recipients shortly.