mirror of
https://git.proxmox.com/git/pve-eslint
synced 2025-10-05 14:57:14 +00:00

includes a (minimal) working wrapper Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
145 lines
3.2 KiB
Markdown
145 lines
3.2 KiB
Markdown
# disallow variable or `function` declarations in nested blocks (no-inner-declarations)
|
|
|
|
In JavaScript, prior to ES6, a function declaration is only allowed in the first level of a program or the body of another function, though parsers sometimes [erroneously accept them elsewhere](https://code.google.com/p/esprima/issues/detail?id=422). This only applies to function declarations; named or anonymous function expressions can occur anywhere an expression is permitted.
|
|
|
|
```js
|
|
// Good
|
|
function doSomething() { }
|
|
|
|
// Bad
|
|
if (test) {
|
|
function doSomethingElse () { }
|
|
}
|
|
|
|
function anotherThing() {
|
|
var fn;
|
|
|
|
if (test) {
|
|
|
|
// Good
|
|
fn = function expression() { };
|
|
|
|
// Bad
|
|
function declaration() { }
|
|
}
|
|
}
|
|
```
|
|
|
|
A variable declaration is permitted anywhere a statement can go, even nested deeply inside other blocks. This is often undesirable due to variable hoisting, and moving declarations to the root of the program or function body can increase clarity. Note that [block bindings](https://leanpub.com/understandinges6/read#leanpub-auto-block-bindings) (`let`, `const`) are not hoisted and therefore they are not affected by this rule.
|
|
|
|
```js
|
|
/*eslint-env es6*/
|
|
|
|
// Good
|
|
var foo = 42;
|
|
|
|
// Good
|
|
if (foo) {
|
|
let bar1;
|
|
}
|
|
|
|
// Bad
|
|
while (test) {
|
|
var bar2;
|
|
}
|
|
|
|
function doSomething() {
|
|
// Good
|
|
var baz = true;
|
|
|
|
// Bad
|
|
if (baz) {
|
|
var quux;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Rule Details
|
|
|
|
This rule requires that function declarations and, optionally, variable declarations be in the root of a program or the body of a function.
|
|
|
|
## Options
|
|
|
|
This rule has a string option:
|
|
|
|
* `"functions"` (default) disallows `function` declarations in nested blocks
|
|
* `"both"` disallows `function` and `var` declarations in nested blocks
|
|
|
|
### functions
|
|
|
|
Examples of **incorrect** code for this rule with the default `"functions"` option:
|
|
|
|
```js
|
|
/*eslint no-inner-declarations: "error"*/
|
|
|
|
if (test) {
|
|
function doSomething() { }
|
|
}
|
|
|
|
function doSomethingElse() {
|
|
if (test) {
|
|
function doAnotherThing() { }
|
|
}
|
|
}
|
|
```
|
|
|
|
Examples of **correct** code for this rule with the default `"functions"` option:
|
|
|
|
```js
|
|
/*eslint no-inner-declarations: "error"*/
|
|
|
|
function doSomething() { }
|
|
|
|
function doSomethingElse() {
|
|
function doAnotherThing() { }
|
|
}
|
|
|
|
if (test) {
|
|
asyncCall(id, function (err, data) { });
|
|
}
|
|
|
|
var fn;
|
|
if (test) {
|
|
fn = function fnExpression() { };
|
|
}
|
|
```
|
|
|
|
### both
|
|
|
|
Examples of **incorrect** code for this rule with the `"both"` option:
|
|
|
|
```js
|
|
/*eslint no-inner-declarations: ["error", "both"]*/
|
|
|
|
if (test) {
|
|
var foo = 42;
|
|
}
|
|
|
|
function doAnotherThing() {
|
|
if (test) {
|
|
var bar = 81;
|
|
}
|
|
}
|
|
```
|
|
|
|
Examples of **correct** code for this rule with the `"both"` option:
|
|
|
|
```js
|
|
/*eslint no-inner-declarations: "error"*/
|
|
/*eslint-env es6*/
|
|
|
|
var bar = 42;
|
|
|
|
if (test) {
|
|
let baz = 43;
|
|
}
|
|
|
|
function doAnotherThing() {
|
|
var baz = 81;
|
|
}
|
|
```
|
|
|
|
## When Not To Use It
|
|
|
|
The function declaration portion rule will be rendered obsolete when [block-scoped functions](https://bugzilla.mozilla.org/show_bug.cgi?id=585536) land in ES6, but until then, it should be left on to enforce valid constructions. Disable checking variable declarations when using [block-scoped-var](block-scoped-var.md) or if declaring variables in nested blocks is acceptable despite hoisting.
|