From 9bb24c1deb7ab6743080520d11aefe9c6b8b8f2d Mon Sep 17 00:00:00 2001 From: Deepshika S Date: Wed, 22 Jun 2022 08:04:15 +0530 Subject: [PATCH] chore: add correct incorrect in all rules doc (#16021) * fix: add correct incorrect in all rules doc * chore: add incorrect correct to all other files --- docs/src/rules/accessor-pairs.md | 30 +++ docs/src/rules/array-bracket-newline.md | 48 ++++ docs/src/rules/array-bracket-spacing.md | 40 ++++ docs/src/rules/array-callback-return.md | 20 ++ docs/src/rules/array-element-newline.md | 56 +++++ docs/src/rules/arrow-body-style.md | 32 +++ docs/src/rules/arrow-parens.md | 24 ++ docs/src/rules/arrow-spacing.md | 24 ++ docs/src/rules/block-scoped-var.md | 8 + docs/src/rules/block-spacing.md | 16 ++ docs/src/rules/brace-style.md | 36 +++ docs/src/rules/callback-return.md | 16 ++ docs/src/rules/camelcase.md | 60 +++++ docs/src/rules/capitalized-comments.md | 48 ++++ docs/src/rules/class-methods-use-this.md | 28 +++ docs/src/rules/comma-dangle.md | 48 ++++ docs/src/rules/comma-spacing.md | 24 ++ docs/src/rules/comma-style.md | 24 ++ docs/src/rules/complexity.md | 16 ++ docs/src/rules/computed-property-spacing.md | 28 +++ docs/src/rules/consistent-return.md | 20 ++ docs/src/rules/consistent-this.md | 16 ++ docs/src/rules/constructor-super.md | 8 + docs/src/rules/curly.md | 44 ++++ docs/src/rules/default-case-last.md | 8 + docs/src/rules/default-case.md | 12 + docs/src/rules/default-param-last.md | 8 + docs/src/rules/dot-location.md | 16 ++ docs/src/rules/dot-notation.md | 20 ++ docs/src/rules/eol-last.md | 8 + docs/src/rules/eqeqeq.md | 20 ++ docs/src/rules/for-direction.md | 8 + docs/src/rules/func-call-spacing.md | 24 ++ docs/src/rules/func-name-matching.md | 20 ++ docs/src/rules/func-names.md | 56 +++++ docs/src/rules/func-style.md | 20 ++ .../rules/function-call-argument-newline.md | 24 ++ docs/src/rules/function-paren-newline.md | 48 ++++ docs/src/rules/generator-star-spacing.md | 24 ++ docs/src/rules/getter-return.md | 12 + docs/src/rules/global-require.md | 8 + docs/src/rules/grouped-accessor-pairs.md | 24 ++ docs/src/rules/guard-for-in.md | 8 + docs/src/rules/handle-callback-err.md | 12 + docs/src/rules/id-denylist.md | 8 + docs/src/rules/id-length.md | 36 +++ docs/src/rules/id-match.md | 32 +++ docs/src/rules/implicit-arrow-linebreak.md | 16 ++ docs/src/rules/indent-legacy.md | 136 +++++++++++ docs/src/rules/indent.md | 220 ++++++++++++++++++ docs/src/rules/init-declarations.md | 20 ++ docs/src/rules/jsx-quotes.md | 16 ++ docs/src/rules/key-spacing.md | 76 ++++++ docs/src/rules/keyword-spacing.md | 36 +++ docs/src/rules/line-comment-position.md | 32 +++ docs/src/rules/linebreak-style.md | 16 ++ docs/src/rules/lines-around-comment.md | 128 ++++++++++ docs/src/rules/lines-around-directive.md | 32 +++ docs/src/rules/lines-between-class-members.md | 24 ++ docs/src/rules/max-classes-per-file.md | 16 ++ docs/src/rules/max-depth.md | 16 ++ docs/src/rules/max-len.md | 48 ++++ docs/src/rules/max-lines-per-function.md | 32 +++ docs/src/rules/max-lines.md | 24 ++ docs/src/rules/max-nested-callbacks.md | 8 + docs/src/rules/max-params.md | 8 + docs/src/rules/max-statements-per-line.md | 16 ++ docs/src/rules/max-statements.md | 16 ++ docs/src/rules/multiline-comment-style.md | 24 ++ docs/src/rules/multiline-ternary.md | 24 ++ docs/src/rules/new-cap.md | 68 ++++++ docs/src/rules/new-parens.md | 16 ++ docs/src/rules/newline-after-var.md | 16 ++ docs/src/rules/newline-before-return.md | 8 + docs/src/rules/newline-per-chained-call.md | 8 + docs/src/rules/no-alert.md | 8 + docs/src/rules/no-array-constructor.md | 8 + docs/src/rules/no-arrow-condition.md | 4 + docs/src/rules/no-async-promise-executor.md | 8 + docs/src/rules/no-await-in-loop.md | 8 + docs/src/rules/no-bitwise.md | 16 ++ docs/src/rules/no-buffer-constructor.md | 8 + docs/src/rules/no-caller.md | 8 + docs/src/rules/no-case-declarations.md | 8 + docs/src/rules/no-catch-shadow.md | 8 + docs/src/rules/no-class-assign.md | 8 + docs/src/rules/no-comma-dangle.md | 8 + docs/src/rules/no-compare-neg-zero.md | 8 + docs/src/rules/no-cond-assign.md | 16 ++ docs/src/rules/no-confusing-arrow.md | 16 ++ docs/src/rules/no-console.md | 12 + docs/src/rules/no-const-assign.md | 8 + .../rules/no-constant-binary-expression.md | 8 + docs/src/rules/no-constant-condition.md | 12 + docs/src/rules/no-constructor-return.md | 8 + docs/src/rules/no-continue.md | 8 + docs/src/rules/no-control-regex.md | 8 + docs/src/rules/no-debugger.md | 8 + docs/src/rules/no-delete-var.md | 4 + docs/src/rules/no-div-regex.md | 8 + docs/src/rules/no-dupe-args.md | 8 + docs/src/rules/no-dupe-class-members.md | 8 + docs/src/rules/no-dupe-else-if.md | 12 + docs/src/rules/no-dupe-keys.md | 8 + docs/src/rules/no-duplicate-case.md | 8 + docs/src/rules/no-duplicate-imports.md | 24 ++ docs/src/rules/no-else-return.md | 16 ++ docs/src/rules/no-empty-character-class.md | 8 + docs/src/rules/no-empty-class.md | 8 + docs/src/rules/no-empty-function.md | 48 ++++ docs/src/rules/no-empty-label.md | 8 + docs/src/rules/no-empty-pattern.md | 8 + docs/src/rules/no-empty.md | 12 + docs/src/rules/no-eq-null.md | 8 + docs/src/rules/no-eval.md | 24 ++ docs/src/rules/no-ex-assign.md | 8 + docs/src/rules/no-extend-native.md | 8 + docs/src/rules/no-extra-bind.md | 8 + docs/src/rules/no-extra-boolean-cast.md | 16 ++ docs/src/rules/no-extra-label.md | 8 + docs/src/rules/no-extra-parens.md | 64 +++++ docs/src/rules/no-extra-semi.md | 8 + docs/src/rules/no-extra-strict.md | 8 + docs/src/rules/no-fallthrough.md | 12 + docs/src/rules/no-floating-decimal.md | 8 + docs/src/rules/no-func-assign.md | 12 + docs/src/rules/no-global-assign.md | 8 + docs/src/rules/no-implicit-coercion.md | 40 ++++ docs/src/rules/no-implicit-globals.md | 40 ++++ docs/src/rules/no-implied-eval.md | 8 + docs/src/rules/no-import-assign.md | 8 + docs/src/rules/no-inline-comments.md | 24 ++ docs/src/rules/no-inner-declarations.md | 16 ++ docs/src/rules/no-invalid-regexp.md | 12 + docs/src/rules/no-invalid-this.md | 16 ++ docs/src/rules/no-irregular-whitespace.md | 20 ++ docs/src/rules/no-iterator.md | 8 + docs/src/rules/no-label-var.md | 8 + docs/src/rules/no-labels.md | 16 ++ docs/src/rules/no-lone-blocks.md | 12 + docs/src/rules/no-lonely-if.md | 8 + docs/src/rules/no-loop-func.md | 8 + docs/src/rules/no-loss-of-precision.md | 8 + docs/src/rules/no-magic-numbers.md | 40 ++++ .../rules/no-misleading-character-class.md | 8 + docs/src/rules/no-mixed-operators.md | 28 +++ docs/src/rules/no-mixed-requires.md | 20 ++ docs/src/rules/no-mixed-spaces-and-tabs.md | 12 + docs/src/rules/no-multi-assign.md | 16 ++ docs/src/rules/no-multi-spaces.md | 40 ++++ docs/src/rules/no-multi-str.md | 8 + docs/src/rules/no-multiple-empty-lines.md | 24 ++ docs/src/rules/no-native-reassign.md | 8 + docs/src/rules/no-negated-condition.md | 8 + docs/src/rules/no-negated-in-lhs.md | 8 + docs/src/rules/no-nested-ternary.md | 8 + docs/src/rules/no-new-func.md | 8 + docs/src/rules/no-new-object.md | 8 + docs/src/rules/no-new-require.md | 8 + docs/src/rules/no-new-symbol.md | 8 + docs/src/rules/no-new-wrappers.md | 8 + docs/src/rules/no-new.md | 8 + docs/src/rules/no-nonoctal-decimal-escape.md | 8 + docs/src/rules/no-obj-calls.md | 8 + docs/src/rules/no-octal-escape.md | 8 + docs/src/rules/no-octal.md | 8 + docs/src/rules/no-param-reassign.md | 24 ++ docs/src/rules/no-path-concat.md | 8 + docs/src/rules/no-plusplus.md | 16 ++ docs/src/rules/no-process-env.md | 8 + docs/src/rules/no-process-exit.md | 8 + docs/src/rules/no-promise-executor-return.md | 8 + docs/src/rules/no-proto.md | 8 + docs/src/rules/no-prototype-builtins.md | 8 + docs/src/rules/no-redeclare.md | 16 ++ docs/src/rules/no-regex-spaces.md | 8 + docs/src/rules/no-reserved-keys.md | 8 + docs/src/rules/no-restricted-exports.md | 16 ++ docs/src/rules/no-restricted-globals.md | 12 + docs/src/rules/no-restricted-imports.md | 8 + docs/src/rules/no-restricted-modules.md | 8 + docs/src/rules/no-restricted-properties.md | 8 + docs/src/rules/no-restricted-syntax.md | 8 + docs/src/rules/no-return-assign.md | 16 ++ docs/src/rules/no-return-await.md | 8 + docs/src/rules/no-script-url.md | 4 + docs/src/rules/no-self-assign.md | 12 + docs/src/rules/no-self-compare.md | 4 + docs/src/rules/no-sequences.md | 24 ++ docs/src/rules/no-setter-return.md | 8 + docs/src/rules/no-shadow-restricted-names.md | 8 + docs/src/rules/no-shadow.md | 36 +++ docs/src/rules/no-space-before-semi.md | 8 + docs/src/rules/no-spaced-func.md | 8 + docs/src/rules/no-sparse-arrays.md | 8 + docs/src/rules/no-sync.md | 16 ++ docs/src/rules/no-tabs.md | 12 + docs/src/rules/no-template-curly-in-string.md | 8 + docs/src/rules/no-ternary.md | 8 + docs/src/rules/no-this-before-super.md | 8 + docs/src/rules/no-throw-literal.md | 12 + docs/src/rules/no-trailing-spaces.md | 16 ++ docs/src/rules/no-undef-init.md | 20 ++ docs/src/rules/no-undef.md | 28 +++ docs/src/rules/no-undefined.md | 8 + docs/src/rules/no-underscore-dangle.md | 36 +++ docs/src/rules/no-unexpected-multiline.md | 8 + .../src/rules/no-unmodified-loop-condition.md | 8 + docs/src/rules/no-unneeded-ternary.md | 12 + docs/src/rules/no-unreachable-loop.md | 16 ++ docs/src/rules/no-unreachable.md | 16 ++ docs/src/rules/no-unsafe-finally.md | 8 + docs/src/rules/no-unsafe-negation.md | 20 ++ docs/src/rules/no-unsafe-optional-chaining.md | 12 + docs/src/rules/no-unused-expressions.md | 52 +++++ docs/src/rules/no-unused-labels.md | 8 + .../rules/no-unused-private-class-members.md | 8 + docs/src/rules/no-unused-vars.md | 60 +++++ docs/src/rules/no-use-before-define.md | 36 +++ docs/src/rules/no-useless-backreference.md | 12 + docs/src/rules/no-useless-call.md | 16 ++ docs/src/rules/no-useless-catch.md | 8 + docs/src/rules/no-useless-computed-key.md | 24 ++ docs/src/rules/no-useless-concat.md | 8 + docs/src/rules/no-useless-constructor.md | 8 + docs/src/rules/no-useless-escape.md | 8 + docs/src/rules/no-useless-rename.md | 20 ++ docs/src/rules/no-useless-return.md | 8 + docs/src/rules/no-var.md | 8 + docs/src/rules/no-void.md | 12 + docs/src/rules/no-warning-comments.md | 16 ++ .../rules/no-whitespace-before-property.md | 8 + docs/src/rules/no-with.md | 8 + docs/src/rules/no-wrap-func.md | 8 + .../rules/nonblock-statement-body-position.md | 24 ++ docs/src/rules/object-curly-newline.md | 56 +++++ docs/src/rules/object-curly-spacing.md | 32 +++ docs/src/rules/object-property-newline.md | 12 + docs/src/rules/object-shorthand.md | 32 +++ .../src/rules/one-var-declaration-per-line.md | 16 ++ docs/src/rules/one-var.md | 96 ++++++++ docs/src/rules/operator-assignment.md | 16 ++ docs/src/rules/operator-linebreak.md | 44 ++++ docs/src/rules/padded-blocks.md | 72 ++++++ .../rules/padding-line-between-statements.md | 32 +++ docs/src/rules/prefer-const.md | 32 +++ docs/src/rules/prefer-destructuring.md | 28 +++ .../rules/prefer-exponentiation-operator.md | 8 + docs/src/rules/prefer-named-capture-group.md | 8 + docs/src/rules/prefer-numeric-literals.md | 8 + docs/src/rules/prefer-object-has-own.md | 8 + docs/src/rules/prefer-object-spread.md | 8 + .../src/rules/prefer-promise-reject-errors.md | 12 + docs/src/rules/prefer-reflect.md | 112 +++++++++ docs/src/rules/prefer-regex-literals.md | 8 + docs/src/rules/prefer-rest-params.md | 8 + docs/src/rules/prefer-spread.md | 8 + docs/src/rules/prefer-template.md | 8 + docs/src/rules/quote-props.md | 48 ++++ docs/src/rules/quotes.md | 44 ++++ docs/src/rules/radix.md | 16 ++ docs/src/rules/require-atomic-updates.md | 20 ++ docs/src/rules/require-await.md | 8 + docs/src/rules/require-jsdoc.md | 8 + docs/src/rules/require-unicode-regexp.md | 8 + docs/src/rules/require-yield.md | 8 + docs/src/rules/rest-spread-spacing.md | 16 ++ docs/src/rules/semi-spacing.md | 16 ++ docs/src/rules/semi-style.md | 16 ++ docs/src/rules/sort-imports.md | 52 +++++ docs/src/rules/sort-keys.md | 40 ++++ docs/src/rules/sort-vars.md | 12 + docs/src/rules/space-after-function-name.md | 8 + docs/src/rules/space-after-keywords.md | 8 + docs/src/rules/space-before-blocks.md | 40 ++++ docs/src/rules/space-before-function-paren.md | 40 ++++ .../space-before-function-parentheses.md | 32 +++ docs/src/rules/space-before-keywords.md | 16 ++ docs/src/rules/space-in-brackets.md | 64 +++++ docs/src/rules/space-in-parens.md | 88 +++++++ docs/src/rules/space-infix-ops.md | 8 + docs/src/rules/space-return-throw-case.md | 8 + docs/src/rules/space-unary-ops.md | 8 + docs/src/rules/space-unary-word-ops.md | 8 + docs/src/rules/spaced-comment.md | 32 +++ docs/src/rules/spaced-line-comment.md | 8 + docs/src/rules/strict.md | 32 +++ docs/src/rules/switch-colon-spacing.md | 16 ++ docs/src/rules/symbol-description.md | 8 + docs/src/rules/template-curly-spacing.md | 16 ++ docs/src/rules/template-tag-spacing.md | 16 ++ docs/src/rules/unicode-bom.md | 16 ++ docs/src/rules/use-isnan.md | 28 +++ docs/src/rules/valid-jsdoc.md | 44 ++++ docs/src/rules/valid-typeof.md | 16 ++ docs/src/rules/vars-on-top.md | 8 + docs/src/rules/wrap-iife.md | 32 +++ docs/src/rules/wrap-regex.md | 8 + docs/src/rules/yield-star-spacing.md | 16 ++ docs/src/rules/yoda.md | 24 ++ 300 files changed, 6010 insertions(+) diff --git a/docs/src/rules/accessor-pairs.md b/docs/src/rules/accessor-pairs.md index 06bc33fe649..a03283e7da2 100644 --- a/docs/src/rules/accessor-pairs.md +++ b/docs/src/rules/accessor-pairs.md @@ -26,6 +26,7 @@ var o = { } }; + // Good var o = { set a(value) { @@ -58,6 +59,8 @@ This rule always checks object literals and property descriptors. By default, it Examples of **incorrect** code for the default `{ "setWithoutGet": true }` option: +:::incorrect + ```js /*eslint accessor-pairs: "error"*/ @@ -67,6 +70,7 @@ var o = { } }; + var o = {d: 1}; Object.defineProperty(o, 'c', { set: function(value) { @@ -75,8 +79,12 @@ Object.defineProperty(o, 'c', { }); ``` +::: + Examples of **correct** code for the default `{ "setWithoutGet": true }` option: +:::correct + ```js /*eslint accessor-pairs: "error"*/ @@ -101,10 +109,14 @@ Object.defineProperty(o, 'c', { ``` +::: + ### getWithoutSet Examples of **incorrect** code for the `{ "getWithoutSet": true }` option: +:::incorrect + ```js /*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/ @@ -135,8 +147,12 @@ Object.defineProperty(o, 'c', { }); ``` +::: + Examples of **correct** code for the `{ "getWithoutSet": true }` option: +:::correct + ```js /*eslint accessor-pairs: ["error", { "getWithoutSet": true }]*/ var o = { @@ -160,6 +176,8 @@ Object.defineProperty(o, 'c', { ``` +::: + ### enforceForClassMembers When `enforceForClassMembers` is set to `true` (default): @@ -169,6 +187,8 @@ When `enforceForClassMembers` is set to `true` (default): Examples of **incorrect** code for `{ "getWithoutSet": true, "enforceForClassMembers": true }`: +:::incorrect + ```js /*eslint accessor-pairs: ["error", { "getWithoutSet": true, "enforceForClassMembers": true }]*/ @@ -194,8 +214,12 @@ const Baz = class { } ``` +::: + Examples of **incorrect** code for `{ "setWithoutGet": true, "enforceForClassMembers": true }`: +:::incorrect + ```js /*eslint accessor-pairs: ["error", { "setWithoutGet": true, "enforceForClassMembers": true }]*/ @@ -212,10 +236,14 @@ const Bar = class { } ``` +::: + When `enforceForClassMembers` is set to `false`, this rule ignores classes. Examples of **correct** code for `{ "getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false }`: +:::correct + ```js /*eslint accessor-pairs: ["error", { "getWithoutSet": true, "setWithoutGet": true, "enforceForClassMembers": false @@ -246,6 +274,8 @@ const Quux = class { } ``` +::: + ## Known Limitations Due to the limits of static analysis, this rule does not account for possible side effects and in certain cases diff --git a/docs/src/rules/array-bracket-newline.md b/docs/src/rules/array-bracket-newline.md index a363f02e425..4d5446ef8fb 100644 --- a/docs/src/rules/array-bracket-newline.md +++ b/docs/src/rules/array-bracket-newline.md @@ -34,6 +34,8 @@ Or an object option (Requires line breaks if any of properties is satisfied. Oth Examples of **incorrect** code for this rule with the `"always"` option: +:::incorrect + ```js /*eslint array-bracket-newline: ["error", "always"]*/ @@ -47,8 +49,12 @@ var e = [function foo() { }]; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +:::correct + ```js /*eslint array-bracket-newline: ["error", "always"]*/ @@ -71,10 +77,14 @@ var e = [ ]; ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +:::incorrect + ```js /*eslint array-bracket-newline: ["error", "never"]*/ @@ -97,8 +107,12 @@ var e = [ ]; ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +:::correct + ```js /*eslint array-bracket-newline: ["error", "never"]*/ @@ -112,10 +126,14 @@ var e = [function foo() { }]; ``` +::: + ### consistent Examples of **incorrect** code for this rule with the `"consistent"` option: +:::incorrect + ```js /*eslint array-bracket-newline: ["error", "consistent"]*/ @@ -133,8 +151,12 @@ var d = [ }] ``` +::: + Examples of **correct** code for this rule with the `"consistent"` option: +:::correct + ```js /*eslint array-bracket-newline: ["error", "consistent"]*/ @@ -155,10 +177,14 @@ var f = [ ]; ``` +::: + ### multiline Examples of **incorrect** code for this rule with the default `{ "multiline": true }` option: +:::incorrect + ```js /*eslint array-bracket-newline: ["error", { "multiline": true }]*/ @@ -177,8 +203,12 @@ var e = [function foo() { }]; ``` +::: + Examples of **correct** code for this rule with the default `{ "multiline": true }` option: +:::correct + ```js /*eslint array-bracket-newline: ["error", { "multiline": true }]*/ @@ -196,10 +226,14 @@ var e = [ ]; ``` +::: + ### minItems Examples of **incorrect** code for this rule with the `{ "minItems": 2 }` option: +:::incorrect + ```js /*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/ @@ -218,8 +252,12 @@ var e = [ ]; ``` +::: + Examples of **correct** code for this rule with the `{ "minItems": 2 }` option: +:::correct + ```js /*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/ @@ -237,10 +275,14 @@ var e = [function foo() { }]; ``` +::: + ### multiline and minItems Examples of **incorrect** code for this rule with the `{ "multiline": true, "minItems": 2 }` options: +:::incorrect + ```js /*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/ @@ -257,8 +299,12 @@ var e = [function foo() { }]; ``` +::: + Examples of **correct** code for this rule with the `{ "multiline": true, "minItems": 2 }` options: +:::correct + ```js /*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/ @@ -278,6 +324,8 @@ var e = [ ]; ``` +::: + ## When Not To Use It If you don't want to enforce line breaks after opening and before closing array brackets, don't enable this rule. diff --git a/docs/src/rules/array-bracket-spacing.md b/docs/src/rules/array-bracket-spacing.md index 1caa2a6c107..46708632238 100644 --- a/docs/src/rules/array-bracket-spacing.md +++ b/docs/src/rules/array-bracket-spacing.md @@ -58,6 +58,8 @@ This rule has built-in exceptions: Examples of **incorrect** code for this rule with the default `"never"` option: +:::incorrect + ```js /*eslint array-bracket-spacing: ["error", "never"]*/ /*eslint-env es6*/ @@ -75,8 +77,12 @@ var [ x, ...y ] = z; var [ ,,x, ] = z; ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +:::correct + ```js /*eslint array-bracket-spacing: ["error", "never"]*/ /*eslint-env es6*/ @@ -102,10 +108,14 @@ var [x, ...y] = z; var [,,x,] = z; ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +:::incorrect + ```js /*eslint array-bracket-spacing: ["error", "always"]*/ /*eslint-env es6*/ @@ -126,8 +136,12 @@ var [x, ...y] = z; var [,,x,] = z; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +:::correct + ```js /*eslint array-bracket-spacing: ["error", "always"]*/ /*eslint-env es6*/ @@ -153,10 +167,14 @@ var [ x, ...y ] = z; var [ ,,x, ] = z; ``` +::: + ### singleValue Examples of **incorrect** code for this rule with the `"always", { "singleValue": false }` options: +:::incorrect + ```js /*eslint array-bracket-spacing: ["error", "always", { "singleValue": false }]*/ @@ -170,8 +188,12 @@ var foo = [ [ 1, 2 ] ]; var foo = [ { 'foo': 'bar' } ]; ``` +::: + Examples of **correct** code for this rule with the `"always", { "singleValue": false }` options: +:::correct + ```js /*eslint array-bracket-spacing: ["error", "always", { "singleValue": false }]*/ @@ -181,10 +203,14 @@ var foo = [[ 1, 1 ]]; var foo = [{ 'foo': 'bar' }]; ``` +::: + ### objectsInArrays Examples of **incorrect** code for this rule with the `"always", { "objectsInArrays": false }` options: +:::incorrect + ```js /*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/ @@ -194,8 +220,12 @@ var arr = [ { } ] ``` +::: + Examples of **correct** code for this rule with the `"always", { "objectsInArrays": false }` options: +:::correct + ```js /*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/ @@ -205,10 +235,14 @@ var arr = [{ }]; ``` +::: + ### arraysInArrays Examples of **incorrect** code for this rule with the `"always", { "arraysInArrays": false }` options: +:::incorrect + ```js /*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/ @@ -216,8 +250,12 @@ var arr = [ [ 1, 2 ], 2, 3, 4 ]; var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ]; ``` +::: + Examples of **correct** code for this rule with the `"always", { "arraysInArrays": false }` options: +:::correct + ```js /*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/ @@ -225,6 +263,8 @@ var arr = [[ 1, 2 ], 2, 3, 4 ]; var arr = [[ 1, 2 ], 2, [ 3, 4 ]]; ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing between array brackets. diff --git a/docs/src/rules/array-callback-return.md b/docs/src/rules/array-callback-return.md index a269a53b5f3..1c63c6f2766 100644 --- a/docs/src/rules/array-callback-return.md +++ b/docs/src/rules/array-callback-return.md @@ -40,6 +40,8 @@ This rule finds callback functions of the following methods, then checks usage o Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint array-callback-return: "error"*/ @@ -62,8 +64,12 @@ var bar = foo.filter(function(x) { }); ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint array-callback-return: "error"*/ @@ -82,6 +88,8 @@ var foo = Array.from(nodes, function(node) { var bar = foo.map(node => node.getAttribute("id")); ``` +::: + ## Options This rule accepts a configuration object with two options: @@ -93,6 +101,8 @@ This rule accepts a configuration object with two options: Examples of **correct** code for the `{ "allowImplicit": true }` option: +:::correct + ```js /*eslint array-callback-return: ["error", { allowImplicit: true }]*/ var undefAllTheThings = myArray.map(function(item) { @@ -100,10 +110,14 @@ var undefAllTheThings = myArray.map(function(item) { }); ``` +::: + ### checkForEach Examples of **incorrect** code for the `{ "checkForEach": true }` option: +:::incorrect + ```js /*eslint array-callback-return: ["error", { checkForEach: true }]*/ @@ -125,8 +139,12 @@ myArray.forEach(item => { }); ``` +::: + Examples of **correct** code for the `{ "checkForEach": true }` option: +:::correct + ```js /*eslint array-callback-return: ["error", { checkForEach: true }]*/ @@ -151,6 +169,8 @@ myArray.forEach(item => { }); ``` +::: + ## Known Limitations This rule checks callback functions of methods with the given names, *even if* the object which has the method is *not* an array. diff --git a/docs/src/rules/array-element-newline.md b/docs/src/rules/array-element-newline.md index 936f8fa9512..9224cfabc90 100644 --- a/docs/src/rules/array-element-newline.md +++ b/docs/src/rules/array-element-newline.md @@ -55,6 +55,8 @@ Alternatively, different configurations can be specified for array expressions a Examples of **incorrect** code for this rule with the default `"always"` option: +:::incorrect + ```js /*eslint array-element-newline: ["error", "always"]*/ @@ -74,8 +76,12 @@ var g = [ ]; ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +:::correct + ```js /*eslint array-element-newline: ["error", "always"]*/ @@ -101,10 +107,14 @@ var e = [ ]; ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +:::incorrect + ```js /*eslint array-element-newline: ["error", "never"]*/ @@ -127,8 +137,12 @@ var e = [ ]; ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +:::correct + ```js /*eslint array-element-newline: ["error", "never"]*/ @@ -150,10 +164,14 @@ var g = [ ]; ``` +::: + ### consistent Examples of **incorrect** code for this rule with the `"consistent"` option: +:::incorrect + ```js /*eslint array-element-newline: ["error", "consistent"]*/ @@ -173,8 +191,12 @@ var b = [ ]; ``` +::: + Examples of **correct** code for this rule with the `"consistent"` option: +:::correct + ```js /*eslint array-element-newline: ["error", "consistent"]*/ @@ -213,10 +235,14 @@ var h = [ ]; ``` +::: + ### multiline Examples of **incorrect** code for this rule with the `{ "multiline": true }` option: +:::incorrect + ```js /*eslint array-element-newline: ["error", { "multiline": true }]*/ @@ -231,8 +257,12 @@ var e = [ ]; ``` +::: + Examples of **correct** code for this rule with the `{ "multiline": true }` option: +:::correct + ```js /*eslint array-element-newline: ["error", { "multiline": true }]*/ @@ -250,10 +280,14 @@ var e = [ ]; ``` +::: + ### minItems Examples of **incorrect** code for this rule with the `{ "minItems": 3 }` option: +:::incorrect + ```js /*eslint array-element-newline: ["error", { "minItems": 3 }]*/ @@ -270,8 +304,12 @@ var e = [ ]; ``` +::: + Examples of **correct** code for this rule with the `{ "minItems": 3 }` option: +:::correct + ```js /*eslint array-element-newline: ["error", { "minItems": 3 }]*/ @@ -290,10 +328,14 @@ var e = [ ]; ``` +::: + ### multiline and minItems Examples of **incorrect** code for this rule with the `{ "multiline": true, "minItems": 3 }` options: +:::incorrect + ```js /*eslint array-element-newline: ["error", { "multiline": true, "minItems": 3 }]*/ @@ -309,8 +351,12 @@ var e = [ ]; ``` +::: + Examples of **correct** code for this rule with the `{ "multiline": true, "minItems": 3 }` options: +:::correct + ```js /*eslint array-element-newline: ["error", { "multiline": true, "minItems": 3 }]*/ @@ -330,10 +376,14 @@ var e = [ ]; ``` +::: + ### ArrayExpression and ArrayPattern Examples of **incorrect** code for this rule with the `{ "ArrayExpression": "always", "ArrayPattern": "never" }` options: +:::incorrect + ```js /*eslint array-element-newline: ["error", { "ArrayExpression": "always", "ArrayPattern": "never" }]*/ @@ -360,8 +410,12 @@ j = function bar() { }] = arr ``` +::: + Examples of **correct** code for this rule with the `{ "ArrayExpression": "always", "ArrayPattern": "never" }` options: +:::correct + ```js /*eslint array-element-newline: ["error", { "ArrayExpression": "always", "ArrayPattern": "never" }]*/ @@ -388,6 +442,8 @@ var [i = function foo() { }] = arr ``` +::: + ## When Not To Use It If you don't want to enforce linebreaks between array elements, don't enable this rule. diff --git a/docs/src/rules/arrow-body-style.md b/docs/src/rules/arrow-body-style.md index 68876696fce..bfba7bfee47 100644 --- a/docs/src/rules/arrow-body-style.md +++ b/docs/src/rules/arrow-body-style.md @@ -33,14 +33,20 @@ The second one is an object for more fine-grained configuration when the first o Examples of **incorrect** code for this rule with the `"always"` option: +:::incorrect + ```js /*eslint arrow-body-style: ["error", "always"]*/ /*eslint-env es6*/ let foo = () => 0; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +:::correct + ```js let foo = () => { return 0; @@ -51,10 +57,14 @@ let foo = (retv, name) => { }; ``` +::: + ### as-needed Examples of **incorrect** code for this rule with the default `"as-needed"` option: +:::incorrect + ```js /*eslint arrow-body-style: ["error", "as-needed"]*/ /*eslint-env es6*/ @@ -72,8 +82,12 @@ let foo = () => { }; ``` +::: + Examples of **correct** code for this rule with the default `"as-needed"` option: +:::correct + ```js /*eslint arrow-body-style: ["error", "as-needed"]*/ /*eslint-env es6*/ @@ -98,12 +112,16 @@ let foo = () => { let foo = () => ({ bar: 0 }); ``` +::: + #### requireReturnForObjectLiteral > This option is only applicable when used in conjunction with the `"as-needed"` option. Examples of **incorrect** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option: +:::incorrect + ```js /*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/ /*eslint-env es6*/ @@ -111,8 +129,12 @@ let foo = () => ({}); let foo = () => ({ bar: 0 }); ``` +::: + Examples of **correct** code for this rule with the `{ "requireReturnForObjectLiteral": true }` option: +:::correct + ```js /*eslint arrow-body-style: ["error", "as-needed", { "requireReturnForObjectLiteral": true }]*/ /*eslint-env es6*/ @@ -121,10 +143,14 @@ let foo = () => {}; let foo = () => { return { bar: 0 }; }; ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +:::incorrect + ```js /*eslint arrow-body-style: ["error", "never"]*/ /*eslint-env es6*/ @@ -138,8 +164,12 @@ let foo = (retv, name) => { }; ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +:::correct + ```js /*eslint arrow-body-style: ["error", "never"]*/ /*eslint-env es6*/ @@ -147,3 +177,5 @@ Examples of **correct** code for this rule with the `"never"` option: let foo = () => 0; let foo = () => ({ foo: 0 }); ``` + +::: diff --git a/docs/src/rules/arrow-parens.md b/docs/src/rules/arrow-parens.md index 522034aacee..97329156197 100644 --- a/docs/src/rules/arrow-parens.md +++ b/docs/src/rules/arrow-parens.md @@ -72,6 +72,8 @@ Object properties for variants of the `"as-needed"` option: Examples of **incorrect** code for this rule with the default `"always"` option: +:::incorrect + ```js /*eslint arrow-parens: ["error", "always"]*/ /*eslint-env es6*/ @@ -84,8 +86,12 @@ a.then(foo => a); a(foo => { if (true) {} }); ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +:::correct + ```js /*eslint arrow-parens: ["error", "always"]*/ /*eslint-env es6*/ @@ -98,6 +104,8 @@ a.then((foo) => {}); a.then((foo) => { if (true) {} }); ``` +::: + #### If Statements One of the benefits of this option is that it prevents the incorrect use of arrow functions in conditionals: @@ -159,6 +167,8 @@ var f = (a) => b ? c: d; Examples of **incorrect** code for this rule with the `"as-needed"` option: +:::incorrect + ```js /*eslint arrow-parens: ["error", "as-needed"]*/ /*eslint-env es6*/ @@ -174,8 +184,12 @@ const g = /* comment */ (a) => a + a; const h = (a) /* comment */ => a + a; ``` +::: + Examples of **correct** code for this rule with the `"as-needed"` option: +:::correct + ```js /*eslint arrow-parens: ["error", "as-needed"]*/ /*eslint-env es6*/ @@ -195,10 +209,14 @@ const g = (/* comment */ a) => a + a; const h = (a /* comment */) => a + a; ``` +::: + ### requireForBlockBody Examples of **incorrect** code for the `{ "requireForBlockBody": true }` option: +:::incorrect + ```js /*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/ /*eslint-env es6*/ @@ -213,8 +231,12 @@ a.map(x => { a.then(foo => {}); ``` +::: + Examples of **correct** code for the `{ "requireForBlockBody": true }` option: +:::correct + ```js /*eslint arrow-parens: [2, "as-needed", { "requireForBlockBody": true }]*/ /*eslint-env es6*/ @@ -232,3 +254,5 @@ a((foo) => { if (true) {} }); ([a, b]) => a; ({a, b}) => a; ``` + +::: diff --git a/docs/src/rules/arrow-spacing.md b/docs/src/rules/arrow-spacing.md index c6453d11e6d..05c6d3a59dd 100644 --- a/docs/src/rules/arrow-spacing.md +++ b/docs/src/rules/arrow-spacing.md @@ -31,6 +31,8 @@ The default configuration is `{ "before": true, "after": true }`. Examples of **incorrect** code for this rule with the default `{ "before": true, "after": true }` option: +:::incorrect + ```js /*eslint arrow-spacing: "error"*/ /*eslint-env es6*/ @@ -45,8 +47,12 @@ a=> a; () =>{'\n'}; ``` +::: + Examples of **correct** code for this rule with the default `{ "before": true, "after": true }` option: +:::correct + ```js /*eslint arrow-spacing: "error"*/ /*eslint-env es6*/ @@ -57,8 +63,12 @@ a => a; () => {'\n'}; ``` +::: + Examples of **incorrect** code for this rule with the `{ "before": false, "after": false }` option: +:::incorrect + ```js /*eslint arrow-spacing: ["error", { "before": false, "after": false }]*/ /*eslint-env es6*/ @@ -68,8 +78,12 @@ Examples of **incorrect** code for this rule with the `{ "before": false, "after ()=> {'\n'}; ``` +::: + Examples of **correct** code for this rule with the `{ "before": false, "after": false }` option: +:::correct + ```js /*eslint arrow-spacing: ["error", { "before": false, "after": false }]*/ /*eslint-env es6*/ @@ -79,8 +93,12 @@ Examples of **correct** code for this rule with the `{ "before": false, "after": ()=>{'\n'}; ``` +::: + Examples of **incorrect** code for this rule with the `{ "before": false, "after": true }` option: +:::incorrect + ```js /*eslint arrow-spacing: ["error", { "before": false, "after": true }]*/ /*eslint-env es6*/ @@ -90,8 +108,12 @@ Examples of **incorrect** code for this rule with the `{ "before": false, "after ()=>{'\n'}; ``` +::: + Examples of **correct** code for this rule with the `{ "before": false, "after": true }` option: +:::correct + ```js /*eslint arrow-spacing: ["error", { "before": false, "after": true }]*/ /*eslint-env es6*/ @@ -100,3 +122,5 @@ Examples of **correct** code for this rule with the `{ "before": false, "after": (a)=> {}; ()=> {'\n'}; ``` + +::: diff --git a/docs/src/rules/block-scoped-var.md b/docs/src/rules/block-scoped-var.md index a9131e6045e..f9fc1926d7e 100644 --- a/docs/src/rules/block-scoped-var.md +++ b/docs/src/rules/block-scoped-var.md @@ -18,6 +18,8 @@ This rule aims to reduce the usage of variables outside of their binding context Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint block-scoped-var: "error"*/ @@ -62,8 +64,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint block-scoped-var: "error"*/ @@ -114,3 +120,5 @@ class C { } } ``` + +::: diff --git a/docs/src/rules/block-spacing.md b/docs/src/rules/block-spacing.md index 76ca2e61273..01679467265 100644 --- a/docs/src/rules/block-spacing.md +++ b/docs/src/rules/block-spacing.md @@ -27,6 +27,8 @@ This rule has a string option: Examples of **incorrect** code for this rule with the default `"always"` option: +:::incorrect + ```js /*eslint block-spacing: "error"*/ @@ -41,8 +43,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +:::correct + ```js /*eslint block-spacing: "error"*/ @@ -54,10 +60,14 @@ class C { } ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +:::incorrect + ```js /*eslint block-spacing: ["error", "never"]*/ @@ -69,8 +79,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +:::correct + ```js /*eslint block-spacing: ["error", "never"]*/ @@ -82,6 +96,8 @@ class C { } ``` +::: + ## When Not To Use It If you don't want to be notified about spacing style inside of blocks, you can safely disable this rule. diff --git a/docs/src/rules/brace-style.md b/docs/src/rules/brace-style.md index 477d866d93d..a6c83cefd57 100644 --- a/docs/src/rules/brace-style.md +++ b/docs/src/rules/brace-style.md @@ -72,6 +72,8 @@ This rule has an object option for an exception: Examples of **incorrect** code for this rule with the default `"1tbs"` option: +:::incorrect + ```js /*eslint brace-style: "error"*/ @@ -109,8 +111,12 @@ class C } ``` +::: + Examples of **correct** code for this rule with the default `"1tbs"` option: +:::correct + ```js /*eslint brace-style: "error"*/ @@ -145,8 +151,12 @@ if (foo) bar(); else if (baz) boom(); ``` +::: + Examples of **correct** code for this rule with the `"1tbs", { "allowSingleLine": true }` options: +:::correct + ```js /*eslint brace-style: ["error", "1tbs", { "allowSingleLine": true }]*/ @@ -186,10 +196,14 @@ class C { class D { static { foo(); } } ``` +::: + ### stroustrup Examples of **incorrect** code for this rule with the `"stroustrup"` option: +:::incorrect + ```js /*eslint brace-style: ["error", "stroustrup"]*/ @@ -226,8 +240,12 @@ if (foo) { } ``` +::: + Examples of **correct** code for this rule with the `"stroustrup"` option: +:::correct + ```js /*eslint brace-style: ["error", "stroustrup"]*/ @@ -264,8 +282,12 @@ if (foo) bar(); else if (baz) boom(); ``` +::: + Examples of **correct** code for this rule with the `"stroustrup", { "allowSingleLine": true }` options: +:::correct + ```js /*eslint brace-style: ["error", "stroustrup", { "allowSingleLine": true }]*/ @@ -286,10 +308,14 @@ class C { class D { static { foo(); } } ``` +::: + ### allman Examples of **incorrect** code for this rule with the `"allman"` option: +:::incorrect + ```js /*eslint brace-style: ["error", "allman"]*/ @@ -322,8 +348,12 @@ if (foo) { } ``` +::: + Examples of **correct** code for this rule with the `"allman"` option: +:::correct + ```js /*eslint brace-style: ["error", "allman"]*/ @@ -368,8 +398,12 @@ if (foo) bar(); else if (baz) boom(); ``` +::: + Examples of **correct** code for this rule with the `"allman", { "allowSingleLine": true }` options: +:::correct + ```js /*eslint brace-style: ["error", "allman", { "allowSingleLine": true }]*/ @@ -394,6 +428,8 @@ class C class D { static { foo(); } } ``` +::: + ## When Not To Use It If you don't want to enforce a particular brace style, don't enable this rule. diff --git a/docs/src/rules/callback-return.md b/docs/src/rules/callback-return.md index 87a927ec98b..57d671d216f 100644 --- a/docs/src/rules/callback-return.md +++ b/docs/src/rules/callback-return.md @@ -44,6 +44,8 @@ The rule takes a single option - an array of possible callback names - which may Examples of **incorrect** code for this rule with the default `["callback", "cb", "next"]` option: +:::incorrect + ```js /*eslint callback-return: "error"*/ @@ -55,8 +57,12 @@ function foo(err, callback) { } ``` +::: + Examples of **correct** code for this rule with the default `["callback", "cb", "next"]` option: +:::correct + ```js /*eslint callback-return: "error"*/ @@ -68,10 +74,14 @@ function foo(err, callback) { } ``` +::: + ### Supplied callback names Examples of **incorrect** code for this rule with the option `["done", "send.error", "send.success"]`: +:::incorrect + ```js /*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/ @@ -90,8 +100,12 @@ function bar(err, send) { } ``` +::: + Examples of **correct** code for this rule with the option `["done", "send.error", "send.success"]`: +:::correct + ```js /*eslint callback-return: ["error", ["done", "send.error", "send.success"]]*/ @@ -110,6 +124,8 @@ function bar(err, send) { } ``` +::: + ## Known Limitations Because it is difficult to understand the meaning of a program through static analysis, this rule has limitations: diff --git a/docs/src/rules/camelcase.md b/docs/src/rules/camelcase.md index 83843c34bd2..fc338debb84 100644 --- a/docs/src/rules/camelcase.md +++ b/docs/src/rules/camelcase.md @@ -31,6 +31,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "properties": "always" }` option: +:::incorrect + ```js /*eslint camelcase: "error"*/ @@ -69,8 +71,12 @@ var { foo: no_camelcased } = bar; var { foo: bar_baz = 1 } = quz; ``` +::: + Examples of **correct** code for this rule with the default `{ "properties": "always" }` option: +:::correct + ```js /*eslint camelcase: "error"*/ @@ -109,10 +115,14 @@ var { foo: isCamelCased = 1 } = quz; ``` +::: + ### properties: "never" Examples of **correct** code for this rule with the `{ "properties": "never" }` option: +:::correct + ```js /*eslint camelcase: ["error", {properties: "never"}]*/ @@ -121,10 +131,14 @@ var obj = { }; ``` +::: + ### ignoreDestructuring: false Examples of **incorrect** code for this rule with the default `{ "ignoreDestructuring": false }` option: +:::incorrect + ```js /*eslint camelcase: "error"*/ @@ -139,10 +153,14 @@ var { category_id: category_alias } = query; var { category_id: categoryId, ...other_props } = query; ``` +::: + ### ignoreDestructuring: true Examples of **incorrect** code for this rule with the `{ "ignoreDestructuring": true }` option: +:::incorrect + ```js /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/ @@ -151,8 +169,12 @@ var { category_id: category_alias } = query; var { category_id, ...other_props } = query; ``` +::: + Examples of **correct** code for this rule with the `{ "ignoreDestructuring": true }` option: +:::correct + ```js /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/ @@ -163,10 +185,14 @@ var { category_id = 1 } = query; var { category_id: category_id } = query; ``` +::: + Please note that this option applies only to identifiers inside destructuring patterns. It doesn't additionally allow any particular use of the created variables later in the code apart from the use that is already allowed by default or by other options. Examples of additional **incorrect** code for this rule with the `{ "ignoreDestructuring": true }` option: +:::incorrect + ```js /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/ @@ -174,10 +200,14 @@ var { some_property } = obj; // allowed by {ignoreDestructuring: true} var foo = some_property + 1; // error, ignoreDestructuring does not apply to this statement ``` +::: + A common use case for this option is to avoid useless renaming when the identifier is not intended to be used later in the code. Examples of additional **correct** code for this rule with the `{ "ignoreDestructuring": true }` option: +:::correct + ```js /*eslint camelcase: ["error", {ignoreDestructuring: true}]*/ @@ -185,10 +215,14 @@ var { some_property, ...rest } = obj; // do something with 'rest', nothing with 'some_property' ``` +::: + Another common use case for this option is in combination with `{ "properties": "never" }`, when the identifier is intended to be used only as a property shorthand. Examples of additional **correct** code for this rule with the `{ "properties": "never", "ignoreDestructuring": true }` options: +:::correct + ```js /*eslint camelcase: ["error", {"properties": "never", ignoreDestructuring: true}]*/ @@ -196,20 +230,28 @@ var { some_property } = obj; doSomething({ some_property }); ``` +::: + ### ignoreImports: false Examples of **incorrect** code for this rule with the default `{ "ignoreImports": false }` option: +:::incorrect + ```js /*eslint camelcase: "error"*/ import { snake_cased } from 'mod'; ``` +::: + ### ignoreImports: true Examples of **incorrect** code for this rule with the `{ "ignoreImports": true }` option: +:::incorrect + ```js /*eslint camelcase: ["error", {ignoreImports: true}]*/ @@ -218,18 +260,26 @@ import default_import from 'mod'; import * as namespaced_import from 'mod'; ``` +::: + Examples of **correct** code for this rule with the `{ "ignoreImports": true }` option: +:::correct + ```js /*eslint camelcase: ["error", {ignoreImports: true}]*/ import { snake_cased } from 'mod'; ``` +::: + ### ignoreGlobals: false Examples of **incorrect** code for this rule with the default `{ "ignoreGlobals": false }` option: +:::incorrect + ```js /*eslint camelcase: ["error", {ignoreGlobals: false}]*/ /* global no_camelcased */ @@ -237,10 +287,14 @@ Examples of **incorrect** code for this rule with the default `{ "ignoreGlobals" const foo = no_camelcased; ``` +::: + ### ignoreGlobals: true Examples of **correct** code for this rule with the `{ "ignoreGlobals": true }` option: +:::correct + ```js /*eslint camelcase: ["error", {ignoreGlobals: true}]*/ /* global no_camelcased */ @@ -248,10 +302,14 @@ Examples of **correct** code for this rule with the `{ "ignoreGlobals": true }` const foo = no_camelcased; ``` +::: + ### allow Examples of **correct** code for this rule with the `allow` option: +:::correct + ```js /*eslint camelcase: ["error", {allow: ["UNSAFE_componentWillMount"]}]*/ @@ -260,6 +318,8 @@ function UNSAFE_componentWillMount() { } ``` +::: + ```js /*eslint camelcase: ["error", {allow: ["^UNSAFE_"]}]*/ diff --git a/docs/src/rules/capitalized-comments.md b/docs/src/rules/capitalized-comments.md index 601de8864e2..9513051eaad 100644 --- a/docs/src/rules/capitalized-comments.md +++ b/docs/src/rules/capitalized-comments.md @@ -21,6 +21,8 @@ By default, this rule will require a non-lowercase letter at the beginning of co Examples of **incorrect** code for this rule: +:::incorrect + ```js /* eslint capitalized-comments: ["error"] */ @@ -28,8 +30,12 @@ Examples of **incorrect** code for this rule: ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js // Capitalized comment @@ -54,6 +60,8 @@ Examples of **correct** code for this rule: ``` +::: + ### Options This rule has two options: a string value `"always"` or `"never"` which determines whether capitalization of the first word of a comment should be required or forbidden, and optionally an object containing more configuration parameters for the rule. @@ -88,6 +96,8 @@ Note that configuration comments and comments which start with URLs are never re Examples of **incorrect** code for this rule: +:::incorrect + ```js /* eslint capitalized-comments: ["error", "always"] */ @@ -95,8 +105,12 @@ Examples of **incorrect** code for this rule: ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /* eslint capitalized-comments: ["error", "always"] */ @@ -122,12 +136,16 @@ Examples of **correct** code for this rule: ``` +::: + #### `"never"` Using the `"never"` option means that this rule will report any comments which start with an uppercase letter. Examples of **incorrect** code with the `"never"` option: +:::incorrect + ```js /* eslint capitalized-comments: ["error", "never"] */ @@ -135,8 +153,12 @@ Examples of **incorrect** code with the `"never"` option: ``` +::: + Examples of **correct** code with the `"never"` option: +:::correct + ```js /* eslint capitalized-comments: ["error", "never"] */ @@ -148,12 +170,16 @@ Examples of **correct** code with the `"never"` option: ``` +::: + #### `ignorePattern` The `ignorePattern` object takes a string value, which is used as a regular expression applied to the first word of a comment. Examples of **correct** code with the `"ignorePattern"` option set to `"pragma"`: +:::correct + ```js /* eslint capitalized-comments: ["error", "always", { "ignorePattern": "pragma" }] */ @@ -163,12 +189,16 @@ function foo() { ``` +::: + #### `ignoreInlineComments` Setting the `ignoreInlineComments` option to `true` means that comments in the middle of code (with a token on the same line as the beginning of the comment, and another token on the same line as the end of the comment) will not be reported by this rule. Examples of **correct** code with the `"ignoreInlineComments"` option set to `true`: +:::correct + ```js /* eslint capitalized-comments: ["error", "always", { "ignoreInlineComments": true }] */ @@ -177,12 +207,16 @@ function foo(/* ignored */ a) { ``` +::: + #### `ignoreConsecutiveComments` If the `ignoreConsecutiveComments` option is set to `true`, then comments which otherwise violate the rule will not be reported as long as they immediately follow another comment. This can be applied more than once. Examples of **correct** code with `ignoreConsecutiveComments` set to `true`: +:::correct + ```js /* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */ @@ -197,8 +231,12 @@ Examples of **correct** code with `ignoreConsecutiveComments` set to `true`: */ ``` +::: + Examples of **incorrect** code with `ignoreConsecutiveComments` set to `true`: +:::incorrect + ```js /* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */ @@ -206,6 +244,8 @@ Examples of **incorrect** code with `ignoreConsecutiveComments` set to `true`: // this comment does NOT get reported, since it is a consecutive comment. ``` +::: + ### Using Different Options for Line and Block Comments If you wish to have a different configuration for line comments and block comments, you can do so by using two different object configurations (note that the capitalization option will be enforced consistently for line and block comments): @@ -230,6 +270,8 @@ If you wish to have a different configuration for line comments and block commen Examples of **incorrect** code with different line and block comment configuration: +:::incorrect + ```js /* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */ @@ -238,8 +280,12 @@ Examples of **incorrect** code with different line and block comment configurati ``` +::: + Examples of **correct** code with different line and block comment configuration: +:::correct + ```js /* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */ @@ -248,6 +294,8 @@ Examples of **correct** code with different line and block comment configuration ``` +::: + ## When Not To Use It This rule can be disabled if you do not care about the grammatical style of comments in your codebase. diff --git a/docs/src/rules/class-methods-use-this.md b/docs/src/rules/class-methods-use-this.md index b02cf487ed2..152038360be 100644 --- a/docs/src/rules/class-methods-use-this.md +++ b/docs/src/rules/class-methods-use-this.md @@ -61,6 +61,8 @@ This rule is aimed to flag class methods that do not use `this`. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint class-methods-use-this: "error"*/ /*eslint-env es6*/ @@ -72,8 +74,12 @@ class A { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint class-methods-use-this: "error"*/ /*eslint-env es6*/ @@ -100,6 +106,8 @@ class A { } ``` +::: + ## Options This rule has two options: @@ -117,6 +125,8 @@ The `exceptMethods` option allows you to pass an array of method names for which Examples of **incorrect** code for this rule when used without exceptMethods: +::: incorrect + ```js /*eslint class-methods-use-this: "error"*/ @@ -126,8 +136,12 @@ class A { } ``` +::: + Examples of **correct** code for this rule when used with exceptMethods: +::: correct + ```js /*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */ @@ -139,6 +153,8 @@ class A { } ``` +::: + ### enforceForClassFields ```js @@ -149,6 +165,8 @@ The `enforceForClassFields` option enforces that arrow functions and function ex Examples of **incorrect** code for this rule with the `{ "enforceForClassFields": true }` option (default): +::: incorrect + ```js /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */ @@ -157,8 +175,12 @@ class A { } ``` +::: + Examples of **correct** code for this rule with the `{ "enforceForClassFields": true }` option (default): +::: correct + ```js /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */ @@ -167,8 +189,12 @@ class A { } ``` +::: + Examples of **correct** code for this rule with the `{ "enforceForClassFields": false }` option: +::: correct + ```js /*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */ @@ -176,3 +202,5 @@ class A { foo = () => {} } ``` + +::: diff --git a/docs/src/rules/comma-dangle.md b/docs/src/rules/comma-dangle.md index a40f1cb76ca..b44a49728cd 100644 --- a/docs/src/rules/comma-dangle.md +++ b/docs/src/rules/comma-dangle.md @@ -82,6 +82,8 @@ The default for each option is `"never"` unless otherwise specified. Examples of **incorrect** code for this rule with the default `"never"` option: +:::incorrect + ```js /*eslint comma-dangle: ["error", "never"]*/ @@ -98,8 +100,12 @@ foo({ }); ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +:::correct + ```js /*eslint comma-dangle: ["error", "never"]*/ @@ -116,10 +122,14 @@ foo({ }); ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +:::incorrect + ```js /*eslint comma-dangle: ["error", "always"]*/ @@ -136,8 +146,12 @@ foo({ }); ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +:::correct + ```js /*eslint comma-dangle: ["error", "always"]*/ @@ -154,10 +168,14 @@ foo({ }); ``` +::: + ### always-multiline Examples of **incorrect** code for this rule with the `"always-multiline"` option: +:::incorrect + ```js /*eslint comma-dangle: ["error", "always-multiline"]*/ @@ -184,8 +202,12 @@ foo({ }); ``` +::: + Examples of **correct** code for this rule with the `"always-multiline"` option: +:::correct + ```js /*eslint comma-dangle: ["error", "always-multiline"]*/ @@ -211,10 +233,14 @@ foo({ }); ``` +::: + ### only-multiline Examples of **incorrect** code for this rule with the `"only-multiline"` option: +:::incorrect + ```js /*eslint comma-dangle: ["error", "only-multiline"]*/ @@ -227,8 +253,12 @@ var arr = [1, ``` +::: + Examples of **correct** code for this rule with the `"only-multiline"` option: +:::correct + ```js /*eslint comma-dangle: ["error", "only-multiline"]*/ @@ -269,10 +299,14 @@ foo({ }); ``` +::: + ### functions Examples of **incorrect** code for this rule with the `{"functions": "never"}` option: +:::incorrect + ```js /*eslint comma-dangle: ["error", {"functions": "never"}]*/ @@ -283,8 +317,12 @@ foo(a, b,); new foo(a, b,); ``` +::: + Examples of **correct** code for this rule with the `{"functions": "never"}` option: +:::correct + ```js /*eslint comma-dangle: ["error", {"functions": "never"}]*/ @@ -295,8 +333,12 @@ foo(a, b); new foo(a, b); ``` +::: + Examples of **incorrect** code for this rule with the `{"functions": "always"}` option: +:::incorrect + ```js /*eslint comma-dangle: ["error", {"functions": "always"}]*/ @@ -307,8 +349,12 @@ foo(a, b); new foo(a, b); ``` +::: + Examples of **correct** code for this rule with the `{"functions": "always"}` option: +:::correct + ```js /*eslint comma-dangle: ["error", {"functions": "always"}]*/ @@ -319,6 +365,8 @@ foo(a, b,); new foo(a, b,); ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with dangling commas. diff --git a/docs/src/rules/comma-spacing.md b/docs/src/rules/comma-spacing.md index 55eb9ccdcde..c64c6068257 100644 --- a/docs/src/rules/comma-spacing.md +++ b/docs/src/rules/comma-spacing.md @@ -50,6 +50,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "before": false, "after": true }` options: +:::incorrect + ```js /*eslint comma-spacing: ["error", { "before": false, "after": true }]*/ @@ -62,8 +64,12 @@ function foo(a ,b){} a ,b ``` +::: + Examples of **correct** code for this rule with the default `{ "before": false, "after": true }` options: +:::correct + ```js /*eslint comma-spacing: ["error", { "before": false, "after": true }]*/ @@ -78,8 +84,12 @@ function foo(a, b){} a, b ``` +::: + Example of **correct** code for this rule with initial null element for the default `{ "before": false, "after": true }` options: +:::correct + ```js /*eslint comma-spacing: ["error", { "before": false, "after": true }]*/ /*eslint array-bracket-spacing: ["error", "always"]*/ @@ -87,10 +97,14 @@ Example of **correct** code for this rule with initial null element for the defa var arr = [ , 2, 3 ] ``` +::: + ### before Examples of **incorrect** code for this rule with the `{ "before": true, "after": false }` options: +:::incorrect + ```js /*eslint comma-spacing: ["error", { "before": true, "after": false }]*/ @@ -102,8 +116,12 @@ function foo(a,b){} a, b ``` +::: + Examples of **correct** code for this rule with the `{ "before": true, "after": false }` options: +:::correct + ```js /*eslint comma-spacing: ["error", { "before": true, "after": false }]*/ @@ -118,8 +136,12 @@ function foo(a ,b){} a ,b ``` +::: + Examples of **correct** code for this rule with initial null element for the `{ "before": true, "after": false }` options: +:::correct + ```js /*eslint comma-spacing: ["error", { "before": true, "after": false }]*/ /*eslint array-bracket-spacing: ["error", "never"]*/ @@ -127,6 +149,8 @@ Examples of **correct** code for this rule with initial null element for the `{ var arr = [,2 ,3] ``` +::: + ## When Not To Use It If your project will not be following a consistent comma-spacing pattern, turn this rule off. diff --git a/docs/src/rules/comma-style.md b/docs/src/rules/comma-style.md index e72c59b79f5..eb455f334b3 100644 --- a/docs/src/rules/comma-style.md +++ b/docs/src/rules/comma-style.md @@ -58,6 +58,8 @@ A way to determine the node types as defined by [ESTree](https://github.com/estr Examples of **incorrect** code for this rule with the default `"last"` option: +:::incorrect + ```js /*eslint comma-style: ["error", "last"]*/ @@ -79,8 +81,12 @@ function bar() { } ``` +::: + Examples of **correct** code for this rule with the default `"last"` option: +:::correct + ```js /*eslint comma-style: ["error", "last"]*/ @@ -100,10 +106,14 @@ function bar() { } ``` +::: + ### first Examples of **incorrect** code for this rule with the `"first"` option: +:::incorrect + ```js /*eslint comma-style: ["error", "first"]*/ @@ -121,8 +131,12 @@ function bar() { } ``` +::: + Examples of **correct** code for this rule with the `"first"` option: +:::correct + ```js /*eslint comma-style: ["error", "first"]*/ @@ -142,12 +156,16 @@ function bar() { } ``` +::: + ### exceptions An example use case is to enforce comma style *only* in var statements. Examples of **incorrect** code for this rule with sample `"first", { "exceptions": { … } }` options: +:::incorrect + ```js /*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/ @@ -155,8 +173,12 @@ var o = {}, a = []; ``` +::: + Examples of **correct** code for this rule with sample `"first", { "exceptions": { … } }` options: +:::correct + ```js /*eslint comma-style: ["error", "first", { "exceptions": { "ArrayExpression": true, "ObjectExpression": true } }]*/ @@ -166,6 +188,8 @@ var o = {fst:1, , a = []; ``` +::: + ## When Not To Use It This rule can safely be turned off if your project does not care about enforcing a consistent comma style. diff --git a/docs/src/rules/complexity.md b/docs/src/rules/complexity.md index e58e75415e7..72b40d83608 100644 --- a/docs/src/rules/complexity.md +++ b/docs/src/rules/complexity.md @@ -41,6 +41,8 @@ This rule is aimed at reducing code complexity by capping the amount of cyclomat Examples of **incorrect** code for a maximum of 2: +::: incorrect + ```js /*eslint complexity: ["error", 2]*/ @@ -60,8 +62,12 @@ function b() { } ``` +::: + Examples of **correct** code for a maximum of 2: +::: correct + ```js /*eslint complexity: ["error", 2]*/ @@ -78,10 +84,14 @@ function b() { } ``` +::: + Class field initializers and class static blocks are implicit functions. Therefore, their complexity is calculated separately for each initializer and each static block, and it doesn't contribute to the complexity of the enclosing code. Examples of additional **incorrect** code for a maximum of 2: +::: incorrect + ```js /*eslint complexity: ["error", 2]*/ @@ -98,8 +108,12 @@ class D { // this static block has complexity = 3 } ``` +::: + Examples of additional **correct** code for a maximum of 2: +::: correct + ```js /*eslint complexity: ["error", 2]*/ @@ -125,6 +139,8 @@ function foo() { // this function has complexity = 1 } ``` +::: + ## Options Optionally, you may specify a `max` object property: diff --git a/docs/src/rules/computed-property-spacing.md b/docs/src/rules/computed-property-spacing.md index e0546c6dbce..99999b8827d 100644 --- a/docs/src/rules/computed-property-spacing.md +++ b/docs/src/rules/computed-property-spacing.md @@ -57,6 +57,8 @@ Object option: Examples of **incorrect** code for this rule with the default `"never"` option: +::: incorrect + ```js /*eslint computed-property-spacing: ["error", "never"]*/ /*eslint-env es6*/ @@ -70,8 +72,12 @@ const { [ a ]: someProp } = obj; ({ [ b ]: anotherProp } = anotherObj); ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +::: correct + ```js /*eslint computed-property-spacing: ["error", "never"]*/ /*eslint-env es6*/ @@ -85,10 +91,14 @@ const { [a]: someProp } = obj; ({ [b]: anotherProp } = anotherObj); ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint computed-property-spacing: ["error", "always"]*/ /*eslint-env es6*/ @@ -103,8 +113,12 @@ const { [a]: someProp } = obj; ({ [b ]: anotherProp } = anotherObj); ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint computed-property-spacing: ["error", "always"]*/ /*eslint-env es6*/ @@ -117,12 +131,16 @@ const { [ a ]: someProp } = obj; ({ [ b ]: anotherProp } = anotherObj); ``` +::: + #### enforceForClassMembers With `enforceForClassMembers` set to `true` (default), the rule also disallows/enforces spaces inside of computed keys of class methods, getters and setters. Examples of **incorrect** code for this rule with `"never"` and `{ "enforceForClassMembers": true }` (default): +::: incorrect + ```js /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/ /*eslint-env es6*/ @@ -141,8 +159,12 @@ const Bar = class { } ``` +::: + Examples of **correct** code for this rule with `"never"` and `{ "enforceForClassMembers": true }` (default): +::: correct + ```js /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/ /*eslint-env es6*/ @@ -161,8 +183,12 @@ const Bar = class { } ``` +::: + Examples of **correct** code for this rule with `"never"` and `{ "enforceForClassMembers": false }`: +::: correct + ```js /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": false }]*/ /*eslint-env es6*/ @@ -181,6 +207,8 @@ const Bar = class { } ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of computed properties. diff --git a/docs/src/rules/consistent-return.md b/docs/src/rules/consistent-return.md index 76cf86f0ba2..8f54ef0a96e 100644 --- a/docs/src/rules/consistent-return.md +++ b/docs/src/rules/consistent-return.md @@ -38,6 +38,8 @@ This rule requires `return` statements to either always or never specify values. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint consistent-return: "error"*/ @@ -56,8 +58,12 @@ function doSomething(condition) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint consistent-return: "error"*/ @@ -78,6 +84,8 @@ function Foo() { } ``` +::: + ## Options This rule has an object option: @@ -89,6 +97,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "treatUndefinedAsUnspecified": false }` option: +::: incorrect + ```js /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": false }]*/ @@ -107,8 +117,12 @@ function bar(condition) { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "treatUndefinedAsUnspecified": true }` option: +::: incorrect + ```js /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/ @@ -127,8 +141,12 @@ function bar(condition) { } ``` +::: + Examples of **correct** code for this rule with the `{ "treatUndefinedAsUnspecified": true }` option: +::: correct + ```js /*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/ @@ -147,6 +165,8 @@ function bar(condition) { } ``` +::: + ## When Not To Use It If you want to allow functions to have different `return` behavior depending on code branching, then it is safe to disable this rule. diff --git a/docs/src/rules/consistent-this.md b/docs/src/rules/consistent-this.md index 18ff87ea1eb..998bef0b074 100644 --- a/docs/src/rules/consistent-this.md +++ b/docs/src/rules/consistent-this.md @@ -34,6 +34,8 @@ This rule has one or more string options: Examples of **incorrect** code for this rule with the default `"that"` option: +::: incorrect + ```js /*eslint consistent-this: ["error", "that"]*/ @@ -46,8 +48,12 @@ that = 42; self = this; ``` +::: + Examples of **correct** code for this rule with the default `"that"` option: +::: correct + ```js /*eslint consistent-this: ["error", "that"]*/ @@ -62,8 +68,12 @@ that = this; foo.bar = this; ``` +::: + Examples of **incorrect** code for this rule with the default `"that"` option, if the variable is not initialized: +::: incorrect + ```js /*eslint consistent-this: ["error", "that"]*/ @@ -73,8 +83,12 @@ function f() { } ``` +::: + Examples of **correct** code for this rule with the default `"that"` option, if the variable is not initialized: +::: correct + ```js /*eslint consistent-this: ["error", "that"]*/ @@ -86,6 +100,8 @@ foo = 42; that = this; ``` +::: + ## When Not To Use It If you need to capture nested context, `consistent-this` is going to be problematic. Code of that nature is usually difficult to read and maintain and you should consider refactoring it. diff --git a/docs/src/rules/constructor-super.md b/docs/src/rules/constructor-super.md index c6510372b4f..376ca8374ee 100644 --- a/docs/src/rules/constructor-super.md +++ b/docs/src/rules/constructor-super.md @@ -21,6 +21,8 @@ This rule is aimed to flag invalid/missing `super()` calls. Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint constructor-super: "error"*/ /*eslint-env es6*/ @@ -47,8 +49,12 @@ class A extends null { } ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint constructor-super: "error"*/ /*eslint-env es6*/ @@ -64,6 +70,8 @@ class A extends B { } ``` +::: + ## When Not To Use It If you don't want to be notified about invalid/missing `super()` callings in constructors, you can safely disable this rule. diff --git a/docs/src/rules/curly.md b/docs/src/rules/curly.md index e7f7dd0b1a8..96fd2681df4 100644 --- a/docs/src/rules/curly.md +++ b/docs/src/rules/curly.md @@ -35,6 +35,8 @@ This rule is aimed at preventing bugs and increasing code clarity by ensuring th Examples of **incorrect** code for the default `"all"` option: +::: incorrect + ```js /*eslint curly: "error"*/ @@ -48,8 +50,12 @@ if (foo) { } else qux(); ``` +::: + Examples of **correct** code for the default `"all"` option: +::: correct + ```js /*eslint curly: "error"*/ @@ -68,12 +74,16 @@ if (foo) { } ``` +::: + ### multi By default, this rule warns whenever `if`, `else`, `for`, `while`, or `do` are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block. Examples of **incorrect** code for the `"multi"` option: +::: incorrect + ```js /*eslint curly: ["error", "multi"]*/ @@ -95,8 +105,12 @@ for (var i=0; i < items.length; i++) { } ``` +::: + Examples of **correct** code for the `"multi"` option: +::: correct + ```js /*eslint curly: ["error", "multi"]*/ @@ -110,12 +124,16 @@ while (true) { } ``` +::: + ### multi-line Alternatively, you can relax the rule to allow brace-less single-line `if`, `else if`, `else`, `for`, `while`, or `do`, while still enforcing the use of curly braces for other instances. Examples of **incorrect** code for the `"multi-line"` option: +::: incorrect + ```js /*eslint curly: ["error", "multi-line"]*/ @@ -129,8 +147,12 @@ if (foo) foo( baz); ``` +::: + Examples of **correct** code for the `"multi-line"` option: +::: correct + ```js /*eslint curly: ["error", "multi-line"]*/ @@ -158,12 +180,16 @@ while (true) { } ``` +::: + ### multi-or-nest You can use another configuration that forces brace-less `if`, `else if`, `else`, `for`, `while`, or `do` if their body contains only one single-line statement. And forces braces in all other cases. Examples of **incorrect** code for the `"multi-or-nest"` option: +::: incorrect + ```js /*eslint curly: ["error", "multi-or-nest"]*/ @@ -192,8 +218,12 @@ for (var i = 0; foo; i++) { } ``` +::: + Examples of **correct** code for the `"multi-or-nest"` option: +::: correct + ```js /*eslint curly: ["error", "multi-or-nest"]*/ @@ -221,10 +251,14 @@ for (var i = 0; foo; i++) doSomething(); ``` +::: + For single-line statements preceded by a comment, braces are allowed but not required. Examples of additional **correct** code for the `"multi-or-nest"` option: +::: correct + ```js /*eslint curly: ["error", "multi-or-nest"]*/ @@ -238,6 +272,8 @@ if (foo) { } ``` +::: + ### consistent When using any of the `multi*` options, you can add an option to enforce all bodies of a `if`, @@ -245,6 +281,8 @@ When using any of the `multi*` options, you can add an option to enforce all bod Examples of **incorrect** code for the `"multi", "consistent"` options: +::: incorrect + ```js /*eslint curly: ["error", "multi", "consistent"]*/ @@ -274,8 +312,12 @@ if (foo) { } ``` +::: + Examples of **correct** code for the `"multi", "consistent"` options: +::: correct + ```js /*eslint curly: ["error", "multi", "consistent"]*/ @@ -305,6 +347,8 @@ if (foo) ``` +::: + ## When Not To Use It If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule. diff --git a/docs/src/rules/default-case-last.md b/docs/src/rules/default-case-last.md index 279d11280b9..9a50eae631a 100644 --- a/docs/src/rules/default-case-last.md +++ b/docs/src/rules/default-case-last.md @@ -29,6 +29,8 @@ This rule does not enforce the existence of `default` clauses. See [default-case Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint default-case-last: "error"*/ @@ -79,8 +81,12 @@ switch (foo) { } ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint default-case-last: "error"*/ @@ -126,3 +132,5 @@ if (foo !== 0) { } doSomethingAnyway(); ``` + +::: diff --git a/docs/src/rules/default-case.md b/docs/src/rules/default-case.md index f43bc21b023..61a2ac85b9d 100644 --- a/docs/src/rules/default-case.md +++ b/docs/src/rules/default-case.md @@ -52,6 +52,8 @@ This rule aims to require `default` case in `switch` statements. You may optiona Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint default-case: "error"*/ @@ -63,8 +65,12 @@ switch (a) { ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint default-case: "error"*/ @@ -95,6 +101,8 @@ switch (a) { } ``` +::: + ## Options This rule accepts a single options argument: @@ -105,6 +113,8 @@ This rule accepts a single options argument: Examples of **correct** code for the `{ "commentPattern": "^skip\\sdefault" }` option: +::: correct + ```js /*eslint default-case: ["error", { "commentPattern": "^skip\\sdefault" }]*/ @@ -125,6 +135,8 @@ switch(a) { } ``` +::: + ## When Not To Use It If you don't want to enforce a `default` case for `switch` statements, you can safely disable this rule. diff --git a/docs/src/rules/default-param-last.md b/docs/src/rules/default-param-last.md index a2eea8ea37a..17d119d40f3 100644 --- a/docs/src/rules/default-param-last.md +++ b/docs/src/rules/default-param-last.md @@ -25,6 +25,8 @@ This rule enforces default parameters to be the last of parameters. Examples of **incorrect** code for this rule: +::: incorrect + ```js /* eslint default-param-last: ["error"] */ @@ -33,10 +35,16 @@ function f(a = 0, b) {} function f(a, b = 0, c) {} ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /* eslint default-param-last: ["error"] */ function f(a, b = 0) {} ``` + +::: diff --git a/docs/src/rules/dot-location.md b/docs/src/rules/dot-location.md index 73b0481edab..987fe0369d3 100644 --- a/docs/src/rules/dot-location.md +++ b/docs/src/rules/dot-location.md @@ -41,6 +41,8 @@ The default `"object"` option requires the dot to be on the same line as the obj Examples of **incorrect** code for the default `"object"` option: +::: incorrect + ```js /*eslint dot-location: ["error", "object"]*/ @@ -48,8 +50,12 @@ var foo = object .property; ``` +::: + Examples of **correct** code for the default `"object"` option: +::: correct + ```js /*eslint dot-location: ["error", "object"]*/ @@ -64,12 +70,16 @@ property; var baz = object.property; ``` +::: + ### property The `"property"` option requires the dot to be on the same line as the property. Examples of **incorrect** code for the `"property"` option: +::: incorrect + ```js /*eslint dot-location: ["error", "property"]*/ @@ -77,8 +87,12 @@ var foo = object. property; ``` +::: + Examples of **correct** code for the `"property"` option: +::: correct + ```js /*eslint dot-location: ["error", "property"]*/ @@ -87,6 +101,8 @@ var foo = object var bar = object.property; ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of newlines before or after dots in member expressions. diff --git a/docs/src/rules/dot-notation.md b/docs/src/rules/dot-notation.md index d386b4df4e6..0e15b18800b 100644 --- a/docs/src/rules/dot-notation.md +++ b/docs/src/rules/dot-notation.md @@ -21,14 +21,20 @@ This rule is aimed at maintaining code consistency and improving code readabilit Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint dot-notation: "error"*/ var x = foo["bar"]; ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint dot-notation: "error"*/ @@ -37,6 +43,8 @@ var x = foo.bar; var x = foo[bar]; // Property name is a variable, square-bracket notation required ``` +::: + ## Options This rule accepts a single options argument: @@ -48,6 +56,8 @@ This rule accepts a single options argument: Examples of **correct** code for the `{ "allowKeywords": false }` option: +:::correct + ```js /*eslint dot-notation: ["error", { "allowKeywords": false }]*/ @@ -55,8 +65,12 @@ var foo = { "class": "CS 101" } var x = foo["class"]; // Property name is a reserved word, square-bracket notation required ``` +::: + Examples of additional **correct** code for the `{ "allowKeywords": false }` option: +:::correct + ```js /*eslint dot-notation: ["error", { "allowKeywords": false }]*/ @@ -68,12 +82,16 @@ class C { } ``` +::: + ### allowPattern 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: +:::correct + ```js /*eslint camelcase: "error"*/ /*eslint dot-notation: ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }]*/ @@ -87,3 +105,5 @@ data["fooBar"] = 42; var data = {}; data["foo_bar"] = 42; // no warning ``` + +::: diff --git a/docs/src/rules/eol-last.md b/docs/src/rules/eol-last.md index 442b06694fa..65684c89138 100644 --- a/docs/src/rules/eol-last.md +++ b/docs/src/rules/eol-last.md @@ -25,6 +25,8 @@ the end of the file. If you still want this behavior, consider enabling Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint eol-last: ["error", "always"]*/ @@ -33,8 +35,12 @@ function doSomething() { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint eol-last: ["error", "always"]*/ @@ -43,6 +49,8 @@ function doSomething() { }\n ``` +::: + ## Options This rule has a string option: diff --git a/docs/src/rules/eqeqeq.md b/docs/src/rules/eqeqeq.md index 6874896ada0..a9016bc4919 100644 --- a/docs/src/rules/eqeqeq.md +++ b/docs/src/rules/eqeqeq.md @@ -26,6 +26,8 @@ This rule is aimed at eliminating the type-unsafe equality operators. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint eqeqeq: "error"*/ @@ -36,6 +38,8 @@ if ("" == text) { } if (obj.getStuff() != undefined) { } ``` +::: + The `--fix` option on the command line automatically fixes some problems reported by this rule. A problem is only fixed if one of the operands is a `typeof` expression, or if both operands are literals with the same type. ## Options @@ -46,6 +50,8 @@ The `"always"` option (default) enforces the use of `===` and `!==` in every sit Examples of **incorrect** code for the `"always"` option: +::: incorrect + ```js /*eslint eqeqeq: ["error", "always"]*/ @@ -61,8 +67,12 @@ foo == null ``` +::: + Examples of **correct** code for the `"always"` option: +::: correct + ```js /*eslint eqeqeq: ["error", "always"]*/ @@ -78,6 +88,8 @@ foo === null ``` +::: + This rule optionally takes a second argument, which should be an object with the following supported properties: * `"null"`: Customize how this rule treats `null` literals. Possible values: @@ -95,6 +107,8 @@ The `"smart"` option enforces the use of `===` and `!==` except for these cases: Examples of **incorrect** code for the `"smart"` option: +::: incorrect + ```js /*eslint eqeqeq: ["error", "smart"]*/ @@ -109,8 +123,12 @@ bananas != 1 value == undefined ``` +::: + Examples of **correct** code for the `"smart"` option: +::: correct + ```js /*eslint eqeqeq: ["error", "smart"]*/ @@ -121,6 +139,8 @@ true == true foo == null ``` +::: + ### allow-null **Deprecated:** Instead of using this option use "always" and pass a "null" option property with value "ignore". This will tell ESLint to always enforce strict equality except when comparing with the `null` literal. diff --git a/docs/src/rules/for-direction.md b/docs/src/rules/for-direction.md index f349d2662b8..b98410a5fb6 100644 --- a/docs/src/rules/for-direction.md +++ b/docs/src/rules/for-direction.md @@ -15,6 +15,8 @@ A `for` loop with a stop condition that can never be reached, such as one with a Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint for-direction: "error"*/ for (var i = 0; i < 10; i--) { @@ -27,10 +29,16 @@ for (var i = 0; i > 10; i++) { } ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint for-direction: "error"*/ for (var i = 0; i < 10; i++) { } ``` + +::: diff --git a/docs/src/rules/func-call-spacing.md b/docs/src/rules/func-call-spacing.md index 0f5032b322c..63e8b9b6a1c 100644 --- a/docs/src/rules/func-call-spacing.md +++ b/docs/src/rules/func-call-spacing.md @@ -41,6 +41,8 @@ Further, in `"always"` mode, a second object option is available that contains a Examples of **incorrect** code for this rule with the default `"never"` option: +::: incorrect + ```js /*eslint func-call-spacing: ["error", "never"]*/ @@ -50,18 +52,26 @@ fn (); ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +::: correct + ```js /*eslint func-call-spacing: ["error", "never"]*/ fn(); ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint func-call-spacing: ["error", "always"]*/ @@ -71,28 +81,40 @@ fn (); ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint func-call-spacing: ["error", "always"]*/ fn (); ``` +::: + #### allowNewlines By default, `"always"` does not allow newlines. To permit newlines when in `"always"` mode, set the `allowNewlines` option to `true`. Newlines are never required. Examples of **incorrect** code for this rule with `allowNewlines` option enabled: +::: incorrect + ```js /*eslint func-call-spacing: ["error", "always", { "allowNewlines": true }]*/ fn(); ``` +::: + Examples of **correct** code for this rule with the `allowNewlines` option enabled: +::: correct + ```js /*eslint func-call-spacing: ["error", "always", { "allowNewlines": true }]*/ @@ -102,6 +124,8 @@ fn (); ``` +::: + ## When Not To Use It This rule can safely be turned off if your project does not care about enforcing a consistent style for spacing within function calls. diff --git a/docs/src/rules/func-name-matching.md b/docs/src/rules/func-name-matching.md index 21217c7140c..6d3d631d40c 100644 --- a/docs/src/rules/func-name-matching.md +++ b/docs/src/rules/func-name-matching.md @@ -13,6 +13,8 @@ This rule requires function names to match the name of the variable or property Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint func-name-matching: "error"*/ @@ -28,6 +30,8 @@ class C { } ``` +::: + ```js /*eslint func-name-matching: ["error", "never"] */ @@ -45,6 +49,8 @@ class C { Examples of **correct** code for this rule: +::: correct + ```js /*eslint func-name-matching: "error"*/ /*eslint func-name-matching: ["error", "always"]*/ // these are equivalent @@ -88,6 +94,8 @@ module.exports = function foo(name) {}; module['exports'] = function foo(name) {}; ``` +::: + ```js /*eslint func-name-matching: ["error", "never"] */ /*eslint-env es6*/ @@ -140,6 +148,8 @@ A boolean value that defaults to `false`. If `considerPropertyDescriptor` is set Examples of **correct** code for the `{ considerPropertyDescriptor: true }` option: +::: correct + ```js /*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/ /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent @@ -150,8 +160,12 @@ Object.defineProperties(obj, {baz:{value: function baz() {} }}); Reflect.defineProperty(obj, 'foo', {value: function foo() {}}); ``` +::: + Examples of **incorrect** code for the `{ considerPropertyDescriptor: true }` option: +::: incorrect + ```js /*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/ /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/ // these are equivalent @@ -162,12 +176,16 @@ Object.defineProperties(obj, {baz:{value: function foo() {} }}); Reflect.defineProperty(obj, 'foo', {value: function value() {}}); ``` +::: + ### includeCommonJSModuleExports A boolean value that defaults to `false`. If `includeCommonJSModuleExports` is set to true, `module.exports` and `module["exports"]` will be checked by this rule. Examples of **incorrect** code for the `{ includeCommonJSModuleExports: true }` option: +::: incorrect + ```js /*eslint func-name-matching: ["error", { "includeCommonJSModuleExports": true }]*/ /*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/ // these are equivalent @@ -176,6 +194,8 @@ module.exports = function foo(name) {}; module['exports'] = function foo(name) {}; ``` +::: + ## When Not To Use It Do not use this rule if you want to allow named functions to have different names from the variable or property to which they are assigned. diff --git a/docs/src/rules/func-names.md b/docs/src/rules/func-names.md index b626860066d..12cd9a2c62d 100644 --- a/docs/src/rules/func-names.md +++ b/docs/src/rules/func-names.md @@ -45,6 +45,8 @@ Please note that `"always"` and `"as-needed"` require function expressions and f Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint func-names: ["error", "always"]*/ @@ -61,8 +63,12 @@ const cat = { export default function() {} ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint func-names: ["error", "always"]*/ @@ -79,12 +85,16 @@ const cat = { export default function foo() {} ``` +::: + ### as-needed ECMAScript 6 introduced a `name` property on all functions. The value of `name` is determined by evaluating the code around the function to see if a name can be inferred. For example, a function assigned to a variable will automatically have a `name` property equal to the name of the variable. The value of `name` is then used in stack traces for easier debugging. Examples of **incorrect** code for this rule with the `"as-needed"` option: +::: incorrect + ```js /*eslint func-names: ["error", "as-needed"]*/ @@ -97,8 +107,12 @@ Foo.prototype.bar = function() {}; export default function() {} ``` +::: + Examples of **correct** code for this rule with the `"as-needed"` option: +::: correct + ```js /*eslint func-names: ["error", "as-needed"]*/ @@ -122,10 +136,14 @@ quux ??= function() {}; export default function foo() {} ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint func-names: ["error", "never"]*/ @@ -136,8 +154,12 @@ Foo.prototype.bar = function bar() {}; }()) ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint func-names: ["error", "never"]*/ @@ -148,10 +170,14 @@ Foo.prototype.bar = function() {}; }()) ``` +::: + ### generators Examples of **incorrect** code for this rule with the `"always", { "generators": "as-needed" }` options: +::: incorrect + ```js /*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/ @@ -160,62 +186,92 @@ Examples of **incorrect** code for this rule with the `"always", { "generators": }()) ``` +::: + Examples of **correct** code for this rule with the `"always", { "generators": "as-needed" }` options: +::: correct + ```js /*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/ var foo = function*() {}; ``` +::: + Examples of **incorrect** code for this rule with the `"always", { "generators": "never" }` options: +::: incorrect + ```js /*eslint func-names: ["error", "always", { "generators": "never" }]*/ var foo = bar(function *baz() {}); ``` +::: + Examples of **correct** code for this rule with the `"always", { "generators": "never" }` options: +::: correct + ```js /*eslint func-names: ["error", "always", { "generators": "never" }]*/ var foo = bar(function *() {}); ``` +::: + Examples of **incorrect** code for this rule with the `"as-needed", { "generators": "never" }` options: +::: incorrect + ```js /*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/ var foo = bar(function *baz() {}); ``` +::: + Examples of **correct** code for this rule with the `"as-needed", { "generators": "never" }` options: +::: correct + ```js /*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/ var foo = bar(function *() {}); ``` +::: + Examples of **incorrect** code for this rule with the `"never", { "generators": "always" }` options: +::: incorrect + ```js /*eslint func-names: ["error", "never", { "generators": "always" }]*/ var foo = bar(function *() {}); ``` +::: + Examples of **correct** code for this rule with the `"never", { "generators": "always" }` options: +::: correct + ```js /*eslint func-names: ["error", "never", { "generators": "always" }]*/ var foo = bar(function *baz() {}); ``` +::: + ## Compatibility * **JSCS**: [requireAnonymousFunctions](https://jscs-dev.github.io/rule/requireAnonymousFunctions) diff --git a/docs/src/rules/func-style.md b/docs/src/rules/func-style.md index 769a7a1f8a5..cb36e42e9da 100644 --- a/docs/src/rules/func-style.md +++ b/docs/src/rules/func-style.md @@ -70,6 +70,8 @@ This rule has an object option for an exception: Examples of **incorrect** code for this rule with the default `"expression"` option: +::: incorrect + ```js /*eslint func-style: ["error", "expression"]*/ @@ -78,8 +80,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the default `"expression"` option: +::: correct + ```js /*eslint func-style: ["error", "expression"]*/ @@ -92,10 +98,14 @@ var foo = () => {}; // allowed as allowArrowFunctions : false is applied only for declaration ``` +::: + ### declaration Examples of **incorrect** code for this rule with the `"declaration"` option: +::: incorrect + ```js /*eslint func-style: ["error", "declaration"]*/ @@ -106,8 +116,12 @@ var foo = function() { var foo = () => {}; ``` +::: + Examples of **correct** code for this rule with the `"declaration"` option: +::: correct + ```js /*eslint func-style: ["error", "declaration"]*/ @@ -121,16 +135,22 @@ SomeObject.foo = function() { }; ``` +::: + ### allowArrowFunctions Examples of additional **correct** code for this rule with the `"declaration", { "allowArrowFunctions": true }` options: +::: correct + ```js /*eslint func-style: ["error", "declaration", { "allowArrowFunctions": true }]*/ var foo = () => {}; ``` +::: + ## When Not To Use It If you want to allow developers to each decide how they want to write functions on their own, then you can disable this rule. diff --git a/docs/src/rules/function-call-argument-newline.md b/docs/src/rules/function-call-argument-newline.md index 53c04991560..61de2433ac3 100644 --- a/docs/src/rules/function-call-argument-newline.md +++ b/docs/src/rules/function-call-argument-newline.md @@ -32,6 +32,8 @@ This rule has a string option: Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint function-call-argument-newline: ["error", "always"]*/ @@ -47,8 +49,12 @@ baz("one", "two", (x) => { }); ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint function-call-argument-newline: ["error", "always"]*/ @@ -82,10 +88,14 @@ baz( ); ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint function-call-argument-newline: ["error", "never"]*/ @@ -110,8 +120,12 @@ baz( ); ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint function-call-argument-newline: ["error", "never"]*/ @@ -133,10 +147,14 @@ baz("one", "two", (x) => { }); ``` +::: + ### consistent Examples of **incorrect** code for this rule with the `"consistent"` option: +::: incorrect + ```js /*eslint function-call-argument-newline: ["error", "consistent"]*/ @@ -155,8 +173,12 @@ baz("one", "two", ); ``` +::: + Examples of **correct** code for this rule with the `"consistent"` option: +::: correct + ```js /*eslint function-call-argument-newline: ["error", "consistent"]*/ @@ -201,6 +223,8 @@ baz( ); ``` +::: + ## When Not To Use It If you don't want to enforce line breaks between arguments, don't enable this rule. diff --git a/docs/src/rules/function-paren-newline.md b/docs/src/rules/function-paren-newline.md index 3c3f04d569e..456f750c4b5 100644 --- a/docs/src/rules/function-paren-newline.md +++ b/docs/src/rules/function-paren-newline.md @@ -46,6 +46,8 @@ Example configurations: Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /* eslint function-paren-newline: ["error", "always"] */ @@ -58,8 +60,12 @@ var foo = (bar, baz) => {}; foo(bar, baz); ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /* eslint function-paren-newline: ["error", "always"] */ @@ -83,8 +89,12 @@ foo( ); ``` +::: + Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /* eslint function-paren-newline: ["error", "never"] */ @@ -108,8 +118,12 @@ foo( ); ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /* eslint function-paren-newline: ["error", "never"] */ @@ -128,8 +142,12 @@ foo(bar, baz); ``` +::: + Examples of **incorrect** code for this rule with the default `"multiline"` option: +::: incorrect + ```js /* eslint function-paren-newline: ["error", "multiline"] */ @@ -155,8 +173,12 @@ foo( ); ``` +::: + Examples of **correct** code for this rule with the default `"multiline"` option: +::: correct + ```js /* eslint function-paren-newline: ["error", "multiline"] */ @@ -182,8 +204,12 @@ foo(function() { }); ``` +::: + Examples of **incorrect** code for this rule with the `"consistent"` option: +::: incorrect + ```js /* eslint function-paren-newline: ["error", "consistent"] */ @@ -209,8 +235,12 @@ foo( }); ``` +::: + Examples of **correct** code for this rule with the `"consistent"` option: +::: correct + ```js /* eslint function-paren-newline: ["error", "consistent"] */ @@ -235,8 +265,12 @@ foo( ); ``` +::: + Examples of **incorrect** code for this rule with the `"multiline-arguments"` option: +::: incorrect + ```js /* eslint function-paren-newline: ["error", "multiline-arguments"] */ @@ -262,8 +296,12 @@ foo( ); ``` +::: + Examples of **correct** code for this rule with the consistent `"multiline-arguments"` option: +::: correct + ```js /* eslint function-paren-newline: ["error", "multiline-arguments"] */ @@ -285,8 +323,12 @@ foo( ); ``` +::: + Examples of **incorrect** code for this rule with the `{ "minItems": 3 }` option: +::: incorrect + ```js /* eslint function-paren-newline: ["error", { "minItems": 3 }] */ @@ -308,8 +350,12 @@ foo(bar, baz); ``` +::: + Examples of **correct** code for this rule with the `{ "minItems": 3 }` option: +::: correct + ```js /* eslint function-paren-newline: ["error", { "minItems": 3 }] */ @@ -332,6 +378,8 @@ foo( ); ``` +::: + ## When Not To Use It If don't want to enforce consistent linebreaks inside function parentheses, do not turn on this rule. diff --git a/docs/src/rules/generator-star-spacing.md b/docs/src/rules/generator-star-spacing.md index 0833382a6c8..f2cedb49fdc 100644 --- a/docs/src/rules/generator-star-spacing.md +++ b/docs/src/rules/generator-star-spacing.md @@ -113,6 +113,8 @@ Overrides can be either an object with "before" and "after", or a shorthand stri Examples of **correct** code for this rule with the `"before"` option: +::: correct + ```js /*eslint generator-star-spacing: ["error", {"before": true, "after": false}]*/ /*eslint-env es6*/ @@ -124,10 +126,14 @@ var anonymous = function *() {}; var shorthand = { *generator() {} }; ``` +::: + ### after Examples of **correct** code for this rule with the `"after"` option: +::: correct + ```js /*eslint generator-star-spacing: ["error", {"before": false, "after": true}]*/ /*eslint-env es6*/ @@ -139,10 +145,14 @@ var anonymous = function* () {}; var shorthand = { * generator() {} }; ``` +::: + ### both Examples of **correct** code for this rule with the `"both"` option: +::: correct + ```js /*eslint generator-star-spacing: ["error", {"before": true, "after": true}]*/ /*eslint-env es6*/ @@ -154,10 +164,14 @@ var anonymous = function * () {}; var shorthand = { * generator() {} }; ``` +::: + ### neither Examples of **correct** code for this rule with the `"neither"` option: +::: correct + ```js /*eslint generator-star-spacing: ["error", {"before": false, "after": false}]*/ /*eslint-env es6*/ @@ -169,8 +183,12 @@ var anonymous = function*() {}; var shorthand = { *generator() {} }; ``` +::: + Examples of **incorrect** code for this rule with overrides present: +::: incorrect + ```js /*eslint generator-star-spacing: ["error", { "before": false, @@ -189,8 +207,12 @@ var shorthand = { *generator() {} }; class Class { static* method() {} } ``` +::: + Examples of **correct** code for this rule with overrides present: +::: correct + ```js /*eslint generator-star-spacing: ["error", { "before": false, @@ -209,6 +231,8 @@ var shorthand = { * generator() {} }; class Class { static * method() {} } ``` +::: + ## When Not To Use It If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule. diff --git a/docs/src/rules/getter-return.md b/docs/src/rules/getter-return.md index 4b3770d84e2..b3eb85c8f96 100644 --- a/docs/src/rules/getter-return.md +++ b/docs/src/rules/getter-return.md @@ -36,6 +36,8 @@ This rule enforces that a return statement is present in property getters. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint getter-return: "error"*/ @@ -58,8 +60,12 @@ class P{ } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint getter-return: "error"*/ @@ -82,6 +88,8 @@ class P{ } ``` +::: + ## Options This rule has an object option: @@ -90,6 +98,8 @@ This rule has an object option: Examples of **correct** code for the `{ "allowImplicit": true }` option: +::: correct + ```js /*eslint getter-return: ["error", { allowImplicit: true }]*/ p = { @@ -99,6 +109,8 @@ p = { }; ``` +::: + ## When Not To Use It If your project will not be using ES5 property getters you do not need this rule. diff --git a/docs/src/rules/global-require.md b/docs/src/rules/global-require.md index 0650cb07eaf..18c390905e1 100644 --- a/docs/src/rules/global-require.md +++ b/docs/src/rules/global-require.md @@ -35,6 +35,8 @@ This rule requires all calls to `require()` to be at the top level of the module Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint global-require: "error"*/ /*eslint-env es6*/ @@ -73,8 +75,12 @@ try { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint global-require: "error"*/ @@ -100,6 +106,8 @@ var x = require("x"), z = require("z"); ``` +::: + ## When Not To Use It If you have a module that must be initialized with information that comes from the file-system or if a module is only used in very rare situations and will cause significant overhead to load it may make sense to disable the rule. If you need to `require()` an optional dependency inside of a `try`/`catch`, you can disable this rule for just that dependency using the `// eslint-disable-line global-require` comment. diff --git a/docs/src/rules/grouped-accessor-pairs.md b/docs/src/rules/grouped-accessor-pairs.md index 45fd15f862a..fdddf8b7942 100644 --- a/docs/src/rules/grouped-accessor-pairs.md +++ b/docs/src/rules/grouped-accessor-pairs.md @@ -55,6 +55,8 @@ This rule does not enforce the existence of the pair for a getter or a setter. S Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint grouped-accessor-pairs: "error"*/ @@ -99,8 +101,12 @@ const Bar = class { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint grouped-accessor-pairs: "error"*/ @@ -145,6 +151,8 @@ const Bar = class { } ``` +::: + ## Options This rule has a string option: @@ -157,6 +165,8 @@ This rule has a string option: Examples of **incorrect** code for this rule with the `"getBeforeSet"` option: +::: incorrect + ```js /*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/ @@ -188,8 +198,12 @@ const Bar = class { } ``` +::: + Examples of **correct** code for this rule with the `"getBeforeSet"` option: +::: correct + ```js /*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/ @@ -221,10 +235,14 @@ const Bar = class { } ``` +::: + ### setBeforeGet Examples of **incorrect** code for this rule with the `"setBeforeGet"` option: +::: incorrect + ```js /*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/ @@ -256,8 +274,12 @@ const Bar = class { } ``` +::: + Examples of **correct** code for this rule with the `"setBeforeGet"` option: +::: correct + ```js /*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/ @@ -289,6 +311,8 @@ const Bar = class { } ``` +::: + ## Known Limitations Due to the limits of static analysis, this rule does not account for possible side effects and in certain cases diff --git a/docs/src/rules/guard-for-in.md b/docs/src/rules/guard-for-in.md index dec9df17999..26c42b053c6 100644 --- a/docs/src/rules/guard-for-in.md +++ b/docs/src/rules/guard-for-in.md @@ -28,6 +28,8 @@ This rule is aimed at preventing unexpected behavior that could arise from using Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint guard-for-in: "error"*/ @@ -36,8 +38,12 @@ for (key in foo) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint guard-for-in: "error"*/ @@ -53,3 +59,5 @@ for (key in foo) { } } ``` + +::: diff --git a/docs/src/rules/handle-callback-err.md b/docs/src/rules/handle-callback-err.md index fb2b7c72b95..bee6c1586c7 100644 --- a/docs/src/rules/handle-callback-err.md +++ b/docs/src/rules/handle-callback-err.md @@ -32,6 +32,8 @@ The rule takes a single string option: the name of the error parameter. The defa Examples of **incorrect** code for this rule with the default `"err"` parameter name: +::: incorrect + ```js /*eslint handle-callback-err: "error"*/ @@ -41,8 +43,12 @@ function loadData (err, data) { ``` +::: + Examples of **correct** code for this rule with the default `"err"` parameter name: +::: correct + ```js /*eslint handle-callback-err: "error"*/ @@ -58,8 +64,12 @@ function generateError (err) { } ``` +::: + Examples of **correct** code for this rule with a sample `"error"` parameter name: +::: correct + ```js /*eslint handle-callback-err: ["error", "error"]*/ @@ -71,6 +81,8 @@ function loadData (error, data) { } ``` +::: + ### regular expression Sometimes (especially in big projects) the name of the error variable is not consistent across the project, diff --git a/docs/src/rules/id-denylist.md b/docs/src/rules/id-denylist.md index c5678a50fd5..bbb4aec1d52 100644 --- a/docs/src/rules/id-denylist.md +++ b/docs/src/rules/id-denylist.md @@ -42,6 +42,8 @@ For example, to restrict the use of common generic identifiers: Examples of **incorrect** code for this rule with sample `"data", "callback"` restricted identifiers: +::: incorrect + ```js /*eslint id-denylist: ["error", "data", "callback"] */ @@ -76,8 +78,12 @@ class Foo { } ``` +::: + Examples of **correct** code for this rule with sample `"data", "callback"` restricted identifiers: +::: correct + ```js /*eslint id-denylist: ["error", "data", "callback"] */ @@ -118,6 +124,8 @@ class Foo { } ``` +::: + ## When Not To Use It You can turn this rule off if you do not want to restrict the use of certain identifiers. diff --git a/docs/src/rules/id-length.md b/docs/src/rules/id-length.md index 318b2be6571..1f22f31b5f0 100644 --- a/docs/src/rules/id-length.md +++ b/docs/src/rules/id-length.md @@ -26,6 +26,8 @@ This rule enforces a minimum and/or maximum identifier length convention. Examples of **incorrect** code for this rule with the default options: +::: incorrect + ```js /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 }) /*eslint-env es6*/ @@ -55,8 +57,12 @@ var { prop: a} = {}; ({ prop: obj.x } = {}); ``` +::: + Examples of **correct** code for this rule with the default options: +::: correct + ```js /*eslint id-length: "error"*/ // default is minimum 2-chars ({ "min": 2 }) /*eslint-env es6*/ @@ -93,6 +99,8 @@ var data = { "x": 1 }; // excused because of quotes data["y"] = 3; // excused because of calculated property access ``` +::: + This rule has an object option: * `"min"` (default: 2) enforces a minimum identifier length @@ -106,6 +114,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the `{ "min": 4 }` option: +::: incorrect + ```js /*eslint id-length: ["error", { "min": 4 }]*/ /*eslint-env es6*/ @@ -130,8 +140,12 @@ var { prop: [x]} = {}; ({ prop: obj.x } = {}); ``` +::: + Examples of **correct** code for this rule with the `{ "min": 4 }` option: +::: correct + ```js /*eslint id-length: ["error", { "min": 4 }]*/ /*eslint-env es6*/ @@ -160,10 +174,14 @@ var data = { "x": 1 }; // excused because of quotes data["y"] = 3; // excused because of calculated property access ``` +::: + ### max Examples of **incorrect** code for this rule with the `{ "max": 10 }` option: +::: incorrect + ```js /*eslint id-length: ["error", { "max": 10 }]*/ /*eslint-env es6*/ @@ -181,8 +199,12 @@ try { var [reallyLongFirstElementName] = arr; ``` +::: + Examples of **correct** code for this rule with the `{ "max": 10 }` option: +::: correct + ```js /*eslint id-length: ["error", { "max": 10 }]*/ /*eslint-env es6*/ @@ -200,10 +222,14 @@ try { var [first] = arr; ``` +::: + ### properties Examples of **correct** code for this rule with the `{ "properties": "never" }` option: +::: correct + ```js /*eslint id-length: ["error", { "properties": "never" }]*/ /*eslint-env es6*/ @@ -213,10 +239,14 @@ var myObj = { a: 1 }; ({ prop: obj.i } = {}); ``` +::: + ### exceptions Examples of additional **correct** code for this rule with the `{ "exceptions": ["x"] }` option: +::: correct + ```js /*eslint id-length: ["error", { "exceptions": ["x"] }]*/ /*eslint-env es6*/ @@ -236,10 +266,14 @@ const { x } = foo; const { a: x } = foo; ``` +::: + ### exceptionPatterns Examples of additional **correct** code for this rule with the `{ "exceptionPatterns": ["E|S", "[x-z]"] }` option: +::: correct + ```js /*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/ /*eslint-env es6*/ @@ -258,3 +292,5 @@ var [E] = arr; const { y } = foo; const { a: z } = foo; ``` + +::: diff --git a/docs/src/rules/id-match.md b/docs/src/rules/id-match.md index 606a605d400..8aa52ae830f 100644 --- a/docs/src/rules/id-match.md +++ b/docs/src/rules/id-match.md @@ -32,6 +32,8 @@ For example, to enforce a camelcase naming convention: Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option: +::: incorrect + ```js /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/ @@ -58,8 +60,12 @@ class myClass { } ``` +::: + Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$"` option: +::: correct + ```js /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$"]*/ @@ -82,6 +88,8 @@ class myClass { } ``` +::: + This rule has an object option: * `"properties": false` (default) does not check object properties @@ -97,6 +105,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "properties": true }` options: +::: incorrect + ```js /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/ @@ -105,10 +115,14 @@ var obj = { }; ``` +::: + ### classFields Examples of **incorrect** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "classFields": true }` options: +::: incorrect + ```js /*eslint id-match: ["error", "^[a-z]+([A-Z][a-z]+)*$", { "properties": true }]*/ @@ -121,20 +135,28 @@ class myClass { } ``` +::: + ### onlyDeclarations Examples of **correct** code for this rule with the `"^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }` options: +::: correct + ```js /*eslint id-match: [2, "^[a-z]+([A-Z][a-z]+)*$", { "onlyDeclarations": true }]*/ do_something(__dirname); ``` +::: + ### ignoreDestructuring: false Examples of **incorrect** code for this rule with the default `"^[^_]+$", { "ignoreDestructuring": false }` option: +::: incorrect + ```js /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": false }]*/ @@ -149,10 +171,14 @@ var { category_id: category_alias } = query; var { category_id: categoryId, ...other_props } = query; ``` +::: + ### ignoreDestructuring: true Examples of **incorrect** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option: +::: incorrect + ```js /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/ @@ -161,8 +187,12 @@ var { category_id: category_alias } = query; var { category_id, ...other_props } = query; ``` +::: + Examples of **correct** code for this rule with the `"^[^_]+$", { "ignoreDestructuring": true }` option: +::: correct + ```js /*eslint id-match: [2, "^[^_]+$", { "ignoreDestructuring": true }]*/ @@ -173,6 +203,8 @@ var { category_id = 1 } = query; var { category_id: category_id } = query; ``` +::: + ## When Not To Use It If you don't want to enforce any particular naming convention for all identifiers, or your naming convention is too complex to be enforced by configuring this rule, then you should not enable this rule. diff --git a/docs/src/rules/implicit-arrow-linebreak.md b/docs/src/rules/implicit-arrow-linebreak.md index 1a0941bd1bd..5bc5ee51002 100644 --- a/docs/src/rules/implicit-arrow-linebreak.md +++ b/docs/src/rules/implicit-arrow-linebreak.md @@ -26,6 +26,8 @@ This rule accepts a string option: Examples of **incorrect** code for this rule with the default `"beside"` option: +::: incorrect + ```js /* eslint implicit-arrow-linebreak: ["error", "beside"] */ @@ -45,8 +47,12 @@ Examples of **incorrect** code for this rule with the default `"beside"` option: ); ``` +::: + Examples of **correct** code for this rule with the default `"beside"` option: +::: correct + ```js /* eslint implicit-arrow-linebreak: ["error", "beside"] */ @@ -72,8 +78,12 @@ Examples of **correct** code for this rule with the default `"beside"` option: } ``` +::: + Examples of **incorrect** code for this rule with the `"below"` option: +::: incorrect + ```js /* eslint implicit-arrow-linebreak: ["error", "below"] */ @@ -84,8 +94,12 @@ Examples of **incorrect** code for this rule with the `"below"` option: (foo) => bar => baz; ``` +::: + Examples of **correct** code for this rule with the `"below"` option: +::: correct + ```js /* eslint implicit-arrow-linebreak: ["error", "below"] */ @@ -100,6 +114,8 @@ Examples of **correct** code for this rule with the `"below"` option: baz; ``` +::: + ## When Not To Use It If you're not concerned about consistent locations of implicitly returned arrow function expressions, you should not turn on this rule. diff --git a/docs/src/rules/indent-legacy.md b/docs/src/rules/indent-legacy.md index d63b125a65b..6a2003ecf07 100644 --- a/docs/src/rules/indent-legacy.md +++ b/docs/src/rules/indent-legacy.md @@ -57,6 +57,8 @@ Or for tabbed indentation: Examples of **incorrect** code for this rule with the default options: +::: incorrect + ```js /*eslint indent: "error"*/ @@ -68,8 +70,12 @@ if (a) { } ``` +::: + Examples of **correct** code for this rule with the default options: +::: correct + ```js /*eslint indent: "error"*/ @@ -81,6 +87,8 @@ if (a) { } ``` +::: + This rule has an object option: * `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements @@ -119,6 +127,8 @@ Level of indentation denotes the multiple of the indent specified. Example: Examples of **incorrect** code for this rule with the `"tab"` option: +::: incorrect + ```js /*eslint indent: ["error", "tab"]*/ @@ -130,8 +140,12 @@ function foo(d) { } ``` +::: + Examples of **correct** code for this rule with the `"tab"` option: +::: correct + ```js /*eslint indent: ["error", "tab"]*/ @@ -143,10 +157,14 @@ if (a) { } ``` +::: + ### SwitchCase Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options: +::: incorrect + ```js /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/ @@ -158,8 +176,12 @@ case "b": } ``` +::: + Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/ @@ -171,10 +193,14 @@ switch(a){ } ``` +::: + ### VariableDeclarator Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options: +::: incorrect + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/ /*eslint-env es6*/ @@ -190,8 +216,12 @@ const a = 1, c = 3; ``` +::: + Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options: +::: correct + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/ /*eslint-env es6*/ @@ -207,8 +237,12 @@ const a = 1, c = 3; ``` +::: + Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options: +::: correct + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/ /*eslint-env es6*/ @@ -224,8 +258,12 @@ const a = 1, c = 3; ``` +::: + Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options: +::: correct + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/ /*eslint-env es6*/ @@ -241,10 +279,14 @@ const a = 1, c = 3; ``` +::: + ### outerIIFEBody Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`: +::: incorrect + ```js /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/ @@ -261,8 +303,12 @@ console.log('foo'); } ``` +::: + Examples of **correct** code for this rule with the options `2, {"outerIIFEBody": 0}`: +::: correct + ```js /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/ @@ -279,10 +325,14 @@ if(y) { } ``` +::: + ### MemberExpression Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options: +::: incorrect + ```js /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/ @@ -291,8 +341,12 @@ foo .baz() ``` +::: + Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/ @@ -305,10 +359,14 @@ var bip = aardvark.badger .coyote; ``` +::: + ### FunctionDeclaration Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/ @@ -319,8 +377,12 @@ function foo(bar, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/ @@ -331,8 +393,12 @@ function foo(bar, } ``` +::: + Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/ @@ -342,8 +408,12 @@ function foo(bar, baz, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/ @@ -353,10 +423,14 @@ function foo(bar, baz, } ``` +::: + ### FunctionExpression Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/ @@ -367,8 +441,12 @@ var foo = function(bar, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/ @@ -379,8 +457,12 @@ var foo = function(bar, } ``` +::: + Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/ @@ -390,8 +472,12 @@ var foo = function(bar, baz, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/ @@ -401,10 +487,14 @@ var foo = function(bar, baz, } ``` +::: + ### CallExpression Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/ @@ -414,8 +504,12 @@ foo(bar, ); ``` +::: + Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/ @@ -425,8 +519,12 @@ foo(bar, ); ``` +::: + Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/ @@ -434,8 +532,12 @@ foo(bar, baz, baz, boop, beep); ``` +::: + Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/ @@ -443,10 +545,14 @@ foo(bar, baz, baz, boop, beep); ``` +::: + ### ArrayExpression Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/ @@ -457,8 +563,12 @@ baz, ]; ``` +::: + Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/ @@ -469,8 +579,12 @@ var foo = [ ]; ``` +::: + Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/ @@ -480,8 +594,12 @@ var foo = [bar, ]; ``` +::: + Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/ @@ -491,10 +609,14 @@ var foo = [bar, ]; ``` +::: + ### ObjectExpression Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/ @@ -505,8 +627,12 @@ baz: 2, }; ``` +::: + Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/ @@ -517,8 +643,12 @@ var foo = { }; ``` +::: + Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/ @@ -526,8 +656,12 @@ var foo = { bar: 1, baz: 2 }; ``` +::: + Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/ @@ -535,6 +669,8 @@ var foo = { bar: 1, baz: 2 }; ``` +::: + ## Compatibility * **JSHint**: `indent` diff --git a/docs/src/rules/indent.md b/docs/src/rules/indent.md index 2b83a5d3d11..d2ddfb73922 100644 --- a/docs/src/rules/indent.md +++ b/docs/src/rules/indent.md @@ -51,6 +51,8 @@ Or for tabbed indentation: Examples of **incorrect** code for this rule with the default options: +::: incorrect + ```js /*eslint indent: "error"*/ @@ -62,8 +64,12 @@ if (a) { } ``` +::: + Examples of **correct** code for this rule with the default options: +::: correct + ```js /*eslint indent: "error"*/ @@ -75,6 +81,8 @@ if (a) { } ``` +::: + This rule has an object option: * `"ignoredNodes"` can be used to disable indentation checking for any AST node. This accepts an array of [selectors](/docs/developer-guide/selectors). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern. @@ -120,6 +128,8 @@ Level of indentation denotes the multiple of the indent specified. Example: Examples of **incorrect** code for this rule with the `"tab"` option: +::: incorrect + ```js /*eslint indent: ["error", "tab"]*/ @@ -131,8 +141,12 @@ function foo(d) { } ``` +::: + Examples of **correct** code for this rule with the `"tab"` option: +::: correct + ```js /*eslint indent: ["error", "tab"]*/ @@ -144,12 +158,16 @@ if (a) { } ``` +::: + ### ignoredNodes The following configuration ignores the indentation of `ConditionalExpression` ("ternary expression") nodes: Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["ConditionalExpression"] }` option: +::: correct + ```js /*eslint indent: ["error", 4, { "ignoredNodes": ["ConditionalExpression"] }]*/ @@ -162,10 +180,14 @@ var a = foo : baz; ``` +::: + The following configuration ignores indentation in the body of IIFEs. Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }` option: +::: correct + ```js /*eslint indent: ["error", 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }]*/ @@ -177,12 +199,16 @@ bar(); }) ``` +::: + All AST node types can be found at [ESTree](https://github.com/estree/estree) specification. You can use [AST Explorer](https://astexplorer.net/) with the espree parser to examine AST tree of a code snippet. ### SwitchCase Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options: +::: incorrect + ```js /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/ @@ -194,8 +220,12 @@ case "b": } ``` +::: + Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/ @@ -207,10 +237,14 @@ switch(a){ } ``` +::: + ### VariableDeclarator Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options: +::: incorrect + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/ /*eslint-env es6*/ @@ -226,8 +260,12 @@ const a = 1, c = 3; ``` +::: + Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options: +::: correct + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/ /*eslint-env es6*/ @@ -243,8 +281,12 @@ const a = 1, c = 3; ``` +::: + Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options: +::: correct + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/ /*eslint-env es6*/ @@ -260,8 +302,12 @@ const a = 1, c = 3; ``` +::: + Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": "first" }` options: +::: incorrect + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/ /*eslint-env es6*/ @@ -277,8 +323,12 @@ const a = 1, c = 3; ``` +::: + Examples of **correct** code for this rule with the `2, { "VariableDeclarator": "first" }` options: +::: correct + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/ /*eslint-env es6*/ @@ -294,8 +344,12 @@ const a = 1, c = 3; ``` +::: + Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options: +::: correct + ```js /*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/ /*eslint-env es6*/ @@ -311,10 +365,14 @@ const a = 1, c = 3; ``` +::: + ### outerIIFEBody Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`: +::: incorrect + ```js /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/ @@ -331,8 +389,12 @@ console.log('foo'); } ``` +::: + Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": 0 }`: +::: correct + ```js /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/ @@ -349,8 +411,12 @@ if (y) { } ``` +::: + Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": "off" }`: +::: correct + ```js /*eslint indent: ["error", 2, { "outerIIFEBody": "off" }]*/ @@ -375,10 +441,14 @@ if (y) { } ``` +::: + ### MemberExpression Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options: +::: incorrect + ```js /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/ @@ -387,8 +457,12 @@ foo .baz() ``` +::: + Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/ @@ -397,10 +471,14 @@ foo .baz(); ``` +::: + ### FunctionDeclaration Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/ @@ -411,8 +489,12 @@ function foo(bar, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/ @@ -423,8 +505,12 @@ function foo(bar, } ``` +::: + Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/ @@ -434,8 +520,12 @@ function foo(bar, baz, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/ @@ -445,10 +535,14 @@ function foo(bar, baz, } ``` +::: + ### FunctionExpression Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/ @@ -459,8 +553,12 @@ var foo = function(bar, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/ @@ -471,8 +569,12 @@ var foo = function(bar, } ``` +::: + Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/ @@ -482,8 +584,12 @@ var foo = function(bar, baz, } ``` +::: + Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/ @@ -493,10 +599,14 @@ var foo = function(bar, baz, } ``` +::: + ### StaticBlock Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/ @@ -507,8 +617,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/ @@ -519,8 +633,12 @@ class C { } ``` +::: + Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/ @@ -531,8 +649,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/ @@ -543,10 +665,14 @@ class C { } ``` +::: + ### CallExpression Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/ @@ -556,8 +682,12 @@ foo(bar, ); ``` +::: + Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/ @@ -567,8 +697,12 @@ foo(bar, ); ``` +::: + Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/ @@ -576,8 +710,12 @@ foo(bar, baz, baz, boop, beep); ``` +::: + Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/ @@ -585,10 +723,14 @@ foo(bar, baz, baz, boop, beep); ``` +::: + ### ArrayExpression Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/ @@ -599,8 +741,12 @@ baz, ]; ``` +::: + Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/ @@ -611,8 +757,12 @@ var foo = [ ]; ``` +::: + Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/ @@ -622,8 +772,12 @@ var foo = [bar, ]; ``` +::: + Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/ @@ -633,10 +787,14 @@ var foo = [bar, ]; ``` +::: + ### ObjectExpression Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/ @@ -647,8 +805,12 @@ baz: 2, }; ``` +::: + Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/ @@ -659,8 +821,12 @@ var foo = { }; ``` +::: + Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/ @@ -668,8 +834,12 @@ var foo = { bar: 1, baz: 2 }; ``` +::: + Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option: +::: correct + ```js /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/ @@ -677,10 +847,14 @@ var foo = { bar: 1, baz: 2 }; ``` +::: + ### ImportDeclaration Examples of **correct** code for this rule with the `4, { "ImportDeclaration": 1 }` option (the default): +::: correct + ```js /*eslint indent: ["error", 4, { "ImportDeclaration": 1 }]*/ @@ -696,8 +870,12 @@ import { } from 'qux'; ``` +::: + Examples of **incorrect** code for this rule with the `4, { "ImportDeclaration": "first" }` option: +::: incorrect + ```js /*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/ @@ -707,8 +885,12 @@ import { foo, } from 'qux'; ``` +::: + Examples of **correct** code for this rule with the `4, { "ImportDeclaration": "first" }` option: +::: correct + ```js /*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/ @@ -718,10 +900,14 @@ import { foo, } from 'qux'; ``` +::: + ### flatTernaryExpressions Examples of **incorrect** code for this rule with the default `4, { "flatTernaryExpressions": false }` option: +::: incorrect + ```js /*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/ @@ -731,8 +917,12 @@ var a = boop; ``` +::: + Examples of **correct** code for this rule with the default `4, { "flatTernaryExpressions": false }` option: +::: correct + ```js /*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/ @@ -742,8 +932,12 @@ var a = boop; ``` +::: + Examples of **incorrect** code for this rule with the `4, { "flatTernaryExpressions": true }` option: +::: incorrect + ```js /*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/ @@ -753,8 +947,12 @@ var a = boop; ``` +::: + Examples of **correct** code for this rule with the `4, { "flatTernaryExpressions": true }` option: +::: correct + ```js /*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/ @@ -764,10 +962,14 @@ var a = boop; ``` +::: + ### offsetTernaryExpressions Examples of **incorrect** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/ @@ -780,8 +982,12 @@ condition } ``` +::: + Examples of **correct** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/ @@ -798,8 +1004,12 @@ condition } ``` +::: + Examples of **incorrect** code for this rule with the `2, { "offsetTernaryExpressions": true }` option: +::: incorrect + ```js /*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/ @@ -816,8 +1026,12 @@ condition } ``` +::: + Examples of **correct** code for this rule with the `2, { "offsetTernaryExpressions": true }` option: +::: correct + ```js /*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/ @@ -834,10 +1048,14 @@ condition } ``` +::: + ### ignoreComments Examples of additional **correct** code for this rule with the `4, { "ignoreComments": true }` option: +::: correct + ```js /*eslint indent: ["error", 4, { "ignoreComments": true }] */ @@ -849,6 +1067,8 @@ if (foo) { } ``` +::: + ## Compatibility * **JSHint**: `indent` diff --git a/docs/src/rules/init-declarations.md b/docs/src/rules/init-declarations.md index c5d5c88be34..a890b3cf9f4 100644 --- a/docs/src/rules/init-declarations.md +++ b/docs/src/rules/init-declarations.md @@ -70,6 +70,8 @@ Variables must not be initialized at declaration, except in for loops, where it Examples of **incorrect** code for the default `"always"` option: +::: incorrect + ```js /*eslint init-declarations: ["error", "always"]*/ /*eslint-env es6*/ @@ -80,8 +82,12 @@ function foo() { } ``` +::: + Examples of **correct** code for the default `"always"` option: +::: correct + ```js /*eslint init-declarations: ["error", "always"]*/ /*eslint-env es6*/ @@ -93,10 +99,14 @@ function foo() { } ``` +::: + ### never Examples of **incorrect** code for the `"never"` option: +::: incorrect + ```js /*eslint init-declarations: ["error", "never"]*/ /*eslint-env es6*/ @@ -109,8 +119,12 @@ function foo() { } ``` +::: + Examples of **correct** code for the `"never"` option: +::: correct + ```js /*eslint init-declarations: ["error", "never"]*/ /*eslint-env es6*/ @@ -122,17 +136,23 @@ function foo() { } ``` +::: + The `"never"` option ignores `const` variable initializations. ### ignoreForLoopInit Examples of **correct** code for the `"never", { "ignoreForLoopInit": true }` options: +::: correct + ```js /*eslint init-declarations: ["error", "never", { "ignoreForLoopInit": true }]*/ for (var i = 0; i < 1; i++) {} ``` +::: + ## When Not To Use It When you are indifferent as to how your variables are initialized. diff --git a/docs/src/rules/jsx-quotes.md b/docs/src/rules/jsx-quotes.md index 14d0aa533dc..a417ba71ec0 100644 --- a/docs/src/rules/jsx-quotes.md +++ b/docs/src/rules/jsx-quotes.md @@ -41,14 +41,20 @@ This rule has a string option: Examples of **incorrect** code for this rule with the default `"prefer-double"` option: +:::incorrect + ```xml /*eslint jsx-quotes: ["error", "prefer-double"]*/ ``` +::: + Examples of **correct** code for this rule with the default `"prefer-double"` option: +:::correct + ```xml /*eslint jsx-quotes: ["error", "prefer-double"]*/ @@ -56,18 +62,26 @@ Examples of **correct** code for this rule with the default `"prefer-double"` op ``` +::: + ### prefer-single Examples of **incorrect** code for this rule with the `"prefer-single"` option: +:::incorrect + ```xml /*eslint jsx-quotes: ["error", "prefer-single"]*/ ``` +::: + Examples of **correct** code for this rule with the `"prefer-single"` option: +:::correct + ```xml /*eslint jsx-quotes: ["error", "prefer-single"]*/ @@ -75,6 +89,8 @@ Examples of **correct** code for this rule with the `"prefer-single"` option: ``` +::: + ## When Not To Use It You can turn this rule off if you don’t use JSX or if you aren’t concerned with a consistent usage of quotes within JSX attributes. diff --git a/docs/src/rules/key-spacing.md b/docs/src/rules/key-spacing.md index 1ba5b065047..a19f2a5a701 100644 --- a/docs/src/rules/key-spacing.md +++ b/docs/src/rules/key-spacing.md @@ -41,74 +41,108 @@ Please note that you can either use the top-level options or the grouped options Examples of **incorrect** code for this rule with the default `{ "beforeColon": false }` option: +::: incorrect + ```js /*eslint key-spacing: ["error", { "beforeColon": false }]*/ var obj = { "foo" : 42 }; ``` +::: + Examples of **correct** code for this rule with the default `{ "beforeColon": false }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "beforeColon": false }]*/ var obj = { "foo": 42 }; ``` +::: + Examples of **incorrect** code for this rule with the `{ "beforeColon": true }` option: +::: incorrect + ```js /*eslint key-spacing: ["error", { "beforeColon": true }]*/ var obj = { "foo": 42 }; ``` +::: + Examples of **correct** code for this rule with the `{ "beforeColon": true }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "beforeColon": true }]*/ var obj = { "foo" : 42 }; ``` +::: + ### afterColon Examples of **incorrect** code for this rule with the default `{ "afterColon": true }` option: +::: incorrect + ```js /*eslint key-spacing: ["error", { "afterColon": true }]*/ var obj = { "foo":42 }; ``` +::: + Examples of **correct** code for this rule with the default `{ "afterColon": true }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "afterColon": true }]*/ var obj = { "foo": 42 }; ``` +::: + Examples of **incorrect** code for this rule with the `{ "afterColon": false }` option: +::: incorrect + ```js /*eslint key-spacing: ["error", { "afterColon": false }]*/ var obj = { "foo": 42 }; ``` +::: + Examples of **correct** code for this rule with the `{ "afterColon": false }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "afterColon": false }]*/ var obj = { "foo":42 }; ``` +::: + ### mode Examples of **incorrect** code for this rule with the default `{ "mode": "strict" }` option: +::: incorrect + ```js /*eslint key-spacing: ["error", { "mode": "strict" }]*/ @@ -118,8 +152,12 @@ call({ }); ``` +::: + Examples of **correct** code for this rule with the default `{ "mode": "strict" }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "mode": "strict" }]*/ @@ -129,8 +167,12 @@ call({ }); ``` +::: + Examples of **correct** code for this rule with the `{ "mode": "minimum" }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "mode": "minimum" }]*/ @@ -140,10 +182,14 @@ call({ }); ``` +::: + ### align Examples of **incorrect** code for this rule with the `{ "align": "value" }` option: +::: incorrect + ```js /*eslint key-spacing: ["error", { "align": "value" }]*/ @@ -154,8 +200,12 @@ var obj = { }; ``` +::: + Examples of **correct** code for this rule with the `{ "align": "value" }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "align": "value" }]*/ @@ -173,8 +223,12 @@ var obj = { var obj = { a: "foo", longPropertyName: "bar" }; ``` +::: + Examples of **incorrect** code for this rule with the `{ "align": "colon" }` option: +::: incorrect + ```js /*eslint key-spacing: ["error", { "align": "colon" }]*/ @@ -184,8 +238,12 @@ call({ }); ``` +::: + Examples of **correct** code for this rule with the `{ "align": "colon" }` option: +::: correct + ```js /*eslint key-spacing: ["error", { "align": "colon" }]*/ @@ -195,6 +253,8 @@ call({ }); ``` +::: + ### align The `align` option can take additional configuration through the `beforeColon`, `afterColon`, `mode`, and `on` options. @@ -213,6 +273,8 @@ align: { Examples of **correct** code for this rule with sample `{ "align": { } }` options: +::: correct + ```js /*eslint key-spacing: ["error", { "align": { @@ -228,6 +290,8 @@ var obj = { } ``` +::: + ```js /*eslint key-spacing: ["error", { "align": { @@ -263,6 +327,8 @@ var myObj = { Examples of **incorrect** code for this rule with sample `{ "align": { }, "multiLine": { } }` options: +::: incorrect + ```js /*eslint key-spacing: ["error", { "multiLine": { @@ -285,8 +351,12 @@ var obj = { } ``` +::: + Examples of **correct** code for this rule with sample `{ "align": { }, "multiLine": { } }` options: +::: correct + ```js /*eslint key-spacing: ["error", { "multiLine": { @@ -311,10 +381,14 @@ var obj = { } ``` +::: + ### singleLine and multiLine Examples of **correct** code for this rule with sample `{ "singleLine": { }, "multiLine": { } }` options: +::: correct + ```js /*eslint "key-spacing": [2, { "singleLine": { @@ -334,6 +408,8 @@ var obj2 = { }; ``` +::: + ## When Not To Use It If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule. diff --git a/docs/src/rules/keyword-spacing.md b/docs/src/rules/keyword-spacing.md index d48990c31e6..008efd77c70 100644 --- a/docs/src/rules/keyword-spacing.md +++ b/docs/src/rules/keyword-spacing.md @@ -44,6 +44,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "before": true }` option: +::: incorrect + ```js /*eslint keyword-spacing: ["error", { "before": true }]*/ @@ -56,8 +58,12 @@ if (foo) { } ``` +::: + Examples of **correct** code for this rule with the default `{ "before": true }` option: +::: correct + ```js /*eslint keyword-spacing: ["error", { "before": true }]*/ /*eslint-env es6*/ @@ -111,8 +117,12 @@ if (10+this.foo<= this.bar) {} let a = ``` +::: + Examples of **incorrect** code for this rule with the `{ "before": false }` option: +::: incorrect + ```js /*eslint keyword-spacing: ["error", { "before": false }]*/ @@ -125,8 +135,12 @@ if (foo) { } ``` +::: + Examples of **correct** code for this rule with the `{ "before": false }` option: +::: correct + ```js /*eslint keyword-spacing: ["error", { "before": false }]*/ @@ -139,10 +153,14 @@ if (foo) { } ``` +::: + ### after Examples of **incorrect** code for this rule with the default `{ "after": true }` option: +::: incorrect + ```js /*eslint keyword-spacing: ["error", { "after": true }]*/ @@ -155,8 +173,12 @@ if(foo) { } ``` +::: + Examples of **correct** code for this rule with the default `{ "after": true }` option: +::: correct + ```js /*eslint keyword-spacing: ["error", { "after": true }]*/ @@ -222,8 +244,12 @@ function* foo(a) { let a = ``` +::: + Examples of **incorrect** code for this rule with the `{ "after": false }` option: +::: incorrect + ```js /*eslint keyword-spacing: ["error", { "after": false }]*/ @@ -236,8 +262,12 @@ if (foo) { } ``` +::: + Examples of **correct** code for this rule with the `{ "after": false }` option: +::: correct + ```js /*eslint keyword-spacing: ["error", { "after": false }]*/ @@ -250,10 +280,14 @@ if(foo) { } ``` +::: + ### overrides Examples of **correct** code for this rule with the `{ "overrides": { "if": { "after": false }, "for": { "after": false }, "while": { "after": false }, "static": { "after": false }, "as": { "after": false } } }` option: +::: correct + ```js /*eslint keyword-spacing: ["error", { "overrides": { "if": { "after": false }, @@ -286,6 +320,8 @@ class C { export { C as"my class" }; ``` +::: + ## When Not To Use It If you don't want to enforce consistency on keyword spacing, then it's safe to disable this rule. diff --git a/docs/src/rules/line-comment-position.md b/docs/src/rules/line-comment-position.md index 7c033e398b5..2d45409b22a 100644 --- a/docs/src/rules/line-comment-position.md +++ b/docs/src/rules/line-comment-position.md @@ -33,72 +33,104 @@ The `position` option has two settings: Examples of **correct** code for the `{ "position": "above" }` option: +::: correct + ```js /*eslint line-comment-position: ["error", { "position": "above" }]*/ // valid comment 1 + 1; ``` +::: + Examples of **incorrect** code for the `{ "position": "above" }` option: +::: incorrect + ```js /*eslint line-comment-position: ["error", { "position": "above" }]*/ 1 + 1; // invalid comment ``` +::: + #### position: beside Examples of **correct** code for the `{ "position": "beside" }` option: +::: correct + ```js /*eslint line-comment-position: ["error", { "position": "beside" }]*/ 1 + 1; // valid comment ``` +::: + Examples of **incorrect** code for the `{ "position": "beside" }` option: +::: incorrect + ```js /*eslint line-comment-position: ["error", { "position": "beside" }]*/ // invalid comment 1 + 1; ``` +::: + ### ignorePattern By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`, `falls through`. An alternative regular expression can be provided. Examples of **correct** code for the `ignorePattern` option: +::: correct + ```js /*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/ 1 + 1; // pragma valid comment ``` +::: + Examples of **incorrect** code for the `ignorePattern` option: +::: incorrect + ```js /*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/ 1 + 1; // invalid comment ``` +::: + ### applyDefaultIgnorePatterns Default ignore patterns are applied even when `ignorePattern` is provided. If you want to omit default patterns, set this option to `false`. Examples of **correct** code for the `{ "applyDefaultIgnorePatterns": false }` option: +::: correct + ```js /*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/ 1 + 1; // pragma valid comment ``` +::: + Examples of **incorrect** code for the `{ "applyDefaultIgnorePatterns": false }` option: +::: incorrect + ```js /*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/ 1 + 1; // falls through ``` +::: + **Deprecated:** the object property `applyDefaultPatterns` is deprecated. Please use the property `applyDefaultIgnorePatterns` instead. ## When Not To Use It diff --git a/docs/src/rules/linebreak-style.md b/docs/src/rules/linebreak-style.md index c1a8a0a83dd..f20b79690df 100644 --- a/docs/src/rules/linebreak-style.md +++ b/docs/src/rules/linebreak-style.md @@ -32,6 +32,8 @@ This rule has a string option: Examples of **incorrect** code for this rule with the default `"unix"` option: +::: incorrect + ```js /*eslint linebreak-style: ["error", "unix"]*/ @@ -39,8 +41,12 @@ var a = 'a'; // \r\n ``` +::: + Examples of **correct** code for this rule with the default `"unix"` option: +::: correct + ```js /*eslint linebreak-style: ["error", "unix"]*/ @@ -52,18 +58,26 @@ function foo(params) { // \n }// \n ``` +::: + ### windows Examples of **incorrect** code for this rule with the `"windows"` option: +::: incorrect + ```js /*eslint linebreak-style: ["error", "windows"]*/ var a = 'a'; // \n ``` +::: + Examples of **correct** code for this rule with the `"windows"` option: +::: correct + ```js /*eslint linebreak-style: ["error", "windows"]*/ @@ -75,6 +89,8 @@ function foo(params) { // \r\n } // \r\n ``` +::: + ### Using this rule with version control systems Version control systems sometimes have special behavior for linebreaks. To make it easy for developers to contribute to your codebase from different platforms, you may want to configure your VCS to handle linebreaks appropriately. diff --git a/docs/src/rules/lines-around-comment.md b/docs/src/rules/lines-around-comment.md index 100cd2daf2c..6de964d4275 100644 --- a/docs/src/rules/lines-around-comment.md +++ b/docs/src/rules/lines-around-comment.md @@ -42,6 +42,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "beforeBlockComment": true }` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/ @@ -50,8 +52,12 @@ var night = "long"; var day = "great" ``` +::: + Examples of **correct** code for this rule with the default `{ "beforeBlockComment": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/ @@ -61,10 +67,14 @@ var night = "long"; var day = "great" ``` +::: + ### afterBlockComment Examples of **incorrect** code for this rule with the `{ "afterBlockComment": true }` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/ @@ -74,8 +84,12 @@ var night = "long"; var day = "great" ``` +::: + Examples of **correct** code for this rule with the `{ "afterBlockComment": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/ @@ -86,10 +100,14 @@ var night = "long"; var day = "great" ``` +::: + ### beforeLineComment Examples of **incorrect** code for this rule with the `{ "beforeLineComment": true }` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/ @@ -98,8 +116,12 @@ var night = "long"; var day = "great" ``` +::: + Examples of **correct** code for this rule with the `{ "beforeLineComment": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/ @@ -109,10 +131,14 @@ var night = "long"; var day = "great" ``` +::: + ### afterLineComment Examples of **incorrect** code for this rule with the `{ "afterLineComment": true }` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/ @@ -121,8 +147,12 @@ var night = "long"; var day = "great" ``` +::: + Examples of **correct** code for this rule with the `{ "afterLineComment": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/ @@ -132,10 +162,14 @@ var night = "long"; var day = "great" ``` +::: + ### allowBlockStart Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowBlockStart": true }` options: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowBlockStart": true }]*/ @@ -165,8 +199,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowBlockStart": true }` options: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowBlockStart": true }]*/ @@ -196,10 +234,14 @@ class C { } ``` +::: + ### allowBlockEnd Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowBlockEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowBlockEnd": true }]*/ @@ -230,8 +272,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowBlockEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowBlockEnd": true }]*/ @@ -266,10 +312,14 @@ class C { } ``` +::: + ### allowClassStart Examples of **incorrect** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": false }` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/ @@ -279,8 +329,12 @@ class foo { }; ``` +::: + Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": false }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/ @@ -291,8 +345,12 @@ class foo { }; ``` +::: + Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowClassStart": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": true }]*/ @@ -302,8 +360,12 @@ class foo { }; ``` +::: + Examples of **incorrect** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": false }` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/ @@ -313,8 +375,12 @@ class foo { }; ``` +::: + Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": false }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/ @@ -325,8 +391,12 @@ class foo { }; ``` +::: + Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowClassStart": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": true }]*/ @@ -336,10 +406,14 @@ class foo { }; ``` +::: + ### allowClassEnd Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowClassEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/ @@ -349,8 +423,12 @@ class foo { }; ``` +::: + Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowClassEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowClassEnd": true }]*/ @@ -361,10 +439,14 @@ class foo { }; ``` +::: + ### allowObjectStart Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowObjectStart": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/ @@ -384,8 +466,12 @@ const { } = {day: "great"}; ``` +::: + Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowObjectStart": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowObjectStart": true }]*/ @@ -405,10 +491,14 @@ const { } = {day: "great"}; ``` +::: + ### allowObjectEnd Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowObjectEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowObjectEnd": true }]*/ @@ -428,8 +518,12 @@ const { } = {day: "great"}; ``` +::: + Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowObjectEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowObjectEnd": true }]*/ @@ -452,10 +546,14 @@ const { } = {day: "great"}; ``` +::: + ### allowArrayStart Examples of **correct** code for this rule with the `{ "beforeLineComment": true, "allowArrayStart": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowArrayStart": true }]*/ @@ -471,8 +569,12 @@ const [ ] = ["great", "not great"]; ``` +::: + Examples of **correct** code for this rule with the `{ "beforeBlockComment": true, "allowArrayStart": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowArrayStart": true }]*/ @@ -488,10 +590,14 @@ const [ ] = ["great", "not great"]; ``` +::: + ### allowArrayEnd Examples of **correct** code for this rule with the `{ "afterLineComment": true, "allowArrayEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowArrayEnd": true }]*/ @@ -507,8 +613,12 @@ const [ ] = ["great", "not great"]; ``` +::: + Examples of **correct** code for this rule with the `{ "afterBlockComment": true, "allowArrayEnd": true }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowArrayEnd": true }]*/ @@ -526,12 +636,16 @@ const [ ] = ["great", "not great"]; ``` +::: + ### ignorePattern By default this rule ignores comments starting with the following words: `eslint`, `jshint`, `jslint`, `istanbul`, `global`, `exported`, `jscs`. To ignore more comments in addition to the defaults, set the `ignorePattern` option to a string pattern that will be passed to the [`RegExp` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp). Examples of **correct** code for the `ignorePattern` option: +::: correct + ```js /*eslint lines-around-comment: ["error"]*/ @@ -545,8 +659,12 @@ foo(); /* a valid comment using pragma in it */ ``` +::: + Examples of **incorrect** code for the `ignorePattern` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */ @@ -554,12 +672,16 @@ Examples of **incorrect** code for the `ignorePattern` option: /* something else */ ``` +::: + ### applyDefaultIgnorePatterns Default ignore patterns are applied even when `ignorePattern` is provided. If you want to omit default patterns, set this option to `false`. Examples of **correct** code for the `{ "applyDefaultIgnorePatterns": false }` option: +::: correct + ```js /*eslint lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */ @@ -567,8 +689,12 @@ foo(); /* a valid comment using pragma in it */ ``` +::: + Examples of **incorrect** code for the `{ "applyDefaultIgnorePatterns": false }` option: +::: incorrect + ```js /*eslint lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */ @@ -577,6 +703,8 @@ foo(); ``` +::: + ## When Not To Use It Many people enjoy a terser code style and don't mind comments bumping up against code. If you fall into that category this rule is not for you. diff --git a/docs/src/rules/lines-around-directive.md b/docs/src/rules/lines-around-directive.md index 6c31f436885..4bb7fa6359c 100644 --- a/docs/src/rules/lines-around-directive.md +++ b/docs/src/rules/lines-around-directive.md @@ -64,6 +64,8 @@ This is the default option. Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /* eslint lines-around-directive: ["error", "always"] */ @@ -90,8 +92,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /* eslint lines-around-directive: ["error", "always"] */ @@ -124,10 +130,14 @@ function foo() { } ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /* eslint lines-around-directive: ["error", "never"] */ @@ -161,8 +171,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /* eslint lines-around-directive: ["error", "never"] */ @@ -189,10 +203,14 @@ function foo() { } ``` +::: + ### before & after Examples of **incorrect** code for this rule with the `{ "before": "never", "after": "always" }` option: +::: incorrect + ```js /* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */ @@ -222,8 +240,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "before": "never", "after": "always" }` option: +::: correct + ```js /* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */ @@ -254,8 +276,12 @@ function foo() { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "before": "always", "after": "never" }` option: +::: incorrect + ```js /* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */ @@ -286,8 +312,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "before": "always", "after": "never" }` option: +::: correct + ```js /* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */ @@ -316,6 +346,8 @@ function foo() { } ``` +::: + ## When Not To Use It You can safely disable this rule if you do not have any strict conventions about whether or not directive prologues should have blank newlines before or after them. diff --git a/docs/src/rules/lines-between-class-members.md b/docs/src/rules/lines-between-class-members.md index 0a798eb6af6..c263460f8c9 100644 --- a/docs/src/rules/lines-between-class-members.md +++ b/docs/src/rules/lines-between-class-members.md @@ -18,6 +18,8 @@ This rule improves readability by enforcing lines between class members. It will Examples of **incorrect** code for this rule: +::: incorrect + ```js /* eslint lines-between-class-members: ["error", "always"]*/ class MyClass { @@ -31,8 +33,12 @@ class MyClass { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /* eslint lines-between-class-members: ["error", "always"]*/ class MyClass { @@ -48,8 +54,12 @@ class MyClass { } ``` +::: + Examples of additional **correct** code for this rule: +::: correct + ```js /* eslint lines-between-class-members: ["error", "always"]*/ class MyClass { @@ -59,6 +69,8 @@ class MyClass { } ``` +::: + ### Options This rule has a string option and an object option. @@ -75,6 +87,8 @@ Object option: Examples of **incorrect** code for this rule with the string option: +::: incorrect + ```js /* eslint lines-between-class-members: ["error", "always"]*/ class Foo{ @@ -93,8 +107,12 @@ class Foo{ } ``` +::: + Examples of **correct** code for this rule with the string option: +::: correct + ```js /* eslint lines-between-class-members: ["error", "always"]*/ class Foo{ @@ -113,8 +131,12 @@ class Foo{ } ``` +::: + Examples of **correct** code for this rule with the object option: +::: correct + ```js /* eslint lines-between-class-members: ["error", "always", { "exceptAfterSingleLine": true }]*/ class Foo{ @@ -128,6 +150,8 @@ class Foo{ } ``` +::: + ## When Not To Use It If you don't want to enforce empty lines between class members, you can disable this rule. diff --git a/docs/src/rules/max-classes-per-file.md b/docs/src/rules/max-classes-per-file.md index 5752d540ae9..e34cb2f10bb 100644 --- a/docs/src/rules/max-classes-per-file.md +++ b/docs/src/rules/max-classes-per-file.md @@ -18,6 +18,8 @@ of classes and no more. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint max-classes-per-file: "error"*/ @@ -25,14 +27,20 @@ class Foo {} class Bar {} ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint max-classes-per-file: "error"*/ class Foo {} ``` +::: + ## Options This rule may be configured with either an object or a number. @@ -61,6 +69,8 @@ For example: Examples of **correct** code for this rule with the `max` option set to `2`: +::: correct + ```js /* eslint max-classes-per-file: ["error", 2] */ @@ -68,8 +78,12 @@ class Foo {} class Bar {} ``` +::: + Examples of **correct** code for this rule with the `ignoreExpressions` option set to `true`: +::: correct + ```js /* eslint max-classes-per-file: ["error", { ignoreExpressions: true }] */ @@ -83,3 +97,5 @@ class VisitorFactory { } } ``` + +::: diff --git a/docs/src/rules/max-depth.md b/docs/src/rules/max-depth.md index 0263e343a87..fd3133183a0 100644 --- a/docs/src/rules/max-depth.md +++ b/docs/src/rules/max-depth.md @@ -33,6 +33,8 @@ This rule has a number or object option: Examples of **incorrect** code for this rule with the default `{ "max": 4 }` option: +::: incorrect + ```js /*eslint max-depth: ["error", 4]*/ @@ -50,8 +52,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the default `{ "max": 4 }` option: +::: correct + ```js /*eslint max-depth: ["error", 4]*/ @@ -67,10 +73,14 @@ function foo() { } ``` +::: + Note that class static blocks do not count as nested blocks, and that the depth in them is calculated separately from the enclosing context. Examples of **incorrect** code for this rule with `{ "max": 2 }` option: +::: incorrect + ```js /*eslint max-depth: ["error", 2]*/ @@ -90,8 +100,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with `{ "max": 2 }` option: +::: correct + ```js /*eslint max-depth: ["error", 2]*/ @@ -108,3 +122,5 @@ function foo() { } } ``` + +::: diff --git a/docs/src/rules/max-len.md b/docs/src/rules/max-len.md index a52cefbba70..b45744c240a 100644 --- a/docs/src/rules/max-len.md +++ b/docs/src/rules/max-len.md @@ -42,14 +42,20 @@ This rule has a number or object option: Examples of **incorrect** code for this rule with the default `{ "code": 80 }` option: +::: incorrect + ```js /*eslint max-len: ["error", { "code": 80 }]*/ var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; ``` +::: + Examples of **correct** code for this rule with the default `{ "code": 80 }` option: +::: correct + ```js /*eslint max-len: ["error", { "code": 80 }]*/ @@ -60,18 +66,26 @@ var foo = { }; ``` +::: + ### tabWidth Examples of **incorrect** code for this rule with the default `{ "tabWidth": 4 }` option: +::: incorrect + ```js /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/ \t \t var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } }; ``` +::: + Examples of **correct** code for this rule with the default `{ "tabWidth": 4 }` option: +::: correct + ```js /*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/ @@ -81,10 +95,14 @@ Examples of **correct** code for this rule with the default `{ "tabWidth": 4 }` \t \t }; ``` +::: + ### comments Examples of **incorrect** code for this rule with the `{ "comments": 65 }` option: +::: incorrect + ```js /*eslint max-len: ["error", { "comments": 65 }]*/ @@ -93,10 +111,14 @@ Examples of **incorrect** code for this rule with the `{ "comments": 65 }` optio **/ ``` +::: + ### ignoreComments Examples of **correct** code for this rule with the `{ "ignoreComments": true }` option: +::: correct + ```js /*eslint max-len: ["error", { "ignoreComments": true }]*/ @@ -105,62 +127,88 @@ Examples of **correct** code for this rule with the `{ "ignoreComments": true }` **/ ``` +::: + ### ignoreTrailingComments Examples of **correct** code for this rule with the `{ "ignoreTrailingComments": true }` option: +::: correct + ```js /*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/ var foo = 'bar'; // This is a really really really really really really really long comment ``` +::: + ### ignoreUrls Examples of **correct** code for this rule with the `{ "ignoreUrls": true }` option: +::: correct + ```js /*eslint max-len: ["error", { "ignoreUrls": true }]*/ var url = 'https://www.example.com/really/really/really/really/really/really/really/long'; ``` +::: + ### ignoreStrings Examples of **correct** code for this rule with the `{ "ignoreStrings": true }` option: +::: correct + ```js /*eslint max-len: ["error", { "ignoreStrings": true }]*/ var longString = 'this is a really really really really really long string!'; ``` +::: + ### ignoreTemplateLiterals Examples of **correct** code for this rule with the `{ "ignoreTemplateLiterals": true }` option: +::: correct + ```js /*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/ var longTemplateLiteral = `this is a really really really really really long template literal!`; ``` +::: + ### ignoreRegExpLiterals Examples of **correct** code for this rule with the `{ "ignoreRegExpLiterals": true }` option: +::: correct + ```js /*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/ var longRegExpLiteral = /this is a really really really really really long regular expression!/; ``` +::: + ### ignorePattern Examples of **correct** code for this rule with the `ignorePattern` option: +::: correct + ```js /*eslint max-len: ["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/ var dep = require('really/really/really/really/really/really/really/really/long/module'); ``` + +::: diff --git a/docs/src/rules/max-lines-per-function.md b/docs/src/rules/max-lines-per-function.md index daf6f389d14..0c9f7dc978a 100644 --- a/docs/src/rules/max-lines-per-function.md +++ b/docs/src/rules/max-lines-per-function.md @@ -77,6 +77,8 @@ is equivalent to Examples of **incorrect** code for this rule with a max value of `2`: +::: incorrect + ```js /*eslint max-lines-per-function: ["error", 2]*/ function foo() { @@ -84,6 +86,8 @@ function foo() { } ``` +::: + ```js /*eslint max-lines-per-function: ["error", 2]*/ function foo() { @@ -103,6 +107,8 @@ function foo() { Examples of **correct** code for this rule with a max value of `3`: +::: correct + ```js /*eslint max-lines-per-function: ["error", 3]*/ function foo() { @@ -110,6 +116,8 @@ function foo() { } ``` +::: + ```js /*eslint max-lines-per-function: ["error", 3]*/ function foo() { @@ -131,6 +139,8 @@ function foo() { Examples of **incorrect** code for this rule with the `{ "skipBlankLines": true }` option: +::: incorrect + ```js /*eslint max-lines-per-function: ["error", {"max": 2, "skipBlankLines": true}]*/ function foo() { @@ -139,8 +149,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "skipBlankLines": true }` option: +::: correct + ```js /*eslint max-lines-per-function: ["error", {"max": 3, "skipBlankLines": true}]*/ function foo() { @@ -149,10 +163,14 @@ function foo() { } ``` +::: + ### skipComments Examples of **incorrect** code for this rule with the `{ "skipComments": true }` option: +::: incorrect + ```js /*eslint max-lines-per-function: ["error", {"max": 2, "skipComments": true}]*/ function foo() { @@ -161,8 +179,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "skipComments": true }` option: +::: correct + ```js /*eslint max-lines-per-function: ["error", {"max": 3, "skipComments": true}]*/ function foo() { @@ -171,10 +193,14 @@ function foo() { } ``` +::: + ### IIFEs Examples of **incorrect** code for this rule with the `{ "IIFEs": true }` option: +::: incorrect + ```js /*eslint max-lines-per-function: ["error", {"max": 2, "IIFEs": true}]*/ (function(){ @@ -186,8 +212,12 @@ Examples of **incorrect** code for this rule with the `{ "IIFEs": true }` option })(); ``` +::: + Examples of **correct** code for this rule with the `{ "IIFEs": true }` option: +::: correct + ```js /*eslint max-lines-per-function: ["error", {"max": 3, "IIFEs": true}]*/ (function(){ @@ -199,6 +229,8 @@ Examples of **correct** code for this rule with the `{ "IIFEs": true }` option: })(); ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the number of lines in your functions. diff --git a/docs/src/rules/max-lines.md b/docs/src/rules/max-lines.md index 2ce5e5813fb..7d89abc2d46 100644 --- a/docs/src/rules/max-lines.md +++ b/docs/src/rules/max-lines.md @@ -38,6 +38,8 @@ This rule has a number or object option: Examples of **incorrect** code for this rule with a max value of `2`: +::: incorrect + ```js /*eslint max-lines: ["error", 2]*/ var a, @@ -45,6 +47,8 @@ var a, c; ``` +::: + ```js /*eslint max-lines: ["error", 2]*/ @@ -61,12 +65,16 @@ var a, Examples of **correct** code for this rule with a max value of `2`: +::: correct + ```js /*eslint max-lines: ["error", 2]*/ var a, b, c; ``` +::: + ```js /*eslint max-lines: ["error", 2]*/ @@ -83,6 +91,8 @@ var a, b, c; Examples of **incorrect** code for this rule with the `{ "skipBlankLines": true }` option: +::: incorrect + ```js /*eslint max-lines: ["error", {"max": 2, "skipBlankLines": true}]*/ @@ -91,8 +101,12 @@ var a, c; ``` +::: + Examples of **correct** code for this rule with the `{ "skipBlankLines": true }` option: +::: correct + ```js /*eslint max-lines: ["error", {"max": 2, "skipBlankLines": true}]*/ @@ -100,10 +114,14 @@ var a, b, c; ``` +::: + ### skipComments Examples of **incorrect** code for this rule with the `{ "skipComments": true }` option: +::: incorrect + ```js /*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/ // a comment @@ -112,8 +130,12 @@ var a, c; ``` +::: + Examples of **correct** code for this rule with the `{ "skipComments": true }` option: +::: correct + ```js /*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/ // a comment @@ -121,6 +143,8 @@ var a, b, c; ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the number of lines in your files. diff --git a/docs/src/rules/max-nested-callbacks.md b/docs/src/rules/max-nested-callbacks.md index a541eb7a50f..a0db2698e4a 100644 --- a/docs/src/rules/max-nested-callbacks.md +++ b/docs/src/rules/max-nested-callbacks.md @@ -49,6 +49,8 @@ This rule has a number or object option: Examples of **incorrect** code for this rule with the `{ "max": 3 }` option: +::: incorrect + ```js /*eslint max-nested-callbacks: ["error", 3]*/ @@ -63,8 +65,12 @@ foo1(function() { }); ``` +::: + Examples of **correct** code for this rule with the `{ "max": 3 }` option: +::: correct + ```js /*eslint max-nested-callbacks: ["error", 3]*/ @@ -86,3 +92,5 @@ function handleFoo4() { foo5(); } ``` + +::: diff --git a/docs/src/rules/max-params.md b/docs/src/rules/max-params.md index ad45082ec65..80a880b49f3 100644 --- a/docs/src/rules/max-params.md +++ b/docs/src/rules/max-params.md @@ -39,6 +39,8 @@ This rule has a number or object option: Examples of **incorrect** code for this rule with the default `{ "max": 3 }` option: +:::incorrect + ```js /*eslint max-params: ["error", 3]*/ /*eslint-env es6*/ @@ -52,8 +54,12 @@ let foo = (bar, baz, qux, qxx) => { }; ``` +::: + Examples of **correct** code for this rule with the default `{ "max": 3 }` option: +:::correct + ```js /*eslint max-params: ["error", 3]*/ /*eslint-env es6*/ @@ -66,3 +72,5 @@ let foo = (bar, baz, qux) => { doSomething(); }; ``` + +::: diff --git a/docs/src/rules/max-statements-per-line.md b/docs/src/rules/max-statements-per-line.md index 8d34246ea9a..9f44924641d 100644 --- a/docs/src/rules/max-statements-per-line.md +++ b/docs/src/rules/max-statements-per-line.md @@ -33,6 +33,8 @@ The "max" object property is optional (default: 1). Examples of **incorrect** code for this rule with the default `{ "max": 1 }` option: +::: incorrect + ```js /*eslint max-statements-per-line: ["error", { "max": 1 }]*/ @@ -45,8 +47,12 @@ var foo = function foo() { bar = 1; }; (function foo() { bar = 1; })(); ``` +::: + Examples of **correct** code for this rule with the default `{ "max": 1 }` option: +::: correct + ```js /*eslint max-statements-per-line: ["error", { "max": 1 }]*/ @@ -59,8 +65,12 @@ var foo = function foo() { }; (function foo() { })(); ``` +::: + Examples of **incorrect** code for this rule with the `{ "max": 2 }` option: +::: incorrect + ```js /*eslint max-statements-per-line: ["error", { "max": 2 }]*/ @@ -73,8 +83,12 @@ var foo = function foo() { bar = 1; }; (function foo() { bar = 1; baz = 2; })(); ``` +::: + Examples of **correct** code for this rule with the `{ "max": 2 }` option: +::: correct + ```js /*eslint max-statements-per-line: ["error", { "max": 2 }]*/ @@ -87,6 +101,8 @@ var foo = function foo() { bar = 1; }; (function foo() { var bar = 1; })(); ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the number of statements on each line. diff --git a/docs/src/rules/max-statements.md b/docs/src/rules/max-statements.md index 2d18021f34a..bb4e8c99211 100644 --- a/docs/src/rules/max-statements.md +++ b/docs/src/rules/max-statements.md @@ -45,6 +45,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "max": 10 }` option: +::: incorrect + ```js /*eslint max-statements: ["error", 10]*/ /*eslint-env es6*/ @@ -80,8 +82,12 @@ let foo = () => { }; ``` +::: + Examples of **correct** code for this rule with the default `{ "max": 10 }` option: +::: correct + ```js /*eslint max-statements: ["error", 10]*/ /*eslint-env es6*/ @@ -127,10 +133,14 @@ let foo = () => { } ``` +::: + Note that this rule does not apply to class static blocks, and that statements in class static blocks do not count as statements in the enclosing function. Examples of **correct** code for this rule with `{ "max": 2 }` option: +::: correct + ```js /*eslint max-statements: ["error", 2]*/ @@ -151,10 +161,14 @@ function foo() { } ``` +::: + ### ignoreTopLevelFunctions Examples of additional **correct** code for this rule with the `{ "max": 10 }, { "ignoreTopLevelFunctions": true }` options: +::: correct + ```js /*eslint max-statements: ["error", 10, { "ignoreTopLevelFunctions": true }]*/ @@ -172,3 +186,5 @@ function foo() { var foo11 = 11; } ``` + +::: diff --git a/docs/src/rules/multiline-comment-style.md b/docs/src/rules/multiline-comment-style.md index fca714605aa..4a804590e87 100644 --- a/docs/src/rules/multiline-comment-style.md +++ b/docs/src/rules/multiline-comment-style.md @@ -27,6 +27,8 @@ The rule always ignores directive comments such as `/* eslint-disable */`. Addit Examples of **incorrect** code for this rule with the default `"starred-block"` option: +::: incorrect + ```js /* eslint multiline-comment-style: ["error", "starred-block"] */ @@ -57,8 +59,12 @@ foo(); ``` +::: + Examples of **correct** code for this rule with the default `"starred-block"` option: +::: correct + ```js /* eslint multiline-comment-style: ["error", "starred-block"] */ @@ -71,8 +77,12 @@ foo(); // single-line comment ``` +::: + Examples of **incorrect** code for this rule with the `"bare-block"` option: +::: incorrect + ```js /* eslint multiline-comment-style: ["error", "bare-block"] */ @@ -87,8 +97,12 @@ foo(); foo(); ``` +::: + Examples of **correct** code for this rule with the `"bare-block"` option: +::: correct + ```js /* eslint multiline-comment-style: ["error", "bare-block"] */ @@ -97,8 +111,12 @@ Examples of **correct** code for this rule with the `"bare-block"` option: foo(); ``` +::: + Examples of **incorrect** code for this rule with the `"separate-lines"` option: +::: incorrect + ```js /* eslint multiline-comment-style: ["error", "separate-lines"] */ @@ -115,8 +133,12 @@ foo(); ``` +::: + Examples of **correct** code for this rule with the `"separate-lines"` option: +::: correct + ```js /* eslint multiline-comment-style: ["error", "separate-lines"] */ @@ -126,6 +148,8 @@ foo(); ``` +::: + ## When Not To Use It If you don't want to enforce a particular style for multiline comments, you can disable the rule. diff --git a/docs/src/rules/multiline-ternary.md b/docs/src/rules/multiline-ternary.md index f92d6715f41..bb17b18fa92 100644 --- a/docs/src/rules/multiline-ternary.md +++ b/docs/src/rules/multiline-ternary.md @@ -46,6 +46,8 @@ This is the default option. Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint multiline-ternary: ["error", "always"]*/ @@ -58,8 +60,12 @@ foo > bar ? value : value2; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint multiline-ternary: ["error", "always"]*/ @@ -74,10 +80,14 @@ foo > bar ? value3; ``` +::: + ### always-multiline Examples of **incorrect** code for this rule with the `"always-multiline"` option: +::: incorrect + ```js /*eslint multiline-ternary: ["error", "always-multiline"]*/ @@ -91,8 +101,12 @@ foo > bar && bar > baz ? value1 : value2; ``` +::: + Examples of **correct** code for this rule with the `"always-multiline"` option: +::: correct + ```js /*eslint multiline-ternary: ["error", "always-multiline"]*/ @@ -118,10 +132,14 @@ foo > bar && value2; ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint multiline-ternary: ["error", "never"]*/ @@ -137,8 +155,12 @@ foo > value2; ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint multiline-ternary: ["error", "never"]*/ @@ -151,6 +173,8 @@ foo > bar ? ( ) : value3; ``` +::: + ## When Not To Use It You can safely disable this rule if you do not have any strict conventions about whether the operands of a ternary expression should be separated by newlines. diff --git a/docs/src/rules/new-cap.md b/docs/src/rules/new-cap.md index 8bca67c9e73..170207a3bf9 100644 --- a/docs/src/rules/new-cap.md +++ b/docs/src/rules/new-cap.md @@ -30,6 +30,8 @@ This rule requires constructor names to begin with a capital letter. Certain bui Examples of **correct** code for this rule: +::: correct + ```js /*eslint new-cap: "error"*/ @@ -38,6 +40,8 @@ function foo(arg) { } ``` +::: + ## Options This rule has an object option: @@ -57,58 +61,84 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "newIsCap": true }` option: +::: incorrect + ```js /*eslint new-cap: ["error", { "newIsCap": true }]*/ var friend = new person(); ``` +::: + Examples of **correct** code for this rule with the default `{ "newIsCap": true }` option: +::: correct + ```js /*eslint new-cap: ["error", { "newIsCap": true }]*/ var friend = new Person(); ``` +::: + Examples of **correct** code for this rule with the `{ "newIsCap": false }` option: +::: correct + ```js /*eslint new-cap: ["error", { "newIsCap": false }]*/ var friend = new person(); ``` +::: + ### capIsNew Examples of **incorrect** code for this rule with the default `{ "capIsNew": true }` option: +::: incorrect + ```js /*eslint new-cap: ["error", { "capIsNew": true }]*/ var colleague = Person(); ``` +::: + Examples of **correct** code for this rule with the default `{ "capIsNew": true }` option: +::: correct + ```js /*eslint new-cap: ["error", { "capIsNew": true }]*/ var colleague = new Person(); ``` +::: + Examples of **correct** code for this rule with the `{ "capIsNew": false }` option: +::: correct + ```js /*eslint new-cap: ["error", { "capIsNew": false }]*/ var colleague = Person(); ``` +::: + ### newIsCapExceptions Examples of additional **correct** code for this rule with the `{ "newIsCapExceptions": ["events"] }` option: +::: correct + ```js /*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/ @@ -117,10 +147,14 @@ var events = require('events'); var emitter = new events(); ``` +::: + ### newIsCapExceptionPattern Examples of additional **correct** code for this rule with the `{ "newIsCapExceptionPattern": "^person\\.." }` option: +::: correct + ```js /*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\\.." }]*/ @@ -129,18 +163,26 @@ var friend = new person.acquaintance(); var bestFriend = new person.friend(); ``` +::: + Examples of additional **correct** code for this rule with the `{ "newIsCapExceptionPattern": "\\.bar$" }` option: +::: correct + ```js /*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/ var friend = new person.bar(); ``` +::: + ### capIsNewExceptions Examples of additional **correct** code for this rule with the `{ "capIsNewExceptions": ["Person"] }` option: +::: correct + ```js /*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/ @@ -149,10 +191,14 @@ function foo(arg) { } ``` +::: + ### capIsNewExceptionPattern Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "^person\\.." }` option: +::: correct + ```js /*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^person\\.." }]*/ @@ -160,16 +206,24 @@ var friend = person.Acquaintance(); var bestFriend = person.Friend(); ``` +::: + Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "\\.Bar$" }` option: +::: correct + ```js /*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/ foo.Bar(); ``` +::: + Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "^Foo" }` option: +::: correct + ```js /*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/ @@ -180,32 +234,46 @@ var y = Foobar(42); var z = Foo.Bar(42); ``` +::: + ### properties Examples of **incorrect** code for this rule with the default `{ "properties": true }` option: +::: incorrect + ```js /*eslint new-cap: ["error", { "properties": true }]*/ var friend = new person.acquaintance(); ``` +::: + Examples of **correct** code for this rule with the default `{ "properties": true }` option: +::: correct + ```js /*eslint new-cap: ["error", { "properties": true }]*/ var friend = new person.Acquaintance(); ``` +::: + Examples of **correct** code for this rule with the `{ "properties": false }` option: +::: correct + ```js /*eslint new-cap: ["error", { "properties": false }]*/ var friend = new person.acquaintance(); ``` +::: + ## When Not To Use It If you have conventions that don't require an uppercase letter for constructors, or don't require capitalized functions be only used as constructors, turn this rule off. diff --git a/docs/src/rules/new-parens.md b/docs/src/rules/new-parens.md index 0d7cefcd62b..c294314fa91 100644 --- a/docs/src/rules/new-parens.md +++ b/docs/src/rules/new-parens.md @@ -30,6 +30,8 @@ This rule takes one option. Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint new-parens: "error"*/ @@ -37,8 +39,12 @@ var person = new Person; var person = new (Person); ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint new-parens: "error"*/ @@ -46,10 +52,14 @@ var person = new Person(); var person = new (Person)(); ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint new-parens: ["error", "never"]*/ @@ -57,8 +67,12 @@ var person = new Person(); var person = new (Person)(); ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint new-parens: ["error", "never"]*/ @@ -66,3 +80,5 @@ var person = new Person; var person = (new Person); var person = new Person("Name"); ``` + +::: diff --git a/docs/src/rules/newline-after-var.md b/docs/src/rules/newline-after-var.md index 9cb5c7a48b8..e80fe0caa4e 100644 --- a/docs/src/rules/newline-after-var.md +++ b/docs/src/rules/newline-after-var.md @@ -46,6 +46,8 @@ This rule has a string option: Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint newline-after-var: ["error", "always"]*/ /*eslint-env es6*/ @@ -68,8 +70,12 @@ var name = "world"; console.log(greet, name); ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint newline-after-var: ["error", "always"]*/ /*eslint-env es6*/ @@ -96,10 +102,14 @@ var name = "world"; console.log(greet, name); ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint newline-after-var: ["error", "never"]*/ /*eslint-env es6*/ @@ -126,8 +136,12 @@ var name = "world"; console.log(greet, name); ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint newline-after-var: ["error", "never"]*/ /*eslint-env es6*/ @@ -149,3 +163,5 @@ var name = "world"; // var name = require("world"); console.log(greet, name); ``` + +::: diff --git a/docs/src/rules/newline-before-return.md b/docs/src/rules/newline-before-return.md index 261b8619ffb..f07fdcb5830 100644 --- a/docs/src/rules/newline-before-return.md +++ b/docs/src/rules/newline-before-return.md @@ -48,6 +48,8 @@ This rule requires an empty line before `return` statements to increase code cla Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint newline-before-return: "error"*/ @@ -68,8 +70,12 @@ function foo(bar) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint newline-before-return: "error"*/ @@ -118,6 +124,8 @@ function foo() { } ``` +::: + ## When Not To Use It You can safely disable this rule if you do not have any strict conventions about whitespace before `return` statements. diff --git a/docs/src/rules/newline-per-chained-call.md b/docs/src/rules/newline-per-chained-call.md index 6aaab2ca9c5..575055ffea5 100644 --- a/docs/src/rules/newline-per-chained-call.md +++ b/docs/src/rules/newline-per-chained-call.md @@ -71,6 +71,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "ignoreChainWithDepth": 2 }` option: +::: incorrect + ```js /*eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 2 }]*/ @@ -88,8 +90,12 @@ _ obj.method().method2().method3(); ``` +::: + Examples of **correct** code for this rule with the default `{ "ignoreChainWithDepth": 2 }` option: +::: correct + ```js /*eslint newline-per-chained-call: ["error", { "ignoreChainWithDepth": 2 }]*/ @@ -122,6 +128,8 @@ obj .method3().prop; ``` +::: + ## When Not To Use It If you have conflicting rules or when you are fine with chained calls on one line, you can safely turn this rule off. diff --git a/docs/src/rules/no-alert.md b/docs/src/rules/no-alert.md index 414cce09d12..2b7bc2d416a 100644 --- a/docs/src/rules/no-alert.md +++ b/docs/src/rules/no-alert.md @@ -22,6 +22,8 @@ This rule is aimed at catching debugging code that should be removed and popup U Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint no-alert: "error"*/ @@ -32,8 +34,12 @@ confirm("Are you sure?"); prompt("What's your name?", "John Doe"); ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint no-alert: "error"*/ @@ -48,3 +54,5 @@ function foo() { alert(); } ``` + +::: diff --git a/docs/src/rules/no-array-constructor.md b/docs/src/rules/no-array-constructor.md index da49901c4f4..4a7ac5cc2cf 100644 --- a/docs/src/rules/no-array-constructor.md +++ b/docs/src/rules/no-array-constructor.md @@ -22,6 +22,8 @@ This rule disallows `Array` constructors. Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint no-array-constructor: "error"*/ @@ -30,8 +32,12 @@ Array(0, 1, 2) new Array(0, 1, 2) ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint no-array-constructor: "error"*/ @@ -42,6 +48,8 @@ new Array(someOtherArray.length) [0, 1, 2] ``` +::: + ## When Not To Use It This rule enforces a nearly universal stylistic concern. That being said, this diff --git a/docs/src/rules/no-arrow-condition.md b/docs/src/rules/no-arrow-condition.md index ad15b469848..a6ef8f21348 100644 --- a/docs/src/rules/no-arrow-condition.md +++ b/docs/src/rules/no-arrow-condition.md @@ -39,6 +39,8 @@ var x = a <= 1 ? 2 : 3 Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint no-arrow-condition: "error"*/ /*eslint-env es6*/ @@ -51,3 +53,5 @@ a => 1 ? 2 : 3 var x = a => 1 ? 2 : 3 var x = (a) => 1 ? 2 : 3 ``` + +::: diff --git a/docs/src/rules/no-async-promise-executor.md b/docs/src/rules/no-async-promise-executor.md index 6627f5b8be7..d047a662dfd 100644 --- a/docs/src/rules/no-async-promise-executor.md +++ b/docs/src/rules/no-async-promise-executor.md @@ -34,6 +34,8 @@ This rule aims to disallow async Promise executor functions. Examples of **incorrect** code for this rule: +::: incorrect + ```js const foo = new Promise(async (resolve, reject) => { readFile('foo.txt', function(err, result) { @@ -50,8 +52,12 @@ const result = new Promise(async (resolve, reject) => { }); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js const foo = new Promise((resolve, reject) => { readFile('foo.txt', function(err, result) { @@ -66,6 +72,8 @@ const foo = new Promise((resolve, reject) => { const result = Promise.resolve(foo); ``` +::: + ## When Not To Use It If your codebase doesn't support async function syntax, there's no need to enable this rule. diff --git a/docs/src/rules/no-await-in-loop.md b/docs/src/rules/no-await-in-loop.md index 5e759e60cc2..ff360b82a6a 100644 --- a/docs/src/rules/no-await-in-loop.md +++ b/docs/src/rules/no-await-in-loop.md @@ -48,6 +48,8 @@ This rule disallows the use of `await` within loop bodies. Examples of **correct** code for this rule: +:::correct + ```js /*eslint no-await-in-loop: "error"*/ @@ -62,8 +64,12 @@ async function foo(things) { } ``` +::: + Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint no-await-in-loop: "error"*/ @@ -77,6 +83,8 @@ async function foo(things) { } ``` +::: + ## When Not To Use It In many cases the iterations of a loop are not actually independent of each-other. For example, the diff --git a/docs/src/rules/no-bitwise.md b/docs/src/rules/no-bitwise.md index b2721bac59f..16cc33ea36d 100644 --- a/docs/src/rules/no-bitwise.md +++ b/docs/src/rules/no-bitwise.md @@ -19,6 +19,8 @@ This rule disallows bitwise operators. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-bitwise: "error"*/ @@ -49,8 +51,12 @@ x >>= y; x >>>= y; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-bitwise: "error"*/ @@ -65,6 +71,8 @@ var x = y < z; x += y; ``` +::: + ## Options This rule has an object option: @@ -76,18 +84,26 @@ This rule has an object option: Examples of **correct** code for this rule with the `{ "allow": ["~"] }` option: +::: correct + ```js /*eslint no-bitwise: ["error", { "allow": ["~"] }] */ ~[1,2,3].indexOf(1) === -1; ``` +::: + ### int32Hint Examples of **correct** code for this rule with the `{ "int32Hint": true }` option: +::: correct + ```js /*eslint no-bitwise: ["error", { "int32Hint": true }] */ var b = a|0; ``` + +::: diff --git a/docs/src/rules/no-buffer-constructor.md b/docs/src/rules/no-buffer-constructor.md index 93dde30184d..754e5e61210 100644 --- a/docs/src/rules/no-buffer-constructor.md +++ b/docs/src/rules/no-buffer-constructor.md @@ -21,6 +21,8 @@ This rule disallows calling and constructing the `Buffer()` constructor. Examples of **incorrect** code for this rule: +::: incorrect + ```js new Buffer(5); new Buffer([1, 2, 3]); @@ -32,8 +34,12 @@ new Buffer(res.body.amount); new Buffer(res.body.values); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js Buffer.alloc(5); Buffer.allocUnsafe(5); @@ -43,6 +49,8 @@ Buffer.alloc(res.body.amount); Buffer.from(res.body.values); ``` +::: + ## When Not To Use It If you don't use Node.js, or you still need to support versions of Node.js that lack methods like `Buffer.from`, then you should not enable this rule. diff --git a/docs/src/rules/no-caller.md b/docs/src/rules/no-caller.md index f7be8267288..c11dd9fd5f1 100644 --- a/docs/src/rules/no-caller.md +++ b/docs/src/rules/no-caller.md @@ -21,6 +21,8 @@ This rule is aimed at discouraging the use of deprecated and sub-optimal code by Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-caller: "error"*/ @@ -37,8 +39,12 @@ function foo(n) { }); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-caller: "error"*/ @@ -54,3 +60,5 @@ function foo(n) { return !(n > 1) ? 1 : factorial(n - 1) * n; }); ``` + +::: diff --git a/docs/src/rules/no-case-declarations.md b/docs/src/rules/no-case-declarations.md index feeb25499f5..3d93c257929 100644 --- a/docs/src/rules/no-case-declarations.md +++ b/docs/src/rules/no-case-declarations.md @@ -25,6 +25,8 @@ This rule aims to prevent access to uninitialized lexical bindings as well as ac Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-case-declarations: "error"*/ /*eslint-env es6*/ @@ -44,8 +46,12 @@ switch (foo) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-case-declarations: "error"*/ /*eslint-env es6*/ @@ -77,6 +83,8 @@ switch (foo) { } ``` +::: + ## When Not To Use It If you depend on fall through behavior and want access to bindings introduced in the case block. diff --git a/docs/src/rules/no-catch-shadow.md b/docs/src/rules/no-catch-shadow.md index 3ed3b038161..1032c56bae4 100644 --- a/docs/src/rules/no-catch-shadow.md +++ b/docs/src/rules/no-catch-shadow.md @@ -29,6 +29,8 @@ This rule is aimed at preventing unexpected behavior in your program that may ar Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-catch-shadow: "error"*/ @@ -51,8 +53,12 @@ try { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-catch-shadow: "error"*/ @@ -75,6 +81,8 @@ try { } ``` +::: + ## When Not To Use It If you do not need to support IE 8 and earlier, you should turn this rule off. diff --git a/docs/src/rules/no-class-assign.md b/docs/src/rules/no-class-assign.md index 9666f8ce7aa..987e19260da 100644 --- a/docs/src/rules/no-class-assign.md +++ b/docs/src/rules/no-class-assign.md @@ -26,6 +26,8 @@ This rule is aimed to flag modifying variables of class declarations. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-class-assign: "error"*/ /*eslint-env es6*/ @@ -34,6 +36,8 @@ class A { } A = 0; ``` +::: + ```js /*eslint no-class-assign: "error"*/ /*eslint-env es6*/ @@ -67,6 +71,8 @@ let A = class A { Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-class-assign: "error"*/ /*eslint-env es6*/ @@ -75,6 +81,8 @@ let A = class A { } A = 0; // A is a variable. ``` +::: + ```js /*eslint no-class-assign: "error"*/ /*eslint-env es6*/ diff --git a/docs/src/rules/no-comma-dangle.md b/docs/src/rules/no-comma-dangle.md index 6498c1045b3..3227dbded87 100644 --- a/docs/src/rules/no-comma-dangle.md +++ b/docs/src/rules/no-comma-dangle.md @@ -24,6 +24,8 @@ This rule is aimed at detecting trailing commas in object literals. As such, it Examples of **incorrect** code for this rule: +::: incorrect + ```js var foo = { bar: "baz", @@ -38,8 +40,12 @@ foo({ }); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js var foo = { bar: "baz", @@ -54,6 +60,8 @@ foo({ }); ``` +::: + ## When Not To Use It If your code will not be run in IE8 or below (a Node.js application, for example) and you'd prefer to allow trailing commas, turn this rule off. diff --git a/docs/src/rules/no-compare-neg-zero.md b/docs/src/rules/no-compare-neg-zero.md index bbfea3d8f60..d71b0906fcf 100644 --- a/docs/src/rules/no-compare-neg-zero.md +++ b/docs/src/rules/no-compare-neg-zero.md @@ -15,6 +15,8 @@ The rule should warn against code that tries to compare against `-0`, since that Examples of **incorrect** code for this rule: +::: incorrect + ```js /* eslint no-compare-neg-zero: "error" */ @@ -23,8 +25,12 @@ if (x === -0) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /* eslint no-compare-neg-zero: "error" */ @@ -33,6 +39,8 @@ if (x === 0) { } ``` +::: + ```js /* eslint no-compare-neg-zero: "error" */ diff --git a/docs/src/rules/no-cond-assign.md b/docs/src/rules/no-cond-assign.md index a1e61504ba8..6b8275d8f3d 100644 --- a/docs/src/rules/no-cond-assign.md +++ b/docs/src/rules/no-cond-assign.md @@ -37,6 +37,8 @@ This rule has a string option: Examples of **incorrect** code for this rule with the default `"except-parens"` option: +::: incorrect + ```js /*eslint no-cond-assign: "error"*/ @@ -55,8 +57,12 @@ function setHeight(someNode) { } ``` +::: + Examples of **correct** code for this rule with the default `"except-parens"` option: +::: correct + ```js /*eslint no-cond-assign: "error"*/ @@ -83,10 +89,14 @@ function setHeight(someNode) { } ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint no-cond-assign: ["error", "always"]*/ @@ -121,8 +131,12 @@ function setHeight(someNode) { } ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint no-cond-assign: ["error", "always"]*/ @@ -132,3 +146,5 @@ if (x === 0) { var b = 1; } ``` + +::: diff --git a/docs/src/rules/no-confusing-arrow.md b/docs/src/rules/no-confusing-arrow.md index 094497135ec..99c371fc7f1 100644 --- a/docs/src/rules/no-confusing-arrow.md +++ b/docs/src/rules/no-confusing-arrow.md @@ -31,6 +31,8 @@ var x = a <= 1 ? 2 : 3; Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-confusing-arrow: "error"*/ /*eslint-env es6*/ @@ -39,8 +41,12 @@ var x = a => 1 ? 2 : 3; var x = (a) => 1 ? 2 : 3; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-confusing-arrow: "error"*/ /*eslint-env es6*/ @@ -52,6 +58,8 @@ var x = (a) => { var x = a => { return 1 ? 2 : 3; }; ``` +::: + ## Options This rule accepts two options argument with the following defaults: @@ -74,6 +82,8 @@ This rule accepts two options argument with the following defaults: Examples of **incorrect** code for this rule with the `{"allowParens": false}` option: +::: incorrect + ```js /*eslint no-confusing-arrow: ["error", {"allowParens": false}]*/ /*eslint-env es6*/ @@ -81,6 +91,8 @@ var x = a => (1 ? 2 : 3); var x = (a) => (1 ? 2 : 3); ``` +::: + `onlyOneSimpleParam` is a boolean setting that can be `true` or `false`(default): 1. `true` relaxes the rule and doesn't report errors if the arrow function has 0 or more than 1 parameters, or the parameter is not an identifier. @@ -88,6 +100,8 @@ var x = (a) => (1 ? 2 : 3); Examples of **correct** code for this rule with the `{"onlyOneSimpleParam": true}` option: +::: correct + ```js /*eslint no-confusing-arrow: ["error", {"onlyOneSimpleParam": true}]*/ /*eslint-env es6*/ @@ -98,3 +112,5 @@ Examples of **correct** code for this rule with the `{"onlyOneSimpleParam": true ([a]) => 1 ? 2 : 3; (...a) => 1 ? 2 : 3; ``` + +::: diff --git a/docs/src/rules/no-console.md b/docs/src/rules/no-console.md index 1b258a3d8a5..a970653b28b 100644 --- a/docs/src/rules/no-console.md +++ b/docs/src/rules/no-console.md @@ -23,6 +23,8 @@ This rule disallows calls or assignments to methods of the `console` object. Examples of **incorrect** code for this rule: +::: incorrect + ```js /* eslint no-console: "error" */ @@ -32,8 +34,12 @@ console.error("Log an error level message."); console.log = foo(); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /* eslint no-console: "error" */ @@ -41,6 +47,8 @@ Examples of **correct** code for this rule: Console.log("Hello world!"); ``` +::: + ## Options This rule has an object option for exceptions: @@ -49,6 +57,8 @@ This rule has an object option for exceptions: Examples of additional **correct** code for this rule with a sample `{ "allow": ["warn", "error"] }` option: +::: correct + ```js /* eslint no-console: ["error", { allow: ["warn", "error"] }] */ @@ -56,6 +66,8 @@ console.warn("Log a warn level message."); console.error("Log an error level message."); ``` +::: + ## When Not To Use It If you're using Node.js, however, `console` is used to output information to the user and so is not strictly used for debugging purposes. If you are developing for Node.js then you most likely do not want this rule enabled. diff --git a/docs/src/rules/no-const-assign.md b/docs/src/rules/no-const-assign.md index 74921f96919..e92ce1472ad 100644 --- a/docs/src/rules/no-const-assign.md +++ b/docs/src/rules/no-const-assign.md @@ -20,6 +20,8 @@ This rule is aimed to flag modifying variables that are declared using `const` k Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-const-assign: "error"*/ /*eslint-env es6*/ @@ -28,6 +30,8 @@ const a = 0; a = 1; ``` +::: + ```js /*eslint no-const-assign: "error"*/ /*eslint-env es6*/ @@ -46,6 +50,8 @@ const a = 0; Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-const-assign: "error"*/ /*eslint-env es6*/ @@ -54,6 +60,8 @@ const a = 0; console.log(a); ``` +::: + ```js /*eslint no-const-assign: "error"*/ /*eslint-env es6*/ diff --git a/docs/src/rules/no-constant-binary-expression.md b/docs/src/rules/no-constant-binary-expression.md index 5f3c42aa4a0..4375ed8a934 100644 --- a/docs/src/rules/no-constant-binary-expression.md +++ b/docs/src/rules/no-constant-binary-expression.md @@ -38,6 +38,8 @@ It also identifies `||`, `&&` and `??` logical expressions which will either alw Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-constant-binary-expression: "error"*/ @@ -54,8 +56,12 @@ const objIsEmpty = someObj === {}; const arrIsEmpty = someArr === []; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-constant-binary-expression: "error"*/ @@ -71,3 +77,5 @@ const objIsEmpty = Object.keys(someObj).length === 0; const arrIsEmpty = someArr.length === 0; ``` + +::: diff --git a/docs/src/rules/no-constant-condition.md b/docs/src/rules/no-constant-condition.md index a2b4e025aac..c7fb1cdf00b 100644 --- a/docs/src/rules/no-constant-condition.md +++ b/docs/src/rules/no-constant-condition.md @@ -28,6 +28,8 @@ This rule disallows constant expressions in the test condition of: Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-constant-condition: "error"*/ @@ -78,8 +80,12 @@ do { var result = 0 ? a : b; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-constant-condition: "error"*/ @@ -102,6 +108,8 @@ do { var result = x !== 0 ? a : b; ``` +::: + ## Options ### checkLoops @@ -110,6 +118,8 @@ Set to `true` by default. Setting this option to `false` allows constant express Examples of **correct** code for when `checkLoops` is `false`: +::: correct + ```js /*eslint no-constant-condition: ["error", { "checkLoops": false }]*/ @@ -134,3 +144,5 @@ do { } } while (true) ``` + +::: diff --git a/docs/src/rules/no-constructor-return.md b/docs/src/rules/no-constructor-return.md index 4be5f30bedd..ef29ce7ceee 100644 --- a/docs/src/rules/no-constructor-return.md +++ b/docs/src/rules/no-constructor-return.md @@ -15,6 +15,8 @@ This rule disallows return statements in the constructor of a class. Note that r Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-constructor-return: "error"*/ @@ -34,8 +36,12 @@ class B { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-constructor-return: "error"*/ @@ -55,3 +61,5 @@ class D { } } ``` + +::: diff --git a/docs/src/rules/no-continue.md b/docs/src/rules/no-continue.md index fff6611a3e6..6789b0d469a 100644 --- a/docs/src/rules/no-continue.md +++ b/docs/src/rules/no-continue.md @@ -28,6 +28,8 @@ This rule disallows `continue` statements. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-continue: "error"*/ @@ -43,6 +45,8 @@ for(i = 0; i < 10; i++) { } ``` +::: + ```js /*eslint no-continue: "error"*/ @@ -60,6 +64,8 @@ labeledLoop: for(i = 0; i < 10; i++) { Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-continue: "error"*/ @@ -73,6 +79,8 @@ for(i = 0; i < 10; i++) { } ``` +::: + ## Compatibility * **JSLint**: `continue` diff --git a/docs/src/rules/no-control-regex.md b/docs/src/rules/no-control-regex.md index d7bdff87f18..068a7228fd9 100644 --- a/docs/src/rules/no-control-regex.md +++ b/docs/src/rules/no-control-regex.md @@ -29,6 +29,8 @@ Control escapes such as `\t` and `\n` are allowed by this rule. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-control-regex: "error"*/ @@ -41,8 +43,12 @@ var pattern6 = new RegExp("\x0C"); // raw U+000C character in the pattern var pattern7 = new RegExp("\\x0C"); // \x0C pattern ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-control-regex: "error"*/ @@ -56,6 +62,8 @@ var pattern7 = new RegExp("\\t"); var pattern8 = new RegExp("\\n"); ``` +::: + ## Known Limitations When checking `RegExp` constructor calls, this rule examines evaluated regular expression patterns. Therefore, although this rule intends to allow syntax such as `\t`, it doesn't allow `new RegExp("\t")` since the evaluated pattern (string value of `"\t"`) contains a raw control character (the TAB character). diff --git a/docs/src/rules/no-debugger.md b/docs/src/rules/no-debugger.md index 67588f209d0..08edf8de5c8 100644 --- a/docs/src/rules/no-debugger.md +++ b/docs/src/rules/no-debugger.md @@ -22,6 +22,8 @@ This rule disallows `debugger` statements. Example of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-debugger: "error"*/ @@ -31,8 +33,12 @@ function isTruthy(x) { } ``` +::: + Example of **correct** code for this rule: +::: correct + ```js /*eslint no-debugger: "error"*/ @@ -41,6 +47,8 @@ function isTruthy(x) { } ``` +::: + ## When Not To Use It If your code is still very much in development and don't want to worry about stripping `debugger` statements, then turn this rule off. You'll generally want to turn it back on when testing code prior to deployment. diff --git a/docs/src/rules/no-delete-var.md b/docs/src/rules/no-delete-var.md index b14dd57f536..7ec9a453c4c 100644 --- a/docs/src/rules/no-delete-var.md +++ b/docs/src/rules/no-delete-var.md @@ -19,9 +19,13 @@ If ESLint parses code in strict mode, the parser (instead of this rule) reports Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-delete-var: "error"*/ var x; delete x; ``` + +::: diff --git a/docs/src/rules/no-div-regex.md b/docs/src/rules/no-div-regex.md index 60a1440a284..5c3b794b300 100644 --- a/docs/src/rules/no-div-regex.md +++ b/docs/src/rules/no-div-regex.md @@ -24,16 +24,24 @@ This is used to disambiguate the division operator to not confuse users. Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint no-div-regex: "error"*/ function bar() { return /=foo/; } ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint no-div-regex: "error"*/ function bar() { return /[=]foo/; } ``` + +::: diff --git a/docs/src/rules/no-dupe-args.md b/docs/src/rules/no-dupe-args.md index c28f5cd5fd4..904ca6dd219 100644 --- a/docs/src/rules/no-dupe-args.md +++ b/docs/src/rules/no-dupe-args.md @@ -19,6 +19,8 @@ If ESLint parses code in strict mode, the parser (instead of this rule) reports Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-dupe-args: "error"*/ @@ -31,8 +33,12 @@ var bar = function (a, b, a) { }; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-dupe-args: "error"*/ @@ -44,3 +50,5 @@ var bar = function (a, b, c) { console.log(a, b, c); }; ``` + +::: diff --git a/docs/src/rules/no-dupe-class-members.md b/docs/src/rules/no-dupe-class-members.md index f50088b0da8..f54b06be56e 100644 --- a/docs/src/rules/no-dupe-class-members.md +++ b/docs/src/rules/no-dupe-class-members.md @@ -32,6 +32,8 @@ This rule is aimed to flag the use of duplicate names in class members. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-dupe-class-members: "error"*/ @@ -61,8 +63,12 @@ class Foo { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-dupe-class-members: "error"*/ @@ -92,6 +98,8 @@ class Foo { } ``` +::: + ## When Not To Use It This rule should not be used in ES3/5 environments. diff --git a/docs/src/rules/no-dupe-else-if.md b/docs/src/rules/no-dupe-else-if.md index 38a9c969114..f53128e64be 100644 --- a/docs/src/rules/no-dupe-else-if.md +++ b/docs/src/rules/no-dupe-else-if.md @@ -44,6 +44,8 @@ This rule disallows duplicate conditions in the same `if-else-if` chain. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-dupe-else-if: "error"*/ @@ -78,8 +80,12 @@ if (n === 1) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-dupe-else-if: "error"*/ @@ -114,10 +120,14 @@ if (n === 1) { } ``` +::: + This rule can also detect some cases where the conditions are not identical, but the branch can never execute due to the logic of `||` and `&&` operators. Examples of additional **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-dupe-else-if: "error"*/ @@ -162,6 +172,8 @@ if (a) { } ``` +::: + Please note that this rule does not compare conditions from the chain with conditions inside statements, and will not warn in the cases such as follows: ```js diff --git a/docs/src/rules/no-dupe-keys.md b/docs/src/rules/no-dupe-keys.md index a5bfda0ec1b..58e04139bc8 100644 --- a/docs/src/rules/no-dupe-keys.md +++ b/docs/src/rules/no-dupe-keys.md @@ -24,6 +24,8 @@ This rule disallows duplicate keys in object literals. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-dupe-keys: "error"*/ @@ -43,8 +45,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-dupe-keys: "error"*/ @@ -53,3 +59,5 @@ var foo = { quxx: "qux" }; ``` + +::: diff --git a/docs/src/rules/no-duplicate-case.md b/docs/src/rules/no-duplicate-case.md index 455689f33d9..147fd87cd7e 100644 --- a/docs/src/rules/no-duplicate-case.md +++ b/docs/src/rules/no-duplicate-case.md @@ -17,6 +17,8 @@ This rule disallows duplicate test expressions in `case` clauses of `switch` sta Examples of **incorrect** code for this rule: +:::incorrect + ```js /*eslint no-duplicate-case: "error"*/ @@ -57,8 +59,12 @@ switch (a) { } ``` +::: + Examples of **correct** code for this rule: +:::correct + ```js /*eslint no-duplicate-case: "error"*/ @@ -99,6 +105,8 @@ switch (a) { } ``` +::: + ## When Not To Use It In rare cases where identical test expressions in `case` clauses produce different values, which necessarily means that the expressions are causing and relying on side effects, you will have to disable this rule. diff --git a/docs/src/rules/no-duplicate-imports.md b/docs/src/rules/no-duplicate-imports.md index 88d7a2d8706..ece810a9f60 100644 --- a/docs/src/rules/no-duplicate-imports.md +++ b/docs/src/rules/no-duplicate-imports.md @@ -23,6 +23,8 @@ This rule requires that all imports from a single module that can be merged exis Example of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-duplicate-imports: "error"*/ @@ -31,8 +33,12 @@ import something from 'another-module'; import { find } from 'module'; ``` +::: + Example of **correct** code for this rule: +::: correct + ```js /*eslint no-duplicate-imports: "error"*/ @@ -40,8 +46,12 @@ import { merge, find } from 'module'; import something from 'another-module'; ``` +::: + Example of **correct** code for this rule: +::: correct + ```js /*eslint no-duplicate-imports: "error"*/ @@ -50,6 +60,8 @@ import { merge } from 'module'; import * as something from 'module'; ``` +::: + ## Options This rule takes one optional argument, an object with a single key, `includeExports` which is a `boolean`. It defaults to `false`. @@ -58,6 +70,8 @@ If re-exporting from an imported module, you should add the imports to the `impo Example of **incorrect** code for this rule with the `{ "includeExports": true }` option: +::: incorrect + ```js /*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/ @@ -66,8 +80,12 @@ import { merge } from 'module'; export { find } from 'module'; ``` +::: + Example of **correct** code for this rule with the `{ "includeExports": true }` option: +::: correct + ```js /*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/ @@ -76,8 +94,12 @@ import { merge, find } from 'module'; export { find }; ``` +::: + Example of **correct** code for this rule with the `{ "includeExports": true }` option: +::: correct + ```js /*eslint no-duplicate-imports: ["error", { "includeExports": true }]*/ @@ -89,3 +111,5 @@ export * as something from 'module'; // cannot be written differently export * from 'module'; ``` + +::: diff --git a/docs/src/rules/no-else-return.md b/docs/src/rules/no-else-return.md index 8903c4a6082..9037caa90bf 100644 --- a/docs/src/rules/no-else-return.md +++ b/docs/src/rules/no-else-return.md @@ -36,6 +36,8 @@ This rule has an object option: Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-else-return: "error"*/ @@ -91,8 +93,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-else-return: "error"*/ @@ -133,10 +139,14 @@ function foo() { } ``` +::: + ### `allowElseIf: false` Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-else-return: ["error", {allowElseIf: false}]*/ @@ -149,8 +159,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-else-return: ["error", {allowElseIf: false}]*/ @@ -164,3 +178,5 @@ function foo() { } } ``` + +::: diff --git a/docs/src/rules/no-empty-character-class.md b/docs/src/rules/no-empty-character-class.md index 146e0cdf876..1c27cd96d00 100644 --- a/docs/src/rules/no-empty-character-class.md +++ b/docs/src/rules/no-empty-character-class.md @@ -21,6 +21,8 @@ This rule disallows empty character classes in regular expressions. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-empty-character-class: "error"*/ @@ -28,8 +30,12 @@ Examples of **incorrect** code for this rule: "abcdefg".match(/^abc[]/); // null ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-empty-character-class: "error"*/ @@ -40,6 +46,8 @@ Examples of **correct** code for this rule: "abcdefg".match(/^abc[a-z]/); // ["abcd"] ``` +::: + ## Known Limitations This rule does not report empty character classes in the string argument of calls to the `RegExp` constructor. diff --git a/docs/src/rules/no-empty-class.md b/docs/src/rules/no-empty-class.md index 0673cd3f7ed..e9431716084 100644 --- a/docs/src/rules/no-empty-class.md +++ b/docs/src/rules/no-empty-class.md @@ -21,6 +21,8 @@ This rule is aimed at highlighting possible typos and unexpected behavior in reg Examples of **incorrect** code for this rule: +::: incorrect + ```js var foo = /^abc[]/; @@ -29,8 +31,12 @@ var foo = /^abc[]/; bar.match(/^abc[]/); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js var foo = /^abc/; @@ -38,3 +44,5 @@ var foo = /^abc[a-z]/; var bar = new RegExp("^abc[]"); ``` + +::: diff --git a/docs/src/rules/no-empty-function.md b/docs/src/rules/no-empty-function.md index e80566c189d..13df518d3c8 100644 --- a/docs/src/rules/no-empty-function.md +++ b/docs/src/rules/no-empty-function.md @@ -33,6 +33,8 @@ A function will not be considered a problem if it contains a comment. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-empty-function: "error"*/ /*eslint-env es6*/ @@ -82,8 +84,12 @@ class A { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-empty-function: "error"*/ /*eslint-env es6*/ @@ -173,6 +179,8 @@ class A { } ``` +::: + ## Options This rule has an option to allow specific kinds of functions to be empty. @@ -193,6 +201,8 @@ This rule has an option to allow specific kinds of functions to be empty. Examples of **correct** code for the `{ "allow": ["functions"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["functions"] }]*/ @@ -205,10 +215,14 @@ var obj = { }; ``` +::: + ### allow: arrowFunctions Examples of **correct** code for the `{ "allow": ["arrowFunctions"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["arrowFunctions"] }]*/ /*eslint-env es6*/ @@ -216,10 +230,14 @@ Examples of **correct** code for the `{ "allow": ["arrowFunctions"] }` option: var foo = () => {}; ``` +::: + ### allow: generatorFunctions Examples of **correct** code for the `{ "allow": ["generatorFunctions"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["generatorFunctions"] }]*/ /*eslint-env es6*/ @@ -233,10 +251,14 @@ var obj = { }; ``` +::: + ### allow: methods Examples of **correct** code for the `{ "allow": ["methods"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["methods"] }]*/ /*eslint-env es6*/ @@ -251,10 +273,14 @@ class A { } ``` +::: + ### allow: generatorMethods Examples of **correct** code for the `{ "allow": ["generatorMethods"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["generatorMethods"] }]*/ /*eslint-env es6*/ @@ -269,10 +295,14 @@ class A { } ``` +::: + ### allow: getters Examples of **correct** code for the `{ "allow": ["getters"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["getters"] }]*/ /*eslint-env es6*/ @@ -287,10 +317,14 @@ class A { } ``` +::: + ### allow: setters Examples of **correct** code for the `{ "allow": ["setters"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["setters"] }]*/ /*eslint-env es6*/ @@ -305,10 +339,14 @@ class A { } ``` +::: + ### allow: constructors Examples of **correct** code for the `{ "allow": ["constructors"] }` option: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["constructors"] }]*/ /*eslint-env es6*/ @@ -318,10 +356,14 @@ class A { } ``` +::: + ### allow: asyncFunctions Examples of **correct** code for the `{ "allow": ["asyncFunctions"] }` options: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["asyncFunctions"] }]*/ /*eslint-env es2017*/ @@ -329,10 +371,14 @@ Examples of **correct** code for the `{ "allow": ["asyncFunctions"] }` options: async function a(){} ``` +::: + ### allow: asyncMethods Examples of **correct** code for the `{ "allow": ["asyncMethods"] }` options: +::: correct + ```js /*eslint no-empty-function: ["error", { "allow": ["asyncMethods"] }]*/ /*eslint-env es2017*/ @@ -347,6 +393,8 @@ class A { } ``` +::: + ## When Not To Use It If you don't want to be notified about empty functions, then it's safe to disable this rule. diff --git a/docs/src/rules/no-empty-label.md b/docs/src/rules/no-empty-label.md index 72ef07f7587..b12d403aeec 100644 --- a/docs/src/rules/no-empty-label.md +++ b/docs/src/rules/no-empty-label.md @@ -21,6 +21,8 @@ This error occurs when a label is used to mark a statement that is not an iterat Example of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-empty-label: "error"*/ @@ -28,8 +30,12 @@ labeled: var x = 10; ``` +::: + Example of **correct** code for this rule: +::: correct + ```js /*eslint no-empty-label: "error"*/ @@ -39,6 +45,8 @@ for (var i=10; i; i--) { } ``` +::: + ## When Not To Use It If you don't want to be notified about usage of labels, then it's safe to disable this rule. diff --git a/docs/src/rules/no-empty-pattern.md b/docs/src/rules/no-empty-pattern.md index 93b0865b677..509c91e517e 100644 --- a/docs/src/rules/no-empty-pattern.md +++ b/docs/src/rules/no-empty-pattern.md @@ -38,6 +38,8 @@ This rule aims to flag any empty patterns in destructured objects and arrays, an Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-empty-pattern: "error"*/ @@ -51,8 +53,12 @@ function foo({a: {}}) {} function foo({a: []}) {} ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-empty-pattern: "error"*/ @@ -61,3 +67,5 @@ var {a = []} = foo; function foo({a = {}}) {} function foo({a = []}) {} ``` + +::: diff --git a/docs/src/rules/no-empty.md b/docs/src/rules/no-empty.md index 4b1d428a105..f0727af1300 100644 --- a/docs/src/rules/no-empty.md +++ b/docs/src/rules/no-empty.md @@ -19,6 +19,8 @@ This rule disallows empty block statements. This rule ignores block statements w Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-empty: "error"*/ @@ -40,8 +42,12 @@ try { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-empty: "error"*/ @@ -66,6 +72,8 @@ try { } ``` +::: + ## Options This rule has an object option for exceptions: @@ -76,6 +84,8 @@ This rule has an object option for exceptions: Examples of additional **correct** code for this rule with the `{ "allowEmptyCatch": true }` option: +::: correct + ```js /* eslint no-empty: ["error", { "allowEmptyCatch": true }] */ try { @@ -91,6 +101,8 @@ finally { } ``` +::: + ## When Not To Use It If you intentionally use empty block statements then you can disable this rule. diff --git a/docs/src/rules/no-eq-null.md b/docs/src/rules/no-eq-null.md index fdacac401ad..f3d82704a94 100644 --- a/docs/src/rules/no-eq-null.md +++ b/docs/src/rules/no-eq-null.md @@ -21,6 +21,8 @@ The `no-eq-null` rule aims reduce potential bug and unwanted behavior by ensurin Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-eq-null: "error"*/ @@ -33,8 +35,12 @@ while (qux != null) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-eq-null: "error"*/ @@ -47,6 +53,8 @@ while (qux !== null) { } ``` +::: + ## When Not To Use It If you want to enforce type-checking operations in general, use the more powerful [eqeqeq](./eqeqeq) instead. diff --git a/docs/src/rules/no-eval.md b/docs/src/rules/no-eval.md index f559ebe6641..5e2e43750d0 100644 --- a/docs/src/rules/no-eval.md +++ b/docs/src/rules/no-eval.md @@ -26,6 +26,8 @@ This rule is aimed at preventing potentially dangerous, unnecessary, and slow co Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-eval: "error"*/ @@ -42,8 +44,12 @@ foo("var a = 0"); this.eval("var a = 0"); ``` +::: + Example of additional **incorrect** code for this rule when `browser` environment is set to `true`: +::: incorrect + ```js /*eslint no-eval: "error"*/ /*eslint-env browser*/ @@ -51,8 +57,12 @@ Example of additional **incorrect** code for this rule when `browser` environmen window.eval("var a = 0"); ``` +::: + Example of additional **incorrect** code for this rule when `node` environment is set to `true`: +::: incorrect + ```js /*eslint no-eval: "error"*/ /*eslint-env node*/ @@ -60,8 +70,12 @@ Example of additional **incorrect** code for this rule when `node` environment i global.eval("var a = 0"); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-eval: "error"*/ /*eslint-env es6*/ @@ -89,6 +103,8 @@ class A { } ``` +::: + ## Options This rule has an option to allow indirect calls to `eval`. @@ -102,6 +118,8 @@ Indirect calls to `eval` are less dangerous than direct calls to `eval` because Example of **incorrect** code for this rule with the `{"allowIndirect": true}` option: +::: incorrect + ```js /*eslint no-eval: "error"*/ @@ -110,8 +128,12 @@ var obj = { x: "foo" }, value = eval("obj." + key); ``` +::: + Examples of **correct** code for this rule with the `{"allowIndirect": true}` option: +::: correct + ```js /*eslint no-eval: "error"*/ @@ -123,6 +145,8 @@ foo("var a = 0"); this.eval("var a = 0"); ``` +::: + ```js /*eslint no-eval: "error"*/ /*eslint-env browser*/ diff --git a/docs/src/rules/no-ex-assign.md b/docs/src/rules/no-ex-assign.md index a4e186626df..45ec00c4bfd 100644 --- a/docs/src/rules/no-ex-assign.md +++ b/docs/src/rules/no-ex-assign.md @@ -20,6 +20,8 @@ This rule disallows reassigning exceptions in `catch` clauses. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-ex-assign: "error"*/ @@ -30,8 +32,12 @@ try { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-ex-assign: "error"*/ @@ -41,3 +47,5 @@ try { var foo = 10; } ``` + +::: diff --git a/docs/src/rules/no-extend-native.md b/docs/src/rules/no-extend-native.md index eef3c34a0bd..323d5ed07e3 100644 --- a/docs/src/rules/no-extend-native.md +++ b/docs/src/rules/no-extend-native.md @@ -37,6 +37,8 @@ Disallows directly modifying the prototype of builtin objects. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-extend-native: "error"*/ @@ -44,6 +46,8 @@ Object.prototype.a = "a"; Object.defineProperty(Array.prototype, "times", { value: 999 }); ``` +::: + ## Options This rule accepts an `exceptions` option, which can be used to specify a list of builtins for which extensions will be allowed. @@ -52,12 +56,16 @@ This rule accepts an `exceptions` option, which can be used to specify a list of Examples of **correct** code for the sample `{ "exceptions": ["Object"] }` option: +::: correct + ```js /*eslint no-extend-native: ["error", { "exceptions": ["Object"] }]*/ Object.prototype.a = "a"; ``` +::: + ## Known Limitations This rule *does not* report any of the following less obvious approaches to modify the prototype of builtin objects: diff --git a/docs/src/rules/no-extra-bind.md b/docs/src/rules/no-extra-bind.md index eebe43a4f6a..0c7b9d86de9 100644 --- a/docs/src/rules/no-extra-bind.md +++ b/docs/src/rules/no-extra-bind.md @@ -45,6 +45,8 @@ This rule is aimed at avoiding the unnecessary use of `bind()` and as such will Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-extra-bind: "error"*/ /*eslint-env es6*/ @@ -74,8 +76,12 @@ var x = function () { }.bind(baz); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-extra-bind: "error"*/ @@ -88,6 +94,8 @@ var x = function (a) { }.bind(foo, bar); ``` +::: + ## When Not To Use It If you are not concerned about unnecessary calls to `bind()`, you can safely disable this rule. diff --git a/docs/src/rules/no-extra-boolean-cast.md b/docs/src/rules/no-extra-boolean-cast.md index 550f6985afd..201dba0fb17 100644 --- a/docs/src/rules/no-extra-boolean-cast.md +++ b/docs/src/rules/no-extra-boolean-cast.md @@ -33,6 +33,8 @@ This rule disallows unnecessary boolean casts. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-extra-boolean-cast: "error"*/ @@ -65,8 +67,12 @@ for (; !!foo; ) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-extra-boolean-cast: "error"*/ @@ -80,6 +86,8 @@ function foo() { var foo = bar ? !!baz : !!bat; ``` +::: + ## Options This rule has an object option: @@ -90,6 +98,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with `"enforceForLogicalOperands"` option set to `true`: +::: incorrect + ```js /*eslint no-extra-boolean-cast: ["error", {"enforceForLogicalOperands": true}]*/ @@ -110,8 +120,12 @@ foo && Boolean(bar) ? baz : bat var foo = new Boolean(!!bar || baz) ``` +::: + Examples of **correct** code for this rule with `"enforceForLogicalOperands"` option set to `true`: +::: correct + ```js /*eslint no-extra-boolean-cast: ["error", {"enforceForLogicalOperands": true}]*/ @@ -133,3 +147,5 @@ var foo = new Boolean(bar || baz) var foo = !!bar || baz; ``` + +::: diff --git a/docs/src/rules/no-extra-label.md b/docs/src/rules/no-extra-label.md index c5dda3f8d0a..dc37efe8b55 100644 --- a/docs/src/rules/no-extra-label.md +++ b/docs/src/rules/no-extra-label.md @@ -30,6 +30,8 @@ This rule is aimed at eliminating unnecessary labels. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-extra-label: "error"*/ @@ -47,8 +49,12 @@ C: switch (a) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-extra-label: "error"*/ @@ -84,6 +90,8 @@ C: switch (a) { } ``` +::: + ## When Not To Use It If you don't want to be notified about usage of labels, then it's safe to disable this rule. diff --git a/docs/src/rules/no-extra-parens.md b/docs/src/rules/no-extra-parens.md index d5afd39a306..8dc991746ef 100644 --- a/docs/src/rules/no-extra-parens.md +++ b/docs/src/rules/no-extra-parens.md @@ -47,6 +47,8 @@ This rule has an object option for exceptions to the `"all"` option: Examples of **incorrect** code for this rule with the default `"all"` option: +::: incorrect + ```js /* eslint no-extra-parens: "error" */ @@ -73,8 +75,12 @@ class B { } ``` +::: + Examples of **correct** code for this rule with the default `"all"` option: +::: correct + ```js /* eslint no-extra-parens: "error" */ @@ -105,10 +111,14 @@ class B { } ``` +::: + ### conditionalAssign Examples of **correct** code for this rule with the `"all"` and `{ "conditionalAssign": false }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { "conditionalAssign": false }] */ @@ -121,10 +131,14 @@ do; while ((foo = bar())) for (;(a = b);); ``` +::: + ### returnAssign Examples of **correct** code for this rule with the `"all"` and `{ "returnAssign": false }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { "returnAssign": false }] */ @@ -141,10 +155,14 @@ b => (b = 1); b => b ? (c = d) : (c = e); ``` +::: + ### nestedBinaryExpressions Examples of **correct** code for this rule with the `"all"` and `{ "nestedBinaryExpressions": false }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { "nestedBinaryExpressions": false }] */ @@ -153,10 +171,14 @@ x = a + (b * c); x = (a * b) / c; ``` +::: + ### ignoreJSX Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "all" }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "all" }] */ const Component = (
) @@ -167,16 +189,24 @@ const Component = ( ) ``` +::: + Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options: +::: incorrect + ```js /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */ const Component = (
) const Component = (

) ``` +::: + Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "multi-line" }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "multi-line" }] */ const Component = ( @@ -191,8 +221,12 @@ const Component = ( ) ``` +::: + Examples of **incorrect** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options: +::: incorrect + ```js /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */ const Component = ( @@ -207,18 +241,26 @@ const Component = ( ) ``` +::: + Examples of **correct** code for this rule with the `all` and `{ "ignoreJSX": "single-line" }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { ignoreJSX: "single-line" }] */ const Component = (
) const Component = (

) ``` +::: + ### enforceForArrowConditionals Examples of **correct** code for this rule with the `"all"` and `{ "enforceForArrowConditionals": false }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { "enforceForArrowConditionals": false }] */ @@ -226,10 +268,14 @@ const b = a => 1 ? 2 : 3; const d = c => (1 ? 2 : 3); ``` +::: + ### enforceForSequenceExpressions Examples of **correct** code for this rule with the `"all"` and `{ "enforceForSequenceExpressions": false }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { "enforceForSequenceExpressions": false }] */ @@ -240,10 +286,14 @@ if ((val = foo(), val < 10)) {} while ((val = foo(), val < 10)); ``` +::: + ### enforceForNewInMemberExpressions Examples of **correct** code for this rule with the `"all"` and `{ "enforceForNewInMemberExpressions": false }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { "enforceForNewInMemberExpressions": false }] */ @@ -254,10 +304,14 @@ const quux = (new Bar())[baz]; (new Bar()).doSomething(); ``` +::: + ### enforceForFunctionPrototypeMethods Examples of **correct** code for this rule with the `"all"` and `{ "enforceForFunctionPrototypeMethods": false }` options: +::: correct + ```js /* eslint no-extra-parens: ["error", "all", { "enforceForFunctionPrototypeMethods": false }] */ @@ -270,10 +324,14 @@ const baz = (function () {}.call()); const quux = (function () {}.apply()); ``` +::: + ### functions Examples of **incorrect** code for this rule with the `"functions"` option: +::: incorrect + ```js /* eslint no-extra-parens: ["error", "functions"] */ @@ -282,8 +340,12 @@ Examples of **incorrect** code for this rule with the `"functions"` option: var y = (function () {return 1;}); ``` +::: + Examples of **correct** code for this rule with the `"functions"` option: +::: correct + ```js /* eslint no-extra-parens: ["error", "functions"] */ @@ -303,3 +365,5 @@ a = (b * c); typeof (a); ``` + +::: diff --git a/docs/src/rules/no-extra-semi.md b/docs/src/rules/no-extra-semi.md index ae7b65e3697..906162c3a07 100644 --- a/docs/src/rules/no-extra-semi.md +++ b/docs/src/rules/no-extra-semi.md @@ -22,6 +22,8 @@ This rule disallows unnecessary semicolons. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-extra-semi: "error"*/ @@ -44,8 +46,12 @@ class C { }; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-extra-semi: "error"*/ @@ -72,6 +78,8 @@ class C { } ``` +::: + ## When Not To Use It If you intentionally use extra semicolons then you can disable this rule. diff --git a/docs/src/rules/no-extra-strict.md b/docs/src/rules/no-extra-strict.md index 28dba6d8a97..1e6f5c97ee0 100644 --- a/docs/src/rules/no-extra-strict.md +++ b/docs/src/rules/no-extra-strict.md @@ -28,6 +28,8 @@ This rule is aimed at preventing unnecessary `"use strict";` directives. As such Example of **incorrect** code for this rule: +::: incorrect + ```js "use strict"; @@ -37,8 +39,12 @@ Example of **incorrect** code for this rule: }()); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js "use strict"; @@ -47,6 +53,8 @@ Examples of **correct** code for this rule: }()); ``` +::: + ```js (function () { "use strict"; diff --git a/docs/src/rules/no-fallthrough.md b/docs/src/rules/no-fallthrough.md index d7a3e8031cf..c73bf3a0943 100644 --- a/docs/src/rules/no-fallthrough.md +++ b/docs/src/rules/no-fallthrough.md @@ -86,6 +86,8 @@ This rule is aimed at eliminating unintentional fallthrough of one case to the o Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-fallthrough: "error"*/ @@ -98,8 +100,12 @@ switch(foo) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-fallthrough: "error"*/ @@ -159,6 +165,8 @@ switch(foo) { } ``` +::: + Note that the last `case` statement in these examples does not cause a warning because there is nothing to fall through into. ## Options @@ -171,6 +179,8 @@ This rule accepts a single options argument: Examples of **correct** code for the `{ "commentPattern": "break[\\s\\w]*omitted" }` option: +::: correct + ```js /*eslint no-fallthrough: ["error", { "commentPattern": "break[\\s\\w]*omitted" }]*/ @@ -193,6 +203,8 @@ switch(foo) { } ``` +::: + ## When Not To Use It If you don't want to enforce that each `case` statement should end with a `throw`, `return`, `break`, or comment, then you can safely turn this rule off. diff --git a/docs/src/rules/no-floating-decimal.md b/docs/src/rules/no-floating-decimal.md index da348a152ac..e8ae79a3612 100644 --- a/docs/src/rules/no-floating-decimal.md +++ b/docs/src/rules/no-floating-decimal.md @@ -25,6 +25,8 @@ This rule is aimed at eliminating floating decimal points and will warn whenever Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-floating-decimal: "error"*/ @@ -33,8 +35,12 @@ var num = 2.; var num = -.7; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-floating-decimal: "error"*/ @@ -43,6 +49,8 @@ var num = 2.0; var num = -0.7; ``` +::: + ## When Not To Use It If you aren't concerned about misinterpreting floating decimal point values, then you can safely turn this rule off. diff --git a/docs/src/rules/no-func-assign.md b/docs/src/rules/no-func-assign.md index cbd56279286..178f136b67b 100644 --- a/docs/src/rules/no-func-assign.md +++ b/docs/src/rules/no-func-assign.md @@ -22,6 +22,8 @@ This rule disallows reassigning `function` declarations. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-func-assign: "error"*/ @@ -37,8 +39,12 @@ var a = function hello() { }; ``` +::: + Examples of **incorrect** code for this rule, unlike the corresponding rule in JSHint: +::: incorrect + ```js /*eslint no-func-assign: "error"*/ @@ -46,8 +52,12 @@ foo = bar; function foo() {} ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-func-assign: "error"*/ @@ -62,3 +72,5 @@ function foo() { var foo = bar; // `foo` is shadowed. } ``` + +::: diff --git a/docs/src/rules/no-global-assign.md b/docs/src/rules/no-global-assign.md index 4e10466d272..58bd4fc5479 100644 --- a/docs/src/rules/no-global-assign.md +++ b/docs/src/rules/no-global-assign.md @@ -32,6 +32,8 @@ ESLint has the capability to configure global variables as read-only. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-global-assign: "error"*/ @@ -39,6 +41,8 @@ Object = null undefined = 1 ``` +::: + ```js /*eslint no-global-assign: "error"*/ /*eslint-env browser*/ @@ -57,6 +61,8 @@ a = 1 Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-global-assign: "error"*/ @@ -65,6 +71,8 @@ var b = 1 b = 2 ``` +::: + ```js /*eslint no-global-assign: "error"*/ /*eslint-env browser*/ diff --git a/docs/src/rules/no-implicit-coercion.md b/docs/src/rules/no-implicit-coercion.md index cc1338c72fe..cfc5b54f3db 100644 --- a/docs/src/rules/no-implicit-coercion.md +++ b/docs/src/rules/no-implicit-coercion.md @@ -54,6 +54,8 @@ Note that operator `+` in `allow` list would allow `+foo` (number coercion) as w Examples of **incorrect** code for the default `{ "boolean": true }` option: +::: incorrect + ```js /*eslint no-implicit-coercion: "error"*/ @@ -62,8 +64,12 @@ var b = ~foo.indexOf("."); // bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling. ``` +::: + Examples of **correct** code for the default `{ "boolean": true }` option: +::: correct + ```js /*eslint no-implicit-coercion: "error"*/ @@ -73,10 +79,14 @@ var b = foo.indexOf(".") !== -1; var n = ~foo; // This is a just bitwise not. ``` +::: + ### number Examples of **incorrect** code for the default `{ "number": true }` option: +::: incorrect + ```js /*eslint no-implicit-coercion: "error"*/ @@ -84,8 +94,12 @@ var n = +foo; var n = 1 * foo; ``` +::: + Examples of **correct** code for the default `{ "number": true }` option: +::: correct + ```js /*eslint no-implicit-coercion: "error"*/ @@ -94,10 +108,14 @@ var n = parseFloat(foo); var n = parseInt(foo, 10); ``` +::: + ### string Examples of **incorrect** code for the default `{ "string": true }` option: +::: incorrect + ```js /*eslint no-implicit-coercion: "error"*/ @@ -107,8 +125,12 @@ foo += ""; foo += ``; ``` +::: + Examples of **correct** code for the default `{ "string": true }` option: +::: correct + ```js /*eslint no-implicit-coercion: "error"*/ @@ -116,20 +138,28 @@ var s = String(foo); foo = String(foo); ``` +::: + ### disallowTemplateShorthand This option is **not** affected by the `string` option. Examples of **incorrect** code for the `{ "disallowTemplateShorthand": true }` option: +::: incorrect + ```js /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/ var s = `${foo}`; ``` +::: + Examples of **correct** code for the `{ "disallowTemplateShorthand": true }` option: +::: correct + ```js /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/ @@ -144,20 +174,28 @@ var s = `${foo}${bar}`; var s = tag`${foo}`; ``` +::: + Examples of **correct** code for the default `{ "disallowTemplateShorthand": false }` option: +::: correct + ```js /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/ var s = `${foo}`; ``` +::: + ### allow Using `allow` list, we can override and allow specific operators. Examples of **correct** code for the sample `{ "allow": ["!!", "~"] }` option: +::: correct + ```js /*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/ @@ -165,6 +203,8 @@ var b = !!foo; var b = ~foo.indexOf("."); ``` +::: + ## When Not To Use It If you don't want to be notified about shorter notations for the type conversion, you can safely disable this rule. diff --git a/docs/src/rules/no-implicit-globals.md b/docs/src/rules/no-implicit-globals.md index 8c075ab62f7..ddca1632019 100644 --- a/docs/src/rules/no-implicit-globals.md +++ b/docs/src/rules/no-implicit-globals.md @@ -46,6 +46,8 @@ This rule disallows `var` and `function` declarations at the top-level script sc Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-implicit-globals: "error"*/ @@ -54,8 +56,12 @@ var foo = 1; function bar() {} ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-implicit-globals: "error"*/ @@ -71,8 +77,12 @@ window.bar = function() {}; })(); ``` +::: + Examples of **correct** code for this rule with `"parserOptions": { "sourceType": "module" }` in the ESLint configuration: +::: correct + ```js /*eslint no-implicit-globals: "error"*/ @@ -81,6 +91,8 @@ var foo = 1; function bar() {} ``` +::: + ### Global variable leaks When the code is not in `strict` mode, an assignment to an undeclared variable creates @@ -90,6 +102,8 @@ This does not apply to ES modules since the module code is implicitly in `strict Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-implicit-globals: "error"*/ @@ -100,6 +114,8 @@ Bar.prototype.baz = function () { }; ``` +::: + ### Read-only global variables This rule also disallows redeclarations of read-only global variables and assignments to read-only global variables. @@ -113,6 +129,8 @@ or in a `/*global */` comment. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-implicit-globals: "error"*/ @@ -124,6 +142,8 @@ Array = []; var Object; ``` +::: + ### `const`, `let` and `class` declarations Lexical declarations `const` and `let`, as well as `class` declarations, create variables that are block-scoped. @@ -137,6 +157,8 @@ If the variable is intended to be local to the script, wrap the code with a bloc Examples of **correct** code for this rule with `"lexicalBindings"` option set to `false` (default): +::: correct + ```js /*eslint no-implicit-globals: ["error", {"lexicalBindings": false}]*/ @@ -147,8 +169,12 @@ let baz; class Bar {} ``` +::: + Examples of **incorrect** code for this rule with `"lexicalBindings"` option set to `true`: +::: incorrect + ```js /*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ @@ -159,8 +185,12 @@ let baz; class Bar {} ``` +::: + Examples of **correct** code for this rule with `"lexicalBindings"` option set to `true`: +::: correct + ```js /*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ @@ -177,6 +207,8 @@ Examples of **correct** code for this rule with `"lexicalBindings"` option set t }()); ``` +::: + If you intend to create a global `const` or `let` variable or a global `class` declaration, to be used from other scripts, be aware that there are certain differences when compared to the traditional methods, which are `var` declarations and assigning to a property of the global `window` object: @@ -191,6 +223,8 @@ Even the `typeof` check is not safe from TDZ reference exceptions. Examples of **incorrect** code for this rule with `"lexicalBindings"` option set to `true`: +::: incorrect + ```js /*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ @@ -203,8 +237,12 @@ const MyGlobalFunction = (function() { }()); ``` +::: + Examples of **correct** code for this rule with `"lexicalBindings"` option set to `true`: +::: correct + ```js /*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/ @@ -217,6 +255,8 @@ window.MyGlobalFunction = (function() { }()); ``` +::: + ## When Not To Use It In the case of a browser script, if you want to be able to explicitly declare variables and functions in the global scope, diff --git a/docs/src/rules/no-implied-eval.md b/docs/src/rules/no-implied-eval.md index 66596a9e56f..22f5b465038 100644 --- a/docs/src/rules/no-implied-eval.md +++ b/docs/src/rules/no-implied-eval.md @@ -34,6 +34,8 @@ This rule aims to eliminate implied `eval()` through the use of `setTimeout()`, Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-implied-eval: "error"*/ @@ -48,8 +50,12 @@ window.setTimeout("count = 5", 10); window.setInterval("foo = bar", 10); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-implied-eval: "error"*/ @@ -62,6 +68,8 @@ setInterval(function() { }, 100); ``` +::: + ## When Not To Use It If you want to allow `setTimeout()` and `setInterval()` with string arguments, then you can safely disable this rule. diff --git a/docs/src/rules/no-import-assign.md b/docs/src/rules/no-import-assign.md index 56844f47727..4da47a23e67 100644 --- a/docs/src/rules/no-import-assign.md +++ b/docs/src/rules/no-import-assign.md @@ -17,6 +17,8 @@ This rule warns the assignments, increments, and decrements of imported bindings Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-import-assign: "error"*/ @@ -31,8 +33,12 @@ mod_ns = {} // ERROR: 'mod_ns' is readonly. Object.assign(mod_ns, { foo: "foo" }) // ERROR: The members of 'mod_ns' are readonly. ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-import-assign: "error"*/ @@ -50,6 +56,8 @@ function test(obj) { test(mod_ns) // Not errored because it doesn't know that 'test' updates the member of the argument. ``` +::: + ## When Not To Use It If you don't want to be notified about modifying imported bindings, you can disable this rule. diff --git a/docs/src/rules/no-inline-comments.md b/docs/src/rules/no-inline-comments.md index 4ef95d8f154..21912325edd 100644 --- a/docs/src/rules/no-inline-comments.md +++ b/docs/src/rules/no-inline-comments.md @@ -16,6 +16,8 @@ This rule disallows comments on the same line as code. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-inline-comments: "error"*/ @@ -31,8 +33,12 @@ function getRandomNumber(){ var c = 3; /* A block comment after code */ ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-inline-comments: "error"*/ @@ -43,12 +49,16 @@ var bar = 5; //This is a comment below a line of code ``` +::: + ### JSX exception Comments inside the curly braces in JSX are allowed to be on the same line as the braces, but only if they are not on the same line with other code, and the braces do not enclose an actual expression. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-inline-comments: "error"*/ @@ -63,8 +73,12 @@ var bar = ( ); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-inline-comments: "error"*/ @@ -95,6 +109,8 @@ var quux = ( ) ``` +::: + ## Options ### ignorePattern @@ -103,16 +119,24 @@ To make this rule ignore specific comments, set the `ignorePattern` option to a Examples of **correct** code for the `ignorePattern` option: +::: correct + ```js /*eslint no-inline-comments: ["error", { "ignorePattern": "webpackChunkName:\\s.+" }]*/ import(/* webpackChunkName: "my-chunk-name" */ './locale/en'); ``` +::: + Examples of **incorrect** code for the `ignorePattern` option: +::: incorrect + ```js /*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */ var foo = 4; // other thing ``` + +::: diff --git a/docs/src/rules/no-inner-declarations.md b/docs/src/rules/no-inner-declarations.md index 5753d4107f5..e6211d0e6f1 100644 --- a/docs/src/rules/no-inner-declarations.md +++ b/docs/src/rules/no-inner-declarations.md @@ -78,6 +78,8 @@ This rule has a string option: Examples of **incorrect** code for this rule with the default `"functions"` option: +::: incorrect + ```js /*eslint no-inner-declarations: "error"*/ @@ -102,8 +104,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the default `"functions"` option: +::: correct + ```js /*eslint no-inner-declarations: "error"*/ @@ -131,10 +137,14 @@ if (test) { if (foo) var a; ``` +::: + ### both Examples of **incorrect** code for this rule with the `"both"` option: +::: incorrect + ```js /*eslint no-inner-declarations: ["error", "both"]*/ @@ -161,8 +171,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `"both"` option: +::: correct + ```js /*eslint no-inner-declarations: ["error", "both"]*/ @@ -183,6 +197,8 @@ class C { } ``` +::: + ## When Not To Use It The function declaration portion rule will be rendered obsolete when [block-scoped functions](https://bugzilla.mozilla.org/show_bug.cgi?id=585536) land in ES6, but until then, it should be left on to enforce valid constructions. Disable checking variable declarations when using [block-scoped-var](block-scoped-var) or if declaring variables in nested blocks is acceptable despite hoisting. diff --git a/docs/src/rules/no-invalid-regexp.md b/docs/src/rules/no-invalid-regexp.md index 5c70b81aae1..cf96a55541f 100644 --- a/docs/src/rules/no-invalid-regexp.md +++ b/docs/src/rules/no-invalid-regexp.md @@ -19,6 +19,8 @@ This rule disallows invalid regular expression strings in `RegExp` constructors. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-invalid-regexp: "error"*/ @@ -29,8 +31,12 @@ RegExp('.', 'z') new RegExp('\\') ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-invalid-regexp: "error"*/ @@ -41,6 +47,8 @@ new RegExp this.RegExp('[') ``` +::: + Please note that this rule validates regular expressions per the latest ECMAScript specification, regardless of your parser settings. If you want to allow additional constructor flags for any reason, you can specify them using the `allowConstructorFlags` option. These flags will then be ignored by the rule. @@ -55,6 +63,8 @@ This rule has an object option for exceptions: Examples of **correct** code for this rule with the `{ "allowConstructorFlags": ["a", "z"] }` option: +::: correct + ```js /*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["a", "z"] }]*/ @@ -62,3 +72,5 @@ new RegExp('.', 'a') new RegExp('.', 'az') ``` + +::: diff --git a/docs/src/rules/no-invalid-this.md b/docs/src/rules/no-invalid-this.md index 4358deedeee..187ed11df30 100644 --- a/docs/src/rules/no-invalid-this.md +++ b/docs/src/rules/no-invalid-this.md @@ -50,6 +50,8 @@ With `"parserOptions": { "sourceType": "module" }` in the ESLint configuration, Examples of **incorrect** code for this rule in strict mode: +::: incorrect + ```js /*eslint no-invalid-this: "error"*/ /*eslint-env es6*/ @@ -92,8 +94,12 @@ foo.forEach(function() { }); ``` +::: + Examples of **correct** code for this rule in strict mode: +::: correct + ```js /*eslint no-invalid-this: "error"*/ /*eslint-env es6*/ @@ -222,6 +228,8 @@ function foo() { } ``` +::: + ## Options This rule has an object option, with one option: @@ -236,6 +244,8 @@ Set `"capIsConstructor"` to `false` if you want those functions to be treated as Examples of **incorrect** code for this rule with `"capIsConstructor"` option set to `false`: +::: incorrect + ```js /*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/ @@ -258,8 +268,12 @@ Baz = function() { }; ``` +::: + Examples of **correct** code for this rule with `"capIsConstructor"` option set to `false`: +::: correct + ```js /*eslint no-invalid-this: ["error", { "capIsConstructor": false }]*/ @@ -271,6 +285,8 @@ obj.Foo = function Foo() { }; ``` +::: + ## When Not To Use It If you don't want to be notified about usage of `this` keyword outside of classes or class-like objects, you can safely disable this rule. diff --git a/docs/src/rules/no-irregular-whitespace.md b/docs/src/rules/no-irregular-whitespace.md index f470a893875..ffd352563ce 100644 --- a/docs/src/rules/no-irregular-whitespace.md +++ b/docs/src/rules/no-irregular-whitespace.md @@ -70,6 +70,8 @@ This rule has an object option for exceptions: Examples of **incorrect** code for this rule with the default `{ "skipStrings": true }` option: +::: incorrect + ```js /*eslint no-irregular-whitespace: "error"*/ @@ -115,8 +117,12 @@ function thing() { } ``` +::: + Examples of **correct** code for this rule with the default `{ "skipStrings": true }` option: +::: correct + ```js /*eslint no-irregular-whitespace: "error"*/ @@ -133,10 +139,14 @@ function thing() { } ``` +::: + ### skipComments Examples of additional **correct** code for this rule with the `{ "skipComments": true }` option: +::: correct + ```js /*eslint no-irregular-whitespace: ["error", { "skipComments": true }]*/ @@ -149,10 +159,14 @@ Description : some descriptive text */ ``` +::: + ### skipRegExps Examples of additional **correct** code for this rule with the `{ "skipRegExps": true }` option: +::: correct + ```js /*eslint no-irregular-whitespace: ["error", { "skipRegExps": true }]*/ @@ -161,10 +175,14 @@ function thing() { } ``` +::: + ### skipTemplates Examples of additional **correct** code for this rule with the `{ "skipTemplates": true }` option: +::: correct + ```js /*eslint no-irregular-whitespace: ["error", { "skipTemplates": true }]*/ /*eslint-env es6*/ @@ -174,6 +192,8 @@ function thing() { } ``` +::: + ## When Not To Use It If you decide that you wish to use whitespace other than tabs and spaces outside of strings in your application. diff --git a/docs/src/rules/no-iterator.md b/docs/src/rules/no-iterator.md index 87aab8ce4e1..9abb28e96ea 100644 --- a/docs/src/rules/no-iterator.md +++ b/docs/src/rules/no-iterator.md @@ -27,6 +27,8 @@ This rule is aimed at preventing errors that may arise from using the `__iterato Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-iterator: "error"*/ @@ -40,10 +42,16 @@ foo["__iterator__"] = function () {}; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-iterator: "error"*/ var __iterator__ = foo; // Not using the `__iterator__` property. ``` + +::: diff --git a/docs/src/rules/no-label-var.md b/docs/src/rules/no-label-var.md index 3f98e2de32c..9785c9417ac 100644 --- a/docs/src/rules/no-label-var.md +++ b/docs/src/rules/no-label-var.md @@ -17,6 +17,8 @@ This rule aims to create clearer code by disallowing the bad practice of creatin Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-label-var: "error"*/ @@ -29,8 +31,12 @@ x: } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-label-var: "error"*/ @@ -48,6 +54,8 @@ q: } ``` +::: + ## When Not To Use It If you don't want to be notified about usage of labels, then it's safe to disable this rule. diff --git a/docs/src/rules/no-labels.md b/docs/src/rules/no-labels.md index 1165439538c..a772673f814 100644 --- a/docs/src/rules/no-labels.md +++ b/docs/src/rules/no-labels.md @@ -33,6 +33,8 @@ This rule aims to eliminate the use of labeled statements in JavaScript. It will Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-labels: "error"*/ @@ -68,8 +70,12 @@ label: } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-labels: "error"*/ @@ -86,6 +92,8 @@ while (true) { } ``` +::: + ## Options The options allow labels with loop or switch statements: @@ -100,6 +108,8 @@ However, this way is ultra rare, not well-known, so this would be confusing deve Examples of **correct** code for the `{ "allowLoop": true }` option: +::: correct + ```js /*eslint no-labels: ["error", { "allowLoop": true }]*/ @@ -109,10 +119,14 @@ label: } ``` +::: + ### allowSwitch Examples of **correct** code for the `{ "allowSwitch": true }` option: +::: correct + ```js /*eslint no-labels: ["error", { "allowSwitch": true }]*/ @@ -123,6 +137,8 @@ label: } ``` +::: + ## When Not To Use It If you need to use labeled statements everywhere, then you can safely disable this rule. diff --git a/docs/src/rules/no-lone-blocks.md b/docs/src/rules/no-lone-blocks.md index c3ee3c3d69f..fad22b2c227 100644 --- a/docs/src/rules/no-lone-blocks.md +++ b/docs/src/rules/no-lone-blocks.md @@ -23,6 +23,8 @@ This rule aims to eliminate unnecessary and potentially confusing blocks at the Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-lone-blocks: "error"*/ @@ -59,8 +61,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with ES6 environment: +::: correct + ```js /*eslint no-lone-blocks: "error"*/ /*eslint-env es6*/ @@ -107,8 +113,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with ES6 environment and strict mode via `"parserOptions": { "sourceType": "module" }` in the ESLint configuration or `"use strict"` directive in the code: +::: correct + ```js /*eslint no-lone-blocks: "error"*/ /*eslint-env es6*/ @@ -119,3 +129,5 @@ Examples of **correct** code for this rule with ES6 environment and strict mode function foo() {} } ``` + +::: diff --git a/docs/src/rules/no-lonely-if.md b/docs/src/rules/no-lonely-if.md index 8f62be51694..4be0c1a528e 100644 --- a/docs/src/rules/no-lonely-if.md +++ b/docs/src/rules/no-lonely-if.md @@ -37,6 +37,8 @@ This rule disallows `if` statements as the only statement in `else` blocks. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-lonely-if: "error"*/ @@ -59,8 +61,12 @@ if (condition) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-lonely-if: "error"*/ @@ -88,6 +94,8 @@ if (condition) { } ``` +::: + ## When Not To Use It Disable this rule if the code is clearer without requiring the `else if` form. diff --git a/docs/src/rules/no-loop-func.md b/docs/src/rules/no-loop-func.md index 9ab8e60e480..f8aac815a2a 100644 --- a/docs/src/rules/no-loop-func.md +++ b/docs/src/rules/no-loop-func.md @@ -41,6 +41,8 @@ This rule disallows any function within a loop that contains unsafe references ( Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-loop-func: "error"*/ /*eslint-env es6*/ @@ -73,8 +75,12 @@ for (let i = 0; i < 10; ++i) { foo = 100; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-loop-func: "error"*/ /*eslint-env es6*/ @@ -102,3 +108,5 @@ for (let i=10; i; i--) { } //... no modifications of foo after this loop ... ``` + +::: diff --git a/docs/src/rules/no-loss-of-precision.md b/docs/src/rules/no-loss-of-precision.md index b8a4050c529..eb5fe640e79 100644 --- a/docs/src/rules/no-loss-of-precision.md +++ b/docs/src/rules/no-loss-of-precision.md @@ -17,6 +17,8 @@ In JS, `Number`s are stored as double-precision floating-point numbers according Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-loss-of-precision: "error"*/ @@ -28,8 +30,12 @@ const x = 0X20000000000001 const x = 0X2_000000000_0001; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-loss-of-precision: "error"*/ @@ -41,3 +47,5 @@ const x = 0x1FFFFFFFFFFFFF const x = 9007199254740991 const x = 9007_1992547409_91 ``` + +::: diff --git a/docs/src/rules/no-magic-numbers.md b/docs/src/rules/no-magic-numbers.md index f53d2e433b0..7bf5343b5ec 100644 --- a/docs/src/rules/no-magic-numbers.md +++ b/docs/src/rules/no-magic-numbers.md @@ -22,6 +22,8 @@ are declared as constants to make their meaning explicit. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-magic-numbers: "error"*/ @@ -29,6 +31,8 @@ var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * 0.25); ``` +::: + ```js /*eslint no-magic-numbers: "error"*/ @@ -47,6 +51,8 @@ SECONDS = 60; Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-magic-numbers: "error"*/ @@ -56,6 +62,8 @@ var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * TAX); ``` +::: + ## Options ### ignore @@ -68,6 +76,8 @@ If it's a string, the text must be parsed as `bigint` literal (e.g., `"100n"`). Examples of **correct** code for the sample `{ "ignore": [1] }` option: +::: correct + ```js /*eslint no-magic-numbers: ["error", { "ignore": [1] }]*/ @@ -75,14 +85,20 @@ var data = ['foo', 'bar', 'baz']; var dataLast = data.length && data[data.length - 1]; ``` +::: + Examples of **correct** code for the sample `{ "ignore": ["1n"] }` option: +::: correct + ```js /*eslint no-magic-numbers: ["error", { "ignore": ["1n"] }]*/ foo(1n); ``` +::: + ### ignoreArrayIndexes A boolean to specify if numbers used in the context of array indexes (e.g., `data[2]`) are considered okay. `false` by default. @@ -95,6 +111,8 @@ Additionally, since the maximum [array length](https://developer.mozilla.org/en- Examples of **correct** code for the `{ "ignoreArrayIndexes": true }` option: +::: correct + ```js /*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/ @@ -115,8 +133,12 @@ a = data[10n]; // same as data[10], 10n will be coerced to "10" a = data[4294967294]; // max array index ``` +::: + Examples of **incorrect** code for the `{ "ignoreArrayIndexes": true }` option: +::: incorrect + ```js /*eslint no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/ @@ -135,12 +157,16 @@ a = data[4294967295]; // above the max array index a = data[1e500]; // same as data["Infinity"] ``` +::: + ### ignoreDefaultValues A boolean to specify if numbers used in default value assignments are considered okay. `false` by default. Examples of **correct** code for the `{ "ignoreDefaultValues": true }` option: +::: correct + ```js /*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/ @@ -149,6 +175,8 @@ const { tax = 0.25 } = accountancy; function mapParallel(concurrency = 3) { /***/ } ``` +::: + ```js /*eslint no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/ @@ -162,6 +190,8 @@ A boolean to specify if we should check for the const keyword in variable declar Examples of **incorrect** code for the `{ "enforceConst": true }` option: +::: incorrect + ```js /*eslint no-magic-numbers: ["error", { "enforceConst": true }]*/ @@ -171,12 +201,16 @@ var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * TAX); ``` +::: + ### detectObjects A boolean to specify if we should detect numbers when setting object properties for example. `false` by default. Examples of **incorrect** code for the `{ "detectObjects": true }` option: +::: incorrect + ```js /*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/ @@ -188,8 +222,12 @@ var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax); ``` +::: + Examples of **correct** code for the `{ "detectObjects": true }` option: +::: correct + ```js /*eslint no-magic-numbers: ["error", { "detectObjects": true }]*/ @@ -202,3 +240,5 @@ var magic = { var dutyFreePrice = 100, finalPrice = dutyFreePrice + (dutyFreePrice * magic.tax); ``` + +::: diff --git a/docs/src/rules/no-misleading-character-class.md b/docs/src/rules/no-misleading-character-class.md index 9d190820146..bba870dc51b 100644 --- a/docs/src/rules/no-misleading-character-class.md +++ b/docs/src/rules/no-misleading-character-class.md @@ -59,6 +59,8 @@ This rule reports the regular expressions which include multiple code point char Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-misleading-character-class: error */ @@ -70,8 +72,12 @@ Examples of **incorrect** code for this rule: /^[👍]$/ ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-misleading-character-class: error */ @@ -79,6 +85,8 @@ Examples of **correct** code for this rule: /^[👍]$/u ``` +::: + ## When Not To Use It You can turn this rule off if you don't want to check RegExp character class syntax for multiple code point characters. diff --git a/docs/src/rules/no-mixed-operators.md b/docs/src/rules/no-mixed-operators.md index d47e47781dd..67f50e9c2d7 100644 --- a/docs/src/rules/no-mixed-operators.md +++ b/docs/src/rules/no-mixed-operators.md @@ -41,6 +41,8 @@ If you use both this and [no-extra-parens](no-extra-parens) rule together, you n Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-mixed-operators: "error"*/ @@ -48,8 +50,12 @@ var foo = a && b < 0 || c > 0 || d + 1 === 0; var foo = a + b * c; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-mixed-operators: "error"*/ @@ -61,6 +67,8 @@ var foo = a + (b * c); var foo = (a + b) * c; ``` +::: + ## Options ```json @@ -106,6 +114,8 @@ In this case, this rule checks if bitwise operators and logical operators are mi Examples of **incorrect** code for this rule with `{"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}` option: +::: incorrect + ```js /*eslint no-mixed-operators: ["error", {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}]*/ @@ -113,6 +123,8 @@ var foo = a && b < 0 || c > 0 || d + 1 === 0; var foo = a & b | c; ``` +::: + ```js /*eslint no-mixed-operators: ["error", {"groups": [["&&", "||", "?:"]]}]*/ @@ -125,6 +137,8 @@ var baz = a ? b : c || d; Examples of **correct** code for this rule with `{"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}` option: +::: correct + ```js /*eslint no-mixed-operators: ["error", {"groups": [["&", "|", "^", "~", "<<", ">>", ">>>"], ["&&", "||"]]}]*/ @@ -139,6 +153,8 @@ var foo = a + (b * c); var foo = (a + b) * c; ``` +::: + ```js /*eslint no-mixed-operators: ["error", {"groups": [["&&", "||", "?:"]]}]*/ @@ -155,6 +171,8 @@ var baz = (a ? b : c) || d; Examples of **correct** code for this rule with `{"allowSamePrecedence": true}` option: +::: correct + ```js /*eslint no-mixed-operators: ["error", {"allowSamePrecedence": true}]*/ @@ -162,8 +180,12 @@ Examples of **correct** code for this rule with `{"allowSamePrecedence": true}` var foo = a + b - c; ``` +::: + Examples of **incorrect** code for this rule with `{"allowSamePrecedence": false}` option: +::: incorrect + ```js /*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/ @@ -171,8 +193,12 @@ Examples of **incorrect** code for this rule with `{"allowSamePrecedence": false var foo = a + b - c; ``` +::: + Examples of **correct** code for this rule with `{"allowSamePrecedence": false}` option: +::: correct + ```js /*eslint no-mixed-operators: ["error", {"allowSamePrecedence": false}]*/ @@ -180,6 +206,8 @@ Examples of **correct** code for this rule with `{"allowSamePrecedence": false}` var foo = (a + b) - c; ``` +::: + ## When Not To Use It If you don't want to be notified about mixed operators, then it's safe to disable this rule. diff --git a/docs/src/rules/no-mixed-requires.md b/docs/src/rules/no-mixed-requires.md index 4137e9a66fb..9ba7358c218 100644 --- a/docs/src/rules/no-mixed-requires.md +++ b/docs/src/rules/no-mixed-requires.md @@ -46,6 +46,8 @@ Configuring this rule with one boolean option `true` is deprecated. Examples of **incorrect** code for this rule with the default `{ "grouping": false, "allowCall": false }` options: +::: incorrect + ```js /*eslint no-mixed-requires: "error"*/ @@ -57,8 +59,12 @@ var async = require('async'), eslint = require('eslint'); ``` +::: + Examples of **correct** code for this rule with the default `{ "grouping": false, "allowCall": false }` options: +::: correct + ```js /*eslint no-mixed-requires: "error"*/ @@ -78,10 +84,14 @@ var foo = require('foo' + VERSION), baz = require(); ``` +::: + ### grouping Examples of **incorrect** code for this rule with the `{ "grouping": true }` option: +::: incorrect + ```js /*eslint no-mixed-requires: ["error", { "grouping": true }]*/ @@ -94,10 +104,14 @@ var foo = require('foo'), bar = require(getBarModuleName()); ``` +::: + ### allowCall Examples of **incorrect** code for this rule with the `{ "allowCall": true }` option: +::: incorrect + ```js /*eslint no-mixed-requires: ["error", { "allowCall": true }]*/ @@ -106,8 +120,12 @@ var async = require('async'), eslint = require('eslint'); ``` +::: + Examples of **correct** code for this rule with the `{ "allowCall": true }` option: +::: correct + ```js /*eslint no-mixed-requires: ["error", { "allowCall": true }]*/ @@ -116,6 +134,8 @@ var async = require('async'), eslint = require('eslint'); ``` +::: + ## Known Limitations * The implementation is not aware of any local functions with the name `require` that may shadow Node.js' global `require`. diff --git a/docs/src/rules/no-mixed-spaces-and-tabs.md b/docs/src/rules/no-mixed-spaces-and-tabs.md index 2e7dc0cfccc..429c8909969 100644 --- a/docs/src/rules/no-mixed-spaces-and-tabs.md +++ b/docs/src/rules/no-mixed-spaces-and-tabs.md @@ -19,6 +19,8 @@ This rule disallows mixed spaces and tabs for indentation. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-mixed-spaces-and-tabs: "error"*/ @@ -37,8 +39,12 @@ function main() { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-mixed-spaces-and-tabs: "error"*/ @@ -48,6 +54,8 @@ function add(x, y) { } ``` +::: + ## Options This rule has a string option. @@ -58,6 +66,8 @@ This rule has a string option. Examples of **correct** code for this rule with the `"smart-tabs"` option: +::: correct + ```js /*eslint no-mixed-spaces-and-tabs: ["error", "smart-tabs"]*/ @@ -69,3 +79,5 @@ function main() { y = 7; } ``` + +::: diff --git a/docs/src/rules/no-multi-assign.md b/docs/src/rules/no-multi-assign.md index 34909f92c2b..2335e755cf7 100644 --- a/docs/src/rules/no-multi-assign.md +++ b/docs/src/rules/no-multi-assign.md @@ -25,6 +25,8 @@ This rule disallows using multiple assignments within a single statement. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-multi-assign: "error"*/ @@ -43,8 +45,12 @@ class Foo { a = b = "quux"; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-multi-assign: "error"*/ @@ -67,6 +73,8 @@ a = "quux"; b = "quux"; ``` +::: + ## Options This rule has an object option: @@ -77,6 +85,8 @@ This rule has an object option: Examples of **correct** code for the `{ "ignoreNonDeclaration": true }` option: +::: correct + ```js /*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/ @@ -89,8 +99,12 @@ const y = {}; x.one = y.one = 1; ``` +::: + Examples of **incorrect** code for the `{ "ignoreNonDeclaration": true }` option: +::: incorrect + ```js /*eslint no-multi-assign: ["error", { "ignoreNonDeclaration": true }]*/ @@ -102,3 +116,5 @@ class Foo { a = b = 10; } ``` + +::: diff --git a/docs/src/rules/no-multi-spaces.md b/docs/src/rules/no-multi-spaces.md index 373ef1ea4ec..e790756004f 100644 --- a/docs/src/rules/no-multi-spaces.md +++ b/docs/src/rules/no-multi-spaces.md @@ -39,6 +39,8 @@ This rule aims to disallow multiple whitespace around logical expressions, condi Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-multi-spaces: "error"*/ @@ -53,8 +55,12 @@ var arr = [1, 2]; a ? b: c ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-multi-spaces: "error"*/ @@ -69,6 +75,8 @@ var arr = [1, 2]; a ? b: c ``` +::: + ## Options This rule's configuration consists of an object with the following properties: @@ -80,6 +88,8 @@ This rule's configuration consists of an object with the following properties: Examples of **incorrect** code for this rule with the `{ "ignoreEOLComments": false }` (default) option: +::: incorrect + ```js /*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/ @@ -89,8 +99,12 @@ var x = 5; /* multiline */ ``` +::: + Examples of **correct** code for this rule with the `{ "ignoreEOLComments": false }` (default) option: +::: correct + ```js /*eslint no-multi-spaces: ["error", { ignoreEOLComments: false }]*/ @@ -100,8 +114,12 @@ var x = 5; /* multiline */ ``` +::: + Examples of **correct** code for this rule with the `{ "ignoreEOLComments": true }` option: +::: correct + ```js /*eslint no-multi-spaces: ["error", { ignoreEOLComments: true }]*/ @@ -115,6 +133,8 @@ var x = 5; /* multiline */ ``` +::: + ### exceptions To avoid contradictions with other rules that require multiple spaces, this rule has an `exceptions` option to ignore certain nodes. @@ -125,6 +145,8 @@ Only the `Property` node type is ignored by default, because for the [key-spacin Examples of **correct** code for the default `"exceptions": { "Property": true }` option: +::: correct + ```js /*eslint no-multi-spaces: "error"*/ /*eslint key-spacing: ["error", { align: "value" }]*/ @@ -135,8 +157,12 @@ var obj = { }; ``` +::: + Examples of **incorrect** code for the `"exceptions": { "Property": false }` option: +::: incorrect + ```js /*eslint no-multi-spaces: ["error", { exceptions: { "Property": false } }]*/ /*eslint key-spacing: ["error", { align: "value" }]*/ @@ -147,16 +173,24 @@ var obj = { }; ``` +::: + Examples of **correct** code for the `"exceptions": { "BinaryExpression": true }` option: +::: correct + ```js /*eslint no-multi-spaces: ["error", { exceptions: { "BinaryExpression": true } }]*/ var a = 1 * 2; ``` +::: + Examples of **correct** code for the `"exceptions": { "VariableDeclarator": true }` option: +::: correct + ```js /*eslint no-multi-spaces: ["error", { exceptions: { "VariableDeclarator": true } }]*/ @@ -164,8 +198,12 @@ var someVar = 'foo'; var someOtherVar = 'barBaz'; ``` +::: + Examples of **correct** code for the `"exceptions": { "ImportDeclaration": true }` option: +::: correct + ```js /*eslint no-multi-spaces: ["error", { exceptions: { "ImportDeclaration": true } }]*/ @@ -173,6 +211,8 @@ import mod from 'mod'; import someOtherMod from 'some-other-mod'; ``` +::: + ## When Not To Use It If you don't want to check and disallow multiple spaces, then you should turn this rule off. diff --git a/docs/src/rules/no-multi-str.md b/docs/src/rules/no-multi-str.md index caeb62defce..e5e946ff051 100644 --- a/docs/src/rules/no-multi-str.md +++ b/docs/src/rules/no-multi-str.md @@ -22,6 +22,8 @@ This rule is aimed at preventing the use of multiline strings. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-multi-str: "error"*/ @@ -29,8 +31,12 @@ var x = "some very \ long text"; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-multi-str: "error"*/ @@ -39,3 +45,5 @@ var x = "some very long text"; var x = "some very " + "long text"; ``` + +::: diff --git a/docs/src/rules/no-multiple-empty-lines.md b/docs/src/rules/no-multiple-empty-lines.md index 8da7a54dbce..30916751571 100644 --- a/docs/src/rules/no-multiple-empty-lines.md +++ b/docs/src/rules/no-multiple-empty-lines.md @@ -27,6 +27,8 @@ This rule has an object option: Examples of **incorrect** code for this rule with the default `{ "max": 2 }` option: +::: incorrect + ```js /*eslint no-multiple-empty-lines: "error"*/ @@ -36,8 +38,12 @@ var foo = 5; var bar = 3; ``` +::: + Examples of **correct** code for this rule with the default `{ "max": 2 }` option: +::: correct + ```js /*eslint no-multiple-empty-lines: "error"*/ @@ -46,10 +52,14 @@ var foo = 5; var bar = 3; ``` +::: + ### maxEOF Examples of **incorrect** code for this rule with the `{ max: 2, maxEOF: 0 }` options: +::: incorrect + ```js /*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxEOF": 0 }]*/ @@ -59,8 +69,12 @@ var bar = 3; ``` +::: + Examples of **correct** code for this rule with the `{ max: 2, maxEOF: 0 }` options: +::: correct + ```js /*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxEOF": 0 }]*/ @@ -69,6 +83,8 @@ var foo = 5; var bar = 3; ``` +::: + **Note**: Although this ensures zero empty lines at the EOF, most editors will still show one empty line at the end if the file ends with a line break, as illustrated below. There is no empty line at the end of a file after the last `\n`, although editors may show an additional line. A true additional line would be represented by `\n\n`. **Incorrect**: @@ -100,6 +116,8 @@ var bar = 3; Examples of **incorrect** code for this rule with the `{ max: 2, maxBOF: 1 }` options: +::: incorrect + ```js /*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxBOF": 1 }]*/ @@ -108,8 +126,12 @@ var foo = 5; var bar = 3; ``` +::: + Examples of **correct** code for this rule with the `{ max: 2, maxBOF: 1 }` options: +::: correct + ```js /*eslint no-multiple-empty-lines: ["error", { "max": 2, "maxBOF": 1}]*/ @@ -118,6 +140,8 @@ var foo = 5; var bar = 3; ``` +::: + ## When Not To Use It If you do not care about extra blank lines, turn this off. diff --git a/docs/src/rules/no-native-reassign.md b/docs/src/rules/no-native-reassign.md index ce6b557b18d..6e96e61e8d2 100644 --- a/docs/src/rules/no-native-reassign.md +++ b/docs/src/rules/no-native-reassign.md @@ -32,6 +32,8 @@ ESLint has the capability to configure global variables as read-only. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-native-reassign: "error"*/ @@ -39,6 +41,8 @@ Object = null undefined = 1 ``` +::: + ```js /*eslint no-native-reassign: "error"*/ /*eslint-env browser*/ @@ -57,6 +61,8 @@ a = 1 Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-native-reassign: "error"*/ @@ -65,6 +71,8 @@ var b = 1 b = 2 ``` +::: + ```js /*eslint no-native-reassign: "error"*/ /*eslint-env browser*/ diff --git a/docs/src/rules/no-negated-condition.md b/docs/src/rules/no-negated-condition.md index c6f62f0f08c..2da90a60dcd 100644 --- a/docs/src/rules/no-negated-condition.md +++ b/docs/src/rules/no-negated-condition.md @@ -18,6 +18,8 @@ This rule disallows negated conditions in either of the following: Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-negated-condition: "error"*/ @@ -42,8 +44,12 @@ if (a !== b) { !a ? c : b ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-negated-condition: "error"*/ @@ -63,3 +69,5 @@ if (a != b) { a ? b : c ``` + +::: diff --git a/docs/src/rules/no-negated-in-lhs.md b/docs/src/rules/no-negated-in-lhs.md index 052b863202d..51061110212 100644 --- a/docs/src/rules/no-negated-in-lhs.md +++ b/docs/src/rules/no-negated-in-lhs.md @@ -17,6 +17,8 @@ This rule disallows negating the left operand in `in` expressions. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-negated-in-lhs: "error"*/ @@ -26,8 +28,12 @@ if(!key in object) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-negated-in-lhs: "error"*/ @@ -41,6 +47,8 @@ if(('' + !key) in object) { } ``` +::: + ## When Not To Use It Never. diff --git a/docs/src/rules/no-nested-ternary.md b/docs/src/rules/no-nested-ternary.md index c1906cb1677..4a9c2bcc960 100644 --- a/docs/src/rules/no-nested-ternary.md +++ b/docs/src/rules/no-nested-ternary.md @@ -22,6 +22,8 @@ The `no-nested-ternary` rule disallows nested ternary expressions. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-nested-ternary: "error"*/ @@ -30,8 +32,12 @@ var thing = foo ? bar : baz === qux ? quxx : foobar; foo ? baz === qux ? quxx() : foobar() : bar(); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-nested-ternary: "error"*/ @@ -47,3 +53,5 @@ if (foo) { thing = foobar; } ``` + +::: diff --git a/docs/src/rules/no-new-func.md b/docs/src/rules/no-new-func.md index 28e663b4455..5f855045a5c 100644 --- a/docs/src/rules/no-new-func.md +++ b/docs/src/rules/no-new-func.md @@ -25,6 +25,8 @@ This error is raised to highlight the use of a bad practice. By passing a string Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-new-func: "error"*/ @@ -36,8 +38,12 @@ var x = Function.bind(null, "a", "b", "return a + b")(); var f = Function.bind(null, "a", "b", "return a + b"); // assuming that the result of Function.bind(...) will be eventually called. ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-new-func: "error"*/ @@ -46,6 +52,8 @@ var x = function (a, b) { }; ``` +::: + ## When Not To Use It In more advanced cases where you really need to use the `Function` constructor. diff --git a/docs/src/rules/no-new-object.md b/docs/src/rules/no-new-object.md index 9655b13bbdb..eede27cb437 100644 --- a/docs/src/rules/no-new-object.md +++ b/docs/src/rules/no-new-object.md @@ -32,6 +32,8 @@ This rule disallows `Object` constructors. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-new-object: "error"*/ @@ -40,8 +42,12 @@ var myObject = new Object(); new Object(); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-new-object: "error"*/ @@ -53,6 +59,8 @@ var Object = function Object() {}; new Object(); ``` +::: + ## When Not To Use It If you wish to allow the use of the `Object` constructor, you can safely turn this rule off. diff --git a/docs/src/rules/no-new-require.md b/docs/src/rules/no-new-require.md index 13500baec20..52761720842 100644 --- a/docs/src/rules/no-new-require.md +++ b/docs/src/rules/no-new-require.md @@ -35,14 +35,20 @@ This rule aims to eliminate use of the `new require` expression. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-new-require: "error"*/ var appHeader = new require('app-header'); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-new-require: "error"*/ @@ -50,6 +56,8 @@ var AppHeader = require('app-header'); var appHeader = new AppHeader(); ``` +::: + ## When Not To Use It If you are using a custom implementation of `require` and your code will never be used in projects where a standard `require` (CommonJS, Node.js, AMD) is expected, you can safely turn this rule off. diff --git a/docs/src/rules/no-new-symbol.md b/docs/src/rules/no-new-symbol.md index 461cdc8f054..e605ae10cea 100644 --- a/docs/src/rules/no-new-symbol.md +++ b/docs/src/rules/no-new-symbol.md @@ -27,6 +27,8 @@ This rule is aimed at preventing the accidental calling of `Symbol` with the `ne Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-new-symbol: "error"*/ /*eslint-env es6*/ @@ -34,8 +36,12 @@ Examples of **incorrect** code for this rule: var foo = new Symbol('foo'); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-new-symbol: "error"*/ /*eslint-env es6*/ @@ -49,6 +55,8 @@ function bar(Symbol) { ``` +::: + ## When Not To Use It This rule should not be used in ES3/5 environments. diff --git a/docs/src/rules/no-new-wrappers.md b/docs/src/rules/no-new-wrappers.md index 174d27d9c02..939f4b623a0 100644 --- a/docs/src/rules/no-new-wrappers.md +++ b/docs/src/rules/no-new-wrappers.md @@ -53,6 +53,8 @@ This rule aims to eliminate the use of `String`, `Number`, and `Boolean` with th Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-new-wrappers: "error"*/ @@ -65,8 +67,12 @@ var numberObject = new Number; var booleanObject = new Boolean; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-new-wrappers: "error"*/ @@ -76,6 +82,8 @@ var num = Number(someValue); var object = new MyString(); ``` +::: + ## When Not To Use It If you want to allow the use of primitive wrapper objects, then you can safely disable this rule. diff --git a/docs/src/rules/no-new.md b/docs/src/rules/no-new.md index 41d46310df0..893b006691e 100644 --- a/docs/src/rules/no-new.md +++ b/docs/src/rules/no-new.md @@ -27,14 +27,20 @@ This rule is aimed at maintaining consistency and convention by disallowing cons Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-new: "error"*/ new Thing(); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-new: "error"*/ @@ -42,3 +48,5 @@ var thing = new Thing(); Thing(); ``` + +::: diff --git a/docs/src/rules/no-nonoctal-decimal-escape.md b/docs/src/rules/no-nonoctal-decimal-escape.md index 9fb530a7f0d..8a063f3e736 100644 --- a/docs/src/rules/no-nonoctal-decimal-escape.md +++ b/docs/src/rules/no-nonoctal-decimal-escape.md @@ -34,6 +34,8 @@ This rule disallows `\8` and `\9` escape sequences in string literals. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-nonoctal-decimal-escape: "error"*/ @@ -50,8 +52,12 @@ var baz = "Don't use \8 and \9 escapes."; var quux = "\0\8"; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-nonoctal-decimal-escape: "error"*/ @@ -67,3 +73,5 @@ var baz = "Don't use \\8 and \\9 escapes."; var quux = "\0\u0038"; ``` + +::: diff --git a/docs/src/rules/no-obj-calls.md b/docs/src/rules/no-obj-calls.md index cd5bb376afc..309e1b9d823 100644 --- a/docs/src/rules/no-obj-calls.md +++ b/docs/src/rules/no-obj-calls.md @@ -33,6 +33,8 @@ This rule also disallows using these objects as constructors with the `new` oper Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-obj-calls: "error"*/ /*eslint-env es2017*/ @@ -54,8 +56,12 @@ var atomics = Atomics(); var newAtomics = new Atomics(); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-obj-calls: "error"*/ /*eslint-env es2017*/ @@ -70,3 +76,5 @@ var value = Reflect.get({ x: 1, y: 2 }, "x"); var first = Atomics.load(foo, 0); ``` + +::: diff --git a/docs/src/rules/no-octal-escape.md b/docs/src/rules/no-octal-escape.md index b6a06a4d7eb..e0f530cb45f 100644 --- a/docs/src/rules/no-octal-escape.md +++ b/docs/src/rules/no-octal-escape.md @@ -21,14 +21,20 @@ If ESLint parses code in strict mode, the parser (instead of this rule) reports Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-octal-escape: "error"*/ var foo = "Copyright \251"; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-octal-escape: "error"*/ @@ -36,3 +42,5 @@ var foo = "Copyright \u00A9"; // unicode var foo = "Copyright \xA9"; // hexadecimal ``` + +::: diff --git a/docs/src/rules/no-octal.md b/docs/src/rules/no-octal.md index 513ac64781a..54cbbae8cc7 100644 --- a/docs/src/rules/no-octal.md +++ b/docs/src/rules/no-octal.md @@ -25,6 +25,8 @@ If ESLint parses code in strict mode, the parser (instead of this rule) reports Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-octal: "error"*/ @@ -32,14 +34,20 @@ var num = 071; var result = 5 + 07; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-octal: "error"*/ var num = "071"; ``` +::: + ## Compatibility * **JSHint**: W115 diff --git a/docs/src/rules/no-param-reassign.md b/docs/src/rules/no-param-reassign.md index 8ee3eb57337..84240673f3e 100644 --- a/docs/src/rules/no-param-reassign.md +++ b/docs/src/rules/no-param-reassign.md @@ -19,6 +19,8 @@ This rule aims to prevent unintended behavior caused by modification or reassign Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-param-reassign: "error"*/ @@ -39,8 +41,12 @@ function foo(bar) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-param-reassign: "error"*/ @@ -49,6 +55,8 @@ function foo(bar) { } ``` +::: + ## Options This rule takes one option, an object, with a boolean property `"props"`, and arrays `"ignorePropertyModificationsFor"` and `"ignorePropertyModificationsForRegex"`. `"props"` is `false` by default. If `"props"` is set to `true`, this rule warns against the modification of parameter properties unless they're included in `"ignorePropertyModificationsFor"` or `"ignorePropertyModificationsForRegex"`, which is an empty array by default. @@ -57,6 +65,8 @@ This rule takes one option, an object, with a boolean property `"props"`, and a Examples of **correct** code for the default `{ "props": false }` option: +::: correct + ```js /*eslint no-param-reassign: ["error", { "props": false }]*/ @@ -81,8 +91,12 @@ function foo(bar) { } ``` +::: + Examples of **incorrect** code for the `{ "props": true }` option: +::: incorrect + ```js /*eslint no-param-reassign: ["error", { "props": true }]*/ @@ -107,8 +121,12 @@ function foo(bar) { } ``` +::: + Examples of **correct** code for the `{ "props": true }` option with `"ignorePropertyModificationsFor"` set: +::: correct + ```js /*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["bar"] }]*/ @@ -133,8 +151,12 @@ function foo(bar) { } ``` +::: + Examples of **correct** code for the `{ "props": true }` option with `"ignorePropertyModificationsForRegex"` set: +::: correct + ```js /*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsForRegex": ["^bar"] }]*/ @@ -159,6 +181,8 @@ function foo(barBaz) { } ``` +::: + ## When Not To Use It If you want to allow assignment to function parameters, then you can safely disable this rule. diff --git a/docs/src/rules/no-path-concat.md b/docs/src/rules/no-path-concat.md index e419526769c..43d57f235b0 100644 --- a/docs/src/rules/no-path-concat.md +++ b/docs/src/rules/no-path-concat.md @@ -37,6 +37,8 @@ This rule aims to prevent string concatenation of directory paths in Node.js Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-path-concat: "error"*/ @@ -46,14 +48,20 @@ var fullPath = __filename + "/foo.js"; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-path-concat: "error"*/ var fullPath = dirname + "/foo.js"; ``` +::: + ## When Not To Use It If you want to allow string concatenation of path names. diff --git a/docs/src/rules/no-plusplus.md b/docs/src/rules/no-plusplus.md index 5f93853717f..db4b0a2ac7e 100644 --- a/docs/src/rules/no-plusplus.md +++ b/docs/src/rules/no-plusplus.md @@ -34,6 +34,8 @@ This rule disallows the unary operators `++` and `--`. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-plusplus: "error"*/ @@ -48,8 +50,12 @@ for (i = 0; i < l; i++) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-plusplus: "error"*/ @@ -64,6 +70,8 @@ for (i = 0; i < l; i += 1) { } ``` +::: + ## Options This rule has an object option. @@ -74,6 +82,8 @@ This rule has an object option. Examples of **correct** code for this rule with the `{ "allowForLoopAfterthoughts": true }` option: +::: correct + ```js /*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/ @@ -90,8 +100,12 @@ for (i = 0, j = l; i < l; i++, j--) { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "allowForLoopAfterthoughts": true }` option: +::: incorrect + ```js /*eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }]*/ @@ -105,3 +119,5 @@ for (i = l; i--;) { for (i = 0; i < l;) i++; ``` + +::: diff --git a/docs/src/rules/no-process-env.md b/docs/src/rules/no-process-env.md index f9223d46dc0..e53f7413bca 100644 --- a/docs/src/rules/no-process-env.md +++ b/docs/src/rules/no-process-env.md @@ -20,6 +20,8 @@ This rule is aimed at discouraging use of `process.env` to avoid global dependen Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-process-env: "error"*/ @@ -28,8 +30,12 @@ if(process.env.NODE_ENV === "development") { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-process-env: "error"*/ @@ -40,6 +46,8 @@ if(config.env === "development") { } ``` +::: + ## When Not To Use It If you prefer to use `process.env` throughout your project to retrieve values from environment variables, then you can safely disable this rule. diff --git a/docs/src/rules/no-process-exit.md b/docs/src/rules/no-process-exit.md index 59913153395..7f45f0509cb 100644 --- a/docs/src/rules/no-process-exit.md +++ b/docs/src/rules/no-process-exit.md @@ -36,6 +36,8 @@ This rule aims to prevent the use of `process.exit()` in Node.js JavaScript. As Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-process-exit: "error"*/ @@ -43,8 +45,12 @@ process.exit(1); process.exit(0); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-process-exit: "error"*/ @@ -52,6 +58,8 @@ Process.exit(); var exit = process.exit; ``` +::: + ## When Not To Use It There may be a part of a Node.js application that is responsible for determining the correct exit code to return upon exiting. In that case, you should turn this rule off to allow proper handling of the exit code. diff --git a/docs/src/rules/no-promise-executor-return.md b/docs/src/rules/no-promise-executor-return.md index 1a694603d69..0b228506c65 100644 --- a/docs/src/rules/no-promise-executor-return.md +++ b/docs/src/rules/no-promise-executor-return.md @@ -37,6 +37,8 @@ Only `return` without a value is allowed, as it's a control flow statement. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-promise-executor-return: "error"*/ @@ -66,8 +68,12 @@ new Promise(() => { }); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-promise-executor-return: "error"*/ @@ -97,3 +103,5 @@ new Promise((resolve, reject) => { Promise.resolve(1); ``` + +::: diff --git a/docs/src/rules/no-proto.md b/docs/src/rules/no-proto.md index 07ee394123f..d15130406bc 100644 --- a/docs/src/rules/no-proto.md +++ b/docs/src/rules/no-proto.md @@ -17,6 +17,8 @@ When an object is created with the `new` operator, `__proto__` is set to the ori Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-proto: "error"*/ @@ -29,8 +31,12 @@ obj.__proto__ = b; obj["__proto__"] = b; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-proto: "error"*/ @@ -41,6 +47,8 @@ Object.setPrototypeOf(obj, b); var c = { __proto__: a }; ``` +::: + ## When Not To Use It You might want to turn this rule off if you need to support legacy browsers which implement the diff --git a/docs/src/rules/no-prototype-builtins.md b/docs/src/rules/no-prototype-builtins.md index c2e9f1d8ecf..e27eaec2bf6 100644 --- a/docs/src/rules/no-prototype-builtins.md +++ b/docs/src/rules/no-prototype-builtins.md @@ -21,6 +21,8 @@ This rule disallows calling some `Object.prototype` methods directly on object i Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-prototype-builtins: "error"*/ @@ -31,8 +33,12 @@ var isPrototypeOfBar = foo.isPrototypeOf(bar); var barIsEnumerable = foo.propertyIsEnumerable("bar"); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-prototype-builtins: "error"*/ @@ -43,6 +49,8 @@ var isPrototypeOfBar = Object.prototype.isPrototypeOf.call(foo, bar); var barIsEnumerable = {}.propertyIsEnumerable.call(foo, "bar"); ``` +::: + ## When Not To Use It You may want to turn this rule off if your code only touches objects with hardcoded keys, and you will never use an object that shadows an `Object.prototype` method or which does not inherit from `Object.prototype`. diff --git a/docs/src/rules/no-redeclare.md b/docs/src/rules/no-redeclare.md index 6a5de912090..e2ed45a56c4 100644 --- a/docs/src/rules/no-redeclare.md +++ b/docs/src/rules/no-redeclare.md @@ -19,6 +19,8 @@ This rule is aimed at eliminating variables that have multiple declarations in t Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-redeclare: "error"*/ @@ -38,8 +40,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-redeclare: "error"*/ @@ -60,6 +66,8 @@ class C { ``` +::: + ## Options This rule takes one optional argument, an object with a boolean property `"builtinGlobals"`. It defaults to `true`. @@ -71,14 +79,20 @@ The `"builtinGlobals"` option will check for redeclaration of built-in globals i Examples of **incorrect** code for the `{ "builtinGlobals": true }` option: +::: incorrect + ```js /*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/ var Object = 0; ``` +::: + Examples of **incorrect** code for the `{ "builtinGlobals": true }` option and the `browser` environment: +::: incorrect + ```js /*eslint no-redeclare: ["error", { "builtinGlobals": true }]*/ /*eslint-env browser*/ @@ -86,6 +100,8 @@ Examples of **incorrect** code for the `{ "builtinGlobals": true }` option and t var top = 0; ``` +::: + The `browser` environment has many built-in global variables (for example, `top`). Some of built-in global variables cannot be redeclared. Note that when using the `node` or `commonjs` environments (or `ecmaFeatures.globalReturn`, if using the default parser), the top scope of a program is not actually the global scope, but rather a "module" scope. When this is the case, declaring a variable named after a builtin global is not a redeclaration, but rather a shadowing of the global variable. In that case, the [`no-shadow`](no-shadow) rule with the `"builtinGlobals"` option should be used. diff --git a/docs/src/rules/no-regex-spaces.md b/docs/src/rules/no-regex-spaces.md index 8bb2133031b..65ae5888d40 100644 --- a/docs/src/rules/no-regex-spaces.md +++ b/docs/src/rules/no-regex-spaces.md @@ -34,6 +34,8 @@ This rule disallows multiple spaces in regular expression literals. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-regex-spaces: "error"*/ @@ -41,8 +43,12 @@ var re = /foo bar/; var re = new RegExp("foo bar"); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-regex-spaces: "error"*/ @@ -50,6 +56,8 @@ var re = /foo {3}bar/; var re = new RegExp("foo {3}bar"); ``` +::: + ## When Not To Use It If you want to allow multiple spaces in a regular expression, then you can safely turn this rule off. diff --git a/docs/src/rules/no-reserved-keys.md b/docs/src/rules/no-reserved-keys.md index 8608eeb6060..66cc08e4317 100644 --- a/docs/src/rules/no-reserved-keys.md +++ b/docs/src/rules/no-reserved-keys.md @@ -29,6 +29,8 @@ This rule is aimed at eliminating the use of ECMAScript 3 keywords and reserved Examples of **incorrect** code for this rule: +::: incorrect + ```js var superman = { class: "Superhero", @@ -40,8 +42,12 @@ var values = { }; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js var superman = { "class": "Superhero", @@ -53,6 +59,8 @@ var values = { }; ``` +::: + ## When Not To Use It If your code is only going to be executed in an ECMAScript 5 or higher environment, then you can safely leave this rule off. diff --git a/docs/src/rules/no-restricted-exports.md b/docs/src/rules/no-restricted-exports.md index e168c1cdca4..e776a1fa62c 100644 --- a/docs/src/rules/no-restricted-exports.md +++ b/docs/src/rules/no-restricted-exports.md @@ -23,6 +23,8 @@ This rule has an object option: Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["foo", "bar", "Baz", "a", "b", "c", "d", "e", "👍"] @@ -49,8 +51,12 @@ export { something as e } from "some_module"; export { "👍" } from "some_module"; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["foo", "bar", "Baz", "a", "b", "c", "d", "e", "👍"] @@ -77,12 +83,16 @@ export { something } from "some_module"; export { "👍" as thumbsUp } from "some_module"; ``` +::: + ### Default exports By design, this rule doesn't disallow `export default` declarations. If you configure `"default"` as a restricted name, that restriction will apply only to named export declarations. Examples of additional **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/ @@ -91,6 +101,8 @@ function foo() {} export { foo as default }; ``` +::: + ```js /*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default"] }]*/ @@ -99,12 +111,16 @@ export { default } from "some_module"; Examples of additional **correct** code for this rule: +::: correct + ```js /*eslint no-restricted-exports: ["error", { "restrictedNamedExports": ["default", "foo"] }]*/ export default function foo() {} ``` +::: + ## Known Limitations This rule doesn't inspect the content of source modules in re-export declarations. In particular, if you are re-exporting everything from another module's export, that export may include a restricted name. This rule cannot detect such cases. diff --git a/docs/src/rules/no-restricted-globals.md b/docs/src/rules/no-restricted-globals.md index 4e9de8a0efd..1361731e282 100644 --- a/docs/src/rules/no-restricted-globals.md +++ b/docs/src/rules/no-restricted-globals.md @@ -55,6 +55,8 @@ Alternatively, the rule also accepts objects, where the global name and an optio Examples of **incorrect** code for sample `"event", "fdescribe"` global variable names: +::: incorrect + ```js /*global event, fdescribe*/ /*eslint no-restricted-globals: ["error", "event", "fdescribe"]*/ @@ -67,8 +69,12 @@ fdescribe("foo", function() { }); ``` +::: + Examples of **correct** code for a sample `"event"` global variable name: +::: correct + ```js /*global event*/ /*eslint no-restricted-globals: ["error", "event"]*/ @@ -76,6 +82,8 @@ Examples of **correct** code for a sample `"event"` global variable name: import event from "event-module"; ``` +::: + ```js /*global event*/ /*eslint no-restricted-globals: ["error", "event"]*/ @@ -85,6 +93,8 @@ var event = 1; Examples of **incorrect** code for a sample `"event"` global variable name, along with a custom error message: +::: incorrect + ```js /*global event*/ /* eslint no-restricted-globals: ["error", { name: "event", message: "Use local parameter instead." }] */ @@ -93,3 +103,5 @@ function onClick() { console.log(event); // Unexpected global variable 'event'. Use local parameter instead. } ``` + +::: diff --git a/docs/src/rules/no-restricted-imports.md b/docs/src/rules/no-restricted-imports.md index d4543c20e30..5c03814855d 100644 --- a/docs/src/rules/no-restricted-imports.md +++ b/docs/src/rules/no-restricted-imports.md @@ -121,12 +121,16 @@ To restrict the use of all Node.js core imports (via (console.log('bar'), val); @@ -85,8 +95,12 @@ const foo = () => ((bar = 123), 10); const foo = () => { return (bar = 123), 10 } ``` +::: + Examples of **correct** code for arrow functions: +::: correct + ```js /*eslint no-sequences: "error"*/ const foo = (val) => ((console.log('bar'), val)); @@ -96,6 +110,8 @@ const foo = () => (((bar = 123), 10)); const foo = () => { return ((bar = 123), 10) } ``` +::: + ## Options This rule takes one option, an object, with the following properties: @@ -106,6 +122,8 @@ This rule takes one option, an object, with the following properties: Examples of **incorrect** code for this rule with the `{ "allowInParentheses": false }` option: +::: incorrect + ```js /*eslint no-sequences: ["error", { "allowInParentheses": false }]*/ @@ -128,14 +146,20 @@ with ((doSomething(), val)) {} const foo = (val) => ((console.log('bar'), val)); ``` +::: + Examples of **correct** code for this rule with the `{ "allowInParentheses": false }` option: +::: correct + ```js /*eslint no-sequences: ["error", { "allowInParentheses": false }]*/ for (i = 0, j = 10; i < j; i++, j--); ``` +::: + ## When Not To Use It Disable this rule if sequence expressions with the comma operator are acceptable. diff --git a/docs/src/rules/no-setter-return.md b/docs/src/rules/no-setter-return.md index b2dd68ed4ca..b86d1adb206 100644 --- a/docs/src/rules/no-setter-return.md +++ b/docs/src/rules/no-setter-return.md @@ -31,6 +31,8 @@ This rule checks setters in: Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-setter-return: "error"*/ @@ -68,8 +70,12 @@ Object.defineProperty(foo, "bar", { }); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-setter-return: "error"*/ @@ -104,3 +110,5 @@ Object.defineProperty(foo, "bar", { } }); ``` + +::: diff --git a/docs/src/rules/no-shadow-restricted-names.md b/docs/src/rules/no-shadow-restricted-names.md index 95a8553ad34..e933c701dfb 100644 --- a/docs/src/rules/no-shadow-restricted-names.md +++ b/docs/src/rules/no-shadow-restricted-names.md @@ -26,6 +26,8 @@ Then any code used within the same scope would not get the global `undefined`, b Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-shadow-restricted-names: "error"*/ @@ -38,8 +40,12 @@ var undefined = 5; try {} catch(eval){} ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-shadow-restricted-names: "error"*/ @@ -50,3 +56,5 @@ function f(a, b){} // Exception: `undefined` may be shadowed if the variable is never assigned a value. var undefined; ``` + +::: diff --git a/docs/src/rules/no-shadow.md b/docs/src/rules/no-shadow.md index fee157c5435..1bb49c90668 100644 --- a/docs/src/rules/no-shadow.md +++ b/docs/src/rules/no-shadow.md @@ -28,6 +28,8 @@ This rule aims to eliminate shadowed variable declarations. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-shadow: "error"*/ /*eslint-env es6*/ @@ -51,6 +53,8 @@ if (true) { } ``` +::: + ## Options This rule takes one option, an object, with properties `"builtinGlobals"`, `"hoist"`, `"allow"` and `"ignoreOnInitialization"`. @@ -68,6 +72,8 @@ If it is `true`, the rule prevents shadowing of built-in global variables: `Obje Examples of **incorrect** code for the `{ "builtinGlobals": true }` option: +::: incorrect + ```js /*eslint no-shadow: ["error", { "builtinGlobals": true }]*/ @@ -76,6 +82,8 @@ function foo() { } ``` +::: + ### hoist The `hoist` option has three settings: @@ -88,6 +96,8 @@ The `hoist` option has three settings: Examples of **incorrect** code for the default `{ "hoist": "functions" }` option: +::: incorrect + ```js /*eslint no-shadow: ["error", { "hoist": "functions" }]*/ /*eslint-env es6*/ @@ -99,10 +109,14 @@ if (true) { function b() {} ``` +::: + Although `let b` in the `if` statement is before the *function* declaration in the outer scope, it is incorrect. Examples of **correct** code for the default `{ "hoist": "functions" }` option: +::: correct + ```js /*eslint no-shadow: ["error", { "hoist": "functions" }]*/ /*eslint-env es6*/ @@ -114,12 +128,16 @@ if (true) { let a = 5; ``` +::: + Because `let a` in the `if` statement is before the *variable* declaration in the outer scope, it is correct. #### hoist: all Examples of **incorrect** code for the `{ "hoist": "all" }` option: +::: incorrect + ```js /*eslint no-shadow: ["error", { "hoist": "all" }]*/ /*eslint-env es6*/ @@ -133,10 +151,14 @@ let a = 5; function b() {} ``` +::: + #### hoist: never Examples of **correct** code for the `{ "hoist": "never" }` option: +::: correct + ```js /*eslint no-shadow: ["error", { "hoist": "never" }]*/ /*eslint-env es6*/ @@ -150,6 +172,8 @@ let a = 5; function b() {} ``` +::: + Because `let a` and `let b` in the `if` statement are before the declarations in the outer scope, they are correct. ### allow @@ -158,6 +182,8 @@ The `allow` option is an array of identifier names for which shadowing is allowe Examples of **correct** code for the `{ "allow": ["done"] }` option: +::: correct + ```js /*eslint no-shadow: ["error", { "allow": ["done"] }]*/ /*eslint-env es6*/ @@ -175,6 +201,8 @@ foo(function (err, result) { }); ``` +::: + ### ignoreOnInitialization The `ignoreOnInitialization` option is `false` by default. If it is `true`, it prevents reporting shadowing of variables in their initializers when the shadowed variable is presumably still uninitialized. @@ -183,16 +211,22 @@ The shadowed variable must be on the left side. The shadowing variable must be o Examples of **incorrect** code for the `{ "ignoreOnInitialization": "true" }` option: +::: incorrect + ```js /*eslint no-shadow: ["error", { "ignoreOnInitialization": true }]*/ var x = x => x; ``` +::: + Because the shadowing variable `x` will shadow the already initialized shadowed variable `x`. Examples of **correct** code for the `{ "ignoreOnInitialization": true }` option: +::: correct + ```js /*eslint no-shadow: ["error", { "ignoreOnInitialization": true }]*/ @@ -201,4 +235,6 @@ var x = foo(x => x) var y = (y => y)() ``` +::: + The rationale for callback functions is the assumption that they will be called during the initialization, so that at the time when the shadowing variable will be used, the shadowed variable has not yet been initialized. diff --git a/docs/src/rules/no-space-before-semi.md b/docs/src/rules/no-space-before-semi.md index 627003d3cda..03a39fe47a8 100644 --- a/docs/src/rules/no-space-before-semi.md +++ b/docs/src/rules/no-space-before-semi.md @@ -28,6 +28,8 @@ This rule prevents the use of spaces before a semicolon in expressions. Examples of **incorrect** code for this rule: +::: incorrect + ```js var foo = "bar" ; @@ -39,10 +41,16 @@ var foo = function() { var foo = 1 + 2 ; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js ;(function(){}()); var foo = "bar"; ``` + +::: diff --git a/docs/src/rules/no-spaced-func.md b/docs/src/rules/no-spaced-func.md index b06d81ae426..b116d0111cf 100644 --- a/docs/src/rules/no-spaced-func.md +++ b/docs/src/rules/no-spaced-func.md @@ -19,6 +19,8 @@ This rule disallows spacing between function identifiers and their applications. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-spaced-func: "error"*/ @@ -28,10 +30,16 @@ fn () ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-spaced-func: "error"*/ fn() ``` + +::: diff --git a/docs/src/rules/no-sparse-arrays.md b/docs/src/rules/no-sparse-arrays.md index df061c50af5..a26a74e6045 100644 --- a/docs/src/rules/no-sparse-arrays.md +++ b/docs/src/rules/no-sparse-arrays.md @@ -33,6 +33,8 @@ This rule disallows sparse array literals which have "holes" where commas are no Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-sparse-arrays: "error"*/ @@ -40,8 +42,12 @@ var items = [,]; var colors = [ "red",, "blue" ]; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-sparse-arrays: "error"*/ @@ -52,6 +58,8 @@ var items = new Array(23); var colors = [ "red", "blue", ]; ``` +::: + ## When Not To Use It If you want to use sparse arrays, then it is safe to disable this rule. diff --git a/docs/src/rules/no-sync.md b/docs/src/rules/no-sync.md index 2c839f42e64..3f71e387cf7 100644 --- a/docs/src/rules/no-sync.md +++ b/docs/src/rules/no-sync.md @@ -21,6 +21,8 @@ This rule has an optional object option `{ allowAtRootLevel: }`, which Examples of **incorrect** code for this rule with the default `{ allowAtRootLevel: false }` option: +::: incorrect + ```js /*eslint no-sync: "error"*/ @@ -31,8 +33,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the default `{ allowAtRootLevel: false }` option: +::: correct + ```js /*eslint no-sync: "error"*/ @@ -43,8 +49,12 @@ async(function() { }); ``` +::: + Examples of **incorrect** code for this rule with the `{ allowAtRootLevel: true }` option +::: incorrect + ```js /*eslint no-sync: ["error", { allowAtRootLevel: true }]*/ @@ -55,14 +65,20 @@ function foo() { var bar = baz => fs.readFileSync(qux); ``` +::: + Examples of **correct** code for this rule with the `{ allowAtRootLevel: true }` option +::: correct + ```js /*eslint no-sync: ["error", { allowAtRootLevel: true }]*/ fs.readFileSync(somePath).toString(); ``` +::: + ## When Not To Use It If you want to allow synchronous operations in your script, do not enable this rule. diff --git a/docs/src/rules/no-tabs.md b/docs/src/rules/no-tabs.md index fa26235ffce..8782b70b509 100644 --- a/docs/src/rules/no-tabs.md +++ b/docs/src/rules/no-tabs.md @@ -15,6 +15,8 @@ This rule looks for tabs anywhere inside a file: code, comments or anything else Examples of **incorrect** code for this rule: +::: incorrect + ```js var a \t= 2; @@ -26,8 +28,12 @@ function test(){} var x = 1; // \t test ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js var a = 2; @@ -39,6 +45,8 @@ function test(){} var x = 1; // test ``` +::: + ### Options This rule has an optional object option with the following properties: @@ -49,6 +57,8 @@ This rule has an optional object option with the following properties: Examples of **correct** code for this rule with the `allowIndentationTabs: true` option: +::: correct + ```js /* eslint no-tabs: ["error", { allowIndentationTabs: true }] */ @@ -59,6 +69,8 @@ function test() { \t// comment with leading indentation tab ``` +::: + ## When Not To Use It If you have established a standard where having tabs is fine, then you can disable this rule. diff --git a/docs/src/rules/no-template-curly-in-string.md b/docs/src/rules/no-template-curly-in-string.md index 64b08187d94..6760c0125ee 100644 --- a/docs/src/rules/no-template-curly-in-string.md +++ b/docs/src/rules/no-template-curly-in-string.md @@ -17,6 +17,8 @@ This rule aims to warn when a regular string contains what looks like a template Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-template-curly-in-string: "error"*/ "Hello ${name}!"; @@ -24,8 +26,12 @@ Examples of **incorrect** code for this rule: "Time: ${12 * 60 * 60 * 1000}"; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-template-curly-in-string: "error"*/ `Hello ${name}!`; @@ -34,6 +40,8 @@ Examples of **correct** code for this rule: templateFunction`Hello ${name}`; ``` +::: + ## When Not To Use It This rule should not be used in ES3/5 environments. diff --git a/docs/src/rules/no-ternary.md b/docs/src/rules/no-ternary.md index 2f9eb38b32a..b6efa40550f 100644 --- a/docs/src/rules/no-ternary.md +++ b/docs/src/rules/no-ternary.md @@ -22,6 +22,8 @@ This rule disallows ternary operators. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-ternary: "error"*/ @@ -32,8 +34,12 @@ function quux() { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-ternary: "error"*/ @@ -53,3 +59,5 @@ function quux() { } } ``` + +::: diff --git a/docs/src/rules/no-this-before-super.md b/docs/src/rules/no-this-before-super.md index 7b679d85059..6832d0dd843 100644 --- a/docs/src/rules/no-this-before-super.md +++ b/docs/src/rules/no-this-before-super.md @@ -21,6 +21,8 @@ This rule is aimed to flag `this`/`super` keywords before `super()` callings. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-this-before-super: "error"*/ /*eslint-env es6*/ @@ -53,8 +55,12 @@ class A extends B { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-this-before-super: "error"*/ /*eslint-env es6*/ @@ -79,6 +85,8 @@ class A extends B { } ``` +::: + ## When Not To Use It If you don't want to be notified about using `this`/`super` before `super()` in constructors, you can safely disable this rule. diff --git a/docs/src/rules/no-throw-literal.md b/docs/src/rules/no-throw-literal.md index 52d5ad44236..285f858dd52 100644 --- a/docs/src/rules/no-throw-literal.md +++ b/docs/src/rules/no-throw-literal.md @@ -18,6 +18,8 @@ This rule is aimed at maintaining consistency when throwing exception by disallo Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-throw-literal: "error"*/ /*eslint-env es6*/ @@ -39,8 +41,12 @@ throw `${err}` ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-throw-literal: "error"*/ @@ -58,12 +64,16 @@ try { } ``` +::: + ## Known Limitations Due to the limits of static analysis, this rule cannot guarantee that you will only throw `Error` objects. Examples of **correct** code for this rule, but which do not throw an `Error` object: +::: correct + ```js /*eslint no-throw-literal: "error"*/ @@ -82,3 +92,5 @@ var foo = { }; throw foo.bar; ``` + +::: diff --git a/docs/src/rules/no-trailing-spaces.md b/docs/src/rules/no-trailing-spaces.md index 515e8ad6f8c..9d8af10363e 100644 --- a/docs/src/rules/no-trailing-spaces.md +++ b/docs/src/rules/no-trailing-spaces.md @@ -17,6 +17,8 @@ This rule disallows trailing whitespace (spaces, tabs, and other Unicode whitesp Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-trailing-spaces: "error"*/ @@ -25,8 +27,12 @@ var baz = 5;//•• //••••• ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-trailing-spaces: "error"*/ @@ -34,6 +40,8 @@ var foo = 0; var baz = 5; ``` +::: + ## Options This rule has an object option: @@ -47,6 +55,8 @@ This rule has an object option: Examples of **correct** code for this rule with the `{ "skipBlankLines": true }` option: +::: correct + ```js /*eslint no-trailing-spaces: ["error", { "skipBlankLines": true }]*/ @@ -55,10 +65,14 @@ var baz = 5; //••••• ``` +::: + ### ignoreComments Examples of **correct** code for this rule with the `{ "ignoreComments": true }` option: +::: correct + ```js /*eslint no-trailing-spaces: ["error", { "ignoreComments": true }]*/ @@ -70,3 +84,5 @@ Examples of **correct** code for this rule with the `{ "ignoreComments": true }` *•bar */ ``` + +::: diff --git a/docs/src/rules/no-undef-init.md b/docs/src/rules/no-undef-init.md index 84908ace717..8006e669658 100644 --- a/docs/src/rules/no-undef-init.md +++ b/docs/src/rules/no-undef-init.md @@ -34,6 +34,8 @@ This rule aims to eliminate `var` and `let` variable declarations that initializ Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-undef-init: "error"*/ @@ -41,8 +43,12 @@ var foo = undefined; let bar = undefined; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-undef-init: "error"*/ @@ -50,10 +56,14 @@ var foo; let bar; ``` +::: + Please note that this rule does not check `const` declarations, destructuring patterns, function parameters, and class fields. Examples of additional **correct** code for this rule: +::: correct + ```js /*eslint no-undef-init: "error"*/ @@ -70,12 +80,16 @@ class Foo { } ``` +::: + ## When Not To Use It There is one situation where initializing to `undefined` behaves differently than omitting the initialization, and that's when a `var` declaration occurs inside of a loop. For example: Example of **incorrect** code for this rule: +::: incorrect + ```js for (i = 0; i < 10; i++) { var x = undefined; @@ -84,6 +98,8 @@ for (i = 0; i < 10; i++) { } ``` +::: + In this case, the `var x` is hoisted out of the loop, effectively creating: ```js @@ -123,6 +139,8 @@ If you're using such an initialization inside of a loop, then you should disable Example of **correct** code for this rule, because it is disabled on a specific line: +::: correct + ```js /*eslint no-undef-init: "error"*/ @@ -132,3 +150,5 @@ for (i = 0; i < 10; i++) { x = i; } ``` + +::: diff --git a/docs/src/rules/no-undef.md b/docs/src/rules/no-undef.md index 33f2abbfd19..a6003d16469 100644 --- a/docs/src/rules/no-undef.md +++ b/docs/src/rules/no-undef.md @@ -20,6 +20,8 @@ Any reference to an undeclared variable causes a warning, unless the variable is Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-undef: "error"*/ @@ -27,8 +29,12 @@ var foo = someFunction(); var bar = a + 1; ``` +::: + Examples of **correct** code for this rule with `global` declaration: +::: correct + ```js /*global someFunction, a*/ /*eslint no-undef: "error"*/ @@ -37,6 +43,8 @@ var foo = someFunction(); var bar = a + 1; ``` +::: + Note that this rule does not disallow assignments to read-only global variables. See [no-global-assign](no-global-assign) if you also want to disallow those assignments. @@ -51,6 +59,8 @@ See [no-redeclare](no-redeclare) if you also want to disallow those redeclaratio Examples of **correct** code for the default `{ "typeof": false }` option: +::: correct + ```js /*eslint no-undef: "error"*/ @@ -59,18 +69,26 @@ if (typeof UndefinedIdentifier === "undefined") { } ``` +::: + You can use this option if you want to prevent `typeof` check on a variable which has not been declared. Examples of **incorrect** code for the `{ "typeof": true }` option: +::: incorrect + ```js /*eslint no-undef: ["error", { "typeof": true }] */ if(typeof a === "string"){} ``` +::: + Examples of **correct** code for the `{ "typeof": true }` option with `global` declaration: +::: correct + ```js /*global a*/ /*eslint no-undef: ["error", { "typeof": true }] */ @@ -78,6 +96,8 @@ Examples of **correct** code for the `{ "typeof": true }` option with `global` d if(typeof a === "string"){} ``` +::: + ## Environments For convenience, ESLint provides shortcuts that pre-define global variables exposed by popular libraries and runtime environments. This rule supports these environments, as listed in [Specifying Environments](../user-guide/configuring/language-options#specifying-environments). A few examples are given below. @@ -86,6 +106,8 @@ For convenience, ESLint provides shortcuts that pre-define global variables expo Examples of **correct** code for this rule with `browser` environment: +::: correct + ```js /*eslint no-undef: "error"*/ /*eslint-env browser*/ @@ -95,10 +117,14 @@ setTimeout(function() { }); ``` +::: + ### Node.js Examples of **correct** code for this rule with `node` environment: +::: correct + ```js /*eslint no-undef: "error"*/ /*eslint-env node*/ @@ -109,6 +135,8 @@ module.exports = function() { }; ``` +::: + ## When Not To Use It If explicit declaration of global variables is not to your taste. diff --git a/docs/src/rules/no-undefined.md b/docs/src/rules/no-undefined.md index 9398312a05a..52961fefb73 100644 --- a/docs/src/rules/no-undefined.md +++ b/docs/src/rules/no-undefined.md @@ -44,6 +44,8 @@ This rule aims to eliminate the use of `undefined`, and as such, generates a war Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-undefined: "error"*/ @@ -60,8 +62,12 @@ function foo(undefined) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-undefined: "error"*/ @@ -76,6 +82,8 @@ if (typeof foo === "undefined") { global.undefined = "foo"; ``` +::: + ## When Not To Use It If you want to allow the use of `undefined` in your code, then you can safely turn this rule off. diff --git a/docs/src/rules/no-underscore-dangle.md b/docs/src/rules/no-underscore-dangle.md index 15f082629e5..ddef65ad274 100644 --- a/docs/src/rules/no-underscore-dangle.md +++ b/docs/src/rules/no-underscore-dangle.md @@ -23,6 +23,8 @@ This rule disallows dangling underscores in identifiers. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-underscore-dangle: "error"*/ @@ -31,8 +33,12 @@ var __proto__ = {}; foo._bar(); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-underscore-dangle: "error"*/ @@ -45,6 +51,8 @@ const foo = { onClick(_bar) {} }; const foo = (_bar) => {}; ``` +::: + ## Options This rule has an object option: @@ -61,6 +69,8 @@ This rule has an object option: Examples of additional **correct** code for this rule with the `{ "allow": ["foo_", "_bar"] }` option: +::: correct + ```js /*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/ @@ -68,10 +78,14 @@ var foo_; foo._bar(); ``` +::: + ### allowAfterThis Examples of **correct** code for this rule with the `{ "allowAfterThis": true }` option: +::: correct + ```js /*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/ @@ -79,10 +93,14 @@ var a = this.foo_; this._bar(); ``` +::: + ### allowAfterSuper Examples of **correct** code for this rule with the `{ "allowAfterSuper": true }` option: +::: correct + ```js /*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/ @@ -90,10 +108,14 @@ var a = super.foo_; super._bar(); ``` +::: + ### allowAfterThisConstructor Examples of **correct** code for this rule with the `{ "allowAfterThisConstructor": true }` option: +::: correct + ```js /*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/ @@ -101,10 +123,14 @@ var a = this.constructor.foo_; this.constructor._bar(); ``` +::: + ### enforceInMethodNames Examples of **incorrect** code for this rule with the `{ "enforceInMethodNames": true }` option: +::: incorrect + ```js /*eslint no-underscore-dangle: ["error", { "enforceInMethodNames": true }]*/ @@ -125,10 +151,14 @@ const o = { }; ``` +::: + ### enforceInClassFields Examples of **incorrect** code for this rule with the `{ "enforceInClassFields": true }` option: +::: incorrect + ```js /*eslint no-underscore-dangle: ["error", { "enforceInClassFields": true }]*/ @@ -153,10 +183,14 @@ class Foo { } ``` +::: + ### allowFunctionParams Examples of **incorrect** code for this rule with the `{ "allowFunctionParams": false }` option: +::: incorrect + ```js /*eslint no-underscore-dangle: ["error", { "allowFunctionParams": false }]*/ @@ -173,6 +207,8 @@ const foo = (_bar = 0) => {}; const foo = (..._bar) => {}; ``` +::: + ## When Not To Use It If you want to allow dangling underscores in identifiers, then you can safely turn this rule off. diff --git a/docs/src/rules/no-unexpected-multiline.md b/docs/src/rules/no-unexpected-multiline.md index cae4d74500d..e562d42d3f1 100644 --- a/docs/src/rules/no-unexpected-multiline.md +++ b/docs/src/rules/no-unexpected-multiline.md @@ -30,6 +30,8 @@ This rule disallows confusing multiline expressions where a newline looks like i Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unexpected-multiline: "error"*/ @@ -50,8 +52,12 @@ let x = foo /regex/g.test(bar) ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unexpected-multiline: "error"*/ @@ -74,6 +80,8 @@ let tag = function() {} tag `hello` ``` +::: + ## When Not To Use It You can turn this rule off if you are confident that you will not accidentally introduce code like this. diff --git a/docs/src/rules/no-unmodified-loop-condition.md b/docs/src/rules/no-unmodified-loop-condition.md index 154d7c05d0f..b27e2d58f53 100644 --- a/docs/src/rules/no-unmodified-loop-condition.md +++ b/docs/src/rules/no-unmodified-loop-condition.md @@ -35,6 +35,8 @@ If a reference is inside of a dynamic expression (e.g. `CallExpression`, Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unmodified-loop-condition: "error"*/ @@ -54,8 +56,12 @@ while (node !== root) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unmodified-loop-condition: "error"*/ @@ -92,6 +98,8 @@ while (check(obj)) { } ``` +::: + ## When Not To Use It If you don't want to notified about references inside of loop conditions, then it's safe to disable this rule. diff --git a/docs/src/rules/no-unneeded-ternary.md b/docs/src/rules/no-unneeded-ternary.md index a9a8b540f62..6188de64df8 100644 --- a/docs/src/rules/no-unneeded-ternary.md +++ b/docs/src/rules/no-unneeded-ternary.md @@ -46,6 +46,8 @@ This rule disallow ternary operators when simpler alternatives exist. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unneeded-ternary: "error"*/ @@ -54,8 +56,12 @@ var a = x === 2 ? true : false; var a = x ? true : false; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unneeded-ternary: "error"*/ @@ -70,6 +76,8 @@ var a = x ? y : x; f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below. ``` +::: + ## Options This rule has an object option: @@ -83,6 +91,8 @@ When set to `true`, which it is by default, The defaultAssignment option allows Examples of additional **incorrect** code for this rule with the `{ "defaultAssignment": false }` option: +::: incorrect + ```js /*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/ @@ -91,6 +101,8 @@ var a = x ? x : 1; f(x ? x : 1); ``` +::: + Note that `defaultAssignment: false` still allows expressions of the form `x ? expr : x` (where the identifier is on the right hand side of the ternary). ## When Not To Use It diff --git a/docs/src/rules/no-unreachable-loop.md b/docs/src/rules/no-unreachable-loop.md index 8cdd64ee353..f63bc041e14 100644 --- a/docs/src/rules/no-unreachable-loop.md +++ b/docs/src/rules/no-unreachable-loop.md @@ -36,6 +36,8 @@ This rule checks `while`, `do-while`, `for`, `for-in` and `for-of` loops. You ca Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unreachable-loop: "error"*/ @@ -83,8 +85,12 @@ for (foo of bar) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unreachable-loop: "error"*/ @@ -132,10 +138,14 @@ for (foo of bar) { } ``` +::: + Please note that this rule is not designed to check loop conditions, and will not warn in cases such as the following examples. Examples of additional **correct** code for this rule: +::: correct + ```js /*eslint no-unreachable-loop: "error"*/ @@ -152,6 +162,8 @@ for (const a of [1]) { } ``` +::: + ## Options This rule has an object option, with one option: @@ -170,6 +182,8 @@ You can specify up to 5 different elements in the `"ignore"` array: Examples of **correct** code for this rule with the `"ignore"` option: +::: correct + ```js /*eslint no-unreachable-loop: ["error", { "ignore": ["ForInStatement", "ForOfStatement"] }]*/ @@ -181,6 +195,8 @@ for (var key in obj) { for (const a of b) break; ``` +::: + ## Known Limitations Static code path analysis, in general, does not evaluate conditions. Due to this fact, this rule might miss reporting cases such as the following: diff --git a/docs/src/rules/no-unreachable.md b/docs/src/rules/no-unreachable.md index 2af7ae0407c..db5b486ac36 100644 --- a/docs/src/rules/no-unreachable.md +++ b/docs/src/rules/no-unreachable.md @@ -37,6 +37,8 @@ This rule disallows unreachable code after `return`, `throw`, `continue`, and `b Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unreachable: "error"*/ @@ -71,8 +73,12 @@ for (;;) {} console.log("done"); ``` +::: + Examples of **correct** code for this rule, because of JavaScript function and variable hoisting: +::: correct + ```js /*eslint no-unreachable: "error"*/ @@ -95,8 +101,12 @@ switch (foo) { } ``` +::: + Examples of additional **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unreachable: "error"*/ @@ -112,8 +122,12 @@ class C extends B { } ``` +::: + Examples of additional **correct** code for this rule: +::: correct + ```js /*eslint no-unreachable: "error"*/ @@ -148,3 +162,5 @@ class F extends B { } } ``` + +::: diff --git a/docs/src/rules/no-unsafe-finally.md b/docs/src/rules/no-unsafe-finally.md index 51b4afe2786..012e7e698d6 100644 --- a/docs/src/rules/no-unsafe-finally.md +++ b/docs/src/rules/no-unsafe-finally.md @@ -74,6 +74,8 @@ This rule disallows `return`, `throw`, `break`, and `continue` statements inside Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unsafe-finally: "error"*/ let foo = function() { @@ -87,6 +89,8 @@ let foo = function() { }; ``` +::: + ```js /*eslint no-unsafe-finally: "error"*/ let foo = function() { @@ -102,6 +106,8 @@ let foo = function() { Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unsafe-finally: "error"*/ let foo = function() { @@ -115,6 +121,8 @@ let foo = function() { }; ``` +::: + ```js /*eslint no-unsafe-finally: "error"*/ let foo = function() { diff --git a/docs/src/rules/no-unsafe-negation.md b/docs/src/rules/no-unsafe-negation.md index a9379d58bfd..6c5ef0c2fc1 100644 --- a/docs/src/rules/no-unsafe-negation.md +++ b/docs/src/rules/no-unsafe-negation.md @@ -22,6 +22,8 @@ This rule disallows negating the left operand of the following relational operat Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unsafe-negation: "error"*/ @@ -36,8 +38,12 @@ if (!obj instanceof Ctor) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unsafe-negation: "error"*/ @@ -50,6 +56,8 @@ if (!(obj instanceof Ctor)) { } ``` +::: + ### Exception For rare situations when negating the left operand is intended, this rule allows an exception. @@ -57,6 +65,8 @@ If the whole negation is explicitly wrapped in parentheses, the rule will not re Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unsafe-negation: "error"*/ @@ -71,8 +81,12 @@ if(("" + !foo) in object) { } ``` +::: + Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unsafe-negation: "error"*/ @@ -81,6 +95,8 @@ if (!(foo) in object) { } ``` +::: + ## Options This rule has an object option: @@ -101,6 +117,8 @@ The purpose is to avoid expressions such as `! a < b` (which is equivalent to `( Examples of additional **incorrect** code for this rule with the `{ "enforceForOrderingRelations": true }` option: +::: incorrect + ```js /*eslint no-unsafe-negation: ["error", { "enforceForOrderingRelations": true }]*/ @@ -113,6 +131,8 @@ foo = ! a <= b; foo = ! a >= b; ``` +::: + ## When Not To Use It If you don't want to notify unsafe logical negations, then it's safe to disable this rule. diff --git a/docs/src/rules/no-unsafe-optional-chaining.md b/docs/src/rules/no-unsafe-optional-chaining.md index 879662df4ea..27aa184f10b 100644 --- a/docs/src/rules/no-unsafe-optional-chaining.md +++ b/docs/src/rules/no-unsafe-optional-chaining.md @@ -36,6 +36,8 @@ This rule aims to detect some cases where the use of optional chaining doesn't p Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unsafe-optional-chaining: "error"*/ @@ -88,8 +90,12 @@ async function foo () { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unsafe-optional-chaining: "error"*/ @@ -120,6 +126,8 @@ async function foo () { } ``` +::: + ## Options This rule has an object option: @@ -136,6 +144,8 @@ With this option set to `true` the rule is enforced for: Examples of additional **incorrect** code for this rule with the `{ "disallowArithmeticOperators": true }` option: +::: incorrect + ```js /*eslint no-unsafe-optional-chaining: ["error", { "disallowArithmeticOperators": true }]*/ @@ -162,3 +172,5 @@ async function foo () { baz += await obj?.foo; } ``` + +::: diff --git a/docs/src/rules/no-unused-expressions.md b/docs/src/rules/no-unused-expressions.md index fb69dbd4909..2fa712d9ef3 100644 --- a/docs/src/rules/no-unused-expressions.md +++ b/docs/src/rules/no-unused-expressions.md @@ -44,6 +44,8 @@ These options allow unused expressions *only if all* of the code paths either di Examples of **incorrect** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options: +::: incorrect + ```js /*eslint no-unused-expressions: "error"*/ @@ -69,8 +71,12 @@ injectGlobal`body{ color: red; }` ``` +::: + Examples of **correct** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options: +::: correct + ```js /*eslint no-unused-expressions: "error"*/ @@ -93,10 +99,14 @@ delete a.b void a ``` +::: + Note that one or more string expression statements (with or without semi-colons) will only be considered as unused if they are not in the beginning of a script, module, or function (alone and uninterrupted by other statements). Otherwise, they will be treated as part of a "directive prologue", a section potentially usable by JavaScript engines. This includes "strict mode" directives. Examples of **correct** code for this rule in regard to directives: +::: correct + ```js /*eslint no-unused-expressions: "error"*/ @@ -118,8 +128,12 @@ class Foo { } ``` +::: + Examples of **incorrect** code for this rule in regard to directives: +::: incorrect + ```js /*eslint no-unused-expressions: "error"*/ @@ -137,18 +151,26 @@ class Foo { } ``` +::: + ### allowShortCircuit Examples of **incorrect** code for the `{ "allowShortCircuit": true }` option: +::: incorrect + ```js /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/ a || b ``` +::: + Examples of **correct** code for the `{ "allowShortCircuit": true }` option: +::: correct + ```js /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/ @@ -156,10 +178,14 @@ a && b() a() || (b = c) ``` +::: + ### allowTernary Examples of **incorrect** code for the `{ "allowTernary": true }` option: +::: incorrect + ```js /*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/ @@ -167,8 +193,12 @@ a ? b : 0 a ? b : c() ``` +::: + Examples of **correct** code for the `{ "allowTernary": true }` option: +::: correct + ```js /*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/ @@ -176,40 +206,56 @@ a ? b() : c() a ? (b = c) : d() ``` +::: + ### allowShortCircuit and allowTernary Examples of **correct** code for the `{ "allowShortCircuit": true, "allowTernary": true }` options: +::: correct + ```js /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }]*/ a ? b() || (c = d) : e() ``` +::: + ### allowTaggedTemplates Examples of **incorrect** code for the `{ "allowTaggedTemplates": true }` option: +::: incorrect + ```js /*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/ `some untagged template string`; ``` +::: + Examples of **correct** code for the `{ "allowTaggedTemplates": true }` option: +::: correct + ```js /*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/ tag`some tagged template string`; ``` +::: + ### enforceForJSX JSX is most-commonly used in the React ecosystem, where it is compiled to `React.createElement` expressions. Though free from side-effects, these calls are not automatically flagged by the `no-unused-expression` rule. If you're using React, or any other side-effect-free JSX pragma, this option can be enabled to flag these expressions. Examples of **incorrect** code for the `{ "enforceForJSX": true }` option: +::: incorrect + ```jsx /*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/ @@ -218,8 +264,12 @@ Examples of **incorrect** code for the `{ "enforceForJSX": true }` option: <>; ``` +::: + Examples of **correct** code for the `{ "enforceForJSX": true }` option: +::: correct + ```jsx /*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/ @@ -227,3 +277,5 @@ var myComponentPartial = ; var myFragment = <>; ``` + +::: diff --git a/docs/src/rules/no-unused-labels.md b/docs/src/rules/no-unused-labels.md index 6bdc389b4f0..0bf720da1fe 100644 --- a/docs/src/rules/no-unused-labels.md +++ b/docs/src/rules/no-unused-labels.md @@ -36,6 +36,8 @@ This rule is aimed at eliminating unused labels. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unused-labels: "error"*/ @@ -51,8 +53,12 @@ for (let i = 0; i < 10; ++i) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unused-labels: "error"*/ @@ -72,6 +78,8 @@ for (let i = 0; i < 10; ++i) { } ``` +::: + ## When Not To Use It If you don't want to be notified about unused labels, then it's safe to disable this rule. diff --git a/docs/src/rules/no-unused-private-class-members.md b/docs/src/rules/no-unused-private-class-members.md index c3852c5aab3..6be1e70a791 100644 --- a/docs/src/rules/no-unused-private-class-members.md +++ b/docs/src/rules/no-unused-private-class-members.md @@ -18,6 +18,8 @@ This rule reports unused private class members. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unused-private-class-members: "error"*/ @@ -49,8 +51,12 @@ class Foo { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unused-private-class-members: "error"*/ @@ -80,6 +86,8 @@ class Foo { } ``` +::: + ## When Not To Use It If you don't want to be notified about unused private class members, you can safely turn this rule off. diff --git a/docs/src/rules/no-unused-vars.md b/docs/src/rules/no-unused-vars.md index cea0d7f1ccd..63f57dfc6f8 100644 --- a/docs/src/rules/no-unused-vars.md +++ b/docs/src/rules/no-unused-vars.md @@ -26,6 +26,8 @@ A variable is *not* considered to be used if it is only ever declared (`var foo Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-unused-vars: "error"*/ /*global some_unused_var*/ @@ -60,8 +62,12 @@ function getY([x, y]) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-unused-vars: "error"*/ @@ -89,6 +95,8 @@ function getY([, y]) { } ``` +::: + ### exported In environments outside of CommonJS or ECMAScript modules, you may use `var` to create a global variable that may be used by other scripts. You can use the `/* exported variableName */` comment block to indicate that this variable is being exported and therefore should not be considered unused. @@ -103,12 +111,16 @@ The line comment `// exported variableName` will not work as `exported` is not l Examples of **correct** code for `/* exported variableName */` operation: +::: correct + ```js /* exported global_var */ var global_var = 42; ``` +::: + ## Options This rule takes one argument which can be a string or an object. The string settings are the same as those of the `vars` property (explained below). @@ -134,6 +146,8 @@ The `vars` option has two settings: Examples of **correct** code for the `{ "vars": "local" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "vars": "local" }]*/ /*global some_unused_var */ @@ -141,12 +155,16 @@ Examples of **correct** code for the `{ "vars": "local" }` option: some_unused_var = 42; ``` +::: + ### varsIgnorePattern The `varsIgnorePattern` option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain `ignored` or `Ignored`. Examples of **correct** code for the `{ "varsIgnorePattern": "[iI]gnored" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/ @@ -155,6 +173,8 @@ var secondVar = 2; console.log(secondVar); ``` +::: + ### args The `args` option has three settings: @@ -167,6 +187,8 @@ The `args` option has three settings: Examples of **incorrect** code for the default `{ "args": "after-used" }` option: +::: incorrect + ```js /*eslint no-unused-vars: ["error", { "args": "after-used" }]*/ @@ -178,8 +200,12 @@ Examples of **incorrect** code for the default `{ "args": "after-used" }` option })(); ``` +::: + Examples of **correct** code for the default `{ "args": "after-used" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", {"args": "after-used"}]*/ @@ -188,10 +214,14 @@ Examples of **correct** code for the default `{ "args": "after-used" }` option: })(); ``` +::: + #### args: all Examples of **incorrect** code for the `{ "args": "all" }` option: +::: incorrect + ```js /*eslint no-unused-vars: ["error", { "args": "all" }]*/ @@ -203,10 +233,14 @@ Examples of **incorrect** code for the `{ "args": "all" }` option: })(); ``` +::: + #### args: none Examples of **correct** code for the `{ "args": "none" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "args": "none" }]*/ @@ -215,12 +249,16 @@ Examples of **correct** code for the `{ "args": "none" }` option: })(); ``` +::: + ### ignoreRestSiblings The `ignoreRestSiblings` option is a boolean (default: `false`). Using a [Rest Property](https://github.com/tc39/proposal-object-rest-spread) it is possible to "omit" properties from an object, but by default the sibling properties are marked as "unused". With this option enabled the rest property's siblings are ignored. Examples of **correct** code for the `{ "ignoreRestSiblings": true }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/ // 'foo' and 'bar' were ignored because they have a rest property sibling. @@ -230,12 +268,16 @@ var bar; ({ bar, ...coords } = data); ``` +::: + ### argsIgnorePattern The `argsIgnorePattern` option specifies exceptions not to check for usage: arguments whose names match a regexp pattern. For example, variables whose names begin with an underscore. Examples of **correct** code for the `{ "argsIgnorePattern": "^_" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]*/ @@ -245,12 +287,16 @@ function foo(x, _y) { foo(); ``` +::: + ### destructuredArrayIgnorePattern The `destructuredArrayIgnorePattern` option specifies exceptions not to check for usage: elements of array destructuring patterns whose names match a regexp pattern. For example, variables whose names begin with an underscore. Examples of **correct** code for the `{ "destructuredArrayIgnorePattern": "^_" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "destructuredArrayIgnorePattern": "^_" }]*/ @@ -282,6 +328,8 @@ _o = 1; p; ``` +::: + ### caughtErrors The `caughtErrors` option is used for `catch` block arguments validation. @@ -297,6 +345,8 @@ Not specifying this rule is equivalent of assigning it to `none`. Examples of **correct** code for the `{ "caughtErrors": "none" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "caughtErrors": "none" }]*/ @@ -307,10 +357,14 @@ try { } ``` +::: + #### caughtErrors: all Examples of **incorrect** code for the `{ "caughtErrors": "all" }` option: +::: incorrect + ```js /*eslint no-unused-vars: ["error", { "caughtErrors": "all" }]*/ @@ -323,12 +377,16 @@ try { } ``` +::: + ### caughtErrorsIgnorePattern The `caughtErrorsIgnorePattern` option specifies exceptions not to check for usage: catch arguments whose names match a regexp pattern. For example, variables whose names begin with a string 'ignore'. Examples of **correct** code for the `{ "caughtErrorsIgnorePattern": "^ignore" }` option: +::: correct + ```js /*eslint no-unused-vars: ["error", { "caughtErrorsIgnorePattern": "^ignore" }]*/ @@ -339,6 +397,8 @@ try { } ``` +::: + ## When Not To Use It If you don't want to be notified about unused variables or function arguments, you can safely turn this rule off. diff --git a/docs/src/rules/no-use-before-define.md b/docs/src/rules/no-use-before-define.md index 563ee651c2a..f595f327229 100644 --- a/docs/src/rules/no-use-before-define.md +++ b/docs/src/rules/no-use-before-define.md @@ -17,6 +17,8 @@ This rule will warn when it encounters a reference to an identifier that has not Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-use-before-define: "error"*/ @@ -65,8 +67,12 @@ export { foo }; const foo = 1; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-use-before-define: "error"*/ @@ -117,6 +123,8 @@ const foo = 1; export { foo }; ``` +::: + ## Options ```json @@ -159,6 +167,8 @@ This rule accepts `"nofunc"` string as an option. Examples of **correct** code for the `{ "functions": false }` option: +::: correct + ```js /*eslint no-use-before-define: ["error", { "functions": false }]*/ @@ -166,12 +176,16 @@ f(); function f() {} ``` +::: + This option allows references to function declarations. For function expressions and arrow functions, please see the [`variables`](#variables) option. ### classes Examples of **incorrect** code for the `{ "classes": false }` option: +::: incorrect + ```js /*eslint no-use-before-define: ["error", { "classes": false }]*/ @@ -205,8 +219,12 @@ class A { } ``` +::: + Examples of **correct** code for the `{ "classes": false }` option: +::: correct + ```js /*eslint no-use-before-define: ["error", { "classes": false }]*/ @@ -218,10 +236,14 @@ class A { } ``` +::: + ### variables Examples of **incorrect** code for the `{ "variables": false }` option: +::: incorrect + ```js /*eslint no-use-before-define: ["error", { "variables": false }]*/ @@ -257,8 +279,12 @@ const g = function() {}; } ``` +::: + Examples of **correct** code for the `{ "variables": false }` option: +::: correct + ```js /*eslint no-use-before-define: ["error", { "variables": false }]*/ @@ -283,10 +309,14 @@ const g = function() {} } ``` +::: + ### allowNamedExports Examples of **correct** code for the `{ "allowNamedExports": true }` option: +::: correct + ```js /*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/ @@ -301,8 +331,12 @@ function f () {} class C {} ``` +::: + Examples of **incorrect** code for the `{ "allowNamedExports": true }` option: +::: incorrect + ```js /*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/ @@ -317,3 +351,5 @@ export function foo() { } const d = 1; ``` + +::: diff --git a/docs/src/rules/no-useless-backreference.md b/docs/src/rules/no-useless-backreference.md index 615287b12bd..5111d4cc96c 100644 --- a/docs/src/rules/no-useless-backreference.md +++ b/docs/src/rules/no-useless-backreference.md @@ -56,6 +56,8 @@ This might be surprising to developers coming from other languages where some of Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-useless-backreference: "error"*/ @@ -88,8 +90,12 @@ new RegExp('(\\1)'); // nested reference to (\1) /(? {} ``` +::: + Examples of **correct** code for this rule by default: +::: correct + ```js /*eslint no-useless-rename: "error"*/ @@ -101,16 +107,24 @@ function foo({ bar: baz }) {} ({ foo: bar }) => {} ``` +::: + Examples of **correct** code for this rule with `{ ignoreImport: true }`: +::: correct + ```js /*eslint no-useless-rename: ["error", { ignoreImport: true }]*/ import { foo as foo } from "bar"; ``` +::: + Examples of **correct** code for this rule with `{ ignoreExport: true }`: +::: correct + ```js /*eslint no-useless-rename: ["error", { ignoreExport: true }]*/ @@ -118,8 +132,12 @@ export { foo as foo }; export { foo as foo } from "bar"; ``` +::: + Examples of **correct** code for this rule with `{ ignoreDestructuring: true }`: +::: correct + ```js /*eslint no-useless-rename: ["error", { ignoreDestructuring: true }]*/ @@ -128,6 +146,8 @@ function foo({ bar: bar }) {} ({ foo: foo }) => {} ``` +::: + ## When Not To Use It You can safely disable this rule if you do not care about redundantly renaming import, export, and destructuring assignments. diff --git a/docs/src/rules/no-useless-return.md b/docs/src/rules/no-useless-return.md index b207177d35b..d4ffb2a0c3b 100644 --- a/docs/src/rules/no-useless-return.md +++ b/docs/src/rules/no-useless-return.md @@ -17,6 +17,8 @@ This rule aims to report redundant `return` statements. Examples of **incorrect** code for this rule: +::: incorrect + ```js /* eslint no-useless-return: "error" */ @@ -48,8 +50,12 @@ function foo() { ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /* eslint no-useless-return: "error" */ @@ -87,6 +93,8 @@ function foo() { ``` +::: + ## When Not To Use It If you don't care about disallowing redundant return statements, you can turn off this rule. diff --git a/docs/src/rules/no-var.md b/docs/src/rules/no-var.md index 8d8b754797c..008362ff9ea 100644 --- a/docs/src/rules/no-var.md +++ b/docs/src/rules/no-var.md @@ -34,6 +34,8 @@ This rule is aimed at discouraging the use of `var` and encouraging the use of ` Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-var: "error"*/ @@ -41,8 +43,12 @@ var x = "y"; var CONFIG = {}; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-var: "error"*/ /*eslint-env es6*/ @@ -51,6 +57,8 @@ let x = "y"; const CONFIG = {}; ``` +::: + ## When Not To Use It In addition to non-ES6 environments, existing JavaScript projects that are beginning to introduce ES6 into their diff --git a/docs/src/rules/no-void.md b/docs/src/rules/no-void.md index d37beda429b..d740a09b06b 100644 --- a/docs/src/rules/no-void.md +++ b/docs/src/rules/no-void.md @@ -63,6 +63,8 @@ This rule aims to eliminate use of void operator. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-void: "error"*/ @@ -75,6 +77,8 @@ function baz() { } ``` +::: + ## Options This rule has an object option: @@ -87,6 +91,8 @@ When `allowAsStatement` is set to true, the rule will not error on cases that th Examples of **incorrect** code for `{ "allowAsStatement": true }`: +::: incorrect + ```js /*eslint no-void: ["error", { "allowAsStatement": true }]*/ @@ -96,8 +102,12 @@ function baz() { } ``` +::: + Examples of **correct** code for `{ "allowAsStatement": true }`: +::: correct + ```js /*eslint no-void: ["error", { "allowAsStatement": true }]*/ @@ -105,6 +115,8 @@ void foo; void someFunction(); ``` +::: + ## When Not To Use It If you intentionally use the `void` operator then you can disable this rule. diff --git a/docs/src/rules/no-warning-comments.md b/docs/src/rules/no-warning-comments.md index 6775c41e408..533c1e05162 100644 --- a/docs/src/rules/no-warning-comments.md +++ b/docs/src/rules/no-warning-comments.md @@ -27,6 +27,8 @@ This rule has an options object literal: Example of **incorrect** code for the default `{ "terms": ["todo", "fixme", "xxx"], "location": "start" }` options: +::: incorrect + ```js /*eslint no-warning-comments: "error"*/ @@ -39,8 +41,12 @@ function callback(err, results) { } ``` +::: + Example of **correct** code for the default `{ "terms": ["todo", "fixme", "xxx"], "location": "start" }` options: +::: correct + ```js /*eslint no-warning-comments: "error"*/ @@ -54,10 +60,14 @@ function callback(err, results) { } ``` +::: + ### terms and location Examples of **incorrect** code for the `{ "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }` options: +::: incorrect + ```js /*eslint no-warning-comments: ["error", { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }]*/ @@ -71,8 +81,12 @@ Examples of **incorrect** code for the `{ "terms": ["todo", "fixme", "any other */ ``` +::: + Examples of **correct** code for the `{ "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }` options: +::: correct + ```js /*eslint no-warning-comments: ["error", { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }]*/ @@ -86,6 +100,8 @@ Examples of **correct** code for the `{ "terms": ["todo", "fixme", "any other te */ ``` +::: + ## When Not To Use It * If you have a large code base that was not developed with a policy to not use such warning terms, you might get hundreds of warnings / errors which might be counter-productive if you can't fix all of them (e.g. if you don't get the time to do it) as you might overlook other warnings / errors or get used to many of them and don't pay attention on it anymore. diff --git a/docs/src/rules/no-whitespace-before-property.md b/docs/src/rules/no-whitespace-before-property.md index c90bbe8a575..57830be2fe2 100644 --- a/docs/src/rules/no-whitespace-before-property.md +++ b/docs/src/rules/no-whitespace-before-property.md @@ -28,6 +28,8 @@ foo Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-whitespace-before-property: "error"*/ @@ -46,8 +48,12 @@ foo .bar(). baz() ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-whitespace-before-property: "error"*/ @@ -71,6 +77,8 @@ foo. baz() ``` +::: + ## When Not To Use It Turn this rule off if you do not care about allowing whitespace around the dot or before the opening bracket before properties of objects if they are on the same line. diff --git a/docs/src/rules/no-with.md b/docs/src/rules/no-with.md index 4e479dee010..d90e1320888 100644 --- a/docs/src/rules/no-with.md +++ b/docs/src/rules/no-with.md @@ -21,6 +21,8 @@ If ESLint parses code in strict mode, the parser (instead of this rule) reports Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint no-with: "error"*/ @@ -29,8 +31,12 @@ with (point) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint no-with: "error"*/ /*eslint-env es6*/ @@ -38,6 +44,8 @@ Examples of **correct** code for this rule: const r = ({x, y}) => Math.sqrt(x * x + y * y); ``` +::: + ## When Not To Use It If you intentionally use `with` statements then you can disable this rule. diff --git a/docs/src/rules/no-wrap-func.md b/docs/src/rules/no-wrap-func.md index 0285338ed4b..22c57a390fd 100644 --- a/docs/src/rules/no-wrap-func.md +++ b/docs/src/rules/no-wrap-func.md @@ -27,14 +27,22 @@ This rule will raise a warning when it encounters a function expression wrapped Example of **incorrect** code for this rule: +::: incorrect + ```js var a = (function() {/*...*/}); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js var a = function() {/*...*/}; (function() {/*...*/})(); ``` + +::: diff --git a/docs/src/rules/nonblock-statement-body-position.md b/docs/src/rules/nonblock-statement-body-position.md index 05b2a40ce9a..e15ff91ff3a 100644 --- a/docs/src/rules/nonblock-statement-body-position.md +++ b/docs/src/rules/nonblock-statement-body-position.md @@ -55,6 +55,8 @@ Additionally, the rule accepts an optional object option with an `"overrides"` k Examples of **incorrect** code for this rule with the default `"beside"` option: +::: incorrect + ```js /* eslint nonblock-statement-body-position: ["error", "beside"] */ @@ -75,8 +77,12 @@ while (foo) ``` +::: + Examples of **correct** code for this rule with the default `"beside"` option: +::: correct + ```js /* eslint nonblock-statement-body-position: ["error", "beside"] */ @@ -96,8 +102,12 @@ if (foo) { // block statements are always allowed with this rule } ``` +::: + Examples of **incorrect** code for this rule with the `"below"` option: +::: incorrect + ```js /* eslint nonblock-statement-body-position: ["error", "below"] */ @@ -111,8 +121,12 @@ for (let i = 1; i < foo; i++) bar(); do bar(); while (foo) ``` +::: + Examples of **correct** code for this rule with the `"below"` option: +::: correct + ```js /* eslint nonblock-statement-body-position: ["error", "below"] */ @@ -138,8 +152,12 @@ if (foo) { } ``` +::: + Examples of **incorrect** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule: +::: incorrect + ```js /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */ @@ -149,8 +167,12 @@ if (foo) while (foo) bar(); ``` +::: + Examples of **correct** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule: +::: correct + ```js /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */ @@ -160,6 +182,8 @@ while (foo) bar(); ``` +::: + ## When Not To Use It If you're not concerned about consistent locations of single-line statements, you should not turn on this rule. You can also disable this rule if you're using the `"all"` option for the [`curly`](/docs/rules/curly) rule, because this will disallow single-line statements entirely. diff --git a/docs/src/rules/object-curly-newline.md b/docs/src/rules/object-curly-newline.md index eadbc77680f..e85532befe0 100644 --- a/docs/src/rules/object-curly-newline.md +++ b/docs/src/rules/object-curly-newline.md @@ -55,6 +55,8 @@ You can specify different options for object literals, destructuring assignments Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint object-curly-newline: ["error", "always"]*/ /*eslint-env es6*/ @@ -78,8 +80,12 @@ let {k = function() { }} = obj; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint object-curly-newline: ["error", "always"]*/ /*eslint-env es6*/ @@ -121,10 +127,14 @@ let { } = obj; ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint object-curly-newline: ["error", "never"]*/ /*eslint-env es6*/ @@ -166,8 +176,12 @@ let { } = obj; ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint object-curly-newline: ["error", "never"]*/ /*eslint-env es6*/ @@ -191,10 +205,14 @@ let {k = function() { }} = obj; ``` +::: + ### multiline Examples of **incorrect** code for this rule with the `{ "multiline": true }` option: +::: incorrect + ```js /*eslint object-curly-newline: ["error", { "multiline": true }]*/ /*eslint-env es6*/ @@ -228,8 +246,12 @@ let {k = function() { }} = obj; ``` +::: + Examples of **correct** code for this rule with the `{ "multiline": true }` option: +::: correct + ```js /*eslint object-curly-newline: ["error", { "multiline": true }]*/ /*eslint-env es6*/ @@ -261,10 +283,14 @@ let { } = obj; ``` +::: + ### minProperties Examples of **incorrect** code for this rule with the `{ "minProperties": 2 }` option: +::: incorrect + ```js /*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/ /*eslint-env es6*/ @@ -298,8 +324,12 @@ let { } = obj; ``` +::: + Examples of **correct** code for this rule with the `{ "minProperties": 2 }` option: +::: correct + ```js /*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/ /*eslint-env es6*/ @@ -331,10 +361,14 @@ let {k = function() { }} = obj; ``` +::: + ### consistent Examples of **incorrect** code for this rule with the default `{ "consistent": true }` option: +::: incorrect + ```js /*eslint object-curly-newline: ["error", { "consistent": true }]*/ /*eslint-env es6*/ @@ -377,8 +411,12 @@ let { }} = obj; ``` +::: + Examples of **correct** code for this rule with the default `{ "consistent": true }` option: +::: correct + ```js /*eslint object-curly-newline: ["error", { "consistent": true }]*/ /*eslint-env es6*/ @@ -429,10 +467,14 @@ let { } = obj; ``` +::: + ### ObjectExpression and ObjectPattern Examples of **incorrect** code for this rule with the `{ "ObjectExpression": "always", "ObjectPattern": "never" }` options: +::: incorrect + ```js /*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/ /*eslint-env es6*/ @@ -465,8 +507,12 @@ let { } = obj; ``` +::: + Examples of **correct** code for this rule with the `{ "ObjectExpression": "always", "ObjectPattern": "never" }` options: +::: correct + ```js /*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/ /*eslint-env es6*/ @@ -499,10 +545,14 @@ let {k = function() { }} = obj; ``` +::: + ### ImportDeclaration and ExportDeclaration Examples of **incorrect** code for this rule with the `{ "ImportDeclaration": "always", "ExportDeclaration": "never" }` options: +::: incorrect + ```js /*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/ /*eslint-env es6*/ @@ -522,8 +572,12 @@ export { } from 'foo-bar'; ``` +::: + Examples of **correct** code for this rule with the `{ "ImportDeclaration": "always", "ExportDeclaration": "never" }` options: +::: correct + ```js /*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/ /*eslint-env es6*/ @@ -544,6 +598,8 @@ export { foo, bar } from 'foo-bar'; export { foo as f, bar } from 'foo-bar'; ``` +::: + ## When Not To Use It If you don't want to enforce consistent line breaks after opening and before closing braces, then it's safe to disable this rule. diff --git a/docs/src/rules/object-curly-spacing.md b/docs/src/rules/object-curly-spacing.md index 42f296c2f2c..7ee291b49d0 100644 --- a/docs/src/rules/object-curly-spacing.md +++ b/docs/src/rules/object-curly-spacing.md @@ -56,6 +56,8 @@ Object option: Examples of **incorrect** code for this rule with the default `"never"` option: +::: incorrect + ```js /*eslint object-curly-spacing: ["error", "never"]*/ @@ -67,8 +69,12 @@ var {x } = y; import { foo } from 'bar'; ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +::: correct + ```js /*eslint object-curly-spacing: ["error", "never"]*/ @@ -86,10 +92,14 @@ var {x} = y; import {foo} from 'bar'; ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint object-curly-spacing: ["error", "always"]*/ @@ -105,8 +115,12 @@ var {x} = y; import {foo } from 'bar'; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint object-curly-spacing: ["error", "always"]*/ @@ -120,10 +134,14 @@ var { x } = y; import { foo } from 'bar'; ``` +::: + #### arraysInObjects Examples of additional **correct** code for this rule with the `"never", { "arraysInObjects": true }` options: +::: correct + ```js /*eslint object-curly-spacing: ["error", "never", { "arraysInObjects": true }]*/ @@ -131,8 +149,12 @@ var obj = {"foo": [ 1, 2 ] }; var obj = {"foo": [ "baz", "bar" ] }; ``` +::: + Examples of additional **correct** code for this rule with the `"always", { "arraysInObjects": false }` options: +::: correct + ```js /*eslint object-curly-spacing: ["error", "always", { "arraysInObjects": false }]*/ @@ -140,24 +162,34 @@ var obj = { "foo": [ 1, 2 ]}; var obj = { "foo": [ "baz", "bar" ]}; ``` +::: + #### objectsInObjects Examples of additional **correct** code for this rule with the `"never", { "objectsInObjects": true }` options: +::: correct + ```js /*eslint object-curly-spacing: ["error", "never", { "objectsInObjects": true }]*/ var obj = {"foo": {"baz": 1, "bar": 2} }; ``` +::: + Examples of additional **correct** code for this rule with the `"always", { "objectsInObjects": false }` options: +::: correct + ```js /*eslint object-curly-spacing: ["error", "always", { "objectsInObjects": false }]*/ var obj = { "foo": { "baz": 1, "bar": 2 }}; ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing between curly braces. diff --git a/docs/src/rules/object-property-newline.md b/docs/src/rules/object-property-newline.md index 2407e7bdcc4..11eeb4f57e4 100644 --- a/docs/src/rules/object-property-newline.md +++ b/docs/src/rules/object-property-newline.md @@ -188,6 +188,8 @@ As illustrated above, the `--fix` option, applied to this rule, does not comply Examples of **incorrect** code for this rule, with no object option or with `allowAllPropertiesOnSameLine` set to `false`: +::: incorrect + ```js /*eslint object-property-newline: "error"*/ @@ -222,8 +224,12 @@ const obj5 = { ]: true}; ``` +::: + Examples of **correct** code for this rule, with no object option or with `allowAllPropertiesOnSameLine` set to `false`: +::: correct + ```js /*eslint object-property-newline: "error"*/ @@ -252,8 +258,12 @@ const obj3 = { }; ``` +::: + Examples of additional **correct** code for this rule with the `{ "allowAllPropertiesOnSameLine": true }` option: +::: correct + ```js /*eslint object-property-newline: ["error", { "allowAllPropertiesOnSameLine": true }]*/ @@ -268,6 +278,8 @@ const obj3 = { }; ``` +::: + ## When Not To Use It You can turn this rule off if you want to decide, case-by-case, whether to place property specifications on separate lines. diff --git a/docs/src/rules/object-shorthand.md b/docs/src/rules/object-shorthand.md index 7ff7c2df6f7..b63bcbfae2f 100644 --- a/docs/src/rules/object-shorthand.md +++ b/docs/src/rules/object-shorthand.md @@ -129,6 +129,8 @@ Additionally, the rule takes an optional object configuration: Example of **incorrect** code for this rule with the `"always", { "avoidQuotes": true }` option: +::: incorrect + ```js /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/ /*eslint-env es6*/ @@ -138,8 +140,12 @@ var foo = { }; ``` +::: + Example of **correct** code for this rule with the `"always", { "avoidQuotes": true }` option: +::: correct + ```js /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/ /*eslint-env es6*/ @@ -150,6 +156,8 @@ var foo = { }; ``` +::: + ### `ignoreConstructors` ```json @@ -160,6 +168,8 @@ var foo = { Example of **correct** code for this rule with the `"always", { "ignoreConstructors": true }` option: +::: correct + ```js /*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/ /*eslint-env es6*/ @@ -169,6 +179,8 @@ var foo = { }; ``` +::: + ### `avoidExplicitReturnArrows` ```json @@ -179,6 +191,8 @@ var foo = { Example of **incorrect** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option: +::: incorrect + ```js /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/ /*eslint-env es6*/ @@ -194,8 +208,12 @@ var foo = { }; ``` +::: + Example of **correct** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option: +::: correct + ```js /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/ /*eslint-env es6*/ @@ -209,8 +227,12 @@ var foo = { }; ``` +::: + Example of **incorrect** code for this rule with the `"consistent"` option: +::: incorrect + ```js /*eslint object-shorthand: [2, "consistent"]*/ /*eslint-env es6*/ @@ -221,8 +243,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the `"consistent"` option: +::: correct + ```js /*eslint object-shorthand: [2, "consistent"]*/ /*eslint-env es6*/ @@ -238,8 +264,12 @@ var bar = { }; ``` +::: + Example of **incorrect** code with the `"consistent-as-needed"` option, which is very similar to `"consistent"`: +::: incorrect + ```js /*eslint object-shorthand: [2, "consistent-as-needed"]*/ /*eslint-env es6*/ @@ -250,6 +280,8 @@ var foo = { }; ``` +::: + ## When Not To Use It Anyone not yet in an ES6 environment would not want to apply this rule. Others may find the terseness of the shorthand diff --git a/docs/src/rules/one-var-declaration-per-line.md b/docs/src/rules/one-var-declaration-per-line.md index 0a04ec25fcc..bebe4d7f4e3 100644 --- a/docs/src/rules/one-var-declaration-per-line.md +++ b/docs/src/rules/one-var-declaration-per-line.md @@ -42,6 +42,8 @@ This rule has a single string option: Examples of **incorrect** code for this rule with the default `"initializations"` option: +::: incorrect + ```js /*eslint one-var-declaration-per-line: ["error", "initializations"]*/ /*eslint-env es6*/ @@ -52,8 +54,12 @@ let a, b = 0, c; ``` +::: + Examples of **correct** code for this rule with the default `"initializations"` option: +::: correct + ```js /*eslint one-var-declaration-per-line: ["error", "initializations"]*/ /*eslint-env es6*/ @@ -67,10 +73,14 @@ let a, b = 0; ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint one-var-declaration-per-line: ["error", "always"]*/ /*eslint-env es6*/ @@ -82,8 +92,12 @@ let a, b = 0; const a = 0, b = 0; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint one-var-declaration-per-line: ["error", "always"]*/ /*eslint-env es6*/ @@ -94,3 +108,5 @@ var a, let a, b = 0; ``` + +::: diff --git a/docs/src/rules/one-var.md b/docs/src/rules/one-var.md index c7631e2493e..476f123076a 100644 --- a/docs/src/rules/one-var.md +++ b/docs/src/rules/one-var.md @@ -73,6 +73,8 @@ Alternate object option: Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint one-var: ["error", "always"]*/ @@ -118,8 +120,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint one-var: ["error", "always"]*/ @@ -179,10 +185,14 @@ class C { } ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint one-var: ["error", "never"]*/ @@ -215,8 +225,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint one-var: ["error", "never"]*/ @@ -256,10 +270,14 @@ for (var i = 0, len = arr.length; i < len; i++) { } ``` +::: + ### consecutive Examples of **incorrect** code for this rule with the `"consecutive"` option: +::: incorrect + ```js /*eslint one-var: ["error", "consecutive"]*/ @@ -288,8 +306,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `"consecutive"` option: +::: correct + ```js /*eslint one-var: ["error", "consecutive"]*/ @@ -319,10 +341,14 @@ class C { } ``` +::: + ### var, let, and const Examples of **incorrect** code for this rule with the `{ var: "always", let: "never", const: "never" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/ /*eslint-env es6*/ @@ -342,8 +368,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ var: "always", let: "never", const: "never" }` option: +::: correct + ```js /*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/ /*eslint-env es6*/ @@ -363,8 +393,12 @@ function foo() { } ``` +::: + Examples of **incorrect** code for this rule with the `{ var: "never" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { var: "never" }]*/ /*eslint-env es6*/ @@ -375,8 +409,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ var: "never" }` option: +::: correct + ```js /*eslint one-var: ["error", { var: "never" }]*/ /*eslint-env es6*/ @@ -391,8 +429,12 @@ function foo() { } ``` +::: + Examples of **incorrect** code for this rule with the `{ separateRequires: true }` option: +::: incorrect + ```js /*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/ /*eslint-env node*/ @@ -401,8 +443,12 @@ var foo = require("foo"), bar = "bar"; ``` +::: + Examples of **correct** code for this rule with the `{ separateRequires: true }` option: +::: correct + ```js /*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/ /*eslint-env node*/ @@ -411,6 +457,8 @@ var foo = require("foo"); var bar = "bar"; ``` +::: + ```js var foo = require("foo"), bar = require("bar"); @@ -418,6 +466,8 @@ var foo = require("foo"), Examples of **incorrect** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/ /*eslint-env es6*/ @@ -441,8 +491,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ var: "never", let: "consecutive", const: "consecutive" }` option: +::: correct + ```js /*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/ /*eslint-env es6*/ @@ -468,8 +522,12 @@ function foo() { } ``` +::: + Examples of **incorrect** code for this rule with the `{ var: "consecutive" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { var: "consecutive" }]*/ /*eslint-env es6*/ @@ -480,8 +538,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ var: "consecutive" }` option: +::: correct + ```js /*eslint one-var: ["error", { var: "consecutive" }]*/ /*eslint-env es6*/ @@ -496,10 +558,14 @@ function foo() { } ``` +::: + ### initialized and uninitialized Examples of **incorrect** code for this rule with the `{ "initialized": "always", "uninitialized": "never" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/ /*eslint-env es6*/ @@ -511,8 +577,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "initialized": "always", "uninitialized": "never" }` option: +::: correct + ```js /*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/ @@ -534,8 +604,12 @@ for (z of foo) { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "initialized": "never" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { "initialized": "never" }]*/ /*eslint-env es6*/ @@ -546,8 +620,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "initialized": "never" }` option: +::: correct + ```js /*eslint one-var: ["error", { "initialized": "never" }]*/ @@ -558,8 +636,12 @@ function foo() { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "initialized": "consecutive", "uninitialized": "never" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/ @@ -573,8 +655,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "initialized": "consecutive", "uninitialized": "never" }` option: +::: correct + ```js /*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/ @@ -588,8 +674,12 @@ function foo() { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "initialized": "consecutive" }` option: +::: incorrect + ```js /*eslint one-var: ["error", { "initialized": "consecutive" }]*/ @@ -604,8 +694,12 @@ function foo() { } ``` +::: + Examples of **correct** code for this rule with the `{ "initialized": "consecutive" }` option: +::: correct + ```js /*eslint one-var: ["error", { "initialized": "consecutive" }]*/ @@ -620,6 +714,8 @@ function foo() { } ``` +::: + ## Compatibility * **JSHint**: This rule maps to the `onevar` JSHint rule, but allows `let` and `const` to be configured separately. diff --git a/docs/src/rules/operator-assignment.md b/docs/src/rules/operator-assignment.md index 3f535fad92a..b3443c5b1e3 100644 --- a/docs/src/rules/operator-assignment.md +++ b/docs/src/rules/operator-assignment.md @@ -45,6 +45,8 @@ This rule has a single string option: Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint operator-assignment: ["error", "always"]*/ @@ -54,8 +56,12 @@ x[0] = x[0] / y; x.y = x.y << z; ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint operator-assignment: ["error", "always"]*/ @@ -68,10 +74,14 @@ x[foo()] = x[foo()] % 2; x = y + x; // `+` is not always commutative (e.g. x = "abc") ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint operator-assignment: ["error", "never"]*/ @@ -79,8 +89,12 @@ x *= y; x ^= (y + z) / foo(); ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint operator-assignment: ["error", "never"]*/ @@ -88,6 +102,8 @@ x = x + y; x.y = x.y / a.b; ``` +::: + ## When Not To Use It Use of operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis. diff --git a/docs/src/rules/operator-linebreak.md b/docs/src/rules/operator-linebreak.md index 34efbc6fcb3..0b9bd5131b8 100644 --- a/docs/src/rules/operator-linebreak.md +++ b/docs/src/rules/operator-linebreak.md @@ -51,6 +51,8 @@ The default configuration is `"after", { "overrides": { "?": "before", ":": "bef Examples of **incorrect** code for this rule with the `"after"` option: +::: incorrect + ```js /*eslint operator-linebreak: ["error", "after"]*/ @@ -83,8 +85,12 @@ class Foo { } ``` +::: + Examples of **correct** code for this rule with the `"after"` option: +::: correct + ```js /*eslint operator-linebreak: ["error", "after"]*/ @@ -116,10 +122,14 @@ class Foo { } ``` +::: + ### before Examples of **incorrect** code for this rule with the `"before"` option: +::: incorrect + ```js /*eslint operator-linebreak: ["error", "before"]*/ @@ -148,8 +158,12 @@ class Foo { } ``` +::: + Examples of **correct** code for this rule with the `"before"` option: +::: correct + ```js /*eslint operator-linebreak: ["error", "before"]*/ @@ -181,10 +195,14 @@ class Foo { } ``` +::: + ### none Examples of **incorrect** code for this rule with the `"none"` option: +::: incorrect + ```js /*eslint operator-linebreak: ["error", "none"]*/ @@ -228,8 +246,12 @@ class Foo { } ``` +::: + Examples of **correct** code for this rule with the `"none"` option: +::: correct + ```js /*eslint operator-linebreak: ["error", "none"]*/ @@ -254,10 +276,14 @@ class Foo { } ``` +::: + ### overrides Examples of additional **incorrect** code for this rule with the `{ "overrides": { "+=": "before" } }` option: +::: incorrect + ```js /*eslint operator-linebreak: ["error", "after", { "overrides": { "+=": "before" } }]*/ @@ -266,8 +292,12 @@ thing += 's'; ``` +::: + Examples of additional **correct** code for this rule with the `{ "overrides": { "+=": "before" } }` option: +::: correct + ```js /*eslint operator-linebreak: ["error", "after", { "overrides": { "+=": "before" } }]*/ @@ -276,8 +306,12 @@ thing += 's'; ``` +::: + Examples of additional **correct** code for this rule with the `{ "overrides": { "?": "ignore", ":": "ignore" } }` option: +::: correct + ```js /*eslint operator-linebreak: ["error", "after", { "overrides": { "?": "ignore", ":": "ignore" } }]*/ @@ -292,8 +326,12 @@ answer = everything foo; ``` +::: + Examples of **incorrect** code for this rule with the default `"after", { "overrides": { "?": "before", ":": "before" } }` option: +::: incorrect + ```js /*eslint operator-linebreak: ["error", "after", { "overrides": { "?": "before", ":": "before" } }]*/ @@ -316,8 +354,12 @@ answer = everything ? foo; ``` +::: + Examples of **correct** code for this rule with the default `"after", { "overrides": { "?": "before", ":": "before" } }` option: +::: correct + ```js /*eslint operator-linebreak: ["error", "after", { "overrides": { "?": "before", ":": "before" } }]*/ @@ -338,6 +380,8 @@ answer = everything : foo; ``` +::: + ## When Not To Use It If your project will not be using a common operator line break style, turn this rule off. diff --git a/docs/src/rules/padded-blocks.md b/docs/src/rules/padded-blocks.md index 7667c0a02df..647f3492be8 100644 --- a/docs/src/rules/padded-blocks.md +++ b/docs/src/rules/padded-blocks.md @@ -56,6 +56,8 @@ Object option: Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", "always"]*/ @@ -88,8 +90,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint padded-blocks: ["error", "always"]*/ @@ -124,10 +130,14 @@ class C { } ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", "never"]*/ @@ -165,8 +175,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint padded-blocks: ["error", "never"]*/ @@ -186,10 +200,14 @@ class C { } ``` +::: + ### blocks Examples of **incorrect** code for this rule with the `{ "blocks": "always" }` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", { "blocks": "always" }]*/ @@ -229,8 +247,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `{ "blocks": "always" }` option: +::: correct + ```js /*eslint padded-blocks: ["error", { "blocks": "always" }]*/ @@ -274,8 +296,12 @@ class D { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "blocks": "never" }` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", { "blocks": "never" }]*/ @@ -311,8 +337,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with the `{ "blocks": "never" }` option: +::: correct + ```js /*eslint padded-blocks: ["error", { "blocks": "never" }]*/ @@ -340,10 +370,14 @@ class D { } ``` +::: + ### classes Examples of **incorrect** code for this rule with the `{ "classes": "always" }` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", { "classes": "always" }]*/ @@ -353,8 +387,12 @@ class A { } ``` +::: + Examples of **correct** code for this rule with the `{ "classes": "always" }` option: +::: correct + ```js /*eslint padded-blocks: ["error", { "classes": "always" }]*/ @@ -366,8 +404,12 @@ class A { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "classes": "never" }` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", { "classes": "never" }]*/ @@ -379,8 +421,12 @@ class A { } ``` +::: + Examples of **correct** code for this rule with the `{ "classes": "never" }` option: +::: correct + ```js /*eslint padded-blocks: ["error", { "classes": "never" }]*/ @@ -390,10 +436,14 @@ class A { } ``` +::: + ### switches Examples of **incorrect** code for this rule with the `{ "switches": "always" }` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", { "switches": "always" }]*/ @@ -402,8 +452,12 @@ switch (a) { } ``` +::: + Examples of **correct** code for this rule with the `{ "switches": "always" }` option: +::: correct + ```js /*eslint padded-blocks: ["error", { "switches": "always" }]*/ @@ -418,8 +472,12 @@ if (a) { } ``` +::: + Examples of **incorrect** code for this rule with the `{ "switches": "never" }` option: +::: incorrect + ```js /*eslint padded-blocks: ["error", { "switches": "never" }]*/ @@ -430,8 +488,12 @@ switch (a) { } ``` +::: + Examples of **correct** code for this rule with the `{ "switches": "never" }` option: +::: correct + ```js /*eslint padded-blocks: ["error", { "switches": "never" }]*/ @@ -446,10 +508,14 @@ if (a) { } ``` +::: + ### always + allowSingleLineBlocks Examples of **incorrect** code for this rule with the `"always", {"allowSingleLineBlocks": true}` options: +::: incorrect + ```js /*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/ @@ -468,8 +534,12 @@ if (a) { } ``` +::: + Examples of **correct** code for this rule with the `"always", {"allowSingleLineBlocks": true}` options: +::: correct + ```js /*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/ @@ -482,6 +552,8 @@ if (a) { } ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of padding within blocks. diff --git a/docs/src/rules/padding-line-between-statements.md b/docs/src/rules/padding-line-between-statements.md index 3cd684236cf..d3d40f8f14f 100644 --- a/docs/src/rules/padding-line-between-statements.md +++ b/docs/src/rules/padding-line-between-statements.md @@ -98,6 +98,8 @@ This configuration would require blank lines before all `return` statements, lik Examples of **incorrect** code for the `[{ blankLine: "always", prev: "*", next: "return" }]` configuration: +::: incorrect + ```js /*eslint padding-line-between-statements: [ "error", @@ -110,8 +112,12 @@ function foo() { } ``` +::: + Examples of **correct** code for the `[{ blankLine: "always", prev: "*", next: "return" }]` configuration: +::: correct + ```js /*eslint padding-line-between-statements: [ "error", @@ -129,12 +135,16 @@ function foo() { } ``` +::: + ---- This configuration would require blank lines after every sequence of variable declarations, like the [newline-after-var](newline-after-var) rule. Examples of **incorrect** code for the `[{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}]` configuration: +::: incorrect + ```js /*eslint padding-line-between-statements: [ "error", @@ -165,8 +175,12 @@ class C { } ``` +::: + Examples of **correct** code for the `[{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}]` configuration: +::: correct + ```js /*eslint padding-line-between-statements: [ "error", @@ -205,12 +219,16 @@ class C { } ``` +::: + ---- This configuration would require blank lines after all directive prologues, like the [lines-around-directive](lines-around-directive) rule. Examples of **incorrect** code for the `[{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }]` configuration: +::: incorrect + ```js /*eslint padding-line-between-statements: [ "error", @@ -222,8 +240,12 @@ Examples of **incorrect** code for the `[{ blankLine: "always", prev: "directive foo(); ``` +::: + Examples of **correct** code for the `[{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }]` configuration: +::: correct + ```js /*eslint padding-line-between-statements: [ "error", @@ -237,12 +259,16 @@ Examples of **correct** code for the `[{ blankLine: "always", prev: "directive", foo(); ``` +::: + ---- This configuration would require blank lines between clauses in `switch` statements. Examples of **incorrect** code for the `[{ blankLine: "always", prev: ["case", "default"], next: "*" }]` configuration: +::: incorrect + ```js /*eslint padding-line-between-statements: [ "error", @@ -262,8 +288,12 @@ switch (foo) { } ``` +::: + Examples of **correct** code for the `[{ blankLine: "always", prev: ["case", "default"], next: "*" }]` configuration: +::: correct + ```js /*eslint padding-line-between-statements: [ "error", @@ -286,6 +316,8 @@ switch (foo) { } ``` +::: + ## When Not To Use It If you don't want to notify warnings about linebreaks, then it's safe to disable this rule. diff --git a/docs/src/rules/prefer-const.md b/docs/src/rules/prefer-const.md index 7bf20de6904..202498cd1d3 100644 --- a/docs/src/rules/prefer-const.md +++ b/docs/src/rules/prefer-const.md @@ -22,6 +22,8 @@ This rule is aimed at flagging variables that are declared using `let` keyword, Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-const: "error"*/ @@ -52,8 +54,12 @@ for (let a of [1, 2, 3]) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-const: "error"*/ @@ -120,6 +126,8 @@ var b = 3; console.log(b); ``` +::: + ## Options ```json @@ -141,6 +149,8 @@ There are 2 values: Examples of **incorrect** code for the default `{"destructuring": "any"}` option: +::: incorrect + ```js /*eslint prefer-const: "error"*/ /*eslint-env es6*/ @@ -149,8 +159,12 @@ let {a, b} = obj; /*error 'b' is never reassigned, use 'const' instead.*/ a = a + 1; ``` +::: + Examples of **correct** code for the default `{"destructuring": "any"}` option: +::: correct + ```js /*eslint prefer-const: "error"*/ /*eslint-env es6*/ @@ -165,8 +179,12 @@ a = a + 1; b = b + 1; ``` +::: + Examples of **incorrect** code for the `{"destructuring": "all"}` option: +::: incorrect + ```js /*eslint prefer-const: ["error", {"destructuring": "all"}]*/ /*eslint-env es6*/ @@ -176,8 +194,12 @@ let {a, b} = obj; /*error 'a' is never reassigned, use 'const' instead. 'b' is never reassigned, use 'const' instead.*/ ``` +::: + Examples of **correct** code for the `{"destructuring": "all"}` option: +::: correct + ```js /*eslint prefer-const: ["error", {"destructuring": "all"}]*/ /*eslint-env es6*/ @@ -187,6 +209,8 @@ let {a, b} = obj; a = a + 1; ``` +::: + ### ignoreReadBeforeAssign This is an option to avoid conflicting with `no-use-before-define` rule (without `"nofunc"` option). @@ -195,6 +219,8 @@ Default is `false`. Examples of **correct** code for the `{"ignoreReadBeforeAssign": true}` option: +::: correct + ```js /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/ /*eslint-env es6*/ @@ -208,8 +234,12 @@ function initialize() { timer = setInterval(initialize, 100); ``` +::: + Examples of **correct** code for the default `{"ignoreReadBeforeAssign": false}` option: +::: correct + ```js /*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/ /*eslint-env es6*/ @@ -222,6 +252,8 @@ function initialize() { } ``` +::: + ## When Not To Use It If you don't want to be notified about variables that are never reassigned after initial assignment, you can safely disable this rule. diff --git a/docs/src/rules/prefer-destructuring.md b/docs/src/rules/prefer-destructuring.md index e49efb2b02c..95c04f7744c 100644 --- a/docs/src/rules/prefer-destructuring.md +++ b/docs/src/rules/prefer-destructuring.md @@ -37,6 +37,8 @@ The `--fix` option on the command line fixes only problems reported in variable Examples of **incorrect** code for this rule: +::: incorrect + ```javascript // With `array` enabled var foo = array[0]; @@ -46,8 +48,12 @@ var foo = object.foo; var foo = object['foo']; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```javascript // With `array` enabled var [ foo ] = array; @@ -62,20 +68,32 @@ let foo; ({ foo } = object); ``` +::: + Examples of **incorrect** code when `enforceForRenamedProperties` is enabled: +::: incorrect + ```javascript var foo = object.bar; ``` +::: + Examples of **correct** code when `enforceForRenamedProperties` is enabled: +::: correct + ```javascript var { bar: foo } = object; ``` +::: + Examples of additional **correct** code when `enforceForRenamedProperties` is enabled: +::: correct + ```javascript class C { #x; @@ -85,6 +103,8 @@ class C { } ``` +::: + An example configuration, with the defaults `array` and `object` filled in, looks like this: ```json @@ -159,18 +179,26 @@ For example, the following configuration enforces object destructuring in variab Examples of **correct** code when object destructuring in `VariableDeclarator` is enforced: +::: correct + ```javascript /* eslint prefer-destructuring: ["error", {VariableDeclarator: {object: true}}] */ var {bar: foo} = object; ``` +::: + Examples of **correct** code when array destructuring in `AssignmentExpression` is enforced: +::: correct + ```javascript /* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: true}}] */ [bar] = array; ``` +::: + ## When Not To Use It If you want to be able to access array indices or object properties directly, you can either configure the rule to your tastes or disable the rule entirely. diff --git a/docs/src/rules/prefer-exponentiation-operator.md b/docs/src/rules/prefer-exponentiation-operator.md index 591a60e89c9..e782a9a346a 100644 --- a/docs/src/rules/prefer-exponentiation-operator.md +++ b/docs/src/rules/prefer-exponentiation-operator.md @@ -22,6 +22,8 @@ This rule disallows calls to `Math.pow` and suggests using the `**` operator ins Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-exponentiation-operator: "error"*/ @@ -34,8 +36,12 @@ let baz = Math.pow(a + b, c + d); let quux = Math.pow(-1, n); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-exponentiation-operator: "error"*/ @@ -48,6 +54,8 @@ let baz = (a + b) ** (c + d); let quux = (-1) ** n; ``` +::: + ## When Not To Use It This rule should not be used unless ES2016 is supported in your codebase. diff --git a/docs/src/rules/prefer-named-capture-group.md b/docs/src/rules/prefer-named-capture-group.md index 59f9638940d..42197de9700 100644 --- a/docs/src/rules/prefer-named-capture-group.md +++ b/docs/src/rules/prefer-named-capture-group.md @@ -26,6 +26,8 @@ const regex = /(?:cauli|sun)flower/; Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-named-capture-group: "error"*/ @@ -36,8 +38,12 @@ const baz = RegExp('(ba[rz])'); foo.exec('bar')[1]; // Retrieve the group result. ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-named-capture-group: "error"*/ @@ -49,6 +55,8 @@ const xyz = /xyz(?:zy|abc)/; foo.exec('bar').groups.id; // Retrieve the group result. ``` +::: + ## When Not To Use It If you are targeting ECMAScript 2017 and/or older environments, you should not use this rule, because this ECMAScript feature is only supported in ECMAScript 2018 and/or newer environments. diff --git a/docs/src/rules/prefer-numeric-literals.md b/docs/src/rules/prefer-numeric-literals.md index 379bbb7fe48..459a651b198 100644 --- a/docs/src/rules/prefer-numeric-literals.md +++ b/docs/src/rules/prefer-numeric-literals.md @@ -22,6 +22,8 @@ This rule disallows calls to `parseInt()` or `Number.parseInt()` if called with Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-numeric-literals: "error"*/ @@ -34,8 +36,12 @@ Number.parseInt("767", 8) === 503; Number.parseInt("1F7", 16) === 503; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-numeric-literals: "error"*/ /*eslint-env es6*/ @@ -57,6 +63,8 @@ Number.parseInt(foo); Number.parseInt(foo, 2); ``` +::: + ## When Not To Use It If you want to allow use of `parseInt()` or `Number.parseInt()` for binary, octal, or hexadecimal integers, or if you are not using ES6 (because binary and octal literals are not supported in ES5 and below), you may wish to disable this rule. diff --git a/docs/src/rules/prefer-object-has-own.md b/docs/src/rules/prefer-object-has-own.md index 3608bc6b3f2..bcbb3a8e60d 100644 --- a/docs/src/rules/prefer-object-has-own.md +++ b/docs/src/rules/prefer-object-has-own.md @@ -33,6 +33,8 @@ if (Object.hasOwn(object, "foo")) { Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-object-has-own: "error"*/ @@ -45,8 +47,12 @@ Object.hasOwnProperty.call(obj, "a"); const hasProperty = Object.prototype.hasOwnProperty.call(object, property); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-object-has-own: "error"*/ @@ -55,6 +61,8 @@ Object.hasOwn(obj, "a"); const hasProperty = Object.hasOwn(object, property); ``` +::: + ## When Not To Use It This rule should not be used unless ES2022 is supported in your codebase. diff --git a/docs/src/rules/prefer-object-spread.md b/docs/src/rules/prefer-object-spread.md index dd495867690..2bc2b398a2a 100644 --- a/docs/src/rules/prefer-object-spread.md +++ b/docs/src/rules/prefer-object-spread.md @@ -17,6 +17,8 @@ Introduced in ES2018, object spread is a declarative alternative which may perfo Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-object-spread: "error"*/ @@ -36,8 +38,12 @@ Object.assign({}); Object.assign({ foo: bar }); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-object-spread: "error"*/ @@ -55,6 +61,8 @@ Object.assign(foo, { bar, baz }); Object.assign(foo, { ...baz }); ``` +::: + ## When Not To Use It This rule should not be used unless ES2018 is supported in your codebase. diff --git a/docs/src/rules/prefer-promise-reject-errors.md b/docs/src/rules/prefer-promise-reject-errors.md index 6c83c525dc7..5aed65a8a51 100644 --- a/docs/src/rules/prefer-promise-reject-errors.md +++ b/docs/src/rules/prefer-promise-reject-errors.md @@ -25,6 +25,8 @@ This rule takes one optional object argument: Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-promise-reject-errors: "error"*/ @@ -44,8 +46,12 @@ new Promise(function(resolve, reject) { ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-promise-reject-errors: "error"*/ @@ -61,8 +67,12 @@ var foo = getUnknownValue(); Promise.reject(foo); ``` +::: + Examples of **correct** code for this rule with the `allowEmptyReject: true` option: +::: correct + ```js /*eslint prefer-promise-reject-errors: ["error", {"allowEmptyReject": true}]*/ @@ -73,6 +83,8 @@ new Promise(function(resolve, reject) { }); ``` +::: + ## Known Limitations Due to the limits of static analysis, this rule cannot guarantee that you will only reject Promises with `Error` objects. While the rule will report cases where it can guarantee that the rejection reason is clearly not an `Error`, it will not report cases where there is uncertainty about whether a given reason is an `Error`. For more information on this caveat, see the [similar limitations](no-throw-literal#known-limitations) in the `no-throw-literal` rule. diff --git a/docs/src/rules/prefer-reflect.md b/docs/src/rules/prefer-reflect.md index 59063fd50bf..14348779e9f 100644 --- a/docs/src/rules/prefer-reflect.md +++ b/docs/src/rules/prefer-reflect.md @@ -48,6 +48,8 @@ Deprecates `Function.prototype.apply()` and `Function.prototype.call()` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ @@ -62,8 +64,12 @@ obj.myMethod.call(obj, arg); obj.myMethod.call(other, arg); ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ @@ -77,8 +83,12 @@ Reflect.apply(obj.myMethod, obj, [arg]); Reflect.apply(obj.myMethod, other, [arg]); ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["apply"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["apply"] }]*/ @@ -89,8 +99,12 @@ obj.myMethod.apply(obj, args); obj.myMethod.apply(other, args); ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["call"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["call"] }]*/ @@ -101,28 +115,40 @@ obj.myMethod.call(obj, arg); obj.myMethod.call(other, arg); ``` +::: + ### Reflect.defineProperty Deprecates `Object.defineProperty()` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ Object.defineProperty({}, 'foo', {value: 1}) ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ Reflect.defineProperty({}, 'foo', {value: 1}) ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["defineProperty"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["defineProperty"] }]*/ @@ -130,28 +156,40 @@ Object.defineProperty({}, 'foo', {value: 1}) Reflect.defineProperty({}, 'foo', {value: 1}) ``` +::: + ### Reflect.getOwnPropertyDescriptor Deprecates `Object.getOwnPropertyDescriptor()` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ Object.getOwnPropertyDescriptor({}, 'foo') ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ Reflect.getOwnPropertyDescriptor({}, 'foo') ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["getOwnPropertyDescriptor"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyDescriptor"] }]*/ @@ -159,28 +197,40 @@ Object.getOwnPropertyDescriptor({}, 'foo') Reflect.getOwnPropertyDescriptor({}, 'foo') ``` +::: + ### Reflect.getPrototypeOf Deprecates `Object.getPrototypeOf()` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ Object.getPrototypeOf({}, 'foo') ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ Reflect.getPrototypeOf({}, 'foo') ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["getPrototypeOf"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["getPrototypeOf"] }]*/ @@ -188,28 +238,40 @@ Object.getPrototypeOf({}, 'foo') Reflect.getPrototypeOf({}, 'foo') ``` +::: + ### Reflect.setPrototypeOf Deprecates `Object.setPrototypeOf()` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ Object.setPrototypeOf({}, Object.prototype) ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ Reflect.setPrototypeOf({}, Object.prototype) ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["setPrototypeOf"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["setPrototypeOf"] }]*/ @@ -217,28 +279,40 @@ Object.setPrototypeOf({}, Object.prototype) Reflect.setPrototypeOf({}, Object.prototype) ``` +::: + ### Reflect.isExtensible Deprecates `Object.isExtensible` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ Object.isExtensible({}) ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ Reflect.isExtensible({}) ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["isExtensible"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["isExtensible"] }]*/ @@ -246,28 +320,40 @@ Object.isExtensible({}) Reflect.isExtensible({}) ``` +::: + ### Reflect.getOwnPropertyNames Deprecates `Object.getOwnPropertyNames()` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ Object.getOwnPropertyNames({}) ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ Reflect.getOwnPropertyNames({}) ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["getOwnPropertyNames"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["getOwnPropertyNames"] }]*/ @@ -275,28 +361,40 @@ Object.getOwnPropertyNames({}) Reflect.getOwnPropertyNames({}) ``` +::: + ### Reflect.preventExtensions Deprecates `Object.preventExtensions()` Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ Object.preventExtensions({}) ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ Reflect.preventExtensions({}) ``` +::: + Examples of **correct** code for this rule with the `{ "exceptions": ["preventExtensions"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["preventExtensions"] }]*/ @@ -304,20 +402,28 @@ Object.preventExtensions({}) Reflect.preventExtensions({}) ``` +::: + ### Reflect.deleteProperty Deprecates the `delete` keyword Examples of **incorrect** code for this rule when used without exceptions: +::: incorrect + ```js /*eslint prefer-reflect: "error"*/ delete foo.bar; // deleting object property ``` +::: + Examples of **correct** code for this rule when used without exceptions: +::: correct + ```js /*eslint prefer-reflect: "error"*/ @@ -325,10 +431,14 @@ delete bar; // deleting variable Reflect.deleteProperty(foo, 'bar'); ``` +::: + Note: For a rule preventing deletion of variables, see [no-delete-var instead](no-delete-var) Examples of **correct** code for this rule with the `{ "exceptions": ["delete"] }` option: +::: correct + ```js /*eslint prefer-reflect: ["error", { "exceptions": ["delete"] }]*/ @@ -337,6 +447,8 @@ delete foo.bar Reflect.deleteProperty(foo, 'bar'); ``` +::: + ## When Not To Use It This rule should not be used in ES3/5 environments. diff --git a/docs/src/rules/prefer-regex-literals.md b/docs/src/rules/prefer-regex-literals.md index c8821095299..51ca2296909 100644 --- a/docs/src/rules/prefer-regex-literals.md +++ b/docs/src/rules/prefer-regex-literals.md @@ -56,6 +56,8 @@ dynamically generated regular expressions. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-regex-literals: "error"*/ @@ -74,8 +76,12 @@ RegExp(`^\\d\\.$`); new RegExp(String.raw`^\d\.$`); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-regex-literals: "error"*/ @@ -100,6 +106,8 @@ RegExp(`${prefix}abc`); new RegExp(String.raw`^\d\. ${suffix}`); ``` +::: + ## Options This rule has an object option: diff --git a/docs/src/rules/prefer-rest-params.md b/docs/src/rules/prefer-rest-params.md index 3583f8371b8..e018325a156 100644 --- a/docs/src/rules/prefer-rest-params.md +++ b/docs/src/rules/prefer-rest-params.md @@ -22,6 +22,8 @@ This rule is aimed to flag usage of `arguments` variables. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-rest-params: "error"*/ @@ -40,8 +42,12 @@ function foo(action) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-rest-params: "error"*/ @@ -63,6 +69,8 @@ function foo() { } ``` +::: + ## When Not To Use It This rule should not be used in ES3/5 environments. diff --git a/docs/src/rules/prefer-spread.md b/docs/src/rules/prefer-spread.md index f3f1fedce56..9d16dd63c82 100644 --- a/docs/src/rules/prefer-spread.md +++ b/docs/src/rules/prefer-spread.md @@ -33,6 +33,8 @@ This rule is aimed to flag usage of `Function.prototype.apply()` in situations w Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-spread: "error"*/ @@ -41,8 +43,12 @@ foo.apply(null, args); obj.foo.apply(obj, args); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-spread: "error"*/ @@ -62,6 +68,8 @@ foo.apply(null, [1, 2, 3]); obj.foo.apply(obj, [1, 2, 3]); ``` +::: + Known limitations: This rule analyzes code statically to check whether or not the `this` argument is changed. So, if the `this` argument is computed in a dynamic expression, this rule cannot detect a violation. diff --git a/docs/src/rules/prefer-template.md b/docs/src/rules/prefer-template.md index c47f81f1ba9..44cfbaee2c3 100644 --- a/docs/src/rules/prefer-template.md +++ b/docs/src/rules/prefer-template.md @@ -32,6 +32,8 @@ This rule is aimed to flag usage of `+` operators with strings. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint prefer-template: "error"*/ @@ -39,8 +41,12 @@ var str = "Hello, " + name + "!"; var str = "Time: " + (12 * 60 * 60 * 1000); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint prefer-template: "error"*/ /*eslint-env es6*/ @@ -53,6 +59,8 @@ var str = `Time: ${12 * 60 * 60 * 1000}`; var str = "Hello, " + "World!"; ``` +::: + ## When Not To Use It This rule should not be used in ES3/5 environments. diff --git a/docs/src/rules/quote-props.md b/docs/src/rules/quote-props.md index 796278f980f..2a840b590ab 100644 --- a/docs/src/rules/quote-props.md +++ b/docs/src/rules/quote-props.md @@ -68,6 +68,8 @@ Object option: Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint quote-props: ["error", "always"]*/ @@ -77,8 +79,12 @@ var object = { }; ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint quote-props: ["error", "always"]*/ /*eslint-env es6*/ @@ -102,10 +108,14 @@ var object3 = { }; ``` +::: + ### as-needed Examples of **incorrect** code for this rule with the `"as-needed"` option: +::: incorrect + ```js /*eslint quote-props: ["error", "as-needed"]*/ @@ -117,8 +127,12 @@ var object = { }; ``` +::: + Examples of **correct** code for this rule with the `"as-needed"` option: +::: correct + ```js /*eslint quote-props: ["error", "as-needed"]*/ /*eslint-env es6*/ @@ -144,10 +158,14 @@ var object3 = { }; ``` +::: + ### consistent Examples of **incorrect** code for this rule with the `"consistent"` option: +::: incorrect + ```js /*eslint quote-props: ["error", "consistent"]*/ @@ -163,8 +181,12 @@ var object2 = { }; ``` +::: + Examples of **correct** code for this rule with the `"consistent"` option: +::: correct + ```js /*eslint quote-props: ["error", "consistent"]*/ @@ -185,10 +207,14 @@ var object3 = { }; ``` +::: + ### consistent-as-needed Examples of **incorrect** code for this rule with the `"consistent-as-needed"` option: +::: incorrect + ```js /*eslint quote-props: ["error", "consistent-as-needed"]*/ @@ -204,8 +230,12 @@ var object2 = { }; ``` +::: + Examples of **correct** code for this rule with the `"consistent-as-needed"` option: +::: correct + ```js /*eslint quote-props: ["error", "consistent-as-needed"]*/ @@ -221,10 +251,14 @@ var object2 = { }; ``` +::: + ### keywords Examples of additional **incorrect** code for this rule with the `"as-needed", { "keywords": true }` options: +::: incorrect + ```js /*eslint quote-props: ["error", "as-needed", { "keywords": true }]*/ @@ -234,8 +268,12 @@ var x = { }; ``` +::: + Examples of additional **incorrect** code for this rule with the `"consistent-as-needed", { "keywords": true }` options: +::: incorrect + ```js /*eslint quote-props: ["error", "consistent-as-needed", { "keywords": true }]*/ @@ -245,10 +283,14 @@ var x = { }; ``` +::: + ### unnecessary Examples of additional **correct** code for this rule with the `"as-needed", { "unnecessary": false }` options: +::: correct + ```js /*eslint quote-props: ["error", "as-needed", { "keywords": true, "unnecessary": false }]*/ @@ -258,10 +300,14 @@ var x = { }; ``` +::: + ### numbers Examples of additional **incorrect** code for this rule with the `"as-needed", { "numbers": true }` options: +::: incorrect + ```js /*eslint quote-props: ["error", "as-needed", { "numbers": true }]*/ @@ -270,6 +316,8 @@ var x = { } ``` +::: + ## When Not To Use It If you don't care if property names are consistently wrapped in quotes or not, and you don't target legacy ES3 environments, turn this rule off. diff --git a/docs/src/rules/quotes.md b/docs/src/rules/quotes.md index be42f89b717..234787a5318 100644 --- a/docs/src/rules/quotes.md +++ b/docs/src/rules/quotes.md @@ -48,6 +48,8 @@ Object option: Examples of **incorrect** code for this rule with the default `"double"` option: +::: incorrect + ```js /*eslint quotes: ["error", "double"]*/ @@ -56,8 +58,12 @@ var unescaped = 'a string containing "double" quotes'; var backtick = `back\ntick`; // you can use \n in single or double quoted strings ``` +::: + Examples of **correct** code for this rule with the default `"double"` option: +::: correct + ```js /*eslint quotes: ["error", "double"]*/ /*eslint-env es6*/ @@ -68,10 +74,14 @@ tick`; // backticks are allowed due to newline var backtick = tag`backtick`; // backticks are allowed due to tag ``` +::: + ### single Examples of **incorrect** code for this rule with the `"single"` option: +::: incorrect + ```js /*eslint quotes: ["error", "single"]*/ @@ -79,8 +89,12 @@ var double = "double"; var unescaped = "a string containing 'single' quotes"; ``` +::: + Examples of **correct** code for this rule with the `"single"` option: +::: correct + ```js /*eslint quotes: ["error", "single"]*/ /*eslint-env es6*/ @@ -89,10 +103,14 @@ var single = 'single'; var backtick = `back${x}tick`; // backticks are allowed due to substitution ``` +::: + ### backticks Examples of **incorrect** code for this rule with the `"backtick"` option: +::: incorrect + ```js /*eslint quotes: ["error", "backtick"]*/ @@ -101,8 +119,12 @@ var double = "double"; var unescaped = 'a string containing `backticks`'; ``` +::: + Examples of **correct** code for this rule with the `"backtick"` option: +::: correct + ```js /*eslint quotes: ["error", "backtick"]*/ /*eslint-env es6*/ @@ -110,36 +132,52 @@ Examples of **correct** code for this rule with the `"backtick"` option: var backtick = `backtick`; ``` +::: + ### avoidEscape Examples of additional **correct** code for this rule with the `"double", { "avoidEscape": true }` options: +::: correct + ```js /*eslint quotes: ["error", "double", { "avoidEscape": true }]*/ var single = 'a string containing "double" quotes'; ``` +::: + Examples of additional **correct** code for this rule with the `"single", { "avoidEscape": true }` options: +::: correct + ```js /*eslint quotes: ["error", "single", { "avoidEscape": true }]*/ var double = "a string containing 'single' quotes"; ``` +::: + Examples of additional **correct** code for this rule with the `"backtick", { "avoidEscape": true }` options: +::: correct + ```js /*eslint quotes: ["error", "backtick", { "avoidEscape": true }]*/ var double = "a string containing `backtick` quotes" ``` +::: + ### allowTemplateLiterals Examples of additional **correct** code for this rule with the `"double", { "allowTemplateLiterals": true }` options: +::: correct + ```js /*eslint quotes: ["error", "double", { "allowTemplateLiterals": true }]*/ @@ -147,8 +185,12 @@ var double = "double"; var double = `double`; ``` +::: + Examples of additional **correct** code for this rule with the `"single", { "allowTemplateLiterals": true }` options: +::: correct + ```js /*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/ @@ -156,6 +198,8 @@ var single = 'single'; var single = `single`; ``` +::: + `{ "allowTemplateLiterals": false }` will not disallow the usage of all template literals. If you want to forbid any instance of template literals, use [no-restricted-syntax](no-restricted-syntax) and target the `TemplateLiteral` selector. ## When Not To Use It diff --git a/docs/src/rules/radix.md b/docs/src/rules/radix.md index 09a67df4c63..ea4322fd849 100644 --- a/docs/src/rules/radix.md +++ b/docs/src/rules/radix.md @@ -44,6 +44,8 @@ There are two options for this rule: Examples of **incorrect** code for the default `"always"` option: +::: incorrect + ```js /*eslint radix: "error"*/ @@ -58,8 +60,12 @@ var num = parseInt("071", 37); var num = parseInt(); ``` +::: + Examples of **correct** code for the default `"always"` option: +::: correct + ```js /*eslint radix: "error"*/ @@ -70,10 +76,14 @@ var num = parseInt("071", 8); var num = parseFloat(someValue); ``` +::: + ### as-needed Examples of **incorrect** code for the `"as-needed"` option: +::: incorrect + ```js /*eslint radix: ["error", "as-needed"]*/ @@ -84,8 +94,12 @@ var num = parseInt("071", "abc"); var num = parseInt(); ``` +::: + Examples of **correct** code for the `"as-needed"` option: +::: correct + ```js /*eslint radix: ["error", "as-needed"]*/ @@ -96,6 +110,8 @@ var num = parseInt("071", 8); var num = parseFloat(someValue); ``` +::: + ## When Not To Use It If you don't want to enforce either presence or omission of the `10` radix value you can turn this rule off. diff --git a/docs/src/rules/require-atomic-updates.md b/docs/src/rules/require-atomic-updates.md index ee58f8115b0..c0d0de53f48 100644 --- a/docs/src/rules/require-atomic-updates.md +++ b/docs/src/rules/require-atomic-updates.md @@ -64,6 +64,8 @@ Note that the rule does not report the assignment in step 3 in any of the follow Examples of **incorrect** code for this rule: +::: incorrect + ```js /* eslint require-atomic-updates: error */ @@ -92,8 +94,12 @@ function* generator() { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /* eslint require-atomic-updates: error */ @@ -127,6 +133,8 @@ function* generator() { } ``` +::: + ### Properties This rule reports an assignment to a property through a variable when it detects the following execution flow in a generator or async function: @@ -139,6 +147,8 @@ This logic is similar to the logic for variables, but stricter because the prope Example of **incorrect** code for this rule: +::: incorrect + ```js /* eslint require-atomic-updates: error */ @@ -149,8 +159,12 @@ async function foo(obj) { } ``` +::: + Example of **correct** code for this rule: +::: correct + ```js /* eslint require-atomic-updates: error */ @@ -164,6 +178,8 @@ async function foo(obj) { } ``` +::: + ## Options This rule has an object option: @@ -174,6 +190,8 @@ This rule has an object option: Example of **correct** code for this rule with the `{ "allowProperties": true }` option: +::: correct + ```js /* eslint require-atomic-updates: ["error", { "allowProperties": true }] */ @@ -184,6 +202,8 @@ async function foo(obj) { } ``` +::: + ## When Not To Use It If you don't use async or generator functions, you don't need to enable this rule. diff --git a/docs/src/rules/require-await.md b/docs/src/rules/require-await.md index 94226b129f3..18903c21c75 100644 --- a/docs/src/rules/require-await.md +++ b/docs/src/rules/require-await.md @@ -35,6 +35,8 @@ This rule warns async functions which have no `await` expression. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint require-await: "error"*/ @@ -47,8 +49,12 @@ bar(async () => { }); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint require-await: "error"*/ @@ -72,6 +78,8 @@ bar(() => { async function noop() {} ``` +::: + ## When Not To Use It Asynchronous functions are designed to work with promises such that throwing an error will cause a promise's rejection handler (such as `catch()`) to be called. For example: diff --git a/docs/src/rules/require-jsdoc.md b/docs/src/rules/require-jsdoc.md index 8bd899e79e1..e31d5d05de4 100644 --- a/docs/src/rules/require-jsdoc.md +++ b/docs/src/rules/require-jsdoc.md @@ -63,6 +63,8 @@ Default option settings are: Examples of **incorrect** code for this rule with the `{ "require": { "FunctionDeclaration": true, "MethodDefinition": true, "ClassDeclaration": true, "ArrowFunctionExpression": true, "FunctionExpression": true } }` option: +::: incorrect + ```js /*eslint "require-jsdoc": ["error", { "require": { @@ -103,8 +105,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the `{ "require": { "FunctionDeclaration": true, "MethodDefinition": true, "ClassDeclaration": true, "ArrowFunctionExpression": true, "FunctionExpression": true } }` option: +::: correct + ```js /*eslint "require-jsdoc": ["error", { "require": { @@ -189,6 +195,8 @@ var foo = { setTimeout(() => {}, 10); // since it's an anonymous arrow function ``` +::: + ## When Not To Use It If you do not require JSDoc for your functions, then you can leave this rule off. diff --git a/docs/src/rules/require-unicode-regexp.md b/docs/src/rules/require-unicode-regexp.md index 96d03569eb8..ac5937e3a37 100644 --- a/docs/src/rules/require-unicode-regexp.md +++ b/docs/src/rules/require-unicode-regexp.md @@ -32,6 +32,8 @@ This rule aims to enforce the use of `u` flag on regular expressions. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint require-unicode-regexp: error */ @@ -41,8 +43,12 @@ const c = new RegExp("ccc") const d = new RegExp("ddd", "gi") ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint require-unicode-regexp: error */ @@ -57,6 +63,8 @@ function f(flags) { } ``` +::: + ## When Not To Use It If you don't want to notify regular expressions with no `u` flag, then it's safe to disable this rule. diff --git a/docs/src/rules/require-yield.md b/docs/src/rules/require-yield.md index 08d183c1b79..07f200b5801 100644 --- a/docs/src/rules/require-yield.md +++ b/docs/src/rules/require-yield.md @@ -19,6 +19,8 @@ This rule generates warnings for generator functions that do not have the `yield Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint require-yield: "error"*/ /*eslint-env es6*/ @@ -28,8 +30,12 @@ function* foo() { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint require-yield: "error"*/ /*eslint-env es6*/ @@ -47,6 +53,8 @@ function foo() { function* foo() { } ``` +::: + ## When Not To Use It If you don't want to notify generator functions that have no `yield` expression, then it's safe to disable this rule. diff --git a/docs/src/rules/rest-spread-spacing.md b/docs/src/rules/rest-spread-spacing.md index 1a9e71f8fdb..65f32e87902 100644 --- a/docs/src/rules/rest-spread-spacing.md +++ b/docs/src/rules/rest-spread-spacing.md @@ -84,6 +84,8 @@ rest-spread-spacing: ["error", "never"] Examples of **incorrect** code for this rule with `"never"`: +::: incorrect + ```js /*eslint rest-spread-spacing: ["error", "never"]*/ @@ -95,8 +97,12 @@ let { x, y, ... z } = { x: 1, y: 2, a: 3, b: 4 }; let n = { x, y, ... z }; ``` +::: + Examples of **correct** code for this rule with `"never"`: +::: correct + ```js /*eslint rest-spread-spacing: ["error", "never"]*/ @@ -108,6 +114,8 @@ let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 }; let n = { x, y, ...z }; ``` +::: + ### "always" When using the `"always"` option, whitespace is required between spread operators and their expressions. @@ -118,6 +126,8 @@ rest-spread-spacing: ["error", "always"] Examples of **incorrect** code for this rule with `"always"`: +::: incorrect + ```js /*eslint rest-spread-spacing:["error", "always"]*/ @@ -129,8 +139,12 @@ let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 }; let n = { x, y, ...z }; ``` +::: + Examples of **correct** code for this rule with `"always"`: +::: correct + ```js /*eslint rest-spread-spacing: ["error", "always"]*/ @@ -142,6 +156,8 @@ let { x, y, ... z } = { x: 1, y: 2, a: 3, b: 4 }; let n = { x, y, ... z }; ``` +::: + ## When Not To Use It You can safely disable this rule if you do not care about enforcing consistent spacing between spread operators and their expressions. diff --git a/docs/src/rules/semi-spacing.md b/docs/src/rules/semi-spacing.md index 8b60e16bf21..24163047187 100644 --- a/docs/src/rules/semi-spacing.md +++ b/docs/src/rules/semi-spacing.md @@ -56,6 +56,8 @@ This is the default option. It enforces spacing after semicolons and disallows s Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint semi-spacing: "error"*/ @@ -67,8 +69,12 @@ for (i = 0 ; i < 10 ; i++) {} for (i = 0;i < 10;i++) {} ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint semi-spacing: "error"*/ @@ -82,12 +88,16 @@ if (true) {;} ;foo(); ``` +::: + ### `{"before": true, "after": false}` This option enforces spacing before semicolons and disallows spacing after semicolons. Examples of **incorrect** code for this rule with the `{"before": true, "after": false}` option: +::: incorrect + ```js /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/ @@ -99,8 +109,12 @@ for (i = 0;i < 10;i++) {} for (i = 0; i < 10; i++) {} ``` +::: + Examples of **correct** code for this rule with the `{"before": true, "after": false}` option: +::: correct + ```js /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/ @@ -111,6 +125,8 @@ while (a) {break ;} for (i = 0 ;i < 10 ;i++) {} ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing before or after semicolons. diff --git a/docs/src/rules/semi-style.md b/docs/src/rules/semi-style.md index 0cf379c3bcd..a09db703379 100644 --- a/docs/src/rules/semi-style.md +++ b/docs/src/rules/semi-style.md @@ -32,6 +32,8 @@ This rule has an option. Examples of **incorrect** code for this rule with `"last"` option: +::: incorrect + ```js /*eslint semi-style: ["error", "last"]*/ @@ -54,8 +56,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with `"last"` option: +::: correct + ```js /*eslint semi-style: ["error", "last"]*/ @@ -78,8 +84,12 @@ class C { } ``` +::: + Examples of **incorrect** code for this rule with `"first"` option: +::: incorrect + ```js /*eslint semi-style: ["error", "first"]*/ @@ -102,8 +112,12 @@ class C { } ``` +::: + Examples of **correct** code for this rule with `"first"` option: +::: correct + ```js /*eslint semi-style: ["error", "first"]*/ @@ -126,6 +140,8 @@ class C { } ``` +::: + ## When Not To Use It If you don't want to notify the location of semicolons, then it's safe to disable this rule. diff --git a/docs/src/rules/sort-imports.md b/docs/src/rules/sort-imports.md index 9f8459fda99..e0b1517037f 100644 --- a/docs/src/rules/sort-imports.md +++ b/docs/src/rules/sort-imports.md @@ -75,6 +75,8 @@ Default option settings are: Examples of **correct** code for this rule when using default options: +::: correct + ```js /*eslint sort-imports: "error"*/ import 'module-without-export.js'; @@ -101,8 +103,12 @@ import {d} from 'quux.js'; import {a, b, c} from 'foo.js' ``` +::: + Examples of **incorrect** code for this rule when using default options: +::: incorrect + ```js /*eslint sort-imports: "error"*/ import b from 'foo.js'; @@ -132,12 +138,16 @@ import * as b from 'bar.js'; import {b, a, c} from 'foo.js' ``` +::: + ### `ignoreCase` When `true` the rule ignores the case-sensitivity of the imports local name. Examples of **incorrect** code for this rule with the `{ "ignoreCase": true }` option: +::: incorrect + ```js /*eslint sort-imports: ["error", { "ignoreCase": true }]*/ @@ -145,8 +155,12 @@ import B from 'foo.js'; import a from 'bar.js'; ``` +::: + Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option: +::: correct + ```js /*eslint sort-imports: ["error", { "ignoreCase": true }]*/ @@ -155,6 +169,8 @@ import B from 'bar.js'; import c from 'baz.js'; ``` +::: + Default is `false`. ### `ignoreDeclarationSort` @@ -163,20 +179,28 @@ Ignores the sorting of import declaration statements. Examples of **incorrect** code for this rule with the default `{ "ignoreDeclarationSort": false }` option: +::: incorrect + ```js /*eslint sort-imports: ["error", { "ignoreDeclarationSort": false }]*/ import b from 'foo.js' import a from 'bar.js' ``` +::: + Examples of **correct** code for this rule with the `{ "ignoreDeclarationSort": true }` option: +::: correct + ```js /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/ import a from 'foo.js' import b from 'bar.js' ``` +::: + ```js /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/ import b from 'foo.js' @@ -191,18 +215,26 @@ Ignores the member sorting within a `multiple` member import declaration. Examples of **incorrect** code for this rule with the default `{ "ignoreMemberSort": false }` option: +::: incorrect + ```js /*eslint sort-imports: ["error", { "ignoreMemberSort": false }]*/ import {b, a, c} from 'foo.js' ``` +::: + Examples of **correct** code for this rule with the `{ "ignoreMemberSort": true }` option: +::: correct + ```js /*eslint sort-imports: ["error", { "ignoreMemberSort": true }]*/ import {b, a, c} from 'foo.js' ``` +::: + Default is `false`. ### `memberSyntaxSortOrder` @@ -218,14 +250,20 @@ All four options must be specified in the array, but you can customize their ord Examples of **incorrect** code for this rule with the default `{ "memberSyntaxSortOrder": ["none", "all", "multiple", "single"] }` option: +::: incorrect + ```js /*eslint sort-imports: "error"*/ import a from 'foo.js'; import * as b from 'bar.js'; ``` +::: + Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }` option: +::: correct + ```js /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }]*/ @@ -233,8 +271,12 @@ import a from 'foo.js'; import * as b from 'bar.js'; ``` +::: + Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }` option: +::: correct + ```js /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }]*/ @@ -243,6 +285,8 @@ import z from 'zoo.js'; import {a, b} from 'foo.js'; ``` +::: + Default is `["none", "all", "multiple", "single"]`. ### `allowSeparatedGroups` @@ -253,6 +297,8 @@ In other words, a blank line or a comment line or line with any other statement Examples of **incorrect** code for this rule with the `{ "allowSeparatedGroups": true }` option: +::: incorrect + ```js /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/ @@ -261,8 +307,12 @@ import c from 'bar.js'; import a from 'baz.js'; ``` +::: + Examples of **correct** code for this rule with the `{ "allowSeparatedGroups": true }` option: +::: correct + ```js /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/ @@ -272,6 +322,8 @@ import c from 'bar.js'; import a from 'baz.js'; ``` +::: + ```js /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/ diff --git a/docs/src/rules/sort-keys.md b/docs/src/rules/sort-keys.md index 1a803b19c22..f2db04b8ef7 100644 --- a/docs/src/rules/sort-keys.md +++ b/docs/src/rules/sort-keys.md @@ -18,6 +18,8 @@ This rule checks all property definitions of object expressions and verifies tha Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint sort-keys: "error"*/ /*eslint-env es6*/ @@ -38,8 +40,12 @@ let obj = {a: 1, ["c"]: 3, b: 2}; let obj = {a: 1, [S]: 3, b: 2}; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint sort-keys: "error"*/ /*eslint-env es6*/ @@ -67,6 +73,8 @@ let obj = {a: 1, [tag`c`]: 3, b: 2}; let obj = {b: 1, ...c, a: 2}; ``` +::: + ## Options ```json @@ -106,6 +114,8 @@ With `natural` as false, the ordering would be Examples of **incorrect** code for the `"desc"` option: +::: incorrect + ```js /*eslint sort-keys: ["error", "desc"]*/ /*eslint-env es6*/ @@ -120,8 +130,12 @@ let obj = {C: 1, b: 3, a: 2}; let obj = {10: b, 2: c, 1: a}; ``` +::: + Examples of **correct** code for the `"desc"` option: +::: correct + ```js /*eslint sort-keys: ["error", "desc"]*/ /*eslint-env es6*/ @@ -136,10 +150,14 @@ let obj = {b: 3, a: 2, C: 1}; let obj = {2: c, 10: b, 1: a}; ``` +::: + ### insensitive Examples of **incorrect** code for the `{caseSensitive: false}` option: +::: incorrect + ```js /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/ /*eslint-env es6*/ @@ -148,8 +166,12 @@ let obj = {a: 1, c: 3, C: 4, b: 2}; let obj = {a: 1, C: 3, c: 4, b: 2}; ``` +::: + Examples of **correct** code for the `{caseSensitive: false}` option: +::: correct + ```js /*eslint sort-keys: ["error", "asc", {caseSensitive: false}]*/ /*eslint-env es6*/ @@ -158,10 +180,14 @@ let obj = {a: 1, b: 2, c: 3, C: 4}; let obj = {a: 1, b: 2, C: 3, c: 4}; ``` +::: + ### natural Examples of **incorrect** code for the `{natural: true}` option: +::: incorrect + ```js /*eslint sort-keys: ["error", "asc", {natural: true}]*/ /*eslint-env es6*/ @@ -169,8 +195,12 @@ Examples of **incorrect** code for the `{natural: true}` option: let obj = {1: a, 10: c, 2: b}; ``` +::: + Examples of **correct** code for the `{natural: true}` option: +::: correct + ```js /*eslint sort-keys: ["error", "asc", {natural: true}]*/ /*eslint-env es6*/ @@ -178,10 +208,14 @@ Examples of **correct** code for the `{natural: true}` option: let obj = {1: a, 2: b, 10: c}; ``` +::: + ### minKeys Examples of **incorrect** code for the `{minKeys: 4}` option: +::: incorrect + ```js /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/ /*eslint-env es6*/ @@ -204,8 +238,12 @@ let obj = { }; ``` +::: + Examples of **correct** code for the `{minKeys: 4}` option: +::: correct + ```js /*eslint sort-keys: ["error", "asc", {minKeys: 4}]*/ /*eslint-env es6*/ @@ -224,6 +262,8 @@ let obj = { }; ``` +::: + ## When Not To Use It If you don't want to notify about properties' order, then it's safe to disable this rule. diff --git a/docs/src/rules/sort-vars.md b/docs/src/rules/sort-vars.md index a0282a65f82..b2e8917d43d 100644 --- a/docs/src/rules/sort-vars.md +++ b/docs/src/rules/sort-vars.md @@ -21,6 +21,8 @@ The default configuration of the rule is case-sensitive. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint sort-vars: "error"*/ @@ -31,8 +33,12 @@ var a, B, c; var a, A; ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint sort-vars: "error"*/ @@ -46,6 +52,8 @@ var A, a; var B, a, c; ``` +::: + Alphabetical list is maintained starting from the first variable and excluding any that are considered problems. So the following code will produce two problems: ```js @@ -72,6 +80,8 @@ This rule has an object option: Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option: +::: correct + ```js /*eslint sort-vars: ["error", { "ignoreCase": true }]*/ @@ -80,6 +90,8 @@ var a, A; var a, B, c; ``` +::: + ## When Not To Use It This rule is a formatting preference and not following it won't negatively affect the quality of your code. If you alphabetizing variables isn't a part of your coding standards, then you can leave this rule off. diff --git a/docs/src/rules/space-after-function-name.md b/docs/src/rules/space-after-function-name.md index 390198fdd47..047f7be757a 100644 --- a/docs/src/rules/space-after-function-name.md +++ b/docs/src/rules/space-after-function-name.md @@ -29,6 +29,8 @@ This rule aims to enforce a consistent spacing after function names. It takes on Examples of **incorrect** code for this rule: +::: incorrect + ```js function foo (x) { // ... @@ -42,8 +44,12 @@ function bar(x) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js function foo(x) { // ... @@ -56,3 +62,5 @@ function bar (x) { // ... } ``` + +::: diff --git a/docs/src/rules/space-after-keywords.md b/docs/src/rules/space-after-keywords.md index 36ff74aa194..38a96c36d35 100644 --- a/docs/src/rules/space-after-keywords.md +++ b/docs/src/rules/space-after-keywords.md @@ -36,6 +36,8 @@ then there should be no spaces following. The default is `"always"`. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint space-after-keywords: "error"*/ @@ -46,6 +48,8 @@ if (a) {} else{} do{} while (a); ``` +::: + ```js /*eslint space-after-keywords: ["error", "never"]*/ @@ -54,6 +58,8 @@ if (a) {} Examples of **correct** code for this rule: +::: correct + ```js /*eslint space-after-keywords: "error"*/ @@ -62,6 +68,8 @@ if (a) {} if (a) {} else {} ``` +::: + ```js /*eslint space-after-keywords: ["error", "never"]*/ diff --git a/docs/src/rules/space-before-blocks.md b/docs/src/rules/space-before-blocks.md index 0af8e7d0b9b..29da471de62 100644 --- a/docs/src/rules/space-before-blocks.md +++ b/docs/src/rules/space-before-blocks.md @@ -43,6 +43,8 @@ The default is `"always"`. Examples of **incorrect** code for this rule with the "always" option: +::: incorrect + ```js /*eslint space-before-blocks: "error"*/ @@ -63,8 +65,12 @@ class Foo{ } ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint space-before-blocks: "error"*/ @@ -91,10 +97,14 @@ for (;;) { try {} catch(a) {} ``` +::: + ### "never" Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint space-before-blocks: ["error", "never"]*/ @@ -111,8 +121,12 @@ for (;;) { try {} catch(a) {} ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint space-before-blocks: ["error", "never"]*/ @@ -133,8 +147,12 @@ class Foo{ } ``` +::: + Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`: +::: incorrect + ```js /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/ /*eslint-env es6*/ @@ -148,8 +166,12 @@ class Foo{ } ``` +::: + Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "always", "classes": "never" }`: +::: correct + ```js /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/ /*eslint-env es6*/ @@ -167,8 +189,12 @@ class Foo{ } ``` +::: + Examples of **incorrect** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`: +::: incorrect + ```js /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/ /*eslint-env es6*/ @@ -182,8 +208,12 @@ class Foo { } ``` +::: + Examples of **correct** code for this rule when configured `{ "functions": "always", "keywords": "never", "classes": "never" }`: +::: correct + ```js /*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/ /*eslint-env es6*/ @@ -199,8 +229,12 @@ class Foo{ } ``` +::: + Examples of **incorrect** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`: +::: incorrect + ```js /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/ /*eslint-env es6*/ @@ -210,8 +244,12 @@ class Foo{ } ``` +::: + Examples of **correct** code for this rule when configured `{ "functions": "never", "keywords": "never", "classes": "always" }`: +::: correct + ```js /*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/ /*eslint-env es6*/ @@ -221,6 +259,8 @@ class Foo { } ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing before blocks. diff --git a/docs/src/rules/space-before-function-paren.md b/docs/src/rules/space-before-function-paren.md index ad0a20b57a4..1fcaa4ede79 100644 --- a/docs/src/rules/space-before-function-paren.md +++ b/docs/src/rules/space-before-function-paren.md @@ -66,6 +66,8 @@ Each of the following options can be set to `"always"`, `"never"`, or `"ignore"` Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint space-before-function-paren: "error"*/ /*eslint-env es6*/ @@ -97,8 +99,12 @@ var foo = { var foo = async() => 1 ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint space-before-function-paren: "error"*/ /*eslint-env es6*/ @@ -130,10 +136,14 @@ var foo = { var foo = async () => 1 ``` +::: + ### "never" Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint space-before-function-paren: ["error", "never"]*/ /*eslint-env es6*/ @@ -165,8 +175,12 @@ var foo = { var foo = async () => 1 ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint space-before-function-paren: ["error", "never"]*/ /*eslint-env es6*/ @@ -198,10 +212,14 @@ var foo = { var foo = async() => 1 ``` +::: + ### `{"anonymous": "always", "named": "never", "asyncArrow": "always"}` Examples of **incorrect** code for this rule with the `{"anonymous": "always", "named": "never", "asyncArrow": "always"}` option: +::: incorrect + ```js /*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/ /*eslint-env es6*/ @@ -229,8 +247,12 @@ var foo = { var foo = async(a) => await a ``` +::: + Examples of **correct** code for this rule with the `{"anonymous": "always", "named": "never", "asyncArrow": "always"}` option: +::: correct + ```js /*eslint space-before-function-paren: ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}]*/ /*eslint-env es6*/ @@ -258,10 +280,14 @@ var foo = { var foo = async (a) => await a ``` +::: + ### `{"anonymous": "never", "named": "always"}` Examples of **incorrect** code for this rule with the `{"anonymous": "never", "named": "always"}` option: +::: incorrect + ```js /*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/ /*eslint-env es6*/ @@ -287,8 +313,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the `{"anonymous": "never", "named": "always"}` option: +::: correct + ```js /*eslint space-before-function-paren: ["error", { "anonymous": "never", "named": "always" }]*/ /*eslint-env es6*/ @@ -314,10 +344,14 @@ var foo = { }; ``` +::: + ### `{"anonymous": "ignore", "named": "always"}` Examples of **incorrect** code for this rule with the `{"anonymous": "ignore", "named": "always"}` option: +::: incorrect + ```js /*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/ /*eslint-env es6*/ @@ -339,8 +373,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the `{"anonymous": "ignore", "named": "always"}` option: +::: correct + ```js /*eslint space-before-function-paren: ["error", { "anonymous": "ignore", "named": "always" }]*/ /*eslint-env es6*/ @@ -370,6 +408,8 @@ var foo = { }; ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing before function parenthesis. diff --git a/docs/src/rules/space-before-function-parentheses.md b/docs/src/rules/space-before-function-parentheses.md index 2bc0e3b7433..1b2ed43e6e6 100644 --- a/docs/src/rules/space-before-function-parentheses.md +++ b/docs/src/rules/space-before-function-parentheses.md @@ -38,6 +38,8 @@ This rule takes one argument. If it is `"always"`, which is the default option, Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint-env es6*/ @@ -66,8 +68,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint-env es6*/ @@ -96,8 +102,12 @@ var foo = { }; ``` +::: + Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint-env es6*/ @@ -126,8 +136,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint-env es6*/ @@ -156,8 +170,12 @@ var foo = { }; ``` +::: + Examples of **incorrect** code for this rule with the `{"anonymous": "always", "named": "never"}` option: +::: incorrect + ```js /*eslint-env es6*/ @@ -182,8 +200,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the `{"anonymous": "always", "named": "never"}` option: +::: correct + ```js /*eslint-env es6*/ @@ -208,8 +230,12 @@ var foo = { }; ``` +::: + Examples of **incorrect** code for this rule with the `{"anonymous": "never", "named": "always"}` option: +::: incorrect + ```js /*eslint-env es6*/ @@ -234,8 +260,12 @@ var foo = { }; ``` +::: + Examples of **correct** code for this rule with the `{"anonymous": "never", "named": "always"}` option: +::: correct + ```js /*eslint-env es6*/ @@ -260,6 +290,8 @@ var foo = { }; ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing before function parenthesis. diff --git a/docs/src/rules/space-before-keywords.md b/docs/src/rules/space-before-keywords.md index 361a2626804..bc484717779 100644 --- a/docs/src/rules/space-before-keywords.md +++ b/docs/src/rules/space-before-keywords.md @@ -45,6 +45,8 @@ this behavior, consider using the [block-spacing](block-spacing) rule. Examples of **incorrect** code for this rule with the default `"always"` option: +::: incorrect + ```js /*eslint space-before-keywords: ["error", "always"]*/ /*eslint-env es6*/ @@ -62,8 +64,12 @@ function bar() { } ``` +::: + Examples of **correct** code for this rule with the default `"always"` option: +::: correct + ```js /*eslint space-before-keywords: ["error", "always"]*/ /*eslint-env es6*/ @@ -79,8 +85,12 @@ if (foo) { for (let foo of ['bar', 'baz', 'qux']) {} ``` +::: + Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint space-before-keywords: ["error", "never"]*/ @@ -98,8 +108,12 @@ try {} finally {} try {} catch(e) {} ``` +::: + Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint space-before-keywords: ["error", "never"]*/ @@ -114,6 +128,8 @@ try {}finally {} try{}catch(e) {} ``` +::: + ## When Not To Use It If you do not wish to enforce consistency on keyword spacing. diff --git a/docs/src/rules/space-in-brackets.md b/docs/src/rules/space-in-brackets.md index 4deafc6dde5..15941dedb84 100644 --- a/docs/src/rules/space-in-brackets.md +++ b/docs/src/rules/space-in-brackets.md @@ -47,6 +47,8 @@ Depending on your coding conventions, you can choose either option by specifying Examples of **incorrect** code for this rule with the default `"never"` option: +::: incorrect + ```js /*eslint-env es6*/ @@ -67,8 +69,12 @@ var obj = { baz: {'foo': 'qux'}, bar}; var obj = {baz: { 'foo': 'qux' }, bar}; ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +::: correct + ```js // When options are ["error", "never"] @@ -107,10 +113,14 @@ var obj = { var obj = {}; ``` +::: + ### "always" Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint-env es6*/ @@ -140,8 +150,12 @@ var obj = { 'foo':'bar'}; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js foo[ 'bar' ]; foo[ @@ -166,6 +180,8 @@ var obj = { }; ``` +::: + Note that `"always"` has a special case where `{}` and `[]` are not considered problems. ### Exceptions @@ -213,6 +229,8 @@ In each of the following examples, the `"always"` option is assumed. Examples of **incorrect** code for this rule when `"singleValue"` is set to `false`: +::: incorrect + ```js var foo = [ 'foo' ]; var foo = [ 'foo']; @@ -224,8 +242,12 @@ var foo = [ [ 1, 2 ] ]; var foo = [ { 'foo': 'bar' } ]; ``` +::: + Examples of **correct** code for this rule when `"singleValue"` is set to `false`: +::: correct + ```js var foo = ['foo']; var foo = [1]; @@ -233,8 +255,12 @@ var foo = [[ 1, 1 ]]; var foo = [{ 'foo': 'bar' }]; ``` +::: + Examples of **incorrect** code when `"objectsInArrays"` is set to `false`: +::: incorrect + ```js var arr = [ { 'foo': 'bar' } ]; var arr = [ { @@ -242,8 +268,12 @@ var arr = [ { } ] ``` +::: + Examples of **correct** code when `"objectsInArrays"` is set to `false`: +::: correct + ```js var arr = [{ 'foo': 'bar' }]; var arr = [{ @@ -251,62 +281,96 @@ var arr = [{ }]; ``` +::: + Examples of **incorrect** code when `"arraysInArrays"` is set to `false`: +::: incorrect + ```js var arr = [ [ 1, 2 ], 2, 3, 4 ]; var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ]; ``` +::: + Examples of **correct** code when `"arraysInArrays"` is set to `false`: +::: correct + ```js var arr = [[ 1, 2 ], 2, 3, 4 ]; var arr = [[ 1, 2 ], 2, [ 3, 4 ]]; ``` +::: + Examples of **incorrect** code when `"arraysInObjects"` is set to `false`: +::: incorrect + ```js var obj = { "foo": [ 1, 2 ] }; var obj = { "foo": [ "baz", "bar" ] }; ``` +::: + Examples of **correct** code when `"arraysInObjects"` is set to `false`: +::: correct + ```js var obj = { "foo": [ 1, 2 ]}; var obj = { "foo": [ "baz", "bar" ]}; ``` +::: + Examples of **incorrect** code when `"objectsInObjects"` is set to `false`: +::: incorrect + ```js var obj = { "foo": { "baz": 1, "bar": 2 } }; var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 } }; ``` +::: + Examples of **correct** code when `"objectsInObjects"` is set to `false`: +::: correct + ```js var obj = { "foo": { "baz": 1, "bar": 2 }}; var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 }}; ``` +::: + Examples of **incorrect** code when `"propertyName"` is set to `false`: +::: incorrect + ```js var foo = obj[ 1 ]; var foo = obj[ bar ]; ``` +::: + Examples of **correct** code when `"propertyName"` is set to `false`: +::: correct + ```js var foo = obj[bar]; var foo = obj[0, 1]; ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing between brackets. diff --git a/docs/src/rules/space-in-parens.md b/docs/src/rules/space-in-parens.md index 0625410ccf6..9025d0b6956 100644 --- a/docs/src/rules/space-in-parens.md +++ b/docs/src/rules/space-in-parens.md @@ -46,6 +46,8 @@ Depending on your coding conventions, you can choose either option by specifying Examples of **incorrect** code for this rule with the default `"never"` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "never"]*/ @@ -61,8 +63,12 @@ var foo = ( 1 + 2 ) * 3; ( function () { return 'bar'; }() ); ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +::: correct + ```js /*eslint space-in-parens: ["error", "never"]*/ @@ -76,10 +82,14 @@ var foo = (1 + 2) * 3; (function () { return 'bar'; }()); ``` +::: + ### "always" Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "always"]*/ @@ -93,8 +103,12 @@ var foo = (1 + 2) * 3; (function () { return 'bar'; }()); ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint space-in-parens: ["error", "always"]*/ @@ -109,6 +123,8 @@ var foo = ( 1 + 2 ) * 3; ( function () { return 'bar'; }() ); ``` +::: + ### Exceptions An object literal may be used as a third array item to specify exceptions, with the key `"exceptions"` and an array as the value. These exceptions work in the context of the first option. That is, if `"always"` is set to enforce spacing, then any "exception" will *disallow* spacing. Conversely, if `"never"` is set to disallow spacing, then any "exception" will *enforce* spacing. @@ -130,6 +146,8 @@ Empty parens exception and behavior: Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["{}"] }` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/ @@ -137,8 +155,12 @@ foo({bar: 'baz'}); foo(1, {bar: 'baz'}); ``` +::: + Examples of **correct** code for this rule with the `"never", { "exceptions": ["{}"] }` option: +::: correct + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/ @@ -146,8 +168,12 @@ foo( {bar: 'baz'} ); foo(1, {bar: 'baz'} ); ``` +::: + Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["{}"] }` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/ @@ -155,8 +181,12 @@ foo( {bar: 'baz'} ); foo( 1, {bar: 'baz'} ); ``` +::: + Examples of **correct** code for this rule with the `"always", { "exceptions": ["{}"] }` option: +::: correct + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/ @@ -164,8 +194,12 @@ foo({bar: 'baz'}); foo( 1, {bar: 'baz'}); ``` +::: + Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["[]"] }` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/ @@ -173,8 +207,12 @@ foo([bar, baz]); foo([bar, baz], 1); ``` +::: + Examples of **correct** code for this rule with the `"never", { "exceptions": ["[]"] }` option: +::: correct + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/ @@ -182,8 +220,12 @@ foo( [bar, baz] ); foo( [bar, baz], 1); ``` +::: + Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["[]"] }` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/ @@ -191,8 +233,12 @@ foo( [bar, baz] ); foo( [bar, baz], 1 ); ``` +::: + Examples of **correct** code for this rule with the `"always", { "exceptions": ["[]"] }` option: +::: correct + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/ @@ -200,8 +246,12 @@ foo([bar, baz]); foo([bar, baz], 1 ); ``` +::: + Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["()"] }]` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/ @@ -210,8 +260,12 @@ foo((1 + 2), 1); foo(bar()); ``` +::: + Examples of **correct** code for this rule with the `"never", { "exceptions": ["()"] }]` option: +::: correct + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/ @@ -220,8 +274,12 @@ foo( (1 + 2), 1); foo(bar() ); ``` +::: + Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["()"] }]` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/ @@ -229,8 +287,12 @@ foo( ( 1 + 2 ) ); foo( ( 1 + 2 ), 1 ); ``` +::: + Examples of **correct** code for this rule with the `"always", { "exceptions": ["()"] }]` option: +::: correct + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/ @@ -238,44 +300,64 @@ foo(( 1 + 2 )); foo(( 1 + 2 ), 1 ); ``` +::: + The `"empty"` exception concerns empty parentheses, and works the same way as the other exceptions, inverting the first option. Example of **incorrect** code for this rule with the `"never", { "exceptions": ["empty"] }]` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/ foo(); ``` +::: + Example of **correct** code for this rule with the `"never", { "exceptions": ["empty"] }]` option: +::: correct + ```js /*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/ foo( ); ``` +::: + Example of **incorrect** code for this rule with the `"always", { "exceptions": ["empty"] }]` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/ foo( ); ``` +::: + Example of **correct** code for this rule with the `"always", { "exceptions": ["empty"] }]` option: +::: correct + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/ foo(); ``` +::: + You can include multiple entries in the `"exceptions"` array. Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["{}", "[]"] }]` option: +::: incorrect + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/ @@ -284,8 +366,12 @@ baz( 1, [1,2] ); foo( {bar: 'baz'}, [1, 2] ); ``` +::: + Examples of **correct** code for this rule with the `"always", { "exceptions": ["{}", "[]"] }]` option: +::: correct + ```js /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/ @@ -294,6 +380,8 @@ baz( 1, [1,2]); foo({bar: 'baz'}, [1, 2]); ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing between parentheses. diff --git a/docs/src/rules/space-infix-ops.md b/docs/src/rules/space-infix-ops.md index aa079b51114..8c496e6d363 100644 --- a/docs/src/rules/space-infix-ops.md +++ b/docs/src/rules/space-infix-ops.md @@ -45,6 +45,8 @@ var foo = bar|0; // `foo` is forced to be signed 32 bit integer Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint space-infix-ops: "error"*/ /*eslint-env es6*/ @@ -64,8 +66,12 @@ var {a=0}=bar; function foo(a=0) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint space-infix-ops: "error"*/ /*eslint-env es6*/ @@ -83,6 +89,8 @@ var {a = 0} = bar; function foo(a = 0) { } ``` +::: + ## When Not To Use It You can turn this rule off if you are not concerned with the consistency of spacing around infix operators. diff --git a/docs/src/rules/space-return-throw-case.md b/docs/src/rules/space-return-throw-case.md index 6bf9ba199b2..ca65dd75840 100644 --- a/docs/src/rules/space-return-throw-case.md +++ b/docs/src/rules/space-return-throw-case.md @@ -17,6 +17,8 @@ Require spaces following `return`, `throw`, and `case`. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint space-return-throw-case: "error"*/ @@ -27,8 +29,12 @@ function f(){ return-a; } switch(a){ case'a': break; } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint space-return-throw-case: "error"*/ @@ -38,3 +44,5 @@ function f(){ return -a; } switch(a){ case 'a': break; } ``` + +::: diff --git a/docs/src/rules/space-unary-ops.md b/docs/src/rules/space-unary-ops.md index d413ff68651..b53d82e32ed 100644 --- a/docs/src/rules/space-unary-ops.md +++ b/docs/src/rules/space-unary-ops.md @@ -72,6 +72,8 @@ In this case, spacing will be disallowed after a `new` operator and required bef Examples of **incorrect** code for this rule with the default `{"words": true, "nonwords": false}` option: +::: incorrect + ```js /*eslint space-unary-ops: "error"*/ @@ -92,6 +94,8 @@ foo --; + "3"; ``` +::: + ```js /*eslint space-unary-ops: "error"*/ /*eslint-env es6*/ @@ -111,6 +115,8 @@ async function foo() { Examples of **correct** code for this rule with the `{"words": true, "nonwords": false}` option: +::: correct + ```js /*eslint space-unary-ops: "error"*/ @@ -139,6 +145,8 @@ foo--; +"3"; ``` +::: + ```js /*eslint space-unary-ops: "error"*/ /*eslint-env es6*/ diff --git a/docs/src/rules/space-unary-word-ops.md b/docs/src/rules/space-unary-word-ops.md index eb5d3d95e0c..3d9ec250871 100644 --- a/docs/src/rules/space-unary-word-ops.md +++ b/docs/src/rules/space-unary-word-ops.md @@ -15,10 +15,14 @@ Require spaces following unary word operators. Examples of **incorrect** code for this rule: +::: incorrect + ```js typeof!a ``` +::: + ```js void{a:0} ``` @@ -33,10 +37,14 @@ delete(a.b) Examples of **correct** code for this rule: +::: correct + ```js delete a.b ``` +::: + ```js new C ``` diff --git a/docs/src/rules/spaced-comment.md b/docs/src/rules/spaced-comment.md index 6d9c6398220..27ed424367d 100644 --- a/docs/src/rules/spaced-comment.md +++ b/docs/src/rules/spaced-comment.md @@ -76,6 +76,8 @@ You can also define separate exceptions and markers for block and line comments. Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint spaced-comment: ["error", "always"]*/ @@ -84,6 +86,8 @@ Examples of **incorrect** code for this rule with the `"always"` option: /*This is a comment with no whitespace at the beginning */ ``` +::: + ```js /* eslint spaced-comment: ["error", "always", { "block": { "balanced": true } }] */ /* This is a comment with whitespace at the beginning but not the end*/ @@ -91,6 +95,8 @@ Examples of **incorrect** code for this rule with the `"always"` option: Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /* eslint spaced-comment: ["error", "always"] */ @@ -107,6 +113,8 @@ This comment has a newline */ ``` +::: + ```js /* eslint spaced-comment: ["error", "always"] */ @@ -119,6 +127,8 @@ This comment has a newline Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint spaced-comment: ["error", "never"]*/ @@ -129,6 +139,8 @@ Examples of **incorrect** code for this rule with the `"never"` option: /* \nThis is a comment with a whitespace at the beginning */ ``` +::: + ```js /*eslint spaced-comment: ["error", "never", { "block": { "balanced": true } }]*/ /*This is a comment with whitespace at the end */ @@ -136,12 +148,16 @@ Examples of **incorrect** code for this rule with the `"never"` option: Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint spaced-comment: ["error", "never"]*/ /*This is a comment with no whitespace at the beginning */ ``` +::: + ```js /*eslint spaced-comment: ["error", "never"]*/ @@ -154,6 +170,8 @@ Examples of **correct** code for this rule with the `"never"` option: Examples of **incorrect** code for this rule with the `"always"` option combined with `"exceptions"`: +::: incorrect + ```js /* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["-"] } }] */ @@ -162,6 +180,8 @@ Examples of **incorrect** code for this rule with the `"always"` option combined //-------------- ``` +::: + ```js /* eslint spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */ @@ -194,6 +214,8 @@ Examples of **incorrect** code for this rule with the `"always"` option combined Examples of **correct** code for this rule with the `"always"` option combined with `"exceptions"`: +::: correct + ```js /* eslint spaced-comment: ["error", "always", { "exceptions": ["-"] }] */ @@ -202,6 +224,8 @@ Examples of **correct** code for this rule with the `"always"` option combined w //-------------- ``` +::: + ```js /* eslint spaced-comment: ["error", "always", { "line": { "exceptions": ["-"] } }] */ @@ -252,12 +276,16 @@ COMMENT Examples of **incorrect** code for this rule with the `"always"` option combined with `"markers"`: +::: incorrect + ```js /* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */ ///This is a comment with a marker but without whitespace ``` +::: + ```js /*eslint spaced-comment: ["error", "always", { "block": { "markers": ["!"], "balanced": true } }]*/ /*! This is a comment with a marker but without whitespace at the end*/ @@ -270,12 +298,16 @@ Examples of **incorrect** code for this rule with the `"always"` option combined Examples of **correct** code for this rule with the `"always"` option combined with `"markers"`: +::: correct + ```js /* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */ /// This is a comment with a marker ``` +::: + ```js /*eslint spaced-comment: ["error", "never", { "markers": ["!<"] }]*/ diff --git a/docs/src/rules/spaced-line-comment.md b/docs/src/rules/spaced-line-comment.md index ea5247bd6be..0aaff62bfb0 100644 --- a/docs/src/rules/spaced-line-comment.md +++ b/docs/src/rules/spaced-line-comment.md @@ -30,11 +30,15 @@ Exceptions cannot be mixed. Examples of **incorrect** code for this rule: +::: incorrect + ```js // When ["never"] // This is a comment with a whitespace at the beginning ``` +::: + ```js //When ["always"] //This is a comment with no whitespace at the beginning @@ -50,12 +54,16 @@ var foo = 5; Examples of **correct** code for this rule: +::: correct + ```js // When ["always"] // This is a comment with a whitespace at the beginning var foo = 5; ``` +::: + ```js //When ["never"] //This is a comment with no whitespace at the beginning diff --git a/docs/src/rules/strict.md b/docs/src/rules/strict.md index df14488edca..1cf31f7308b 100644 --- a/docs/src/rules/strict.md +++ b/docs/src/rules/strict.md @@ -86,6 +86,8 @@ Otherwise the `"safe"` option corresponds to the `"function"` option. Note that Examples of **incorrect** code for this rule with the `"global"` option: +::: incorrect + ```js /*eslint strict: ["error", "global"]*/ @@ -93,6 +95,8 @@ function foo() { } ``` +::: + ```js /*eslint strict: ["error", "global"]*/ @@ -113,6 +117,8 @@ function foo() { Examples of **correct** code for this rule with the `"global"` option: +::: correct + ```js /*eslint strict: ["error", "global"]*/ @@ -122,12 +128,16 @@ function foo() { } ``` +::: + ### function This option ensures that all function bodies are strict mode code, while global code is not. Particularly if a build step concatenates multiple scripts, a strict mode directive in global code of one script could unintentionally enable strict mode in another script that was not intended to be strict code. Examples of **incorrect** code for this rule with the `"function"` option: +::: incorrect + ```js /*eslint strict: ["error", "function"]*/ @@ -137,6 +147,8 @@ function foo() { } ``` +::: + ```js /*eslint strict: ["error", "function"]*/ @@ -168,6 +180,8 @@ function foo(a = 1) { Examples of **correct** code for this rule with the `"function"` option: +::: correct + ```js /*eslint strict: ["error", "function"]*/ @@ -193,10 +207,14 @@ var foo = (function() { }()); ``` +::: + ### never Examples of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint strict: ["error", "never"]*/ @@ -206,6 +224,8 @@ function foo() { } ``` +::: + ```js /*eslint strict: ["error", "never"]*/ @@ -216,6 +236,8 @@ function foo() { Examples of **correct** code for this rule with the `"never"` option: +::: correct + ```js /*eslint strict: ["error", "never"]*/ @@ -223,6 +245,8 @@ function foo() { } ``` +::: + ### earlier default (removed) (removed) The default option (that is, no string option specified) for this rule was **removed** in ESLint v1.0. The `"function"` option is most similar to the removed option. @@ -231,6 +255,8 @@ This option ensures that all functions are executed in strict mode. A strict mod Examples of **incorrect** code for this rule with the earlier default option which has been removed: +::: incorrect + ```js // "strict": "error" @@ -238,6 +264,8 @@ function foo() { } ``` +::: + ```js // "strict": "error" @@ -250,6 +278,8 @@ function foo() { Examples of **correct** code for this rule with the earlier default option which has been removed: +::: correct + ```js // "strict": "error" @@ -259,6 +289,8 @@ function foo() { } ``` +::: + ```js // "strict": "error" diff --git a/docs/src/rules/switch-colon-spacing.md b/docs/src/rules/switch-colon-spacing.md index 1106bdb8026..55e7d8784e8 100644 --- a/docs/src/rules/switch-colon-spacing.md +++ b/docs/src/rules/switch-colon-spacing.md @@ -31,6 +31,8 @@ This rule has 2 options that are boolean value. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint switch-colon-spacing: "error"*/ @@ -40,8 +42,12 @@ switch (a) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint switch-colon-spacing: "error"*/ @@ -56,8 +62,12 @@ switch (a) { } ``` +::: + Examples of **incorrect** code for this rule with `{"after": false, "before": true}` option: +::: incorrect + ```js /*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/ @@ -67,8 +77,12 @@ switch (a) { } ``` +::: + Examples of **correct** code for this rule with `{"after": false, "before": true}` option: +::: correct + ```js /*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/ @@ -83,6 +97,8 @@ switch (a) { } ``` +::: + ## When Not To Use It If you don't want to notify spacing around colons of switch statements, then it's safe to disable this rule. diff --git a/docs/src/rules/symbol-description.md b/docs/src/rules/symbol-description.md index d33284a46a0..3a458d7ed41 100644 --- a/docs/src/rules/symbol-description.md +++ b/docs/src/rules/symbol-description.md @@ -37,6 +37,8 @@ This rules requires a description when creating symbols. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint symbol-description: "error"*/ /*eslint-env es6*/ @@ -44,8 +46,12 @@ Examples of **incorrect** code for this rule: var foo = Symbol(); ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint symbol-description: "error"*/ /*eslint-env es6*/ @@ -56,6 +62,8 @@ var someString = "some description"; var bar = Symbol(someString); ``` +::: + ## When Not To Use It This rule should not be used in ES3/5 environments. diff --git a/docs/src/rules/template-curly-spacing.md b/docs/src/rules/template-curly-spacing.md index eff2aa98146..3e0c044e088 100644 --- a/docs/src/rules/template-curly-spacing.md +++ b/docs/src/rules/template-curly-spacing.md @@ -40,6 +40,8 @@ This rule has one option which has either `"never"` or `"always"` as value. Examples of **incorrect** code for this rule with the default `"never"` option: +::: incorrect + ```js /*eslint template-curly-spacing: "error"*/ @@ -49,8 +51,12 @@ Examples of **incorrect** code for this rule with the default `"never"` option: `hello, ${ people.name }!`; ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +::: correct + ```js /*eslint template-curly-spacing: "error"*/ @@ -61,10 +67,14 @@ Examples of **correct** code for this rule with the default `"never"` option: }!`; ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint template-curly-spacing: ["error", "always"]*/ @@ -74,8 +84,12 @@ Examples of **incorrect** code for this rule with the `"always"` option: `hello, ${people.name}!`; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint template-curly-spacing: ["error", "always"]*/ @@ -86,6 +100,8 @@ Examples of **correct** code for this rule with the `"always"` option: }!`; ``` +::: + ## When Not To Use It If you don't want to be notified about usage of spacing inside of template strings, then it's safe to disable this rule. diff --git a/docs/src/rules/template-tag-spacing.md b/docs/src/rules/template-tag-spacing.md index a2ec2d6d68c..8403d83883e 100644 --- a/docs/src/rules/template-tag-spacing.md +++ b/docs/src/rules/template-tag-spacing.md @@ -44,38 +44,54 @@ This rule has one option whose value can be set to `"never"` or `"always"` Examples of **incorrect** code for this rule with the default `"never"` option: +::: incorrect + ```js /*eslint template-tag-spacing: "error"*/ func `Hello world`; ``` +::: + Examples of **correct** code for this rule with the default `"never"` option: +::: correct + ```js /*eslint template-tag-spacing: "error"*/ func`Hello world`; ``` +::: + ### always Examples of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint template-tag-spacing: ["error", "always"]*/ func`Hello world`; ``` +::: + Examples of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint template-tag-spacing: ["error", "always"]*/ func `Hello world`; ``` +::: + ## When Not To Use It If you don't want to be notified about usage of spacing between tag functions and their template literals, then it's safe to disable this rule. diff --git a/docs/src/rules/unicode-bom.md b/docs/src/rules/unicode-bom.md index 878dfacca4f..f542556f03f 100644 --- a/docs/src/rules/unicode-bom.md +++ b/docs/src/rules/unicode-bom.md @@ -32,6 +32,8 @@ This rule has a string option: Example of **correct** code for this rule with the `"always"` option: +::: correct + ```js /*eslint unicode-bom: ["error", "always"]*/ @@ -39,26 +41,38 @@ U+FEFF var abc; ``` +::: + Example of **incorrect** code for this rule with the `"always"` option: +::: incorrect + ```js /*eslint unicode-bom: ["error", "always"]*/ var abc; ``` +::: + ### never Example of **correct** code for this rule with the default `"never"` option: +::: correct + ```js /*eslint unicode-bom: ["error", "never"]*/ var abc; ``` +::: + Example of **incorrect** code for this rule with the `"never"` option: +::: incorrect + ```js /*eslint unicode-bom: ["error", "never"]*/ @@ -66,6 +80,8 @@ U+FEFF var abc; ``` +::: + ## When Not To Use It If you use some UTF-16 or UTF-32 files and you want to allow a file to diff --git a/docs/src/rules/use-isnan.md b/docs/src/rules/use-isnan.md index 60a4f055173..410dfd63c43 100644 --- a/docs/src/rules/use-isnan.md +++ b/docs/src/rules/use-isnan.md @@ -24,6 +24,8 @@ This rule disallows comparisons to 'NaN'. Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint use-isnan: "error"*/ @@ -44,8 +46,12 @@ if (foo != Number.NaN) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint use-isnan: "error"*/ @@ -58,6 +64,8 @@ if (!isNaN(foo)) { } ``` +::: + ## Options This rule has an object option, with two options: @@ -72,6 +80,8 @@ Therefore, it can never match `case NaN`. Also, `switch(NaN)` can never match a Examples of **incorrect** code for this rule with `"enforceForSwitchCase"` option set to `true` (default): +::: incorrect + ```js /*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/ @@ -116,8 +126,12 @@ switch (Number.NaN) { } ``` +::: + Examples of **correct** code for this rule with `"enforceForSwitchCase"` option set to `true` (default): +::: correct + ```js /*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/ @@ -139,8 +153,12 @@ if (Number.isNaN(a)) { } // ... ``` +::: + Examples of **correct** code for this rule with `"enforceForSwitchCase"` option set to `false`: +::: correct + ```js /*eslint use-isnan: ["error", {"enforceForSwitchCase": false}]*/ @@ -185,6 +203,8 @@ switch (Number.NaN) { } ``` +::: + ### enforceForIndexOf The following methods internally use the `===` comparison to match the given value with an array element: @@ -198,6 +218,8 @@ Set `"enforceForIndexOf"` to `true` if you want this rule to report `indexOf(NaN Examples of **incorrect** code for this rule with `"enforceForIndexOf"` option set to `true`: +::: incorrect + ```js /*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/ @@ -208,8 +230,12 @@ var firstIndex = myArray.indexOf(NaN); var lastIndex = myArray.lastIndexOf(NaN); ``` +::: + Examples of **correct** code for this rule with `"enforceForIndexOf"` option set to `true`: +::: correct + ```js /*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/ @@ -253,6 +279,8 @@ var firstIndex = myArray.findIndex(Number.isNaN); var hasNaN = myArray.includes(NaN); ``` +::: + #### Known Limitations This option checks methods with the given names, *even if* the object which has the method is *not* an array. diff --git a/docs/src/rules/valid-jsdoc.md b/docs/src/rules/valid-jsdoc.md index c7bed2ef882..636efc4fb83 100644 --- a/docs/src/rules/valid-jsdoc.md +++ b/docs/src/rules/valid-jsdoc.md @@ -50,6 +50,8 @@ This rule does not report missing JSDoc comments for classes, functions, or meth Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint valid-jsdoc: "error"*/ @@ -88,8 +90,12 @@ function sum(num1, num2) { } ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint valid-jsdoc: "error"*/ /*eslint-env es6*/ @@ -165,6 +171,8 @@ class WonderfulWidget extends Widget { } ``` +::: + ## Options This rule has an object option: @@ -184,6 +192,8 @@ This rule has an object option: Examples of additional **incorrect** code for this rule with sample `"prefer": { "arg": "param", "argument": "param", "class": "constructor", "return": "returns", "virtual": "abstract" }` options: +::: incorrect + ```js /*eslint valid-jsdoc: ["error", { "prefer": { "arg": "param", "argument": "param", "class": "constructor", "return": "returns", "virtual": "abstract" } }]*/ /*eslint-env es6*/ @@ -222,10 +232,14 @@ class Widget { } ``` +::: + ### preferType Examples of additional **incorrect** code for this rule with sample `"preferType": { "Boolean": "boolean", "Number": "number", "object": "Object", "String": "string" }` options: +::: incorrect + ```js /*eslint valid-jsdoc: ["error", { "preferType": { "Boolean": "boolean", "Number": "number", "object": "Object", "String": "string" } }]*/ /*eslint-env es6*/ @@ -262,10 +276,14 @@ class Widget { } ``` +::: + ### requireReturn Examples of additional **incorrect** code for this rule with the `"requireReturn": false` option: +::: incorrect + ```js /*eslint valid-jsdoc: ["error", { "requireReturn": false }]*/ @@ -291,8 +309,12 @@ class Widget { } ``` +::: + Example of additional **correct** code for this rule with the `"requireReturn": false` option: +::: correct + ```js /*eslint valid-jsdoc: ["error", { "requireReturn": false }]*/ @@ -304,10 +326,14 @@ function greet(name) { } ``` +::: + ### requireReturnType Example of additional **correct** code for this rule with the `"requireReturnType": false` option: +::: correct + ```js /*eslint valid-jsdoc: ["error", { "requireReturnType": false }]*/ @@ -322,10 +348,14 @@ function add(num1, num2) { } ``` +::: + ### requireParamType Example of additional **correct** code for this rule with the `"requireParamType": false` option: +::: correct + ```js /*eslint valid-jsdoc: ["error", { "requireParamType": false }]*/ @@ -340,10 +370,14 @@ function add(num1, num2) { } ``` +::: + ### matchDescription Example of additional **incorrect** code for this rule with a sample `"matchDescription": ".+"` option: +::: incorrect + ```js /*eslint valid-jsdoc: ["error", { "matchDescription": ".+" }]*/ @@ -357,10 +391,14 @@ function greet(name) { } ``` +::: + ### requireParamDescription Example of additional **correct** code for this rule with the `"requireParamDescription": false` option: +::: correct + ```js /*eslint valid-jsdoc: ["error", { "requireParamDescription": false }]*/ @@ -375,10 +413,14 @@ function add(num1, num2) { } ``` +::: + ### requireReturnDescription Example of additional **correct** code for this rule with the `"requireReturnDescription": false` option: +::: correct + ```js /*eslint valid-jsdoc: ["error", { "requireReturnDescription": false }]*/ @@ -393,6 +435,8 @@ function add(num1, num2) { } ``` +::: + ## When Not To Use It If you aren't using JSDoc, then you can safely turn this rule off. diff --git a/docs/src/rules/valid-typeof.md b/docs/src/rules/valid-typeof.md index b43884fea1d..7d50f593a8b 100644 --- a/docs/src/rules/valid-typeof.md +++ b/docs/src/rules/valid-typeof.md @@ -27,6 +27,8 @@ This rule has an object option: Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint valid-typeof: "error"*/ @@ -36,8 +38,12 @@ typeof bar != "nunber" typeof bar !== "fucntion" ``` +::: + Examples of **correct** code for this rule: +::: correct + ```js /*eslint valid-typeof: "error"*/ @@ -47,8 +53,12 @@ typeof foo === baz typeof bar === typeof qux ``` +::: + Examples of **incorrect** code with the `{ "requireStringLiterals": true }` option: +::: incorrect + ```js /*eslint valid-typeof: ["error", { "requireStringLiterals": true }]*/ @@ -60,8 +70,12 @@ typeof baz === anotherVariable typeof foo == 5 ``` +::: + Examples of **correct** code with the `{ "requireStringLiterals": true }` option: +::: correct + ```js /*eslint valid-typeof: ["error", { "requireStringLiterals": true }]*/ @@ -71,6 +85,8 @@ typeof baz === "string" typeof bar === typeof qux ``` +::: + ## When Not To Use It You may want to turn this rule off if you will be using the `typeof` operator on host objects. diff --git a/docs/src/rules/vars-on-top.md b/docs/src/rules/vars-on-top.md index 39a25c4a483..921801df427 100644 --- a/docs/src/rules/vars-on-top.md +++ b/docs/src/rules/vars-on-top.md @@ -23,6 +23,8 @@ Allowing multiple declarations helps promote maintainability and is thus allowed Examples of **incorrect** code for this rule: +::: incorrect + ```js /*eslint vars-on-top: "error"*/ @@ -40,6 +42,8 @@ function doSomething() { } ``` +::: + ```js /*eslint vars-on-top: "error"*/ @@ -73,6 +77,8 @@ class C { Examples of **correct** code for this rule: +::: correct + ```js /*eslint vars-on-top: "error"*/ @@ -90,6 +96,8 @@ function doSomething() { } ``` +::: + ```js /*eslint vars-on-top: "error"*/ diff --git a/docs/src/rules/wrap-iife.md b/docs/src/rules/wrap-iife.md index 0b9cd2d9748..2b99b73fb48 100644 --- a/docs/src/rules/wrap-iife.md +++ b/docs/src/rules/wrap-iife.md @@ -41,6 +41,8 @@ Object option: Examples of **incorrect** code for the default `"outside"` option: +::: incorrect + ```js /*eslint wrap-iife: ["error", "outside"]*/ @@ -48,18 +50,26 @@ var x = function () { return { y: 1 };}(); // unwrapped var x = (function () { return { y: 1 };})(); // wrapped function expression ``` +::: + Examples of **correct** code for the default `"outside"` option: +::: correct + ```js /*eslint wrap-iife: ["error", "outside"]*/ var x = (function () { return { y: 1 };}()); // wrapped call expression ``` +::: + ### inside Examples of **incorrect** code for the `"inside"` option: +::: incorrect + ```js /*eslint wrap-iife: ["error", "inside"]*/ @@ -67,26 +77,38 @@ var x = function () { return { y: 1 };}(); // unwrapped var x = (function () { return { y: 1 };}()); // wrapped call expression ``` +::: + Examples of **correct** code for the `"inside"` option: +::: correct + ```js /*eslint wrap-iife: ["error", "inside"]*/ var x = (function () { return { y: 1 };})(); // wrapped function expression ``` +::: + ### any Examples of **incorrect** code for the `"any"` option: +::: incorrect + ```js /*eslint wrap-iife: ["error", "any"]*/ var x = function () { return { y: 1 };}(); // unwrapped ``` +::: + Examples of **correct** code for the `"any"` option: +::: correct + ```js /*eslint wrap-iife: ["error", "any"]*/ @@ -94,10 +116,14 @@ var x = (function () { return { y: 1 };}()); // wrapped call expression var x = (function () { return { y: 1 };})(); // wrapped function expression ``` +::: + ### functionPrototypeMethods Examples of **incorrect** code for this rule with the `"inside", { "functionPrototypeMethods": true }` options: +::: incorrect + ```js /* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */ @@ -107,11 +133,17 @@ var x = function(){ foo(); }.call(bar) var x = (function(){ foo(); }.call(bar)) ``` +::: + Examples of **correct** code for this rule with the `"inside", { "functionPrototypeMethods": true }` options: +::: correct + ```js /* eslint wrap-iife: [2, "inside", { functionPrototypeMethods: true }] */ var x = (function(){ foo(); })() var x = (function(){ foo(); }).call(bar) ``` + +::: diff --git a/docs/src/rules/wrap-regex.md b/docs/src/rules/wrap-regex.md index 9249d703ea2..7eb3047045c 100644 --- a/docs/src/rules/wrap-regex.md +++ b/docs/src/rules/wrap-regex.md @@ -23,6 +23,8 @@ This is used to disambiguate the slash operator and facilitates more readable co Example of **incorrect** code for this rule: +::: incorrect + ```js /*eslint wrap-regex: "error"*/ @@ -31,8 +33,12 @@ function a() { } ``` +::: + Example of **correct** code for this rule: +::: correct + ```js /*eslint wrap-regex: "error"*/ @@ -40,3 +46,5 @@ function a() { return (/foo/).test("bar"); } ``` + +::: diff --git a/docs/src/rules/yield-star-spacing.md b/docs/src/rules/yield-star-spacing.md index f97e00b5d69..0faaca706a0 100644 --- a/docs/src/rules/yield-star-spacing.md +++ b/docs/src/rules/yield-star-spacing.md @@ -48,6 +48,8 @@ The option also has a string shorthand: Examples of **correct** code for this rule with the default `"after"` option: +::: correct + ```js /*eslint yield-star-spacing: ["error", "after"]*/ /*eslint-env es6*/ @@ -57,10 +59,14 @@ function* generator() { } ``` +::: + ### before Examples of **correct** code for this rule with the `"before"` option: +::: correct + ```js /*eslint yield-star-spacing: ["error", "before"]*/ /*eslint-env es6*/ @@ -70,10 +76,14 @@ function *generator() { } ``` +::: + ### both Examples of **correct** code for this rule with the `"both"` option: +::: correct + ```js /*eslint yield-star-spacing: ["error", "both"]*/ /*eslint-env es6*/ @@ -83,10 +93,14 @@ function * generator() { } ``` +::: + ### neither Examples of **correct** code for this rule with the `"neither"` option: +::: correct + ```js /*eslint yield-star-spacing: ["error", "neither"]*/ /*eslint-env es6*/ @@ -96,6 +110,8 @@ function*generator() { } ``` +::: + ## When Not To Use It If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule. diff --git a/docs/src/rules/yoda.md b/docs/src/rules/yoda.md index 64ad9f4fbe8..06454a49729 100644 --- a/docs/src/rules/yoda.md +++ b/docs/src/rules/yoda.md @@ -56,6 +56,8 @@ The `onlyEquality` option allows a superset of the exceptions which `exceptRange Examples of **incorrect** code for the default `"never"` option: +::: incorrect + ```js /*eslint yoda: "error"*/ @@ -88,8 +90,12 @@ if (0 <= x && x < 1) { } ``` +::: + Examples of **correct** code for the default `"never"` option: +::: correct + ```js /*eslint yoda: "error"*/ @@ -110,10 +116,14 @@ if (`${value}` === `red`) { } ``` +::: + ### exceptRange Examples of **correct** code for the `"never", { "exceptRange": true }` options: +::: correct + ```js /*eslint yoda: ["error", "never", { "exceptRange": true }]*/ @@ -138,10 +148,14 @@ function howLong(arr) { } ``` +::: + ### onlyEquality Examples of **correct** code for the `"never", { "onlyEquality": true }` options: +::: correct + ```js /*eslint yoda: ["error", "never", { "onlyEquality": true }]*/ @@ -155,10 +169,14 @@ if (x !== `foo` && `bar` != x) { } ``` +::: + ### always Examples of **incorrect** code for the `"always"` option: +::: incorrect + ```js /*eslint yoda: ["error", "always"]*/ @@ -171,8 +189,12 @@ if (color == `blue`) { } ``` +::: + Examples of **correct** code for the `"always"` option: +::: correct + ```js /*eslint yoda: ["error", "always"]*/ @@ -192,3 +214,5 @@ if (-1 < str.indexOf(substr)) { // ... } ``` + +:::