Skip to content

Commit 6242c1f

Browse files
docs: replace var with const in rule examples
1 parent 06b596d commit 6242c1f

File tree

5 files changed

+75
-75
lines changed

5 files changed

+75
-75
lines changed

docs/src/rules/no-implicit-coercion.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ Some of them might be hard to read and understand.
1111
Such as:
1212

1313
```js
14-
var b = !!foo;
15-
var b = ~foo.indexOf(".");
16-
var n = +foo;
17-
var n = -(-foo);
18-
var n = foo - 0;
19-
var n = 1 * foo;
20-
var s = "" + foo;
14+
const b = !!foo;
15+
const b1 = ~foo.indexOf(".");
16+
const n = +foo;
17+
const n1 = -(-foo);
18+
const n2 = foo - 0;
19+
const n3 = 1 * foo;
20+
const s = "" + foo;
2121
foo += ``;
2222
```
2323

2424
Those can be replaced with the following code:
2525

2626
```js
27-
var b = Boolean(foo);
28-
var b = foo.indexOf(".") !== -1;
29-
var n = Number(foo);
30-
var n = Number(foo);
31-
var n = Number(foo);
32-
var n = Number(foo);
33-
var s = String(foo);
27+
const b = Boolean(foo);
28+
const b1 = foo.indexOf(".") !== -1;
29+
const n = Number(foo);
30+
const n1 = Number(foo);
31+
const n2 = Number(foo);
32+
const n3 = Number(foo);
33+
const s = String(foo);
3434
foo = String(foo);
3535
```
3636

@@ -59,8 +59,8 @@ Examples of **incorrect** code for the default `{ "boolean": true }` option:
5959
```js
6060
/*eslint no-implicit-coercion: "error"*/
6161

62-
var b = !!foo;
63-
var b = ~foo.indexOf(".");
62+
const b = !!foo;
63+
const b1 = ~foo.indexOf(".");
6464
// bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.
6565
```
6666

@@ -73,10 +73,10 @@ Examples of **correct** code for the default `{ "boolean": true }` option:
7373
```js
7474
/*eslint no-implicit-coercion: "error"*/
7575

76-
var b = Boolean(foo);
77-
var b = foo.indexOf(".") !== -1;
76+
const b = Boolean(foo);
77+
const b1 = foo.indexOf(".") !== -1;
7878

79-
var n = ~foo; // This is a just bitwise not.
79+
const n = ~foo; // This is a just bitwise not.
8080
```
8181

8282
:::
@@ -90,10 +90,10 @@ Examples of **incorrect** code for the default `{ "number": true }` option:
9090
```js
9191
/*eslint no-implicit-coercion: "error"*/
9292

93-
var n = +foo;
94-
var n = -(-foo);
95-
var n = foo - 0;
96-
var n = 1 * foo;
93+
const n = +foo;
94+
const n1 = -(-foo);
95+
const n2 = foo - 0;
96+
const n3 = 1 * foo;
9797
```
9898

9999
:::
@@ -105,11 +105,11 @@ Examples of **correct** code for the default `{ "number": true }` option:
105105
```js
106106
/*eslint no-implicit-coercion: "error"*/
107107

108-
var n = Number(foo);
109-
var n = parseFloat(foo);
110-
var n = parseInt(foo, 10);
108+
const n = Number(foo);
109+
const n1 = parseFloat(foo);
110+
const n2 = parseInt(foo, 10);
111111

112-
var n = foo * 1/4; // `* 1` is allowed when followed by the `/` operator
112+
const n3 = foo * 1/4; // `* 1` is allowed when followed by the `/` operator
113113
```
114114

115115
:::
@@ -123,8 +123,8 @@ Examples of **incorrect** code for the default `{ "string": true }` option:
123123
```js
124124
/*eslint no-implicit-coercion: "error"*/
125125

126-
var s = "" + foo;
127-
var s = `` + foo;
126+
const s = "" + foo;
127+
const s1 = `` + foo;
128128
foo += "";
129129
foo += ``;
130130
```
@@ -138,7 +138,7 @@ Examples of **correct** code for the default `{ "string": true }` option:
138138
```js
139139
/*eslint no-implicit-coercion: "error"*/
140140

141-
var s = String(foo);
141+
const s = String(foo);
142142
foo = String(foo);
143143
```
144144

@@ -155,7 +155,7 @@ Examples of **incorrect** code for the `{ "disallowTemplateShorthand": true }` o
155155
```js
156156
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
157157

158-
var s = `${foo}`;
158+
const s = `${foo}`;
159159
```
160160

161161
:::
@@ -167,15 +167,15 @@ Examples of **correct** code for the `{ "disallowTemplateShorthand": true }` opt
167167
```js
168168
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
169169

170-
var s = String(foo);
170+
const s = String(foo);
171171

172-
var s = `a${foo}`;
172+
const s1 = `a${foo}`;
173173

174-
var s = `${foo}b`;
174+
const s2 = `${foo}b`;
175175

176-
var s = `${foo}${bar}`;
176+
const s3 = `${foo}${bar}`;
177177

178-
var s = tag`${foo}`;
178+
const s4 = tag`${foo}`;
179179
```
180180

181181
:::
@@ -187,7 +187,7 @@ Examples of **correct** code for the default `{ "disallowTemplateShorthand": fal
187187
```js
188188
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/
189189

190-
var s = `${foo}`;
190+
const s = `${foo}`;
191191
```
192192

193193
:::
@@ -203,8 +203,8 @@ Examples of **correct** code for the sample `{ "allow": ["!!", "~"] }` option:
203203
```js
204204
/*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/
205205

206-
var b = !!foo;
207-
var b = ~foo.indexOf(".");
206+
const b = !!foo;
207+
const b1 = ~foo.indexOf(".");
208208
```
209209

210210
:::

docs/src/rules/no-inline-comments.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ Examples of **incorrect** code for this rule:
1818
```js
1919
/*eslint no-inline-comments: "error"*/
2020

21-
var a = 1; // declaring a to 1
21+
const a = 1; // declaring a to 1
2222

2323
function getRandomNumber(){
2424
return 4; // chosen by fair dice roll.
2525
// guaranteed to be random.
2626
}
2727

28-
/* A block comment before code */ var b = 2;
28+
/* A block comment before code */ const b = 2;
2929

30-
var c = 3; /* A block comment after code */
30+
const c = 3; /* A block comment after code */
3131
```
3232

3333
:::
@@ -40,9 +40,9 @@ Examples of **correct** code for this rule:
4040
/*eslint no-inline-comments: "error"*/
4141

4242
// This is a comment above a line of code
43-
var foo = 5;
43+
const foo = 5;
4444

45-
var bar = 5;
45+
const bar = 5;
4646
//This is a comment below a line of code
4747
```
4848

@@ -59,9 +59,9 @@ Examples of **incorrect** code for this rule:
5959
```jsx
6060
/*eslint no-inline-comments: "error"*/
6161

62-
var foo = <div>{ /* On the same line with other code */ }<h1>Some heading</h1></div>;
62+
const foo = <div>{ /* On the same line with other code */ }<h1>Some heading</h1></div>;
6363

64-
var bar = (
64+
const bar = (
6565
<div>
6666
{ // These braces are not just for the comment, so it can't be on the same line
6767
baz
@@ -79,14 +79,14 @@ Examples of **correct** code for this rule:
7979
```jsx
8080
/*eslint no-inline-comments: "error"*/
8181

82-
var foo = (
82+
const foo = (
8383
<div>
8484
{/* These braces are just for this comment and there is nothing else on this line */}
8585
<h1>Some heading</h1>
8686
</div>
8787
)
8888

89-
var bar = (
89+
const bar = (
9090
<div>
9191
{
9292
// There is nothing else on this line
@@ -95,7 +95,7 @@ var bar = (
9595
</div>
9696
);
9797

98-
var quux = (
98+
const quux = (
9999
<div>
100100
{/*
101101
Multiline
@@ -133,7 +133,7 @@ Examples of **incorrect** code for the `ignorePattern` option:
133133
```js
134134
/*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */
135135

136-
var foo = 4; // other thing
136+
const foo = 4; // other thing
137137
```
138138

139139
:::

docs/src/rules/no-invalid-this.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function foo() {
6666
baz(() => this);
6767
}
6868

69-
var foo = function() {
69+
const bar = function() {
7070
this.a = 0;
7171
baz(() => this);
7272
};
@@ -76,7 +76,7 @@ foo(function() {
7676
baz(() => this);
7777
});
7878

79-
var obj = {
79+
const obj = {
8080
aaa: function() {
8181
return function foo() {
8282
// There is in a method `aaa`, but `foo` is not a method.
@@ -120,28 +120,28 @@ class Bar {
120120
}
121121
}
122122

123-
var obj = {
123+
const obj = {
124124
foo: function foo() {
125125
// OK, this is in a method (this function is on object literal).
126126
this.a = 0;
127127
}
128128
};
129129

130-
var obj = {
130+
const obj1 = {
131131
foo() {
132132
// OK, this is in a method (this function is on object literal).
133133
this.a = 0;
134134
}
135135
};
136136

137-
var obj = {
137+
const obj2 = {
138138
get foo() {
139139
// OK, this is in a method (this function is on object literal).
140140
return this.a;
141141
}
142142
};
143143

144-
var obj = Object.create(null, {
144+
const obj3 = Object.create(null, {
145145
foo: {value: function foo() {
146146
// OK, this is in a method (this function is on object literal).
147147
this.a = 0;
@@ -207,7 +207,7 @@ class Baz {
207207
}
208208
}
209209

210-
var foo = (function foo() {
210+
const bar = (function foo() {
211211
// OK, the `bind` method of this function is called directly.
212212
this.a = 0;
213213
}).bind(obj);
@@ -252,11 +252,11 @@ function Foo() {
252252
this.a = 0;
253253
}
254254

255-
var bar = function Foo() {
255+
const bar = function Foo() {
256256
this.a = 0;
257257
}
258258

259-
var Bar = function() {
259+
const Bar = function() {
260260
this.a = 0;
261261
};
262262

docs/src/rules/no-multi-str.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ rule_type: suggestion
77
It's possible to create multiline strings in JavaScript by using a slash before a newline, such as:
88

99
```js
10-
var x = "Line 1 \
10+
const x = "Line 1 \
1111
Line 2";
1212
```
1313

@@ -24,7 +24,7 @@ Examples of **incorrect** code for this rule:
2424
```js
2525
/*eslint no-multi-str: "error"*/
2626

27-
var x = "some very \
27+
const x = "some very \
2828
long text";
2929
```
3030

@@ -37,9 +37,9 @@ Examples of **correct** code for this rule:
3737
```js
3838
/*eslint no-multi-str: "error"*/
3939

40-
var x = "some very long text";
40+
const x = "some very long text";
4141

42-
var x = "some very " +
42+
const y = "some very " +
4343
"long text";
4444
```
4545

docs/src/rules/no-new-func.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ rule_type: suggestion
77
It's possible to create functions in JavaScript from strings at runtime using the `Function` constructor, such as:
88

99
```js
10-
var x = new Function("a", "b", "return a + b");
11-
var x = Function("a", "b", "return a + b");
12-
var x = Function.call(null, "a", "b", "return a + b");
13-
var x = Function.apply(null, ["a", "b", "return a + b"]);
14-
var x = Function.bind(null, "a", "b", "return a + b")();
10+
const a = new Function("a", "b", "return a + b");
11+
const b = Function("a", "b", "return a + b");
12+
const c = Function.call(null, "a", "b", "return a + b");
13+
const d = Function.apply(null, ["a", "b", "return a + b"]);
14+
const x = Function.bind(null, "a", "b", "return a + b")();
1515
```
1616

1717
This is considered by many to be a bad practice due to the difficulty in debugging and reading these types of functions. In addition, Content-Security-Policy (CSP) directives may disallow the use of `eval()` and similar methods for creating code from strings.
@@ -27,12 +27,12 @@ Examples of **incorrect** code for this rule:
2727
```js
2828
/*eslint no-new-func: "error"*/
2929

30-
var x = new Function("a", "b", "return a + b");
31-
var x = Function("a", "b", "return a + b");
32-
var x = Function.call(null, "a", "b", "return a + b");
33-
var x = Function.apply(null, ["a", "b", "return a + b"]);
34-
var x = Function.bind(null, "a", "b", "return a + b")();
35-
var f = Function.bind(null, "a", "b", "return a + b"); // assuming that the result of Function.bind(...) will be eventually called.
30+
const a = new Function("a", "b", "return a + b");
31+
const b = Function("a", "b", "return a + b");
32+
const c = Function.call(null, "a", "b", "return a + b");
33+
const d = Function.apply(null, ["a", "b", "return a + b"]);
34+
const x = Function.bind(null, "a", "b", "return a + b")();
35+
const y = Function.bind(null, "a", "b", "return a + b"); // assuming that the result of Function.bind(...) will be eventually called.
3636
```
3737

3838
:::
@@ -44,7 +44,7 @@ Examples of **correct** code for this rule:
4444
```js
4545
/*eslint no-new-func: "error"*/
4646

47-
var x = function (a, b) {
47+
const x = function (a, b) {
4848
return a + b;
4949
};
5050
```

0 commit comments

Comments
 (0)