From 3a432d82b3a5710aff7da20302fe0b94fedc46c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96zel?= Date: Fri, 12 Mar 2021 00:33:35 +0300 Subject: [PATCH] Docs: Improve documentation for indent rule (#14168) * Docs: Improve documentation for indent regarding ignoredNodes * Docs: Add AST node description at the end of long description * add blank line * Add deleted sentence again to ignoredNodes * move long description of indent to match short description pos --- docs/rules/indent.md | 70 +++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/docs/rules/indent.md b/docs/rules/indent.md index 7c2e6b7a623..2a626604e01 100644 --- a/docs/rules/indent.md +++ b/docs/rules/indent.md @@ -68,6 +68,7 @@ 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.md). 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. * `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements * `"VariableDeclarator"` (default: 1) enforces indentation level for `var` declarators; can also take an object to define separate rules for `var`, `let` and `const` declarations. It can also be `"first"`, indicating all the declarators should be aligned with the first declarator. * `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs. This can also be set to `"off"` to disable checking for file-level IIFEs. @@ -85,7 +86,6 @@ This rule has an object option: * `"ImportDeclaration"` (default: 1) enforces indentation level for import statements. It can be set to the string `"first"`, indicating that all imported members from a module should be aligned with the first member in the list. This can also be set to `"off"` to disable checking for imported module members. * `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions. * `"offsetTernaryExpressions": true` (`false` by default) requires indentation for values of ternary expressions. -* `"ignoredNodes"` accepts an array of [selectors](/docs/developer-guide/selectors.md). 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. * `"ignoreComments"` (default: false) can be used when comments do not need to be aligned with nodes on the previous or next line. Level of indentation denotes the multiple of the indent specified. Example: @@ -133,6 +133,41 @@ 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: + +```js +/*eslint indent: ["error", 4, { "ignoredNodes": ["ConditionalExpression"] }]*/ + +var a = foo + ? bar + : baz; + +var a = foo + ? bar +: 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: + +```js +/*eslint indent: ["error", 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }]*/ + +(function() { + +foo(); +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: @@ -740,39 +775,6 @@ condition } ``` -### 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: - -```js -/*eslint indent: ["error", 4, { "ignoredNodes": ["ConditionalExpression"] }]*/ - -var a = foo - ? bar - : baz; - -var a = foo - ? bar -: 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: - -```js -/*eslint indent: ["error", 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }]*/ - -(function() { - -foo(); -bar(); - -}) -``` - ### ignoreComments Examples of additional **correct** code for this rule with the `4, { "ignoreComments": true }` option: