From 065222ca795bf2b3dc6b7629e8b10d3b4995b948 Mon Sep 17 00:00:00 2001 From: Tanuj Kanti Date: Fri, 31 Jan 2025 16:44:37 +0530 Subject: [PATCH 1/3] docs: replace var with const and let --- docs/src/rules/prefer-destructuring.md | 28 ++++---- docs/src/rules/radix.md | 32 ++++----- docs/src/rules/sort-keys.md | 98 +++++++++++++------------- docs/src/rules/sort-vars.md | 22 +++--- docs/src/rules/symbol-description.md | 16 ++--- 5 files changed, 98 insertions(+), 98 deletions(-) diff --git a/docs/src/rules/prefer-destructuring.md b/docs/src/rules/prefer-destructuring.md index 256bd9b294b5..7f320683bda8 100644 --- a/docs/src/rules/prefer-destructuring.md +++ b/docs/src/rules/prefer-destructuring.md @@ -47,12 +47,12 @@ Examples of **incorrect** code for this rule: /* eslint prefer-destructuring: "error" */ // With `array` enabled -var foo = array[0]; +const foo = array[0]; bar.baz = array[0]; // With `object` enabled -var foo = object.foo; -var foo = object['foo']; +const obj1 = object.foo; +const obj2 = object['foo']; ``` ::: @@ -65,15 +65,15 @@ Examples of **correct** code for this rule: /* eslint prefer-destructuring: "error" */ // With `array` enabled -var [ foo ] = array; -var foo = array[someIndex]; +const [ foo ] = array; +const arr = array[someIndex]; [bar.baz] = array; // With `object` enabled -var { foo } = object; +const { baz } = object; -var foo = object.bar; +const obj = object.bar; let bar; ({ bar } = object); @@ -108,7 +108,7 @@ Examples of **correct** code when object destructuring in `VariableDeclarator` i ```javascript /* eslint prefer-destructuring: ["error", {VariableDeclarator: {object: true}}] */ -var {bar: foo} = object; +const {bar: foo} = object; ``` ::: @@ -148,7 +148,7 @@ Examples of **incorrect** code when `enforceForRenamedProperties` is enabled: ```javascript /* eslint "prefer-destructuring": ["error", { "object": true }, { "enforceForRenamedProperties": true }] */ -var foo = object.bar; +const foo = object.bar; ``` ::: @@ -159,7 +159,7 @@ Examples of **correct** code when `enforceForRenamedProperties` is enabled: ```javascript /* eslint "prefer-destructuring": ["error", { "object": true }, { "enforceForRenamedProperties": true }] */ -var { bar: foo } = object; +const { bar: foo } = object; ``` ::: @@ -194,7 +194,7 @@ If you want to be able to access array indices or object properties directly, yo Additionally, if you intend to access large array indices directly, like: ```javascript -var foo = array[100]; +const foo = array[100]; ``` Then the `array` part of this rule is not recommended, as destructuring does not match this use case very well. @@ -202,7 +202,7 @@ Then the `array` part of this rule is not recommended, as destructuring does not Or for non-iterable 'array-like' objects: ```javascript -var $ = require('jquery'); -var foo = $('body')[0]; -var [bar] = $('body'); // fails with a TypeError +const $ = require('jquery'); +const foo = $('body')[0]; +const [bar] = $('body'); // fails with a TypeError ``` diff --git a/docs/src/rules/radix.md b/docs/src/rules/radix.md index 8c11f8736f2a..2321071e617c 100644 --- a/docs/src/rules/radix.md +++ b/docs/src/rules/radix.md @@ -12,13 +12,13 @@ When using the `parseInt()` function it is common to omit the second argument, t This confusion led to the suggestion that you always use the radix parameter to `parseInt()` to eliminate unintended consequences. So instead of doing this: ```js -var num = parseInt("071"); // 57 +const num = parseInt("071"); // 57 ``` Do this: ```js -var num = parseInt("071", 10); // 71 +const num = parseInt("071", 10); // 71 ``` ECMAScript 5 changed the behavior of `parseInt()` so that it no longer autodetects octal literals and instead treats them as decimal literals. However, the differences between hexadecimal and decimal interpretation of the first parameter causes many developers to continue using the radix parameter to ensure the string is interpreted in the intended way. @@ -45,15 +45,15 @@ Examples of **incorrect** code for the default `"always"` option: ```js /*eslint radix: "error"*/ -var num = parseInt("071"); +const num = parseInt("071"); -var num = parseInt(someValue); +const num1 = parseInt(someValue); -var num = parseInt("071", "abc"); +const num2 = parseInt("071", "abc"); -var num = parseInt("071", 37); +const num3 = parseInt("071", 37); -var num = parseInt(); +const num4 = parseInt(); ``` ::: @@ -65,11 +65,11 @@ Examples of **correct** code for the default `"always"` option: ```js /*eslint radix: "error"*/ -var num = parseInt("071", 10); +const num = parseInt("071", 10); -var num = parseInt("071", 8); +const num1 = parseInt("071", 8); -var num = parseFloat(someValue); +const num2 = parseFloat(someValue); ``` ::: @@ -83,11 +83,11 @@ Examples of **incorrect** code for the `"as-needed"` option: ```js /*eslint radix: ["error", "as-needed"]*/ -var num = parseInt("071", 10); +const num = parseInt("071", 10); -var num = parseInt("071", "abc"); +const num1 = parseInt("071", "abc"); -var num = parseInt(); +const num2 = parseInt(); ``` ::: @@ -99,11 +99,11 @@ Examples of **correct** code for the `"as-needed"` option: ```js /*eslint radix: ["error", "as-needed"]*/ -var num = parseInt("071"); +const num = parseInt("071"); -var num = parseInt("071", 8); +const num1 = parseInt("071", 8); -var num = parseFloat(someValue); +const num2 = parseFloat(someValue); ``` ::: diff --git a/docs/src/rules/sort-keys.md b/docs/src/rules/sort-keys.md index 1db0652c2247..89d01a0a8c9a 100644 --- a/docs/src/rules/sort-keys.md +++ b/docs/src/rules/sort-keys.md @@ -20,20 +20,20 @@ Examples of **incorrect** code for this rule: ```js /*eslint sort-keys: "error"*/ -let obj1 = {a: 1, c: 3, b: 2}; -let obj2 = {a: 1, "c": 3, b: 2}; +const obj1 = {a: 1, c: 3, b: 2}; +const obj2 = {a: 1, "c": 3, b: 2}; // Case-sensitive by default. -let obj3 = {a: 1, b: 2, C: 3}; +const obj3 = {a: 1, b: 2, C: 3}; // Non-natural order by default. -let obj4 = {1: a, 2: c, 10: b}; +const obj4 = {1: a, 2: c, 10: b}; // This rule checks computed properties which have a simple name as well. // Simple names are names which are expressed by an Identifier node or a Literal node. const S = Symbol("s") -let obj5 = {a: 1, ["c"]: 3, b: 2}; -let obj6 = {a: 1, [S]: 3, b: 2}; +const obj5 = {a: 1, ["c"]: 3, b: 2}; +const obj6 = {a: 1, [S]: 3, b: 2}; ``` ::: @@ -45,27 +45,27 @@ Examples of **correct** code for this rule: ```js /*eslint sort-keys: "error"*/ -let obj1 = {a: 1, b: 2, c: 3}; -let obj2 = {a: 1, "b": 2, c: 3}; +const obj1 = {a: 1, b: 2, c: 3}; +const obj2 = {a: 1, "b": 2, c: 3}; // Case-sensitive by default. -let obj3 = {C: 3, a: 1, b: 2}; +const obj3 = {C: 3, a: 1, b: 2}; // Non-natural order by default. -let obj4 = {1: a, 10: b, 2: c}; +const obj4 = {1: a, 10: b, 2: c}; // This rule checks computed properties which have a simple name as well. -let obj5 = {a: 1, ["b"]: 2, c: 3}; -let obj6 = {a: 1, [b]: 2, c: 3}; +const obj5 = {a: 1, ["b"]: 2, c: 3}; +const obj6 = {a: 1, [b]: 2, c: 3}; // This rule ignores computed properties which have a non-simple name. -let obj7 = {a: 1, [c + d]: 3, b: 2}; -let obj8 = {a: 1, ["c" + "d"]: 3, b: 2}; -let obj9 = {a: 1, [`${c}`]: 3, b: 2}; -let obj10 = {a: 1, [tag`c`]: 3, b: 2}; +const obj7 = {a: 1, [c + d]: 3, b: 2}; +const obj8 = {a: 1, ["c" + "d"]: 3, b: 2}; +const obj9 = {a: 1, [`${c}`]: 3, b: 2}; +const obj10 = {a: 1, [tag`c`]: 3, b: 2}; // This rule does not report unsorted properties that are separated by a spread property. -let obj11 = {b: 1, ...c, a: 2}; +const obj11 = {b: 1, ...c, a: 2}; ``` ::: @@ -116,14 +116,14 @@ Examples of **incorrect** code for the `"desc"` option: ```js /*eslint sort-keys: ["error", "desc"]*/ -let obj1 = {b: 2, c: 3, a: 1}; -let obj2 = {"b": 2, c: 3, a: 1}; +const obj1 = {b: 2, c: 3, a: 1}; +const obj2 = {"b": 2, c: 3, a: 1}; // Case-sensitive by default. -let obj3 = {C: 1, b: 3, a: 2}; +const obj3 = {C: 1, b: 3, a: 2}; // Non-natural order by default. -let obj4 = {10: b, 2: c, 1: a}; +const obj4 = {10: b, 2: c, 1: a}; ``` ::: @@ -135,14 +135,14 @@ Examples of **correct** code for the `"desc"` option: ```js /*eslint sort-keys: ["error", "desc"]*/ -let obj1 = {c: 3, b: 2, a: 1}; -let obj2 = {c: 3, "b": 2, a: 1}; +const obj1 = {c: 3, b: 2, a: 1}; +const obj2 = {c: 3, "b": 2, a: 1}; // Case-sensitive by default. -let obj3 = {b: 3, a: 2, C: 1}; +const obj3 = {b: 3, a: 2, C: 1}; // Non-natural order by default. -let obj4 = {2: c, 10: b, 1: a}; +const obj4 = {2: c, 10: b, 1: a}; ``` ::: @@ -156,8 +156,8 @@ Examples of **incorrect** code for the `{caseSensitive: false}` option: ```js /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/ -let obj1 = {a: 1, c: 3, C: 4, b: 2}; -let obj2 = {a: 1, C: 3, c: 4, b: 2}; +const obj1 = {a: 1, c: 3, C: 4, b: 2}; +const obj2 = {a: 1, C: 3, c: 4, b: 2}; ``` ::: @@ -169,8 +169,8 @@ Examples of **correct** code for the `{caseSensitive: false}` option: ```js /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/ -let obj1 = {a: 1, b: 2, c: 3, C: 4}; -let obj2 = {a: 1, b: 2, C: 3, c: 4}; +const obj1 = {a: 1, b: 2, c: 3, C: 4}; +const obj2 = {a: 1, b: 2, C: 3, c: 4}; ``` ::: @@ -184,7 +184,7 @@ Examples of **incorrect** code for the `{natural: true}` option: ```js /*eslint sort-keys: ["error", "asc", {natural: true}]*/ -let obj = {1: a, 10: c, 2: b}; +const obj = {1: a, 10: c, 2: b}; ``` ::: @@ -196,7 +196,7 @@ Examples of **correct** code for the `{natural: true}` option: ```js /*eslint sort-keys: ["error", "asc", {natural: true}]*/ -let obj = {1: a, 2: b, 10: c}; +const obj = {1: a, 2: b, 10: c}; ``` ::: @@ -211,7 +211,7 @@ Examples of **incorrect** code for the `{minKeys: 4}` option: /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/ // 4 keys -let obj1 = { +const obj1 = { b: 2, a: 1, // not sorted correctly (should be 1st key) c: 3, @@ -219,7 +219,7 @@ let obj1 = { }; // 5 keys -let obj2 = { +const obj2 = { 2: 'a', 1: 'b', // not sorted correctly (should be 1st key) 3: 'c', @@ -238,14 +238,14 @@ Examples of **correct** code for the `{minKeys: 4}` option: /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/ // 3 keys -let obj1 = { +const obj1 = { b: 2, a: 1, c: 3, }; // 2 keys -let obj2 = { +const obj2 = { 2: 'b', 1: 'a', }; @@ -262,7 +262,7 @@ Examples of **incorrect** code for the `{allowLineSeparatedGroups: true}` option ```js /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/ -let obj1 = { +const obj1 = { b: 1, c () { @@ -270,7 +270,7 @@ let obj1 = { a: 3 } -let obj2 = { +const obj2 = { b: 1, c: 2, @@ -280,7 +280,7 @@ let obj2 = { y: 3 } -let obj3 = { +const obj3 = { b: 1, c: 2, @@ -291,7 +291,7 @@ let obj3 = { y: 3, } -let obj4 = { +const obj4 = { b: 1 // comment before comma , a: 2 @@ -307,7 +307,7 @@ Examples of **correct** code for the `{allowLineSeparatedGroups: true}` option: ```js /*eslint sort-keys: ["error", "asc", {allowLineSeparatedGroups: true}]*/ -let obj1 = { +const obj1 = { e: 1, f: 2, g: 3, @@ -317,7 +317,7 @@ let obj1 = { c: 6 } -let obj2 = { +const obj2 = { b: 1, // comment @@ -325,7 +325,7 @@ let obj2 = { c: 5, } -let obj3 = { +const obj3 = { c: 1, d: 2, @@ -335,7 +335,7 @@ let obj3 = { e: 3, } -let obj4 = { +const obj4 = { c: 1, d: 2, // comment @@ -347,14 +347,14 @@ let obj4 = { e: 4 } -let obj5 = { +const obj5 = { b, [foo + bar]: 1, a } -let obj6 = { +const obj6 = { b: 1 // comment before comma @@ -362,7 +362,7 @@ let obj6 = { a: 2 }; -var obj7 = { +const obj7 = { b: 1, a: 2, @@ -382,18 +382,18 @@ Examples of **correct** code for the `{ignoreComputedKeys: true}` option: ```js /*eslint sort-keys: ["error", "asc", {ignoreComputedKeys: true}]*/ -let obj1 = { +const obj1 = { [b]: 1, a: 2 } -let obj2 = { +const obj2 = { c: 1, [b]: 2, a: 3 } -let obj3 = { +const obj3 = { c: 1, ["b"]: 2, a: 3 diff --git a/docs/src/rules/sort-vars.md b/docs/src/rules/sort-vars.md index e9527045f425..70a3f378077b 100644 --- a/docs/src/rules/sort-vars.md +++ b/docs/src/rules/sort-vars.md @@ -22,11 +22,11 @@ Examples of **incorrect** code for this rule: ```js /*eslint sort-vars: "error"*/ -var b, a; +let b, a; -var a, B, c; +let c, D, e; -var a, A; +let f, F; ``` ::: @@ -38,14 +38,14 @@ Examples of **correct** code for this rule: ```js /*eslint sort-vars: "error"*/ -var a, b, c, d; +let a, b, c, d; -var _a = 10; -var _b = 20; +let _a = 10; +let _b = 20; -var A, a; +let E, e; -var B, a, c; +let G, f, h; ``` ::: @@ -55,7 +55,7 @@ Alphabetical list is maintained starting from the first variable and excluding a ```js /*eslint sort-vars: "error"*/ -var c, d, a, b; +let c, d, a, b; ``` But this one, will only produce one: @@ -63,7 +63,7 @@ But this one, will only produce one: ```js /*eslint sort-vars: "error"*/ -var c, d, a, e; +let c, d, a, e; ``` ## Options @@ -83,7 +83,7 @@ Examples of **correct** code for this rule with the `{ "ignoreCase": true }` opt var a, A; -var a, B, c; +var c, D, e; ``` ::: diff --git a/docs/src/rules/symbol-description.md b/docs/src/rules/symbol-description.md index 6ccc284b9288..9322bc32f74b 100644 --- a/docs/src/rules/symbol-description.md +++ b/docs/src/rules/symbol-description.md @@ -9,16 +9,16 @@ further_reading: The `Symbol` function may have an optional description: ```js -var foo = Symbol("some description"); +const foo = Symbol("some description"); -var someString = "some description"; -var bar = Symbol(someString); +const someString = "some description"; +const bar = Symbol(someString); ``` Using `description` promotes easier debugging: when a symbol is logged the description is used: ```js -var foo = Symbol("some description"); +const foo = Symbol("some description"); > console.log(foo); // Symbol(some description) @@ -39,7 +39,7 @@ Examples of **incorrect** code for this rule: ```js /*eslint symbol-description: "error"*/ -var foo = Symbol(); +const foo = Symbol(); ``` ::: @@ -51,10 +51,10 @@ Examples of **correct** code for this rule: ```js /*eslint symbol-description: "error"*/ -var foo = Symbol("some description"); +const foo = Symbol("some description"); -var someString = "some description"; -var bar = Symbol(someString); +const someString = "some description"; +const bar = Symbol(someString); ``` ::: From 80700a533c210e0398f1a70f5453270791bad631 Mon Sep 17 00:00:00 2001 From: Tanuj Kanti Date: Fri, 31 Jan 2025 19:08:07 +0530 Subject: [PATCH 2/3] update variable name in prefer destructuring --- docs/src/rules/prefer-destructuring.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/rules/prefer-destructuring.md b/docs/src/rules/prefer-destructuring.md index 7f320683bda8..d2612de7e0a9 100644 --- a/docs/src/rules/prefer-destructuring.md +++ b/docs/src/rules/prefer-destructuring.md @@ -51,8 +51,8 @@ const foo = array[0]; bar.baz = array[0]; // With `object` enabled -const obj1 = object.foo; -const obj2 = object['foo']; +const qux = object.qux; +const quux = object['quux']; ``` ::: From 94df6afd796ea1a7ac2bc36bfe1371018020dd21 Mon Sep 17 00:00:00 2001 From: Tanuj Kanti Date: Sat, 1 Feb 2025 11:39:37 +0530 Subject: [PATCH 3/3] replace more vars with const --- docs/src/rules/prefer-destructuring.md | 2 +- docs/src/rules/sort-vars.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/rules/prefer-destructuring.md b/docs/src/rules/prefer-destructuring.md index d2612de7e0a9..83079bff6128 100644 --- a/docs/src/rules/prefer-destructuring.md +++ b/docs/src/rules/prefer-destructuring.md @@ -185,7 +185,7 @@ class C { * Accessing an object property whose key is an integer will fall under the category `array` destructuring. * Accessing an array element through a computed index will fall under the category `object` destructuring. -The `--fix` option on the command line fixes only problems reported in variable declarations, and among them only those that fall under the category `object` destructuring. Furthermore, the name of the declared variable has to be the same as the name used for non-computed member access in the initializer. For example, `var foo = object.foo` can be automatically fixed by this rule. Problems that involve computed member access (e.g., `var foo = object[foo]`) or renamed properties (e.g., `var foo = object.bar`) are not automatically fixed. +The `--fix` option on the command line fixes only problems reported in variable declarations, and among them only those that fall under the category `object` destructuring. Furthermore, the name of the declared variable has to be the same as the name used for non-computed member access in the initializer. For example, `const foo = object.foo` can be automatically fixed by this rule. Problems that involve computed member access (e.g., `const foo = object[foo]`) or renamed properties (e.g., `const foo = object.bar`) are not automatically fixed. ## When Not To Use It diff --git a/docs/src/rules/sort-vars.md b/docs/src/rules/sort-vars.md index 70a3f378077b..479b91f62ea1 100644 --- a/docs/src/rules/sort-vars.md +++ b/docs/src/rules/sort-vars.md @@ -81,9 +81,9 @@ Examples of **correct** code for this rule with the `{ "ignoreCase": true }` opt ```js /*eslint sort-vars: ["error", { "ignoreCase": true }]*/ -var a, A; +let a, A; -var c, D, e; +let c, D, e; ``` ::: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy