1 # enforce the location of single-line statements (nonblock-statement-body-position)
3 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.
5 For example, some developers avoid writing code like this:
12 If another developer attempts to add `baz();` to the `if` statement, they might mistakenly change the code to
17 baz(); // this line is not in the `if` statement!
20 To avoid this issue, one might require all single-line `if` statements to appear directly after the conditional, without a linebreak:
28 This rule aims to enforce a consistent location for single-line statements.
30 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`](/docs/rules/curly.md) rule instead.
34 This rule accepts a string option:
36 * `"beside"` (default) disallows a newline before a single-line statement.
37 * `"below"` requires a newline before a single-line statement.
38 * `"any"` does not enforce the position of a single-line statement.
40 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:
42 * `"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.
43 * `"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.
45 Examples of **incorrect** code for this rule with the default `"beside"` option:
48 /* eslint nonblock-statement-body-position: ["error", "beside"] */
58 for (let i = 1; i < foo; i++)
67 Examples of **correct** code for this rule with the default `"beside"` option:
70 /* eslint nonblock-statement-body-position: ["error", "beside"] */
77 for (let i = 1; i < foo; i++) bar();
81 if (foo) { // block statements are always allowed with this rule
88 Examples of **incorrect** code for this rule with the `"below"` option:
91 /* eslint nonblock-statement-body-position: ["error", "below"] */
98 for (let i = 1; i < foo; i++) bar();
100 do bar(); while (foo)
103 Examples of **correct** code for this rule with the `"below"` option:
106 /* eslint nonblock-statement-body-position: ["error", "below"] */
116 for (let i = 1; i < foo; i++)
124 // Although the second `if` statement is on the same line as the `else`, this is a very common
125 // pattern, so it's not checked by this rule.
130 Examples of **incorrect** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
133 /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
141 Examples of **correct** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
144 /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
152 ## When Not To Use It
154 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`](/docs/rules/curly.md) rule, because this will disallow single-line statements entirely.
158 * JSCS: [requireNewlineBeforeSingleStatementsInIf](https://jscs-dev.github.io/rule/requireNewlineBeforeSingleStatementsInIf)