indent-legacy

强制执行一致的缩进

🔧 Fixable

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

Important

This rule was deprecated in ESLint v4.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

ESLint 4.0.0 引入了对 indent 规则的重写,它现在报告的错误比以前版本中的更多。为了简化迁移到 4.0.0 的过程,引入了 indent-legacy 规则作为 ESLint 3.x 中 indent 规则的快照。如果升级到 4.0.0 后构建失败,你可以禁用 indent 并启用 indent-legacy 作为快速修复。最后,你应该切换回 indent 规则以在未来版本中获得错误修复和改进。

¥ESLint 4.0.0 introduced a rewrite of the indent rule, which now reports more errors than it did in previous versions. To ease the process of migrating to 4.0.0, the indent-legacy rule was introduced as a snapshot of the indent rule from ESLint 3.x. If your build is failing after the upgrade to 4.0.0, you can disable indent and enable indent-legacy as a quick fix. Eventually, you should switch back to the indent rule to get bugfixes and improvements in future versions.


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

¥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-legacy": ["error", 2]
}

或者对于标签缩进:

¥Or for tabbed indentation:

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

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

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

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

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

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

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

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

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

此规则有一个对象选项:

¥This rule has an object option:

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

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

  • "VariableDeclarator"(默认值:1)强制 var 声明符的缩进级别;也可以用一个对象来为 varletconst 声明定义单独的规则。

    ¥"VariableDeclarator" (default: 1) enforces indentation level for var declarators; can also take an object to define separate rules for var, let and const declarations.

  • "outerIIFEBody"(默认值:1)强制文件级 IIFE 的缩进级别。

    ¥"outerIIFEBody" (default: 1) enforces indentation level for file-level IIFEs.

  • "MemberExpression"(默认关闭)强制多行属性链的缩进级别(变量声明和赋值除外)

    ¥"MemberExpression" (off by default) enforces indentation level for multi-line property chains (except in variable declarations and assignments)

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

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

    • parameters(默认关闭)强制函数声明中参数的缩进级别。这可以是表示缩进级别的数字,也可以是表示声明的所有参数必须与第一个参数对齐的字符串 "first"

      ¥parameters (off by default) 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.

    • 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(默认关闭)强制函数表达式中参数的缩进级别。这可以是表示缩进级别的数字,也可以是表示表达式的所有参数必须与第一个参数对齐的字符串 "first"

      ¥parameters (off by default) 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.

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

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

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

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

    • arguments(默认关闭)强制调用表达式中参数的缩进级别。这可以是表示缩进级别的数字,也可以是表示表达式的所有参数必须与第一个参数对齐的字符串 "first"

      ¥arguments (off by default) 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.

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

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

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

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

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

¥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-legacy: ["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-legacy: ["error", "tab"]*/

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

SwitchCase

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

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

在线运行
/*eslint indent-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["error", 2, { "VariableDeclarator": 2 }]*/

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-legacy: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/

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

outerIIFEBody

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

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

在线运行
/*eslint indent-legacy: ["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-legacy: ["error", 2, { "outerIIFEBody": 0 }]*/

(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-legacy: ["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-legacy: ["error", 2, { "MemberExpression": 1 }]*/

foo
  .bar
  .baz();

// Any indentation is permitted in variable declarations and assignments.
var bip = aardvark.badger
                  .coyote;

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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/

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

CallExpression

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

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

在线运行
/*eslint indent-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["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-legacy: ["error", 2, {"ObjectExpression": "first"}]*/

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

兼容性

¥Compatibility

版本

此规则是在 ESLint v4.0.0-alpha.0 中引入。

资源