Skip to content

docs: replace var with let and const in rules docs #19392

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

Merged
merged 1 commit into from
Feb 2, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions docs/src/rules/no-eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ further_reading:
JavaScript's `eval()` function is potentially dangerous and is often misused. Using `eval()` on untrusted code can open a program up to several different injection attacks. The use of `eval()` in most contexts can be substituted for a better, alternative approach to a problem.

```js
var obj = { x: "foo" },
const obj = { x: "foo" },
key = "x",
value = eval("obj." + key);
```
Expand All @@ -28,17 +28,17 @@ Examples of **incorrect** code for this rule:
```js
/*eslint no-eval: "error"*/

var obj = { x: "foo" },
const obj = { x: "foo" },
key = "x",
value = eval("obj." + key);

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

var foo = eval;
foo("var a = 0");
const foo = eval;
foo("const a = 0");

// This `this` is the global object.
this.eval("var a = 0");
this.eval("const a = 0");
```

:::
Expand All @@ -51,7 +51,7 @@ Example of additional **incorrect** code for this rule with `window` global vari
/*eslint no-eval: "error"*/
/*global window*/

window.eval("var a = 0");
window.eval("const a = 0");
```

:::
Expand All @@ -64,7 +64,7 @@ Example of additional **incorrect** code for this rule with `global` global vari
/*eslint no-eval: "error"*/
/*global global*/

global.eval("var a = 0");
global.eval("const a = 0");
```

:::
Expand All @@ -76,22 +76,22 @@ Examples of **correct** code for this rule:
```js
/*eslint no-eval: "error"*/

var obj = { x: "foo" },
const obj = { x: "foo" },
key = "x",
value = obj[key];

class A {
foo() {
// This is a user-defined method.
this.eval("var a = 0");
this.eval("const a = 0");
}

eval() {
}

static {
// This is a user-defined static method.
this.eval("var a = 0");
this.eval("const a = 0");
}

static eval() {
Expand Down Expand Up @@ -121,7 +121,7 @@ Example of **incorrect** code for this rule with the `{"allowIndirect": true}` o
```js
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/

var obj = { x: "foo" },
const obj = { x: "foo" },
key = "x",
value = eval("obj." + key);
```
Expand All @@ -135,12 +135,12 @@ Examples of **correct** code for this rule with the `{"allowIndirect": true}` op
```js
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/

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

var foo = eval;
foo("var a = 0");
const foo = eval;
foo("const a = 0");

this.eval("var a = 0");
this.eval("const a = 0");
```

:::
Expand All @@ -151,7 +151,7 @@ this.eval("var a = 0");
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
/*global window*/

window.eval("var a = 0");
window.eval("const a = 0");
```

:::
Expand All @@ -162,7 +162,7 @@ window.eval("var a = 0");
/*eslint no-eval: ["error", {"allowIndirect": true} ]*/
/*global global*/

global.eval("var a = 0");
global.eval("const a = 0");
```

:::
Expand All @@ -176,13 +176,13 @@ global.eval("var a = 0");
module.exports = function(eval) {
// If the value of this `eval` is built-in `eval` function, this is a
// call of direct `eval`.
eval("var a = 0");
eval("const a = 0");
};
```

* This rule cannot catch renaming the global object. Such as:

```js
var foo = window;
foo.eval("var a = 0");
const foo = window;
foo.eval("const a = 0");
```
14 changes: 7 additions & 7 deletions docs/src/rules/no-nested-ternary.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ related_rules:
Nesting ternary expressions can make code more difficult to understand.

```js
var foo = bar ? baz : qux === quxx ? bing : bam;
const foo = bar ? baz : qux === quxx ? bing : bam;
```

## Rule Details
Expand All @@ -24,7 +24,7 @@ Examples of **incorrect** code for this rule:
```js
/*eslint no-nested-ternary: "error"*/

var thing = foo ? bar : baz === qux ? quxx : foobar;
const thing = foo ? bar : baz === qux ? quxx : foobar;

foo ? baz === qux ? quxx() : foobar() : bar();
```
Expand All @@ -38,16 +38,16 @@ Examples of **correct** code for this rule:
```js
/*eslint no-nested-ternary: "error"*/

var thing = foo ? bar : foobar;
const thing = foo ? bar : foobar;

var thing;
let otherThing;

if (foo) {
thing = bar;
otherThing = bar;
} else if (baz === qux) {
thing = quxx;
otherThing = quxx;
} else {
thing = foobar;
otherThing = foobar;
}
```

Expand Down
6 changes: 3 additions & 3 deletions docs/src/rules/no-ternary.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ related_rules:
The ternary operator is used to conditionally assign a value to a variable. Some believe that the use of ternary operators leads to unclear code.

```js
var foo = isBar ? baz : qux;
const foo = isBar ? baz : qux;
```

## Rule Details
Expand All @@ -24,7 +24,7 @@ Examples of **incorrect** code for this rule:
```js
/*eslint no-ternary: "error"*/

var foo = isBar ? baz : qux;
const foo = isBar ? baz : qux;

function quux() {
return foo ? bar() : baz();
Expand All @@ -40,7 +40,7 @@ Examples of **correct** code for this rule:
```js
/*eslint no-ternary: "error"*/

var foo;
let foo;

if (isBar) {
foo = baz;
Expand Down
10 changes: 5 additions & 5 deletions docs/src/rules/no-undefined.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The `undefined` variable in JavaScript is actually a property of the global obje

```js
function doSomething(data) {
var undefined = "hi";
const undefined = "hi";

// doesn't do what you think it does
if (data === undefined) {
Expand Down Expand Up @@ -46,9 +46,9 @@ Examples of **incorrect** code for this rule:
```js
/*eslint no-undefined: "error"*/

var foo = undefined;
const foo = undefined;

var undefined = "foo";
const undefined = "foo";

if (foo === undefined) {
// ...
Expand All @@ -70,9 +70,9 @@ Examples of **correct** code for this rule:
```js
/*eslint no-undefined: "error"*/

var foo = void 0;
const foo = void 0;

var Undefined = "foo";
const Undefined = "foo";

if (typeof foo === "undefined") {
// ...
Expand Down
22 changes: 11 additions & 11 deletions docs/src/rules/no-unneeded-ternary.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ Here are some examples:

```js
// Bad
var isYes = answer === 1 ? true : false;
const isYes = answer === 1 ? true : false;

// Good
var isYes = answer === 1;
const isYes = answer === 1;

// Bad
var isNo = answer === 1 ? false : true;
const isNo = answer === 1 ? false : true;

// Good
var isNo = answer !== 1;
const isNo = answer !== 1;
```

Another common mistake is using a single variable as both the conditional test and the consequent. In such cases, the logical `OR` can be used to provide the same functionality.
Expand All @@ -47,9 +47,9 @@ Examples of **incorrect** code for this rule:
```js
/*eslint no-unneeded-ternary: "error"*/

var a = x === 2 ? true : false;
const a = x === 2 ? true : false;

var a = x ? true : false;
const b = x ? true : false;
```

:::
Expand All @@ -61,13 +61,13 @@ Examples of **correct** code for this rule:
```js
/*eslint no-unneeded-ternary: "error"*/

var a = x === 2 ? "Yes" : "No";
const a = x === 2 ? "Yes" : "No";

var a = x !== false;
const b = x !== false;

var a = x ? "Yes" : "No";
const c = x ? "Yes" : "No";

var a = x ? y : x;
const d = x ? y : x;

f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below.
```
Expand All @@ -92,7 +92,7 @@ Examples of additional **incorrect** code for this rule with the `{ "defaultAssi
```js
/*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/

var a = x ? x : 1;
const a = x ? x : 1;

f(x ? x : 1);
```
Expand Down
Loading
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