Skip to content

Commit

Permalink
docs: rewrite examples with var using let and const (eslint#19315)
Browse files Browse the repository at this point in the history
  • Loading branch information
amareshsm authored Jan 6, 2025
1 parent 495aa49 commit 89c8fc5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 52 deletions.
18 changes: 9 additions & 9 deletions docs/src/rules/array-callback-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ If we forget to write `return` statement in a callback of those, it's probably a

```js
// example: convert ['a', 'b', 'c'] --> {a: 0, b: 1, c: 2}
var indexMap = myArray.reduce(function(memo, item, index) {
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {}); // Error: cannot set property 'b' of undefined
```
Expand Down Expand Up @@ -45,17 +45,17 @@ Examples of **incorrect** code for this rule:
```js
/*eslint array-callback-return: "error"*/

var indexMap = myArray.reduce(function(memo, item, index) {
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {});

var foo = Array.from(nodes, function(node) {
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
});

var bar = foo.filter(function(x) {
const bar = foo.filter(function(x) {
if (x) {
return true;
} else {
Expand All @@ -73,19 +73,19 @@ Examples of **correct** code for this rule:
```js
/*eslint array-callback-return: "error"*/

var indexMap = myArray.reduce(function(memo, item, index) {
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
return memo;
}, {});

var foo = Array.from(nodes, function(node) {
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
return false;
});

var bar = foo.map(node => node.getAttribute("id"));
const bar = foo.map(node => node.getAttribute("id"));
```

:::
Expand All @@ -98,7 +98,7 @@ This rule accepts a configuration object with three options:
* `"checkForEach": false` (default) When set to `true`, rule will also report `forEach` callbacks that return a value.
* `"allowVoid": false` (default) When set to `true`, allows `void` in `forEach` callbacks, so rule will not report the return value with a `void` operator.

**Note:** `{ "allowVoid": true }` works only if `checkForEach` option is set to `true`.
**Note:** `{ "allowVoid": true }` works only if `checkForEach` option is set to `true`.

### allowImplicit

Expand All @@ -108,7 +108,7 @@ Examples of **correct** code for the `{ "allowImplicit": true }` option:

```js
/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
var undefAllTheThings = myArray.map(function(item) {
const undefAllTheThings = myArray.map(function(item) {
return;
});
```
Expand Down
12 changes: 6 additions & 6 deletions docs/src/rules/for-direction.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ Examples of **incorrect** code for this rule:

```js
/*eslint for-direction: "error"*/
for (var i = 0; i < 10; i--) {
for (let i = 0; i < 10; i--) {
}

for (var i = 10; i >= 0; i++) {
for (let i = 10; i >= 0; i++) {
}

for (var i = 0; i > 10; i++) {
for (let i = 0; i > 10; i++) {
}

for (var i = 0; 10 > i; i--) {
for (let i = 0; 10 > i; i--) {
}

const n = -2;
Expand All @@ -40,10 +40,10 @@ Examples of **correct** code for this rule:

```js
/*eslint for-direction: "error"*/
for (var i = 0; i < 10; i++) {
for (let i = 0; i < 10; i++) {
}

for (var i = 0; 10 > i; i++) { // with counter "i" on the right
for (let i = 0; 10 > i; i++) { // with counter "i" on the right
}

for (let i = 10; i >= 0; i += this.step) { // direction unknown
Expand Down
8 changes: 4 additions & 4 deletions docs/src/rules/getter-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ further_reading:
The get syntax binds an object property to a function that will be called when that property is looked up. It was first introduced in ECMAScript 5:

```js
var p = {
const p = {
get name(){
return "nicholas";
}
Expand All @@ -38,7 +38,7 @@ Examples of **incorrect** code for this rule:
```js
/*eslint getter-return: "error"*/

p = {
const p = {
get name(){
// no returns.
}
Expand Down Expand Up @@ -66,7 +66,7 @@ Examples of **correct** code for this rule:
```js
/*eslint getter-return: "error"*/

p = {
const p = {
get name(){
return "nicholas";
}
Expand Down Expand Up @@ -99,7 +99,7 @@ Examples of **correct** code for the `{ "allowImplicit": true }` option:

```js
/*eslint getter-return: ["error", { allowImplicit: true }]*/
p = {
const p = {
get name(){
return; // return undefined implicitly.
}
Expand Down
28 changes: 14 additions & 14 deletions docs/src/rules/no-cond-assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ Examples of **incorrect** code for this rule with the default `"except-parens"`
/*eslint no-cond-assign: "error"*/

// Unintentional assignment
var x;
let x;
if (x = 0) {
var b = 1;
const b = 1;
}

// Practical example that is similar to an error
var setHeight = function (someNode) {
const setHeight = function (someNode) {
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
Expand All @@ -62,20 +62,20 @@ Examples of **correct** code for this rule with the default `"except-parens"` op
/*eslint no-cond-assign: "error"*/

// Assignment replaced by comparison
var x;
let x;
if (x === 0) {
var b = 1;
const b = 1;
}

// Practical example that wraps the assignment in parentheses
var setHeight = function (someNode) {
const setHeight = function (someNode) {
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}

// Practical example that wraps the assignment and tests for 'null'
var setHeight = function (someNode) {
const set_height = function (someNode) {
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
Expand All @@ -94,27 +94,27 @@ Examples of **incorrect** code for this rule with the `"always"` option:
/*eslint no-cond-assign: ["error", "always"]*/

// Unintentional assignment
var x;
let x;
if (x = 0) {
var b = 1;
const b = 1;
}

// Practical example that is similar to an error
var setHeight = function (someNode) {
const setHeight = function (someNode) {
do {
someNode.height = "100px";
} while (someNode = someNode.parentNode);
}

// Practical example that wraps the assignment in parentheses
var setHeight = function (someNode) {
const set_height = function (someNode) {
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode));
}

// Practical example that wraps the assignment and tests for 'null'
var setHeight = function (someNode) {
const heightSetter = function (someNode) {
do {
someNode.height = "100px";
} while ((someNode = someNode.parentNode) !== null);
Expand All @@ -131,9 +131,9 @@ Examples of **correct** code for this rule with the `"always"` option:
/*eslint no-cond-assign: ["error", "always"]*/

// Assignment replaced by comparison
var x;
let x;
if (x === 0) {
var b = 1;
const b = 1;
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/no-constant-condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ do {
doSomethingForever();
} while (x = -1);

var result = 0 ? a : b;
const result = 0 ? a : b;

if(input === "hello" || "bye"){
output(input);
Expand Down Expand Up @@ -105,7 +105,7 @@ do {
doSomething();
} while (x);

var result = x !== 0 ? a : b;
const result = x !== 0 ? a : b;

if(input === "hello" || input === "bye"){
output(input);
Expand Down
30 changes: 15 additions & 15 deletions docs/src/rules/no-control-regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Examples of **incorrect** code for this rule:
```js
/*eslint no-control-regex: "error"*/

var pattern1 = /\x00/;
var pattern2 = /\x0C/;
var pattern3 = /\x1F/;
var pattern4 = /\u000C/;
var pattern5 = /\u{C}/u;
var pattern6 = new RegExp("\x0C"); // raw U+000C character in the pattern
var pattern7 = new RegExp("\\x0C"); // \x0C pattern
const pattern1 = /\x00/;
const pattern2 = /\x0C/;
const pattern3 = /\x1F/;
const pattern4 = /\u000C/;
const pattern5 = /\u{C}/u;
const pattern6 = new RegExp("\x0C"); // raw U+000C character in the pattern
const pattern7 = new RegExp("\\x0C"); // \x0C pattern
```

:::
Expand All @@ -48,14 +48,14 @@ Examples of **correct** code for this rule:
```js
/*eslint no-control-regex: "error"*/

var pattern1 = /\x20/;
var pattern2 = /\u0020/;
var pattern3 = /\u{20}/u;
var pattern4 = /\t/;
var pattern5 = /\n/;
var pattern6 = new RegExp("\x20");
var pattern7 = new RegExp("\\t");
var pattern8 = new RegExp("\\n");
const pattern1 = /\x20/;
const pattern2 = /\u0020/;
const pattern3 = /\u{20}/u;
const pattern4 = /\t/;
const pattern5 = /\n/;
const pattern6 = new RegExp("\x20");
const pattern7 = new RegExp("\\t");
const pattern8 = new RegExp("\\n");
```

:::
Expand Down
4 changes: 2 additions & 2 deletions docs/src/rules/no-dupe-args.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function foo(a, b, a) {
console.log("value of the second a:", a);
}

var bar = function (a, b, a) {
const bar = function (a, b, a) {
console.log("value of the second a:", a);
};
```
Expand All @@ -43,7 +43,7 @@ function foo(a, b, c) {
console.log(a, b, c);
}

var bar = function (a, b, c) {
const bar = function (a, b, c) {
console.log(a, b, c);
};
```
Expand Down

0 comments on commit 89c8fc5

Please sign in to comment.