jshint“前‘案例’预计一个‘休息’声明”抛出一个

问题描述 投票:54回答:2

当我的框架使用jshint来验证我的javascript代码你好我有一个麻烦。我已经使用的switch-case没有break语句故意,但这个代码部分被捕获为错误时jshint检查。我的代码是一样的东西下面。

    switch (<no>){
    case 1:
        // does something
    case 2:
        //does something more
    default:
        // does something even more
   }

Error from 'jshint' is like Line 203 character 41: Expected a 'break' statement before 'case'.如何避免它有什么想法?或者是一个不好的做法,在所有使用开关的情况下在这种情况下?

javascript jshint
2个回答
117
投票

复制和粘贴from the documentation

switch语句

默认情况下,当你忽略switch语句中休息或return语句JSHint警告:

[...]

如果你真的知道你在做什么,你可以告诉JSHint您的预期情况下,块通过添加/* falls through */评论告吹

所以你的情况:

switch (<no>) {
  case 1:
    // does something
    /* falls through */
  case 2:
    //does something more
    /* falls through */
  default:
    // does something even more
}

0
投票

没错,breaks可能完全是多余的,就像这个例子

function mapX(x){
  switch (x){
    case 1:
      return A;
    case 2:
      return B;
    default:
      return C;
  }
}

在这种情况下,如果你将不得不breakreturnJS Standard会抛出一个警告,即Unreachable code

试图调解jshint和JS标准是棘手,但指出,该解决方案将是

function mapX(x){
  switch (x){
    case 1:
      return A;
      /* falls through */
    case 2:
      return B;
      /* falls through */
    default:
      return C;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.