Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix correct/incorrect examples of rules #17789

Merged
merged 3 commits into from Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/src/rules/capitalized-comments.md
Expand Up @@ -216,10 +216,12 @@ Examples of **correct** code with `ignoreConsecutiveComments` set to `true`:
```js
/* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */

foo();
// This comment is valid since it has the correct capitalization.
// this comment is ignored since it follows another comment,
// and this one as well because it follows yet another comment.

bar();
/* Here is a block comment which has the correct capitalization, */
/* but this one is ignored due to being consecutive; */
/*
Expand All @@ -236,6 +238,7 @@ Examples of **incorrect** code with `ignoreConsecutiveComments` set to `true`:
```js
/* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */

foo();
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
// this comment is invalid, but only on this line.
// this comment does NOT get reported, since it is a consecutive comment.
```
Expand Down
19 changes: 12 additions & 7 deletions docs/src/rules/dot-notation.md
Expand Up @@ -84,22 +84,27 @@ class C {

For example, when preparing data to be sent to an external API, it is often required to use property names that include underscores. If the `camelcase` rule is in effect, these [snake case](https://en.wikipedia.org/wiki/Snake_case) properties would not be allowed. By providing an `allowPattern` to the `dot-notation` rule, these snake case properties can be accessed with bracket notation.

Examples of **correct** code for the sample `{ "allowPattern": "^[a-z]+(_[a-z]+)+$" }` option:
Examples of **incorrect** code for the sample `{ "allowPattern": "^[a-z]+(_[a-z]+)+$" }` (pattern to find snake case named properties) option:

:::correct
:::incorrect

```js
/*eslint camelcase: "error"*/
/*eslint dot-notation: ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }]*/

var data = {};
data.foo_bar = 42;

var data = {};
data["fooBar"] = 42;
```

:::
Examples of **correct** code for the sample `{ "allowPattern": "^[a-z]+(_[a-z]+)+$" }` (pattern to find snake case named properties) option:

:::correct

```js
/*eslint dot-notation: ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }]*/

var data = {};
data["foo_bar"] = 42; // no warning
data["foo_bar"] = 42;
```

:::
12 changes: 6 additions & 6 deletions docs/src/rules/max-lines-per-function.md
Expand Up @@ -72,7 +72,7 @@ is equivalent to

### code

Examples of **incorrect** code for this rule with a max value of `2`:
Examples of **incorrect** code for this rule with a particular max value:

::: incorrect

Expand All @@ -88,7 +88,7 @@ function foo() {
::: incorrect

```js
/*eslint max-lines-per-function: ["error", 2]*/
/*eslint max-lines-per-function: ["error", 3]*/
function foo() {
// a comment
var x = 0;
Expand All @@ -100,7 +100,7 @@ function foo() {
::: incorrect

```js
/*eslint max-lines-per-function: ["error", 2]*/
/*eslint max-lines-per-function: ["error", 4]*/
function foo() {
// a comment followed by a blank line

Expand All @@ -110,7 +110,7 @@ function foo() {

:::

Examples of **correct** code for this rule with a max value of `3`:
Examples of **correct** code for this rule with a particular max value:

::: correct

Expand All @@ -126,7 +126,7 @@ function foo() {
::: correct

```js
/*eslint max-lines-per-function: ["error", 3]*/
/*eslint max-lines-per-function: ["error", 4]*/
function foo() {
// a comment
var x = 0;
Expand All @@ -138,7 +138,7 @@ function foo() {
::: correct

```js
/*eslint max-lines-per-function: ["error", 3]*/
/*eslint max-lines-per-function: ["error", 5]*/
function foo() {
// a comment followed by a blank line

Expand Down
4 changes: 4 additions & 0 deletions docs/src/rules/no-async-promise-executor.md
Expand Up @@ -33,6 +33,8 @@ Examples of **incorrect** code for this rule:
::: incorrect

```js
/*eslint no-async-promise-executor: "error"*/

const foo = new Promise(async (resolve, reject) => {
readFile('foo.txt', function(err, result) {
if (err) {
Expand All @@ -55,6 +57,8 @@ Examples of **correct** code for this rule:
::: correct

```js
/*eslint no-async-promise-executor: "error"*/

const foo = new Promise((resolve, reject) => {
readFile('foo.txt', function(err, result) {
if (err) {
Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/no-else-return.md
Expand Up @@ -28,7 +28,7 @@ This rule has an object option:
* `allowElseIf: true` (default) allows `else if` blocks after a return
* `allowElseIf: false` disallows `else if` blocks after a return

### `allowElseIf: true`
### allowElseIf: true

Examples of **incorrect** code for this rule:

Expand Down Expand Up @@ -137,7 +137,7 @@ function foo4() {

:::

### `allowElseIf: false`
### allowElseIf: false

Examples of **incorrect** code for this rule:

Expand Down
12 changes: 7 additions & 5 deletions docs/src/rules/no-eval.md
Expand Up @@ -104,6 +104,8 @@ class A {

## Options

### allowIndirect

This rule has an option to allow indirect calls to `eval`.
Indirect calls to `eval` are less dangerous than direct calls to `eval` because they cannot dynamically change the scope. Because of this, they also will not negatively impact performance to the degree of direct `eval`.

Expand All @@ -118,7 +120,7 @@ Example of **incorrect** code for this rule with the `{"allowIndirect": true}` o
::: incorrect

```js
/*eslint no-eval: "error"*/
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/

var obj = { x: "foo" },
key = "x",
Expand All @@ -129,10 +131,10 @@ var obj = { x: "foo" },

Examples of **correct** code for this rule with the `{"allowIndirect": true}` option:

::: correct
::: correct { "sourceType": "script" }

```js
/*eslint no-eval: "error"*/
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

(0, eval)("var a = 0");

Expand All @@ -147,7 +149,7 @@ this.eval("var a = 0");
::: correct

```js
/*eslint no-eval: "error"*/
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
/*eslint-env browser*/

window.eval("var a = 0");
Expand All @@ -158,7 +160,7 @@ window.eval("var a = 0");
::: correct

```js
/*eslint no-eval: "error"*/
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
/*eslint-env node*/

global.eval("var a = 0");
Expand Down
2 changes: 1 addition & 1 deletion docs/src/rules/no-implicit-globals.md
Expand Up @@ -79,7 +79,7 @@ window.bar = function() {};

Examples of **correct** code for this rule with `"parserOptions": { "sourceType": "module" }` in the ESLint configuration:

::: correct { "sourceType": "script" }
::: correct { "sourceType": "module" }

```js
/*eslint no-implicit-globals: "error"*/
Expand Down
1 change: 1 addition & 0 deletions docs/src/rules/no-implied-eval.md
Expand Up @@ -35,6 +35,7 @@ Examples of **incorrect** code for this rule:

```js
/*eslint no-implied-eval: "error"*/
/*eslint-env browser*/

setTimeout("alert('Hi!');", 100);

Expand Down
10 changes: 8 additions & 2 deletions docs/src/rules/no-loop-func.md
Expand Up @@ -48,15 +48,21 @@ for (var i=10; i; i--) {
(function() { return i; })();
}

while(i) {
var i = 0;
while(i < 5) {
var a = function() { return i; };
a();

i++;
}

var i = 0;
do {
function a() { return i; };
a();
} while (i);

i++
} while (i < 5);

let foo = 0;
for (let i = 0; i < 10; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/no-redeclare.md
Expand Up @@ -77,7 +77,7 @@ The `"builtinGlobals"` option will check for redeclaration of built-in globals i

Examples of **incorrect** code for the `{ "builtinGlobals": true }` option:

::: incorrect
::: incorrect { "sourceType": "script" }

```js
/*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/
Expand All @@ -89,7 +89,7 @@ var Object = 0;

Examples of **incorrect** code for the `{ "builtinGlobals": true }` option and the `browser` environment:

::: incorrect
::: incorrect { "sourceType": "script" }

```js
/*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/
Expand Down
2 changes: 2 additions & 0 deletions docs/src/rules/no-undef-init.md
Expand Up @@ -87,6 +87,8 @@ Example of **incorrect** code for this rule:
::: incorrect

```js
/*eslint no-undef-init: "error"*/

for (i = 0; i < 10; i++) {
var x = undefined;
console.log(x);
Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/no-unmodified-loop-condition.md
Expand Up @@ -44,8 +44,8 @@ while (node) {
}
node = other;

for (var j = 0; j < items.length; ++i) {
doSomething(items[j]);
for (var j = 0; j < 5;) {
doSomething(j);
}

while (node !== root) {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/rules/no-unused-expressions.md
Expand Up @@ -79,7 +79,7 @@ Examples of **correct** code for the default `{ "allowShortCircuit": false, "all

{} // In this context, this is a block statement, not an object literal

{myLabel: someVar} // In this context, this is a block statement with a label and expression, not an object literal
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
{ myLabel: foo() } // In this context, this is a block statement with a label and expression, not an object literal

function namedFunctionDeclaration () {}

Expand Down
8 changes: 4 additions & 4 deletions docs/src/rules/object-shorthand.md
Expand Up @@ -116,7 +116,7 @@ Additionally, the rule takes an optional object configuration:
* `"methodsIgnorePattern"` (`string`) for methods whose names match this regex pattern, the method shorthand will not be enforced. Note that this option can only be used when the string option is set to `"always"` or `"methods"`.
* `"avoidExplicitReturnArrows": true` indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to `"always"` or `"methods"`.

### `avoidQuotes`
### avoidQuotes

```json
{
Expand Down Expand Up @@ -155,7 +155,7 @@ var foo = {

:::

### `ignoreConstructors`
### ignoreConstructors

```json
{
Expand All @@ -178,7 +178,7 @@ var foo = {

:::

### `methodsIgnorePattern`
### methodsIgnorePattern

Example of **correct** code for this rule with the `"always", { "methodsIgnorePattern": "^bar$" }` option:

Expand All @@ -194,7 +194,7 @@ var foo = {

:::

### `avoidExplicitReturnArrows`
### avoidExplicitReturnArrows

```json
{
Expand Down
3 changes: 3 additions & 0 deletions docs/src/rules/one-var.md
Expand Up @@ -462,6 +462,9 @@ var bar = "bar";
::: correct

```js
/*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/
/*eslint-env node*/

var foo = require("foo"),
bar = require("bar");
```
Expand Down
10 changes: 9 additions & 1 deletion docs/src/rules/prefer-regex-literals.md
Expand Up @@ -110,12 +110,14 @@ This rule has an object option:

* `disallowRedundantWrapping` set to `true` additionally checks for unnecessarily wrapped regex literals (Default `false`).

### `disallowRedundantWrapping`
### disallowRedundantWrapping

By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a `RegExp` constructor call. When the option `disallowRedundantWrapping` is set to `true`, the rule will also disallow such unnecessary patterns.

Examples of `incorrect` code for `{ "disallowRedundantWrapping": true }`

::: incorrect

```js
/*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/

Expand All @@ -124,8 +126,12 @@ new RegExp(/abc/);
new RegExp(/abc/, 'u');
```

:::

Examples of `correct` code for `{ "disallowRedundantWrapping": true }`

::: correct

```js
/*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/

Expand All @@ -135,3 +141,5 @@ Examples of `correct` code for `{ "disallowRedundantWrapping": true }`

new RegExp(/abc/, flags);
```

:::