2 title: nonblock-statement-body-position
5 - https://jscs-dev.github.io/rule/requireNewlineBeforeSingleStatementsInIf
10 When writing `if`, `else`, `while`, `do-while`, and `for` statements, the body can be a single statement instead of a block. It can be useful to enforce a consistent location for these single statements.
12 For example, some developers avoid writing code like this:
19 If another developer attempts to add `baz();` to the `if` statement, they might mistakenly change the code to
24 baz(); // this line is not in the `if` statement!
27 To avoid this issue, one might require all single-line `if` statements to appear directly after the conditional, without a linebreak:
35 This rule aims to enforce a consistent location for single-line statements.
37 Note that this rule does not enforce the usage of single-line statements in general. If you would like to disallow single-line statements, use the [`curly`](curly) rule instead.
41 This rule accepts a string option:
43 * `"beside"` (default) disallows a newline before a single-line statement.
44 * `"below"` requires a newline before a single-line statement.
45 * `"any"` does not enforce the position of a single-line statement.
47 Additionally, the rule accepts an optional object option with an `"overrides"` key. This can be used to specify a location for particular statements that override the default. For example:
49 * `"beside", { "overrides": { "while": "below" } }` requires all single-line statements to appear on the same line as their parent, unless the parent is a `while` statement, in which case the single-line statement must not be on the same line.
50 * `"below", { "overrides": { "do": "any" } }` disallows all single-line statements from appearing on the same line as their parent, unless the parent is a `do-while` statement, in which case the position of the single-line statement is not enforced.
52 Examples of **incorrect** code for this rule with the default `"beside"` option:
57 /* eslint nonblock-statement-body-position: ["error", "beside"] */
67 for (let i = 1; i < foo; i++)
78 Examples of **correct** code for this rule with the default `"beside"` option:
83 /* eslint nonblock-statement-body-position: ["error", "beside"] */
90 for (let i = 1; i < foo; i++) bar();
94 if (foo) { // block statements are always allowed with this rule
103 Examples of **incorrect** code for this rule with the `"below"` option:
108 /* eslint nonblock-statement-body-position: ["error", "below"] */
115 for (let i = 1; i < foo; i++) bar();
117 do bar(); while (foo)
122 Examples of **correct** code for this rule with the `"below"` option:
127 /* eslint nonblock-statement-body-position: ["error", "below"] */
137 for (let i = 1; i < foo; i++)
145 // Although the second `if` statement is on the same line as the `else`, this is a very common
146 // pattern, so it's not checked by this rule.
153 Examples of **incorrect** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
158 /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
168 Examples of **correct** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
173 /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
183 ## When Not To Use It
185 If you're not concerned about consistent locations of single-line statements, you should not turn on this rule. You can also disable this rule if you're using the `"all"` option for the [`curly`](curly) rule, because this will disallow single-line statements entirely.