indent

强制执行一致的缩进

🔧 Fixable

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

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

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

¥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:

  • 两个空格,不是更长也没有制表符:谷歌、npm、Node.js、惯用语、Felix

    ¥Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix

  • 选项卡:jQuery

    ¥Tabs: jQuery

  • 四个空格:Crockford

    ¥Four spaces: Crockford

规则详情

¥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 节点的缩进检查。这接受一个 选择器 的数组。如果 AST 节点与任何选择器匹配,则作为该节点直接子节点的标记的缩进将被忽略。如果你不同意它为特定语法模式强制执行的缩进,这可以用作放宽规则的应急方案。

    ¥"ignoredNodes" can be used to disable indentation checking for any AST node. This accepts an array of 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.

  • "SwitchCase"(默认值:0)强制 switch 语句中 case 子句的缩进级别

    ¥"SwitchCase" (default: 0) enforces indentation level for case clauses in switch statements

  • "VariableDeclarator"(默认值:1)强制 var 声明符的缩进级别;也可以用一个对象来为 varletconst 声明定义单独的规则。也可以是 "first",表示所有的声明符都应该与第一个声明符对齐。

    ¥"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"(默认值:1)强制文件级 IIFE 的缩进级别。这也可以设置为 "off" 以禁用对文件级 IIFE 的检查。

    ¥"outerIIFEBody" (default: 1) enforces indentation level for file-level IIFEs. This can also be set to "off" to disable checking for file-level IIFEs.

  • "MemberExpression"(默认值:1)强制多行属性链的缩进级别。这也可以设置为 "off" 以禁用对 MemberExpression 缩进的检查。

    ¥"MemberExpression" (default: 1) enforces indentation level for multi-line property chains. This can also be set to "off" to disable checking for MemberExpression indentation.

  • "FunctionDeclaration" 采用一个对象来定义函数声明的规则。

    ¥"FunctionDeclaration" takes an object to define rules for function declarations.

    • parameters(默认值:1)强制函数声明中参数的缩进级别。这可以是表示缩进级别的数字,也可以是表示声明的所有参数必须与第一个参数对齐的字符串 "first"。这也可以设置为 "off" 以禁用对 FunctionDeclaration 参数的检查。

      ¥parameters (default: 1) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the declaration must be aligned with the first parameter. This can also be set to "off" to disable checking for FunctionDeclaration parameters.

    • body(默认值:1)强制函数声明主体的缩进级别。

      ¥body (default: 1) enforces indentation level for the body of a function declaration.

  • "FunctionExpression" 使用一个对象来定义函数表达式的规则。

    ¥"FunctionExpression" takes an object to define rules for function expressions.

    • parameters(默认值:1)强制函数表达式中参数的缩进级别。这可以是表示缩进级别的数字,也可以是表示表达式的所有参数必须与第一个参数对齐的字符串 "first"。这也可以设置为 "off" 以禁用对 FunctionExpression 参数的检查。

      ¥parameters (default: 1) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string "first" indicating that all parameters of the expression must be aligned with the first parameter. This can also be set to "off" to disable checking for FunctionExpression parameters.

    • body(默认值:1)强制函数表达式主体的缩进级别。

      ¥body (default: 1) enforces indentation level for the body of a function expression.

  • "StaticBlock" 使用一个对象来定义类静态块的规则。

    ¥"StaticBlock" takes an object to define rules for class static blocks.

    • body(默认值:1)强制类静态块主体的缩进级别。

      ¥body (default: 1) enforces indentation level for the body of a class static block.

  • "CallExpression" 采用一个对象来定义函数调用表达式的规则。

    ¥"CallExpression" takes an object to define rules for function call expressions.

    • arguments(默认值:1)强制调用表达式中参数的缩进级别。这可以是表示缩进级别的数字,也可以是表示表达式的所有参数必须与第一个参数对齐的字符串 "first"。这也可以设置为 "off" 以禁用对 CallExpression 参数的检查。

      ¥arguments (default: 1) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string "first" indicating that all arguments of the expression must be aligned with the first argument. This can also be set to "off" to disable checking for CallExpression arguments.

  • "ArrayExpression"(默认值:1)强制数组中元素的缩进级别。也可以设置为字符串 "first",表示数组中的所有元素都应该与第一个元素对齐。这也可以设置为 "off" 以禁用对数组元素的检查。

    ¥"ArrayExpression" (default: 1) enforces indentation level for elements in arrays. It can also be set to the string "first", indicating that all the elements in the array should be aligned with the first element. This can also be set to "off" to disable checking for array elements.

  • "ObjectExpression"(默认值:1)强制对象中属性的缩进级别。可以设置为字符串 "first",表示对象中的所有属性都应该与第一个属性对齐。这也可以设置为 "off" 以禁用检查对象属性。

    ¥"ObjectExpression" (default: 1) enforces indentation level for properties in objects. It can be set to the string "first", indicating that all properties in the object should be aligned with the first property. This can also be set to "off" to disable checking for object properties.

  • "ImportDeclaration"(默认值:1)强制导入语句的缩进级别。它可以设置为字符串 "first",表示从一个模块中导入的所有成员都应该与列表中的第一个成员对齐。这也可以设置为 "off" 以禁用检查导入的模块成员。

    ¥"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)要求嵌套在其他三元表达式中的三元表达式不需要缩进。

    ¥"flatTernaryExpressions": true (false by default) requires no indentation for ternary expressions which are nested in other ternary expressions.

  • "offsetTernaryExpressions": true(默认为 false)要求三元表达式的值缩进。

    ¥"offsetTernaryExpressions": true (false by default) requires indentation for values of ternary expressions.

  • 当注释不需要与上一行或下一行的节点对齐时,可以使用 "ignoreComments"(默认值: false)。

    ¥"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:

  • VariableDeclarator 设置为 2 缩进 4 个空格将使多行变量声明缩进 8 个空格。

    ¥Indent of 4 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 8 spaces.

  • 缩进 2 个空格并将 VariableDeclarator 设置为 2 将使多行变量声明缩进 4 个空格。

    ¥Indent of 2 spaces with VariableDeclarator set to 2 will indent the multi-line variable declarations with 4 spaces.

  • VariableDeclarator 设置为 {"var": 2, "let": 2, "const": 3} 缩进 2 个空格将缩进多行变量声明,其中 varlet 缩进 4 个空格,const 语句缩进 6 个空格。

    ¥Indent of 2 spaces with VariableDeclarator set to {"var": 2, "let": 2, "const": 3} will indent the multi-line variable declarations with 4 spaces for var and let, 6 spaces for const statements.

  • VariableDeclarator 设置为 2 的制表符缩进将使用 2 个制表符缩进多行变量声明。

    ¥Indent of tab with VariableDeclarator set to 2 will indent the multi-line variable declarations with 2 tabs.

  • 缩进 2 个空格并将 SwitchCase 设置为 0 不会缩进 case 子句相对于 switch 语句。

    ¥Indent of 2 spaces with SwitchCase set to 0 will not indent case clauses with respect to switch statements.

  • 缩进 2 个空格并将 SwitchCase 设置为 1 将缩进 case 子句,其中 2 个空格相对于 switch 语句。

    ¥Indent of 2 spaces with SwitchCase set to 1 will indent case clauses with 2 spaces with respect to switch statements.

  • 缩进 2 个空格并将 SwitchCase 设置为 2 将相对于 switch 语句缩进 4 个空格的 case 子句。

    ¥Indent of 2 spaces with SwitchCase set to 2 will indent case clauses with 4 spaces with respect to switch statements.

  • SwitchCase 设置为 2 的制表符缩进将相对于 switch 语句缩进具有 2 个制表符的 case 子句。

    ¥Indent of tab with SwitchCase set to 2 will indent case clauses with 2 tabs with respect to switch statements.

  • 缩进 2 个空格并将 MemberExpression 设置为 0 将缩进具有 0 个空格的多行属性链。

    ¥Indent of 2 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.

  • 缩进 2 个空格并将 MemberExpression 设置为 1 将缩进具有 2 个空格的多行属性链。

    ¥Indent of 2 spaces with MemberExpression set to 1 will indent the multi-line property chains with 2 spaces.

  • 缩进 2 个空格并将 MemberExpression 设置为 2 将缩进具有 4 个空格的多行属性链。

    ¥Indent of 2 spaces with MemberExpression set to 2 will indent the multi-line property chains with 4 spaces.

  • 缩进 4 个空格并将 MemberExpression 设置为 0 将缩进 0 个空格的多行属性链。

    ¥Indent of 4 spaces with MemberExpression set to 0 will indent the multi-line property chains with 0 spaces.

  • MemberExpression 设置为 1 缩进 4 个空格将缩进 4 个空格的多行属性链。

    ¥Indent of 4 spaces with MemberExpression set to 1 will indent the multi-line property chains with 4 spaces.

  • 缩进 4 个空格并将 MemberExpression 设置为 2 将缩进具有 8 个空格的多行属性链。

    ¥Indent of 4 spaces with MemberExpression set to 2 will indent the multi-line property chains with 8 spaces.

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 规范中找到。你可以将 AST Explorer 与 espree 解析器一起使用来检查代码片段的 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 中引入。

资源