nonblock-statement-body-position

强制执行单行语句的位置

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

在编写 ifelsewhiledo-whilefor 语句时,主体可以是单个语句而不是块。为这些单个语句强制执行一致的位置可能很有用。

例如,一些开发人员避免编写这样的代码:

if (foo)
  bar();

如果其他开发人员尝试将 baz(); 添加到 if 语句中,他们可能会错误地将代码更改为

if (foo)
  bar();
  baz(); // this line is not in the `if` statement!

diktJe/lcP0OqGdUAccnPhkVXfCADKXdVAFlLvHLBse8dXVq1mOdPBTiGtXhJ/k+oeqT33ZYRvfLLq5D8TTi8VnQpzX/p7uSb6Syvbol8PPYn74oa/klSDOWGrXmSqWdROM0+9AsXIUfL05ckEKQ2T+TBaOwqgw5/nRenx+VR5Y=

if (foo) bar();

规则详情

fseQJPyXBwfj5bhsAnzMe1DxmzfR9C3AJ/r9fHpQ4od9vC+XUuquootrV5ML5jn10tRaVPscH+3+PVz/nZrefA==

d+bpcDOpK3Pen/BCSYesDOgn/Nwpe3V2t5817EY8BPaX61yWoENfZR0J4DDcLYTaZ5SWpMdkXTI8OiaNjamqx6pKLY/4OqIxv3HWHO6lDGcTivYswBQyVMu1oYJ3cNy/TNlf/kUvsvoxRijiozGwTV7mlpDP69aO22gK2LAOZKH11XG6jbthDDJ90fSWBY4d/ZU9ykB9EQ2R+wClEt6FkbHh9TDkjeZzITvvQbInZyQHE9lL0M6Eywv2Pt2DxP5O

选项

2ozHSP5eIqZ0F37GH4Z4mUOeJ5q4WmqPt5Ox+smeoaYs619kDdNmlguTKsiMa54R

    SeawA6AFP2bfGwAo67fqvaMlIYbtm74smQeJN65zXZLfQZRkruXCGve70ywXw5wVZ0foKnJe/8Sn9Co+bJs+YicErg4kpdi7tfqR5VkudUjeoBC04sv/YoZhKjYz4RQBu7rRVX9PH14NL43P4xH8URFYbnrSW/K7sR7aoFHEKn1/n8Ixl14JzxW6meljkNV2O96mRI35Rx67S01xA0JaOQalEgZItF2ktMG+SCqQuQJnQmjBKkdyVufkQQxXR8t+B0rPgbo0Aw/BgzyThoCsBQ8n/clHOOjO3eqKAFyYB1me8Yk8h1I1mEfcdrefktcL

/7FEBOhvEklID7lSdnv2xrSHy6uQhQimtmADF2WHqy27SB1P43WjeJaPKkwFa+KgNX6l71Njjyh7PP6Jd/b379mFRCGWovmPapWJ/mQbA6BGHjJCBPbQdRadVr/3TExKHaau9gwuu9GWd3yaP9AOda3cIwsa86TwSi0xU7BSPA7+eHddtBeyNaoe95VI53LDiZP0tMRkFZoFGNthUqVRng==

    SeawA6AFP2bfGwAo67fqvT9/t5rXtEhzaewmV/sKAGppdoXSnxoFrn61pAYCYXRSQUU61Kel/m99UUEvyyrt7u5Dlylle7MSZRgCZizzk7G/yiiFk7BEMHAlgKQAZMkxt6xFmZTsIyzG+QAvLlPW7RjRiB5sxr2Ts/M3AJDNTJX05IinsrdyNI2G8MZeEAmwTVSkfZzQY6JpvHYpcP7J2lG1a8+nvxpCKnh+VIKXUdUuXiCDdOoAj7g7QNvMbmhZFQxQgsk/MS1AooiP7neLXHveAT2J+aHhgksgOz1Nw+IEfuOTMBEF9bt+PL3G87oSNbb/7290yB5umUemIDJMqp84jH/yf6ll7QNNKTKmZxW8xziDbJfdlQahJ7/mvPgdbFFAYcqzvUzKHMl+iTEkNblSG1LUw8Mf3tAX0ulXtd5nAGjtmO9Cc9b+rNZoA1WQ8Jc2fDVQTeWqHO2E1B/epUAPX6TNHjmMS6RqIFQq6J6px3Wg6suENnfy0fOyb15RZmaEZR6Q+S5Ch+nsFFwOx0+47BY65W/Y9snTR1moCs/tOJ3TeEMbhxsPrZ8QsKFDV82mAHZf07G2g0ojVrC9Qgd6TrNhmcJOJccX7R1WRVDhlTX+NfIPiFuOqeQw938j

htl/dXJfTgekkr4jO+eoa8SyqyImBpVF1mhl+gtnd4cg3hTmCpQNa1yNRRZRGKWfxyU37Jv4xMUrfmXnyCVcINfmnSj/EdzjZzD5hNgFFQ0=

/* eslint nonblock-statement-body-position: ["error", "beside"] */

if (foo)
  bar();
else
  baz();

while (foo)
  bar();

for (let i = 1; i < foo; i++)
  bar();

do
  bar();
while (foo)

htl/dXJfTgekkr4jO+eoa8SyqyImBpVF1mhl+gtnd4cg3hTmCpQNa1yNRRZRGKWfWWFllm0yLNt2rXddjTKOaAWAMqD04TCXUZCWwBGGSGA=

/* eslint nonblock-statement-body-position: ["error", "beside"] */

if (foo) bar();
else baz();

while (foo) bar();

for (let i = 1; i < foo; i++) bar();

do bar(); while (foo)

if (foo) { // block statements are always allowed with this rule
  bar();
} else {
  baz();
}

Z7H25lXhtMWN9oIrVJkScwvDzg0XLtUjQ2JgufQNKQeJaCgDOS0psjbVJAyacvylGrDQiMObnRbd6OsG+Yra8cflL1mzdguYIkSL12jE95c=

/* eslint nonblock-statement-body-position: ["error", "below"] */

if (foo) bar();
else baz();

while (foo) bar();

for (let i = 1; i < foo; i++) bar();

do bar(); while (foo)

Z7H25lXhtMWN9oIrVJkScwvDzg0XLtUjQ2JgufQNKQfzh/zZbc5+S8CSjd2aE+tlhQlChmLRArKTKJqCzuKLGXKCO/ZEVoxPUPCYykkZd8A=

/* eslint nonblock-statement-body-position: ["error", "below"] */

if (foo)
  bar();
else
  baz();

while (foo)
  bar();

for (let i = 1; i < foo; i++)
  bar();

do
  bar();
while (foo)

if (foo) {
  // Although the second `if` statement is on the same line as the `else`, this is a very common
  // pattern, so it's not checked by this rule.
} else if (bar) {
}

WfBhzvzzjdX7bDS4lPfckXal51d+ubUiCUiTBksi8KfZu93RXNJ2rT825EkUHIsUpq4dI3Hb5pV39M/V/02cxa1DgRn2eLEkxKFw59QffH1VOdoPhAK6qaI+pbf/V4Jj+VKRAjq59xPh1heygFNOIQ==

/* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */

if (foo)
  bar();

while (foo) bar();

WfBhzvzzjdX7bDS4lPfckXal51d+ubUiCUiTBksi8KfZu93RXNJ2rT825EkUHIsUpq4dI3Hb5pV39M/V/02cxa1DgRn2eLEkxKFw59QffH0HtC7GW3lRysaOhnAFcGlIaL6rbogHP7WXrbFNRQ8smg==

/* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */

if (foo) bar();

while (foo)
  bar();

何时不使用

hCLeKMPzlHfznjAeWCRp84H29ec2lgav3/ubfRVTVCixLX+t6Lq3sOkBe1VlZOw2L/sVb80hVmE61zcrZGygvPNEDPbuMLlxdGTc04GPzq+9bhIdZBEKEtQE98REBBBIIfxG2gGy2NAslPUy/rk62LpaojPMs+DtobbP9Mjo0FxtKXGJDQOO0svBS1K6FSfZLts1X/NozML/2EKehgOOcWQQTXjBtQRgx4B0VbTdiLHaiC/V7ohhsy6begZnKsLDW9D64WFud/2yXspwtWY1jDTD4sufadS39b0hghKKcuYYA3z3xGE0HxbJF+S3zssSqe+E9xFc6lXR1yUeLOz3y73arLLdXHHIppGuAn9zZqg=