Index

indent

强制执行一致的缩进

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

有几个通用准则需要嵌套块和语句的特定缩进,例如:

🌐 There are several common guidelines which require specific indentation of nested blocks and statements, like:

function hello(indentSize, type) {
    if (indentSize === 4 && type !== 'tab') {
        console.log('Each next indentation will increase on 4 spaces');
    }
}

这些是不同风格指南中推荐的最常见场景:

🌐 These are the most common scenarios recommended in different style guides:

  • 两个空格,不要更长,也不要制表符:Google、npm、Node.js、Idiomatic、Felix
  • 标签页:jQuery
  • 四个空格:克罗克福德

规则详情

🌐 Rule Details

此规则强制执行一致的缩进风格。默认风格是 4 spaces

🌐 This rule enforces a consistent indentation style. The default style is 4 spaces.

选项

🌐 Options

此规则有一个混合选项:

🌐 This rule has a mixed option:

例如,对于 2 个空格缩进:

🌐 For example, for 2-space indentation:

{
    "indent": ["error", 2]
}

或者对于标签缩进:

🌐 Or for tabbed indentation:

{
    "indent": ["error", "tab"]
}

使用默认选项的此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default options:

在线运行
/*eslint indent: "error"*/

if (a) {
  b=c;
  function foo(d) {
    e=f;
  }
}

使用默认选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the default options:

在线运行
/*eslint indent: "error"*/

if (a) {
    b=c;
    function foo(d) {
        e=f;
    }
}

此规则有一个对象选项:

🌐 This rule has an object option:

  • "ignoredNodes" 可以用来禁用对任何 AST 节点的缩进检查。它接受一个 selectors 数组。如果一个 AST 节点与任何一个 selector 匹配,则该节点的直接子节点的标记缩进将被忽略。 如果你不同意规则对特定语法模式强制的缩进,可以使用它作为放宽规则的方式。
  • "SwitchCase"(默认值:0)强制在 switch 语句中的 case 子句使用缩进级别
  • "VariableDeclarator"(默认值:1)强制 var 声明符的缩进级别;也可以接受一个对象来为 varletconst 声明定义单独的规则。它也可以是 "first",表示所有声明符应与第一个声明符对齐。
  • "outerIIFEBody"(默认值:1)强制执行文件级 IIFE 的缩进级别。也可以将其设置为 "off" 来禁用对文件级 IIFE 的检查。
  • "MemberExpression"(默认值:1)强制多行属性链的缩进级别。也可以设置为 "off" 来禁用对 MemberExpression 缩进的检查。
  • "FunctionDeclaration" 接受一个对象来定义函数声明的规则。
    • parameters(默认值:1)用于强制函数声明中参数的缩进级别。这可以是表示缩进级别的数字,也可以是字符串 "first",表示声明的所有参数必须与第一个参数对齐。也可以设置为 "off" 来禁用对函数声明参数的检查。
    • body(默认值:1)用于强制函数声明主体的缩进级别。
  • "FunctionExpression" 接受一个对象来定义函数表达式的规则。
    • parameters(默认值:1)用于强制函数表达式中参数的缩进级别。它可以是一个表示缩进级别的数字,也可以是字符串 "first",表示表达式的所有参数必须与第一个参数对齐。它也可以设置为 "off" 以禁用对函数表达式参数的检查。
    • body(默认值:1)用于强制函数表达式主体的缩进级别。
  • "StaticBlock" 接收一个对象来定义类静态块的规则。
    • body(默认值:1)用于强制规定类静态代码块主体的缩进级别。
  • "CallExpression" 接收一个对象来定义函数调用表达式的规则。
    • arguments(默认值:1)用于强制在调用表达式中对参数进行缩进。它可以是一个表示缩进级别的数字,也可以是字符串 "first",表示表达式的所有参数必须与第一个参数对齐。它也可以设置为 "off" 以禁用对 CallExpression 参数的检查。
  • "ArrayExpression"(默认值:1)强制数组中元素的缩进级别。它也可以设置为字符串 "first",表示数组中的所有元素应与第一个元素对齐。它也可以设置为 "off" 来禁用对数组元素的检查。
  • "ObjectExpression"(默认值:1)用于强制对象中属性的缩进级别。它可以设置为字符串 "first",表示对象中的所有属性应与第一个属性对齐。这也可以设置为 "off" 来禁用对象属性的检查。
  • "ImportDeclaration"(默认值:1)强制执行导入语句的缩进级别。它可以设置为字符串 "first",表示模块中的所有导入成员应与列表中的第一个成员对齐。它也可以设置为 "off",以禁用对导入模块成员的检查。
  • "flatTernaryExpressions": true(默认 false)对于嵌套在其他三元表达式中的三元表达式不需要缩进。
  • "offsetTernaryExpressions": true(默认 false)要求对三元表达式的值进行缩进。
  • "ignoreComments"(默认值:false)可以在评论不需要与前一行或下一行的节点对齐时使用。

缩进的级别表示所指定缩进的倍数。例如:

🌐 Level of indentation denotes the multiple of the indent specified. Example:

  • 将缩进设置为4个空格,并将VariableDeclarator设置为2将使多行变量声明缩进8个空格。
  • VariableDeclarator 设置为 2 并缩进 2 个空格将使多行变量声明缩进 4 个空格。
  • VariableDeclarator 设置为 {"var": 2, "let": 2, "const": 3} 时,缩进 2 个空格会将多行变量声明缩进 4 个空格用于 varlet,6 个空格用于 const 语句。
  • 设置为 2VariableDeclarator 标签缩进将使多行变量声明缩进 2 个制表符。
  • 将缩进设为 2 个空格,并将 SwitchCase 设置为 0 不会相对于 switch 语句缩进 case 子句。
  • SwitchCase 设置为 1 时,缩进 2 个空格将使 case 条款相对于 switch 语句缩进 2 个空格。
  • 使用 SwitchCase 设置为 2 时,缩进 2 个空格将使 case 条款相对于 switch 语句缩进 4 个空格。
  • 当将 SwitchCase 的制表符缩进设置为 2 时,case 条款相对于 switch 语句将缩进 2 个制表符。
  • MemberExpression 设置为 0 并缩进 2 个空格将使多行属性链缩进 0 个空格。
  • MemberExpression 设置为 1 并缩进 2 个空格,将会用 2 个空格缩进多行属性链。
  • MemberExpression 设置为 2 的 2 个空格缩进将使多行属性链缩进 4 个空格。
  • MemberExpression 设置为 0 并缩进 4 个空格将使多行属性链缩进 0 个空格。
  • MemberExpression 设置为 1 并缩进 4 个空格,将会用 4 个空格缩进多行属性链。
  • MemberExpression 设置为 2 并缩进 4 个空格将使多行属性链缩进 8 个空格。

tab

使用 "tab" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "tab" option:

在线运行
/*eslint indent: ["error", "tab"]*/

if (a) {
     b=c;
function foo(d) {
           e=f;
 }
}

使用 "tab" 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "tab" option:

::: 正确

/*eslint indent: ["error", "tab"]*/

if (a) {
	b=c;
	function foo(d) {
		e=f;
	}
}

:::

ignoredNodes

以下配置会忽略 ConditionalExpression(“三元表达式”)节点的缩进:

🌐 The following configuration ignores the indentation of ConditionalExpression (“ternary expression”) nodes:

使用 4, { "ignoredNodes": ["ConditionalExpression"] } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 4, { "ignoredNodes": ["ConditionalExpression"] } option:

在线运行
/*eslint indent: ["error", 4, { "ignoredNodes": ["ConditionalExpression"] }]*/

var a = foo
      ? bar
      : baz;

var a = foo
                ? bar
: baz;

以下配置忽略了 IIFE 主体中的缩进。

🌐 The following configuration ignores indentation in the body of IIFEs.

使用 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] } option:

在线运行
/*eslint indent: ["error", 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }]*/

(function() {

foo();
bar();

})();

所有 AST 节点类型可以在 ESTree 规范中找到。你可以使用带有 espree 解析器的 AST Explorer 来检查代码片段的 AST 树。

🌐 All AST node types can be found at ESTree specification. You can use AST Explorer with the espree parser to examine AST tree of a code snippet.

SwitchCase

使用 2, { "SwitchCase": 1 } 选项违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "SwitchCase": 1 } options:

在线运行
/*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/

switch(a){
case "a":
    break;
case "b":
    break;
}

使用 2, { "SwitchCase": 1 } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "SwitchCase": 1 } option:

在线运行
/*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/

switch(a){
  case "a":
    break;
  case "b":
    break;
}

VariableDeclarator

使用 2, { "VariableDeclarator": 1 } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "VariableDeclarator": 1 } options:

在线运行
/*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/

var a,
    b,
    c;
let d,
    e,
    f;
const g = 1,
    h = 2,
    i = 3;

使用 2, { "VariableDeclarator": 1 } 选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "VariableDeclarator": 1 } options:

在线运行
/*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/

var a,
  b,
  c;
let d,
  e,
  f;
const g = 1,
  h = 2,
  i = 3;

使用 2, { "VariableDeclarator": 2 } 选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "VariableDeclarator": 2 } options:

在线运行
/*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/

var a,
    b,
    c;
let d,
    e,
    f;
const g = 1,
    h = 2,
    i = 3;

使用 2, { "VariableDeclarator": "first" } 选项违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "VariableDeclarator": "first" } options:

在线运行
/*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/

var a,
  b,
  c;
let d,
  e,
  f;
const g = 1,
  h = 2,
  i = 3;

使用 2, { "VariableDeclarator": "first" } 选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "VariableDeclarator": "first" } options:

在线运行
/*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/

var a,
    b,
    c;
let d,
    e,
    f;
const g = 1,
      h = 2,
      i = 3;

使用 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } } 选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } } options:

在线运行
/*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/

var a,
    b,
    c;
let d,
    e,
    f;
const g = 1,
      h = 2,
      i = 3;

outerIIFEBody

此规则使用选项 2, { "outerIIFEBody": 0 }错误代码示例:

🌐 Examples of incorrect code for this rule with the options 2, { "outerIIFEBody": 0 }:

在线运行
/*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/

(function() {

  function foo(x) {
    return x + 1;
  }

})();

if (y) {
console.log('foo');
}

使用选项 2, { "outerIIFEBody": 0 } 的此规则的 正确 代码示例:

🌐 Examples of correct code for this rule with the options 2, { "outerIIFEBody": 0 }:

在线运行
/*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/

(function() {

function foo(x) {
  return x + 1;
}

})();

if (y) {
  console.log('foo');
}

使用选项 2, { "outerIIFEBody": "off" } 的此规则的 正确 代码示例:

🌐 Examples of correct code for this rule with the options 2, { "outerIIFEBody": "off" }:

在线运行
/*eslint indent: ["error", 2, { "outerIIFEBody": "off" }]*/

(function() {

function foo(x) {
  return x + 1;
}

})();

(function() {

  function foo(x) {
    return x + 1;
  }

})();

if (y) {
  console.log('foo');
}

MemberExpression

使用 2, { "MemberExpression": 1 } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "MemberExpression": 1 } options:

在线运行
/*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/

foo
.bar
.baz()

使用 2, { "MemberExpression": 1 } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "MemberExpression": 1 } option:

在线运行
/*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/

foo
  .bar
  .baz();

FunctionDeclaration

使用 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} } option:

在线运行
/*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/

function foo(bar,
  baz,
  qux) {
    qux();
}

使用 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} } option:

在线运行
/*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/

function foo(bar,
    baz,
    qux) {
  qux();
}

使用 2, { "FunctionDeclaration": {"parameters": "first"} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "FunctionDeclaration": {"parameters": "first"} } option:

在线运行
/*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/

function foo(bar, baz,
  qux, boop) {
  qux();
}

使用 2, { "FunctionDeclaration": {"parameters": "first"} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "FunctionDeclaration": {"parameters": "first"} } option:

在线运行
/*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/

function foo(bar, baz,
             qux, boop) {
  qux();
}

FunctionExpression

使用 2, { "FunctionExpression": {"body": 1, "parameters": 2} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "FunctionExpression": {"body": 1, "parameters": 2} } option:

在线运行
/*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/

var foo = function(bar,
  baz,
  qux) {
    qux();
}

使用 2, { "FunctionExpression": {"body": 1, "parameters": 2} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "FunctionExpression": {"body": 1, "parameters": 2} } option:

在线运行
/*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/

var foo = function(bar,
    baz,
    qux) {
  qux();
}

使用 2, { "FunctionExpression": {"parameters": "first"} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "FunctionExpression": {"parameters": "first"} } option:

在线运行
/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/

var foo = function(bar, baz,
  qux, boop) {
  qux();
}

使用 2, { "FunctionExpression": {"parameters": "first"} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "FunctionExpression": {"parameters": "first"} } option:

在线运行
/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/

var foo = function(bar, baz,
                   qux, boop) {
  qux();
}

StaticBlock

使用 2, { "StaticBlock": {"body": 1} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "StaticBlock": {"body": 1} } option:

在线运行
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/

class C {
  static {
      foo();
  }
}

使用 2, { "StaticBlock": {"body": 1} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "StaticBlock": {"body": 1} } option:

在线运行
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/

class C {
  static {
    foo();
  }
}

使用 2, { "StaticBlock": {"body": 2} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "StaticBlock": {"body": 2} } option:

在线运行
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/

class C {
  static {
    foo();
  }
}

使用 2, { "StaticBlock": {"body": 2} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "StaticBlock": {"body": 2} } option:

在线运行
/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/

class C {
  static {
      foo();
  }
}

CallExpression

使用 2, { "CallExpression": {"arguments": 1} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "CallExpression": {"arguments": 1} } option:

在线运行
/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/

foo(bar,
    baz,
      qux
);

使用 2, { "CallExpression": {"arguments": 1} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "CallExpression": {"arguments": 1} } option:

在线运行
/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/

foo(bar,
  baz,
  qux
);

使用 2, { "CallExpression": {"arguments": "first"} } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "CallExpression": {"arguments": "first"} } option:

在线运行
/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/

foo(bar, baz,
  baz, boop, beep);

使用 2, { "CallExpression": {"arguments": "first"} } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "CallExpression": {"arguments": "first"} } option:

在线运行
/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/

foo(bar, baz,
    baz, boop, beep);

ArrayExpression

使用 2, { "ArrayExpression": 1 } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "ArrayExpression": 1 } option:

在线运行
/*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/

var foo = [
    bar,
baz,
      qux
];

使用 2, { "ArrayExpression": 1 } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "ArrayExpression": 1 } option:

在线运行
/*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/

var foo = [
  bar,
  baz,
  qux
];

使用 2, { "ArrayExpression": "first" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "ArrayExpression": "first" } option:

在线运行
/*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/

var foo = [bar,
  baz,
  qux
];

使用 2, { "ArrayExpression": "first" } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "ArrayExpression": "first" } option:

在线运行
/*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/

var foo = [bar,
           baz,
           qux
];

ObjectExpression

使用 2, { "ObjectExpression": 1 } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "ObjectExpression": 1 } option:

在线运行
/*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/

var foo = {
    bar: 1,
baz: 2,
      qux: 3
};

使用 2, { "ObjectExpression": 1 } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "ObjectExpression": 1 } option:

在线运行
/*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/

var foo = {
  bar: 1,
  baz: 2,
  qux: 3
};

使用 2, { "ObjectExpression": "first" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "ObjectExpression": "first" } option:

在线运行
/*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/

var foo = { bar: 1,
  baz: 2 };

使用 2, { "ObjectExpression": "first" } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "ObjectExpression": "first" } option:

在线运行
/*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/

var foo = { bar: 1,
            baz: 2 };

ImportDeclaration

使用 4, { "ImportDeclaration": 1 } 选项(默认)时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 4, { "ImportDeclaration": 1 } option (the default):

在线运行
/*eslint indent: ["error", 4, { "ImportDeclaration": 1 }]*/

import { foo,
    bar,
    baz,
} from 'qux';
在线运行
/*eslint indent: ["error", 4, { "ImportDeclaration": 1 }]*/

import {
    foo,
    bar,
    baz,
} from 'qux';

使用 4, { "ImportDeclaration": "first" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 4, { "ImportDeclaration": "first" } option:

在线运行
/*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/

import { foo,
    bar,
    baz,
} from 'qux';

使用 4, { "ImportDeclaration": "first" } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 4, { "ImportDeclaration": "first" } option:

在线运行
/*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/

import { foo,
         bar,
         baz,
} from 'qux';

flatTernaryExpressions

使用默认 4, { "flatTernaryExpressions": false } 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default 4, { "flatTernaryExpressions": false } option:

在线运行
/*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/

var a =
    foo ? bar :
    baz ? qux :
    boop;

使用默认 4, { "flatTernaryExpressions": false } 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default 4, { "flatTernaryExpressions": false } option:

在线运行
/*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/

var a =
    foo ? bar :
        baz ? qux :
            boop;

使用 4, { "flatTernaryExpressions": true } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 4, { "flatTernaryExpressions": true } option:

在线运行
/*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/

var a =
    foo ? bar :
        baz ? qux :
            boop;

使用 4, { "flatTernaryExpressions": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 4, { "flatTernaryExpressions": true } option:

在线运行
/*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/

var a =
    foo ? bar :
    baz ? qux :
    boop;

offsetTernaryExpressions

使用默认 2, { "offsetTernaryExpressions": false } 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default 2, { "offsetTernaryExpressions": false } option:

在线运行
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/

condition
  ? () => {
      return true
    }
  : () => {
      false
    }

使用默认 2, { "offsetTernaryExpressions": false } 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default 2, { "offsetTernaryExpressions": false } option:

在线运行
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/

condition
  ? () => {
    return true
  }
  : condition2
    ? () => {
      return true
    }
    : () => {
      return false
    }

使用 2, { "offsetTernaryExpressions": true } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the 2, { "offsetTernaryExpressions": true } option:

在线运行
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/

condition
  ? () => {
    return true
  }
  : condition2
    ? () => {
      return true
    }
    : () => {
      return false
    }

使用 2, { "offsetTernaryExpressions": true } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the 2, { "offsetTernaryExpressions": true } option:

在线运行
/*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/

condition
  ? () => {
      return true
    }
  : condition2
    ? () => {
        return true
      }
    : () => {
        return false
      }

ignoreComments

使用 4, { "ignoreComments": true } 选项时,此规则的其他 正确 代码示例:

🌐 Examples of additional correct code for this rule with the 4, { "ignoreComments": true } option:

在线运行
/*eslint indent: ["error", 4, { "ignoreComments": true }] */

if (foo) {
    doSomething();

// comment intentionally de-indented
    doSomethingElse();
}

兼容性

🌐 Compatibility

版本

此规则是在 ESLint v0.14.0 中引入。

资源