Skip to content

Commit 5ae3bcb

Browse files
committed
Merge pull request #5851 from scriptdaemon/docs-stylistic-issues-2
Docs: Distinguish examples in rules under Stylistic Issues part 2
2 parents 750cc09 + a8821a5 commit 5ae3bcb

File tree

5 files changed

+357
-232
lines changed

5 files changed

+357
-232
lines changed

docs/rules/func-style.md

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# Enforce Function Style (func-style)
1+
# enforce the consistent use of either `function` declarations or expressions (func-style)
22

3-
There are two ways of defining functions in JavaScript: function declarations and function expressions. Declarations have the `function` keyword first, followed by a name, followed by its arguments and the function body, such as:
3+
There are two ways of defining functions in JavaScript: `function` declarations and `function` expressions. Declarations contain the `function` keyword first, followed by a name and then its arguments and the function body, for example:
44

55
```js
66
function doSomething() {
77
// ...
88
}
99
```
1010

11-
Equivalent function expressions begin with the `var` keyword, followed by a name, and then the function itself, such as:
11+
Equivalent function expressions begin with the `var` keyword, followed by a name and then the function itself, such as:
1212

1313
```js
1414
var doSomething = function() {
1515
// ...
1616
};
1717
```
1818

19-
The primary difference between function declarations and function expressions is that declarations are *hoisted* to the top of the scope in which they are defined, which allows you to write code that uses the function before the declaration. For example:
19+
The primary difference between `function` declarations and `function expressions` is that declarations are *hoisted* to the top of the scope in which they are defined, which allows you to write code that uses the function before its declaration. For example:
2020

2121
```js
2222
doSomething();
@@ -26,9 +26,9 @@ function doSomething() {
2626
}
2727
```
2828

29-
Although this code might seem like an error, it actually works fine because JavaScript engines hoist the function declarations to the top of the scope. That means this code is treated as if the declaration came before the invocation.
29+
Although this code might seem like an error, it actually works fine because JavaScript engines hoist the `function` declarations to the top of the scope. That means this code is treated as if the declaration came before the invocation.
3030

31-
For function expressions, you must define the function before it is used, otherwise it causes an error. Example:
31+
For `function` expressions, you must define the function before it is used, otherwise it causes an error. Example:
3232

3333
```js
3434
doSomething(); // error!
@@ -44,19 +44,22 @@ Due to these different behaviors, it is common to have guidelines as to which st
4444

4545
## Rule Details
4646

47-
This rule is aimed at enforcing a particular type of function style throughout a JavaScript file, either declarations or expressions. You can specify which you prefer in the configuration.
47+
This rule enforces a particular type of `function` style throughout a JavaScript file, either declarations or expressions. You can specify which you prefer in the configuration.
4848

4949
## Options
5050

51-
### "expression"
51+
This rule has a string option:
5252

53-
This is the default configuration. It reports an error when function declarations are used instead of function expressions.
53+
* `"expression"` (default) requires the use of function expressions instead of function declarations
54+
* `"declaration"` requires the use of function declarations instead of function expressions
5455

55-
```json
56-
"func-style": ["error", "expression"]
57-
```
56+
This rule has an object option for an exception:
57+
58+
* `"allowArrowFunctions": true` (default `false`) allows the use of arrow functions
5859

59-
The following patterns are considered problems:
60+
### expression
61+
62+
Examples of **incorrect** code for this rule with the default `"expression"` option:
6063

6164
```js
6265
/*eslint func-style: ["error", "expression"]*/
@@ -66,7 +69,7 @@ function foo() {
6669
}
6770
```
6871

69-
The following patterns are not considered problems:
72+
Examples of **correct** code for this rule with the default `"expression"` option:
7073

7174
```js
7275
/*eslint func-style: ["error", "expression"]*/
@@ -76,37 +79,21 @@ var foo = function() {
7679
};
7780
```
7881

79-
### "declaration"
80-
81-
This reports an error if any function expressions are used where function declarations are expected. You can specify to use expressions instead:
82-
83-
```json
84-
"func-style": ["error", "declaration"]
85-
```
86-
87-
An additional option object can be added with a property `"allowArrowFunctions"`. Setting this to `true` will allow arrow functions.
88-
89-
```json
90-
"func-style": ["error", "declaration", { "allowArrowFunctions": true }]
91-
```
82+
### declaration
9283

93-
The following patterns are considered problems:
84+
Examples of **incorrect** code for this rule with the `"declaration"` option:
9485

9586
```js
9687
/*eslint func-style: ["error", "declaration"]*/
9788

9889
var foo = function() {
9990
// ...
10091
};
101-
```
102-
103-
```js
104-
/*eslint func-style: ["error", "declaration"]*/
10592

10693
var foo = () => {};
10794
```
10895

109-
The following patterns are not considered problems:
96+
Examples of **correct** code for this rule with the `"declaration"` option:
11097

11198
```js
11299
/*eslint func-style: ["error", "declaration"]*/
@@ -121,13 +108,16 @@ SomeObject.foo = function() {
121108
};
122109
```
123110

111+
### allowSingleLine
112+
113+
Examples of additional **correct** code for this rule with the `"declaration", { "allowSingleLine": true }` options:
114+
124115
```js
125116
/*eslint func-style: ["error", "declaration", { "allowArrowFunctions": true }]*/
126117

127118
var foo = () => {};
128119
```
129120

130-
131121
## When Not To Use It
132122

133123
If you want to allow developers to each decide how they want to write functions on their own, then you can disable this rule.

docs/rules/id-blacklist.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
# Blacklist certain identifiers to prevent them being used (id-blacklist)
1+
# disallow specified identifiers (id-blacklist)
22

33
> "There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
44
5-
Bad names can lead to hard to decipher code. Using generic names, such as `data` don't infer much about the code and the values it receives. This rule allows you to configure a blacklist of bad identifier names, that you don't want to see in your code.
5+
Bad names can lead to hard-to-decipher code. Generic names, such as `data`, don't infer much about the code and the values it receives. This rule allows you to configure a blacklist of bad identifier names, that you don't want to see in your code.
66

77
## Rule Details
88

9-
This rule compares assignments and function definitions to a provided list of identifier names. If the identifier is present in the list, it will return an error.
9+
This rule disallows specified identifiers in assignments and `function` definitions.
1010

1111
This rule will catch blacklisted identifiers that are:
1212

1313
- variable declarations
1414
- function declarations
15-
- object properties
15+
- object properties assigned to during object creation
1616

1717
It will not catch blacklisted identifiers that are:
1818

1919
- function calls (so you can still use functions you do not have control over)
2020
- object properties (so you can still use objects you do not have control over)
2121

22-
2322
## Options
2423

25-
This rule needs a a set of identifier names to blacklist, like so:
24+
The rule takes one or more strings as options: the names of restricted identifiers.
25+
26+
For example, to restrict the use of common generic identifiers:
2627

2728
```json
2829
{
29-
"rules": {
30-
"id-blacklist": ["error", "data", "err", "e", "cb", "callback"]
31-
}
30+
"id-blacklist": ["error", "data", "err", "e", "cb", "callback"]
3231
}
3332
```
3433

35-
For the rule in this example, the following patterns are considered problems:
34+
Examples of **incorrect** code for this rule with sample `"data", "callback"` restricted identifiers:
3635

3736
```js
38-
/*eslint id-blacklist: ["error", "data", "err", "e", "cb", "callback"] */
37+
/*eslint id-blacklist: ["error", "data", "callback"] */
3938

4039
var data = {...};
4140

@@ -52,10 +51,10 @@ var itemSet = {
5251
};
5352
```
5453

55-
The following patterns are not considered problems:
54+
Examples of **correct** code for this rule with sample `"data", "callback"` restricted identifiers:
5655

5756
```js
58-
/*eslint id-blacklist: ["error", "data", "err", "e", "cb", "callback"] */
57+
/*eslint id-blacklist: ["error", "data", "callback"] */
5958

6059
var encodingOptions = {...};
6160

@@ -71,11 +70,11 @@ var itemSet = {
7170
entities: [...]
7271
};
7372

74-
callback() // all function calls are ignored
73+
callback(); // all function calls are ignored
7574

76-
foo.callback() // all function calls are ignored
75+
foo.callback(); // all function calls are ignored
7776

78-
foo.data // all property names that are not assignments are ignored
77+
foo.data; // all property names that are not assignments are ignored
7978
```
8079

8180
## When Not To Use It

0 commit comments

Comments
 (0)