Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 36ef8bb

Browse filesBrowse files
amareshsmmdjermanovicTanujkanti4441
authored
docs: rewrite examples with var using let and const (#19298)
* docs: rewrite examples with var using let and const * revert comma-spacing.md changes * consistent this rename to let/const * id-length rename eg with let/const * id-match rename eg with let/const * rewrite missed eg * Update docs/src/rules/consistent-this.md Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update docs/src/rules/consistent-this.md Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update docs/src/rules/no-multi-assign.md Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com> * Update id-match.md --------- Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> Co-authored-by: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com>
1 parent 1610c9e commit 36ef8bb
Copy full SHA for 36ef8bb

File tree

Expand file treeCollapse file tree

7 files changed

+95
-85
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+95
-85
lines changed

‎docs/src/rules/consistent-this.md

Copy file name to clipboardExpand all lines: docs/src/rules/consistent-this.md
+21-10Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ rule_type: suggestion
77
It is often necessary to capture the current execution context in order to make it available subsequently. A prominent example of this are jQuery callbacks:
88

99
```js
10-
var that = this;
10+
const that = this;
1111
jQuery('li').click(function (event) {
1212
// here, "this" is the HTMLElement where the click event occurred
1313
that.setFoo(42);
@@ -36,9 +36,9 @@ Examples of **incorrect** code for this rule with the default `"that"` option:
3636
```js
3737
/*eslint consistent-this: ["error", "that"]*/
3838

39-
var that = 42;
39+
let that = 42;
4040

41-
var self = this;
41+
let self = this;
4242

4343
that = 42;
4444

@@ -54,11 +54,11 @@ Examples of **correct** code for this rule with the default `"that"` option:
5454
```js
5555
/*eslint consistent-this: ["error", "that"]*/
5656

57-
var that = this;
57+
let that = this;
5858

59-
var self = 42;
59+
const self = 42;
6060

61-
var self;
61+
let foo;
6262

6363
that = this;
6464

@@ -74,7 +74,7 @@ Examples of **incorrect** code for this rule with the default `"that"` option, i
7474
```js
7575
/*eslint consistent-this: ["error", "that"]*/
7676

77-
var that;
77+
let that;
7878
function f() {
7979
that = this;
8080
}
@@ -84,16 +84,27 @@ function f() {
8484

8585
Examples of **correct** code for this rule with the default `"that"` option, if the variable is not initialized:
8686

87+
Declaring a variable `that` and assigning `this` to it.
88+
8789
::: correct
8890

8991
```js
9092
/*eslint consistent-this: ["error", "that"]*/
9193

92-
var that;
94+
let that;
9395
that = this;
96+
```
97+
98+
:::
99+
100+
Declaring two variables, `foo` and `that`, with `foo` initialized, and then assigning `this` to `that`.
101+
102+
::: correct
103+
104+
```js
105+
/*eslint consistent-this: ["error", "that"]*/
94106

95-
var foo, that;
96-
foo = 42;
107+
let foo = 42, that;
97108
that = this;
98109
```
99110

‎docs/src/rules/id-length.md

Copy file name to clipboardExpand all lines: docs/src/rules/id-length.md
+45-46Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ related_rules:
1212
Very short identifier names like `e`, `x`, `_t` or very long ones like `hashGeneratorResultOutputContainerObject` can make code harder to read and potentially less maintainable. To prevent this, one may enforce a minimum and/or maximum identifier length.
1313

1414
```js
15-
var x = 5; // too short; difficult to understand its purpose without context
15+
const x = 5; // too short; difficult to understand its purpose without context
1616
```
1717

1818
## Rule Details
@@ -30,15 +30,15 @@ Examples of **incorrect** code for this rule with the default options:
3030
```js
3131
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
3232

33-
var x = 5;
33+
const x = 5;
3434
obj.e = document.body;
35-
var foo = function (e) { };
35+
const foo = function (e) { };
3636
try {
3737
dangerousStuff();
3838
} catch (e) {
3939
// ignore as many do
4040
}
41-
var myObj = { a: 1 };
41+
const myObj = { a: 1 };
4242
(a) => { a * a };
4343
class y { }
4444
class Foo { x() {} }
@@ -47,11 +47,11 @@ class Baz { x = 1 }
4747
class Qux { #x = 1 }
4848
function bar(...x) { }
4949
function baz([x]) { }
50-
var [x] = arr;
51-
var { prop: [x]} = {};
50+
const [z] = arr;
51+
const { prop: [i]} = {};
5252
function qux({x}) { }
53-
var { x } = {};
54-
var { prop: a} = {};
53+
const { j } = {};
54+
const { prop: a} = {};
5555
({ prop: obj.x } = {});
5656
```
5757

@@ -64,17 +64,17 @@ Examples of **correct** code for this rule with the default options:
6464
```js
6565
/*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 })
6666

67-
var num = 5;
67+
const num = 5;
6868
function _f() { return 42; }
6969
function _func() { return 42; }
7070
obj.el = document.body;
71-
var foo = function (evt) { /* do stuff */ };
71+
const foo = function (evt) { /* do stuff */ };
7272
try {
7373
dangerousStuff();
7474
} catch (error) {
7575
// ignore as many do
7676
}
77-
var myObj = { apple: 1 };
77+
const myObj = { apple: 1 };
7878
(num) => { num * num };
7979
function bar(num = 0) { }
8080
class MyClass { }
@@ -84,15 +84,14 @@ class Baz { field = 1 }
8484
class Qux { #field = 1 }
8585
function baz(...args) { }
8686
function qux([longName]) { }
87-
var { prop } = {};
88-
var { prop: [longName] } = {};
89-
var [longName] = arr;
87+
const { prop } = {};
88+
const { prop: [name] } = {};
89+
const [longName] = arr;
9090
function foobar({ prop }) { }
9191
function foobaz({ a: prop }) { }
92-
var { prop } = {};
93-
var { a: prop } = {};
92+
const { a: property } = {};
9493
({ prop: obj.longName } = {});
95-
var data = { "x": 1 }; // excused because of quotes
94+
const data = { "x": 1 }; // excused because of quotes
9695
data["y"] = 3; // excused because of calculated property access
9796
```
9897

@@ -116,23 +115,23 @@ Examples of **incorrect** code for this rule with the `{ "min": 4 }` option:
116115
```js
117116
/*eslint id-length: ["error", { "min": 4 }]*/
118117

119-
var val = 5;
118+
const val = 5;
120119
obj.e = document.body;
121120
function foo (e) { };
122121
try {
123122
dangerousStuff();
124123
} catch (e) {
125124
// ignore as many do
126125
}
127-
var myObj = { a: 1 };
126+
const myObj = { a: 1 };
128127
(val) => { val * val };
129128
class y { }
130129
class Foo { x() {} }
131130
function bar(...x) { }
132-
var { x } = {};
133-
var { prop: a} = {};
134-
var [x] = arr;
135-
var { prop: [x]} = {};
131+
const { x } = {};
132+
const { prop: a} = {};
133+
const [i] = arr;
134+
const { prop: [num]} = {};
136135
({ prop: obj.x } = {});
137136
```
138137

@@ -145,27 +144,27 @@ Examples of **correct** code for this rule with the `{ "min": 4 }` option:
145144
```js
146145
/*eslint id-length: ["error", { "min": 4 }]*/
147146

148-
var value = 5;
147+
const value = 5;
149148
function func() { return 42; }
150149
object.element = document.body;
151-
var foobar = function (event) { /* do stuff */ };
150+
const foobar = function (event) { /* do stuff */ };
152151
try {
153152
dangerousStuff();
154153
} catch (error) {
155154
// ignore as many do
156155
}
157-
var myObj = { apple: 1 };
156+
const myObj = { apple: 1 };
158157
(value) => { value * value };
159158
function foobaz(value = 0) { }
160159
class MyClass { }
161160
class Foobar { method() {} }
162161
function barbaz(...args) { }
163-
var { prop } = {};
164-
var [longName] = foo;
165-
var { a: [prop] } = {};
166-
var { a: longName } = {};
162+
const { prop } = {};
163+
const [longName] = foo;
164+
const { a: [name] } = {};
165+
const { a: record } = {};
167166
({ prop: object.name } = {});
168-
var data = { "x": 1 }; // excused because of quotes
167+
const data = { "x": 1 }; // excused because of quotes
169168
data["y"] = 3; // excused because of calculated property access
170169
```
171170

@@ -180,17 +179,17 @@ Examples of **incorrect** code for this rule with the `{ "max": 10 }` option:
180179
```js
181180
/*eslint id-length: ["error", { "max": 10 }]*/
182181

183-
var reallyLongVarName = 5;
182+
const reallyLongVarName = 5;
184183
function reallyLongFuncName() { return 42; }
185184
obj.reallyLongPropName = document.body;
186-
var foo = function (reallyLongArgName) { /* do stuff */ };
185+
const foo = function (reallyLongArgName) { /* do stuff */ };
187186
try {
188187
dangerousStuff();
189188
} catch (reallyLongErrorName) {
190189
// ignore as many do
191190
}
192191
(reallyLongArgName) => { return !reallyLongArgName; };
193-
var [reallyLongFirstElementName] = arr;
192+
const [reallyLongFirstElementName] = arr;
194193
```
195194

196195
:::
@@ -202,17 +201,17 @@ Examples of **correct** code for this rule with the `{ "max": 10 }` option:
202201
```js
203202
/*eslint id-length: ["error", { "max": 10 }]*/
204203

205-
var varName = 5;
204+
const varName = 5;
206205
function funcName() { return 42; }
207206
obj.propName = document.body;
208-
var foo = function (arg) { /* do stuff */ };
207+
const foo = function (arg) { /* do stuff */ };
209208
try {
210209
dangerousStuff();
211210
} catch (error) {
212211
// ignore as many do
213212
}
214213
(arg) => { return !arg; };
215-
var [first] = arr;
214+
const [first] = arr;
216215
```
217216

218217
:::
@@ -226,7 +225,7 @@ Examples of **correct** code for this rule with the `{ "properties": "never" }`
226225
```js
227226
/*eslint id-length: ["error", { "properties": "never" }]*/
228227

229-
var myObj = { a: 1 };
228+
const myObj = { a: 1 };
230229
({ a: obj.x.y.z } = {});
231230
({ prop: obj.i } = {});
232231
```
@@ -240,19 +239,19 @@ Examples of additional **correct** code for this rule with the `{ "exceptions":
240239
::: correct
241240

242241
```js
243-
/*eslint id-length: ["error", { "exceptions": ["x", "y", "z", "ζ"] }]*/
242+
/*eslint id-length: ["error", { "exceptions": ["x", "y", "z", "ζ", "i"] }]*/
244243

245-
var x = 5;
244+
const x = 5;
246245
function y() { return 42; }
247246
obj.x = document.body;
248-
var foo = function (x) { /* do stuff */ };
247+
const foo = function (x) { /* do stuff */ };
249248
try {
250249
dangerousStuff();
251250
} catch (x) {
252251
// ignore as many do
253252
}
254253
(x) => { return x * x; };
255-
var [x] = arr;
254+
const [i] = arr;
256255
const { z } = foo;
257256
const { a: ζ } = foo;
258257
```
@@ -266,19 +265,19 @@ Examples of additional **correct** code for this rule with the `{ "exceptionPatt
266265
::: correct
267266

268267
```js
269-
/*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/
268+
/*eslint id-length: ["error", { "exceptionPatterns": ["E|S|X", "[x-z]"] }]*/
270269

271-
var E = 5;
270+
const E = 5;
272271
function S() { return 42; }
273272
obj.x = document.body;
274-
var foo = function (x) { /* do stuff */ };
273+
const foo = function (x) { /* do stuff */ };
275274
try {
276275
dangerousStuff();
277276
} catch (x) {
278277
// ignore as many do
279278
}
280279
(y) => {return y * y};
281-
var [E] = arr;
280+
const [X] = arr;
282281
const { y } = foo;
283282
const { a: z } = foo;
284283
```

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.