From a6d9442a9ab34d5d19f78d8c8fd0767a1237bfe3 Mon Sep 17 00:00:00 2001 From: Tanuj Kanti <86398394+Tanujkanti4441@users.noreply.github.com> Date: Mon, 27 Nov 2023 02:09:49 +0530 Subject: [PATCH] docs: fix correct/incorrect examples of rules (#17789) * docs: fix correct/incorrect examples of rules * docs: fix previous commit errors * docs: update previous code --- docs/src/rules/capitalized-comments.md | 3 +++ docs/src/rules/dot-notation.md | 19 ++++++++++++------- docs/src/rules/max-lines-per-function.md | 12 ++++++------ docs/src/rules/no-async-promise-executor.md | 4 ++++ docs/src/rules/no-else-return.md | 4 ++-- docs/src/rules/no-eval.md | 12 +++++++----- docs/src/rules/no-implicit-globals.md | 2 +- docs/src/rules/no-implied-eval.md | 1 + docs/src/rules/no-loop-func.md | 10 ++++++++-- docs/src/rules/no-redeclare.md | 4 ++-- docs/src/rules/no-undef-init.md | 2 ++ .../src/rules/no-unmodified-loop-condition.md | 4 ++-- docs/src/rules/no-unused-expressions.md | 2 +- docs/src/rules/object-shorthand.md | 8 ++++---- docs/src/rules/one-var.md | 3 +++ docs/src/rules/prefer-regex-literals.md | 10 +++++++++- 16 files changed, 67 insertions(+), 33 deletions(-) diff --git a/docs/src/rules/capitalized-comments.md b/docs/src/rules/capitalized-comments.md index 66d03d779eb..d42fc201166 100644 --- a/docs/src/rules/capitalized-comments.md +++ b/docs/src/rules/capitalized-comments.md @@ -216,10 +216,12 @@ Examples of **correct** code with `ignoreConsecutiveComments` set to `true`: ```js /* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */ +foo(); // This comment is valid since it has the correct capitalization. // this comment is ignored since it follows another comment, // and this one as well because it follows yet another comment. +bar(); /* Here is a block comment which has the correct capitalization, */ /* but this one is ignored due to being consecutive; */ /* @@ -236,6 +238,7 @@ Examples of **incorrect** code with `ignoreConsecutiveComments` set to `true`: ```js /* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */ +foo(); // this comment is invalid, but only on this line. // this comment does NOT get reported, since it is a consecutive comment. ``` diff --git a/docs/src/rules/dot-notation.md b/docs/src/rules/dot-notation.md index 7514a1fdad2..7aa8810516b 100644 --- a/docs/src/rules/dot-notation.md +++ b/docs/src/rules/dot-notation.md @@ -84,22 +84,27 @@ class C { For example, when preparing data to be sent to an external API, it is often required to use property names that include underscores. If the `camelcase` rule is in effect, these [snake case](https://en.wikipedia.org/wiki/Snake_case) properties would not be allowed. By providing an `allowPattern` to the `dot-notation` rule, these snake case properties can be accessed with bracket notation. -Examples of **correct** code for the sample `{ "allowPattern": "^[a-z]+(_[a-z]+)+$" }` option: +Examples of **incorrect** code for the sample `{ "allowPattern": "^[a-z]+(_[a-z]+)+$" }` (pattern to find snake case named properties) option: -:::correct +:::incorrect ```js -/*eslint camelcase: "error"*/ /*eslint dot-notation: ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }]*/ -var data = {}; -data.foo_bar = 42; - var data = {}; data["fooBar"] = 42; +``` + +::: +Examples of **correct** code for the sample `{ "allowPattern": "^[a-z]+(_[a-z]+)+$" }` (pattern to find snake case named properties) option: + +:::correct + +```js +/*eslint dot-notation: ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }]*/ var data = {}; -data["foo_bar"] = 42; // no warning +data["foo_bar"] = 42; ``` ::: diff --git a/docs/src/rules/max-lines-per-function.md b/docs/src/rules/max-lines-per-function.md index 45b8a62720f..0bceb71a198 100644 --- a/docs/src/rules/max-lines-per-function.md +++ b/docs/src/rules/max-lines-per-function.md @@ -72,7 +72,7 @@ is equivalent to ### code -Examples of **incorrect** code for this rule with a max value of `2`: +Examples of **incorrect** code for this rule with a particular max value: ::: incorrect @@ -88,7 +88,7 @@ function foo() { ::: incorrect ```js -/*eslint max-lines-per-function: ["error", 2]*/ +/*eslint max-lines-per-function: ["error", 3]*/ function foo() { // a comment var x = 0; @@ -100,7 +100,7 @@ function foo() { ::: incorrect ```js -/*eslint max-lines-per-function: ["error", 2]*/ +/*eslint max-lines-per-function: ["error", 4]*/ function foo() { // a comment followed by a blank line @@ -110,7 +110,7 @@ function foo() { ::: -Examples of **correct** code for this rule with a max value of `3`: +Examples of **correct** code for this rule with a particular max value: ::: correct @@ -126,7 +126,7 @@ function foo() { ::: correct ```js -/*eslint max-lines-per-function: ["error", 3]*/ +/*eslint max-lines-per-function: ["error", 4]*/ function foo() { // a comment var x = 0; @@ -138,7 +138,7 @@ function foo() { ::: correct ```js -/*eslint max-lines-per-function: ["error", 3]*/ +/*eslint max-lines-per-function: ["error", 5]*/ function foo() { // a comment followed by a blank line diff --git a/docs/src/rules/no-async-promise-executor.md b/docs/src/rules/no-async-promise-executor.md index 784c5ea5396..768f3459f25 100644 --- a/docs/src/rules/no-async-promise-executor.md +++ b/docs/src/rules/no-async-promise-executor.md @@ -33,6 +33,8 @@ Examples of **incorrect** code for this rule: ::: incorrect ```js +/*eslint no-async-promise-executor: "error"*/ + const foo = new Promise(async (resolve, reject) => { readFile('foo.txt', function(err, result) { if (err) { @@ -55,6 +57,8 @@ Examples of **correct** code for this rule: ::: correct ```js +/*eslint no-async-promise-executor: "error"*/ + const foo = new Promise((resolve, reject) => { readFile('foo.txt', function(err, result) { if (err) { diff --git a/docs/src/rules/no-else-return.md b/docs/src/rules/no-else-return.md index 79b4fe5fb01..379de922b32 100644 --- a/docs/src/rules/no-else-return.md +++ b/docs/src/rules/no-else-return.md @@ -28,7 +28,7 @@ This rule has an object option: * `allowElseIf: true` (default) allows `else if` blocks after a return * `allowElseIf: false` disallows `else if` blocks after a return -### `allowElseIf: true` +### allowElseIf: true Examples of **incorrect** code for this rule: @@ -137,7 +137,7 @@ function foo4() { ::: -### `allowElseIf: false` +### allowElseIf: false Examples of **incorrect** code for this rule: diff --git a/docs/src/rules/no-eval.md b/docs/src/rules/no-eval.md index 662df5d3acd..64f7c78b3d6 100644 --- a/docs/src/rules/no-eval.md +++ b/docs/src/rules/no-eval.md @@ -104,6 +104,8 @@ class A { ## Options +### allowIndirect + This rule has an option to allow indirect calls to `eval`. Indirect calls to `eval` are less dangerous than direct calls to `eval` because they cannot dynamically change the scope. Because of this, they also will not negatively impact performance to the degree of direct `eval`. @@ -118,7 +120,7 @@ Example of **incorrect** code for this rule with the `{"allowIndirect": true}` o ::: incorrect ```js -/*eslint no-eval: "error"*/ +/*eslint no-eval: ["error", {"allowIndirect": true} ]*/ var obj = { x: "foo" }, key = "x", @@ -129,10 +131,10 @@ var obj = { x: "foo" }, Examples of **correct** code for this rule with the `{"allowIndirect": true}` option: -::: correct +::: correct { "sourceType": "script" } ```js -/*eslint no-eval: "error"*/ +/*eslint no-eval: ["error", {"allowIndirect": true} ]*/ (0, eval)("var a = 0"); @@ -147,7 +149,7 @@ this.eval("var a = 0"); ::: correct ```js -/*eslint no-eval: "error"*/ +/*eslint no-eval: ["error", {"allowIndirect": true} ]*/ /*eslint-env browser*/ window.eval("var a = 0"); @@ -158,7 +160,7 @@ window.eval("var a = 0"); ::: correct ```js -/*eslint no-eval: "error"*/ +/*eslint no-eval: ["error", {"allowIndirect": true} ]*/ /*eslint-env node*/ global.eval("var a = 0"); diff --git a/docs/src/rules/no-implicit-globals.md b/docs/src/rules/no-implicit-globals.md index 6a479a0a765..24ed498928d 100644 --- a/docs/src/rules/no-implicit-globals.md +++ b/docs/src/rules/no-implicit-globals.md @@ -79,7 +79,7 @@ window.bar = function() {}; Examples of **correct** code for this rule with `"parserOptions": { "sourceType": "module" }` in the ESLint configuration: -::: correct { "sourceType": "script" } +::: correct { "sourceType": "module" } ```js /*eslint no-implicit-globals: "error"*/ diff --git a/docs/src/rules/no-implied-eval.md b/docs/src/rules/no-implied-eval.md index 542e39a2cef..cfd067769cc 100644 --- a/docs/src/rules/no-implied-eval.md +++ b/docs/src/rules/no-implied-eval.md @@ -35,6 +35,7 @@ Examples of **incorrect** code for this rule: ```js /*eslint no-implied-eval: "error"*/ +/*eslint-env browser*/ setTimeout("alert('Hi!');", 100); diff --git a/docs/src/rules/no-loop-func.md b/docs/src/rules/no-loop-func.md index 659fd3e62da..8fed2d6d909 100644 --- a/docs/src/rules/no-loop-func.md +++ b/docs/src/rules/no-loop-func.md @@ -48,15 +48,21 @@ for (var i=10; i; i--) { (function() { return i; })(); } -while(i) { +var i = 0; +while(i < 5) { var a = function() { return i; }; a(); + + i++; } +var i = 0; do { function a() { return i; }; a(); -} while (i); + + i++ +} while (i < 5); let foo = 0; for (let i = 0; i < 10; ++i) { diff --git a/docs/src/rules/no-redeclare.md b/docs/src/rules/no-redeclare.md index 009ba889fcc..e3ce497753e 100644 --- a/docs/src/rules/no-redeclare.md +++ b/docs/src/rules/no-redeclare.md @@ -77,7 +77,7 @@ The `"builtinGlobals"` option will check for redeclaration of built-in globals i Examples of **incorrect** code for the `{ "builtinGlobals": true }` option: -::: incorrect +::: incorrect { "sourceType": "script" } ```js /*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/ @@ -89,7 +89,7 @@ var Object = 0; Examples of **incorrect** code for the `{ "builtinGlobals": true }` option and the `browser` environment: -::: incorrect +::: incorrect { "sourceType": "script" } ```js /*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/ diff --git a/docs/src/rules/no-undef-init.md b/docs/src/rules/no-undef-init.md index f49ee8c7de4..f474562ca20 100644 --- a/docs/src/rules/no-undef-init.md +++ b/docs/src/rules/no-undef-init.md @@ -87,6 +87,8 @@ Example of **incorrect** code for this rule: ::: incorrect ```js +/*eslint no-undef-init: "error"*/ + for (i = 0; i < 10; i++) { var x = undefined; console.log(x); diff --git a/docs/src/rules/no-unmodified-loop-condition.md b/docs/src/rules/no-unmodified-loop-condition.md index 29932091697..86a8212d7d9 100644 --- a/docs/src/rules/no-unmodified-loop-condition.md +++ b/docs/src/rules/no-unmodified-loop-condition.md @@ -44,8 +44,8 @@ while (node) { } node = other; -for (var j = 0; j < items.length; ++i) { - doSomething(items[j]); +for (var j = 0; j < 5;) { + doSomething(j); } while (node !== root) { diff --git a/docs/src/rules/no-unused-expressions.md b/docs/src/rules/no-unused-expressions.md index 614ea5b11b7..52379726dce 100644 --- a/docs/src/rules/no-unused-expressions.md +++ b/docs/src/rules/no-unused-expressions.md @@ -79,7 +79,7 @@ Examples of **correct** code for the default `{ "allowShortCircuit": false, "all {} // In this context, this is a block statement, not an object literal -{myLabel: someVar} // In this context, this is a block statement with a label and expression, not an object literal +{ myLabel: foo() } // In this context, this is a block statement with a label and expression, not an object literal function namedFunctionDeclaration () {} diff --git a/docs/src/rules/object-shorthand.md b/docs/src/rules/object-shorthand.md index d694b57b029..299d46b9664 100644 --- a/docs/src/rules/object-shorthand.md +++ b/docs/src/rules/object-shorthand.md @@ -116,7 +116,7 @@ Additionally, the rule takes an optional object configuration: * `"methodsIgnorePattern"` (`string`) for methods whose names match this regex pattern, the method shorthand will not be enforced. Note that this option can only be used when the string option is set to `"always"` or `"methods"`. * `"avoidExplicitReturnArrows": true` indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to `"always"` or `"methods"`. -### `avoidQuotes` +### avoidQuotes ```json { @@ -155,7 +155,7 @@ var foo = { ::: -### `ignoreConstructors` +### ignoreConstructors ```json { @@ -178,7 +178,7 @@ var foo = { ::: -### `methodsIgnorePattern` +### methodsIgnorePattern Example of **correct** code for this rule with the `"always", { "methodsIgnorePattern": "^bar$" }` option: @@ -194,7 +194,7 @@ var foo = { ::: -### `avoidExplicitReturnArrows` +### avoidExplicitReturnArrows ```json { diff --git a/docs/src/rules/one-var.md b/docs/src/rules/one-var.md index cb3bc09b955..0066757e1da 100644 --- a/docs/src/rules/one-var.md +++ b/docs/src/rules/one-var.md @@ -462,6 +462,9 @@ var bar = "bar"; ::: correct ```js +/*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/ +/*eslint-env node*/ + var foo = require("foo"), bar = require("bar"); ``` diff --git a/docs/src/rules/prefer-regex-literals.md b/docs/src/rules/prefer-regex-literals.md index 2159c977104..8468044c6be 100644 --- a/docs/src/rules/prefer-regex-literals.md +++ b/docs/src/rules/prefer-regex-literals.md @@ -110,12 +110,14 @@ This rule has an object option: * `disallowRedundantWrapping` set to `true` additionally checks for unnecessarily wrapped regex literals (Default `false`). -### `disallowRedundantWrapping` +### disallowRedundantWrapping By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a `RegExp` constructor call. When the option `disallowRedundantWrapping` is set to `true`, the rule will also disallow such unnecessary patterns. Examples of `incorrect` code for `{ "disallowRedundantWrapping": true }` +::: incorrect + ```js /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/ @@ -124,8 +126,12 @@ new RegExp(/abc/); new RegExp(/abc/, 'u'); ``` +::: + Examples of `correct` code for `{ "disallowRedundantWrapping": true }` +::: correct + ```js /*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/ @@ -135,3 +141,5 @@ Examples of `correct` code for `{ "disallowRedundantWrapping": true }` new RegExp(/abc/, flags); ``` + +:::