tslint one line rule misplaced 'else'

折月煮酒 提交于 2019-12-10 12:47:07

问题


I have such config in tslint.json for one line rule

one-line": [true,
      "check-open-brace",
      "check-catch",
      "check-else",
      "check-whitespace"
    ],

When I have code lines like that:

if(SomethingTrue) { next("a"); }
else { next("b"); }

I've got warning:

(one-line) file.ts[17, 9]: misplaced 'else'

Why that is happens? Is it bad practice to have one line else?


回答1:


You have :

else { next("b"); }

Else must be one one line. So:

else { 
    next("b"); 
}

Is it bad practice to have one line else?

Just easier to read. Its a styleguide for consistency.




回答2:


if (condition is true) {
  // do something;
}
else {
  // do something else;
}

Notice that else comes next to }

if (condition is true) {
  // do something;
} else {
  // do something else;
}



回答3:


According to the tslint docs the problem is that when "check-else" is specified under one-line the else must be on the same line as the closing brace for your if.

So in your case instead of:

if(SomethingTrue) { next("a"); }
else { next("b"); }

Use this format:

if(SomethingTrue) { next("a"); } else { next("b"); }



回答4:


if (condition) {
  // Your Code
} else {
  // Your Code
}

End of If and start of else should be on the same line.



来源:https://stackoverflow.com/questions/35261282/tslint-one-line-rule-misplaced-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!