object-curly-newline

在打开大括号之后和关闭大括号之前强制执行一致的换行符

🔧 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.

许多风格指南要求或不允许在对象大括号和其他标记内换行。

¥A number of style guides require or disallow line breaks inside of object braces and other tokens.

规则详情

¥Rule Details

此规则要求或不允许在 { 与其后标记之间以及 } 与其前一个对象字面量或解构赋值标记之间换行。

¥This rule requires or disallows a line break between { and its following token, and between } and its preceding token of object literals or destructuring assignments.

选项

¥Options

此规则有一个字符串选项:

¥This rule has either a string option:

  • "always" 在打开大括号之后和关闭大括号之前需要换行

    ¥"always" requires line breaks after opening and before closing braces

  • "never" 不允许在打开大括号之后和关闭大括号之前换行

    ¥"never" disallows line breaks after opening and before closing braces

或对象选项:

¥Or an object option:

  • 如果属性内部或属性之间有换行符,则 "multiline": true 需要换行符。否则,它不允许换行。

    ¥"multiline": true requires line breaks if there are line breaks inside properties or between properties. Otherwise, it disallows line breaks.

  • 如果属性数量至少为给定整数,则 "minProperties" 需要换行符。默认情况下,如果对象包含换行符并且属性少于给定整数,也会报告错误。但是,如果 consistent 选项设置为 true,则禁用第二种行为

    ¥"minProperties" requires line breaks if the number of properties is at least the given integer. By default, an error will also be reported if an object contains linebreaks and has fewer properties than the given integer. However, the second behavior is disabled if the consistent option is set to true

  • "consistent": true(默认)要求两个大括号直接括起换行符,或者两者都不直接括起来。请注意,启用此选项也会更改 minProperties 选项的行为。(有关更多信息,请参见上面的 minProperties

    ¥"consistent": true (default) requires that either both curly braces, or neither, directly enclose newlines. Note that enabling this option will also change the behavior of the minProperties option. (See minProperties above for more information)

你可以为对象字面、解构赋值以及命名导入和导出指定不同的选项:

¥You can specify different options for object literals, destructuring assignments, and named imports and exports:

{
    "object-curly-newline": ["error", {
        "ObjectExpression": "always",
        "ObjectPattern": { "multiline": true },
        "ImportDeclaration": "never",
        "ExportDeclaration": { "multiline": true, "minProperties": 3 }
    }]
}
  • 对象字面量的 "ObjectExpression" 配置

    ¥"ObjectExpression" configuration for object literals

  • 解构赋值的对象模式的 "ObjectPattern" 配置

    ¥"ObjectPattern" configuration for object patterns of destructuring assignments

  • 命名导入的 "ImportDeclaration" 配置

    ¥"ImportDeclaration" configuration for named imports

  • 命名导出的 "ExportDeclaration" 配置

    ¥"ExportDeclaration" configuration for named exports

always

使用 "always" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always" option:

在线运行
/*eslint object-curly-newline: ["error", "always"]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {foo() {
    dosomething();
}};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

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

¥Examples of correct code for this rule with the "always" option:

在线运行
/*eslint object-curly-newline: ["error", "always"]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

never

使用 "never" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint object-curly-newline: ["error", "never"]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

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

¥Examples of correct code for this rule with the "never" option:

在线运行
/*eslint object-curly-newline: ["error", "never"]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {foo: function() {
    dosomething();
}};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

multiline

使用 { "multiline": true } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "multiline": true } option:

在线运行
/*eslint object-curly-newline: ["error", { "multiline": true }]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {foo: 1,
    bar: 2};
let e = {foo: function() {
    dosomething();
}};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

使用 { "multiline": true } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "multiline": true } option:

在线运行
/*eslint object-curly-newline: ["error", { "multiline": true }]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

minProperties

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

¥Examples of incorrect code for this rule with the { "minProperties": 2 } option:

在线运行
/*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/

let a = {
};
let b = {
    foo: 1
};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {
    foo: function() {
        dosomething();
    }
};

let {
} = obj;
let {
    f
} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

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

¥Examples of correct code for this rule with the { "minProperties": 2 } option:

在线运行
/*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/

let a = {};
let b = {foo: 1};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {foo: function() {
    dosomething();
}};

let {} = obj;
let {f} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {k = function() {
    dosomething();
}} = obj;

consistent

使用默认 { "consistent": true } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the default { "consistent": true } option:

在线运行
/*eslint object-curly-newline: ["error", { "consistent": true }]*/

let a = {foo: 1
};
let b = {
    foo: 1};
let c = {foo: 1, bar: 2
};
let d = {
    foo: 1, bar: 2};
let e = {foo: function() {
    dosomething();
    }
};
let f = {
    foo: function() {
    dosomething();}};

let {g
} = obj;
let {
    h} = obj;
let {i, j
} = obj;
let {k, l
} = obj;
let {
    m, n} = obj;
let {
    o, p} = obj;
let {q = function() {
    dosomething();
    }
} = obj;
let {
    r = function() {
        dosomething();
    }} = obj;

使用默认 { "consistent": true } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the default { "consistent": true } option:

在线运行
/*eslint object-curly-newline: ["error", { "consistent": true }]*/

let empty1 = {};
let empty2 = {
};
let a = {foo: 1};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {foo: function() {dosomething();}};
let f = {
    foo: function() {
        dosomething();
    }
};

let {} = obj;
let {
} = obj;
let {g} = obj;
let {
    h
} = obj;
let {i, j} = obj;
let {
    k, l
} = obj;
let {m,
    n} = obj;
let {
    o,
    p
} = obj;
let {q = function() {dosomething();}} = obj;
let {
    r = function() {
        dosomething();
    }
} = obj;

ObjectExpression 和 ObjectPattern

¥ObjectExpression and ObjectPattern

使用 { "ObjectExpression": "always", "ObjectPattern": "never" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "ObjectExpression": "always", "ObjectPattern": "never" } options:

在线运行
/*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2};
let e = {foo: function() {
    dosomething();
}};

let {
} = obj;
let {
    f
} = obj;
let {
    g, h
} = obj;
let {
    i,
    j
} = obj;
let {
    k = function() {
        dosomething();
    }
} = obj;

使用 { "ObjectExpression": "always", "ObjectPattern": "never" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "ObjectExpression": "always", "ObjectPattern": "never" } options:

在线运行
/*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/

let a = {
};
let b = {
    foo: 1
};
let c = {
    foo: 1, bar: 2
};
let d = {
    foo: 1,
    bar: 2
};
let e = {
    foo: function() {
        dosomething();
    }
};

let {} = obj;
let {f} = obj;
let {g, h} = obj;
let {i,
    j} = obj;
let {k = function() {
    dosomething();
}} = obj;

ImportDeclaration 和 ExportDeclaration

¥ImportDeclaration and ExportDeclaration

使用 { "ImportDeclaration": "always", "ExportDeclaration": "never" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "ImportDeclaration": "always", "ExportDeclaration": "never" } options:

在线运行
/*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/

import {foo, bar} from 'foo-bar';
import {foo as f, baz} from 'foo-bar';
import {qux,
    foobar} from 'foo-bar';

export {
   foo,
   bar
};
export {
   foo as f,
   baz
} from 'foo-bar';

使用 { "ImportDeclaration": "always", "ExportDeclaration": "never" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "ImportDeclaration": "always", "ExportDeclaration": "never" } options:

在线运行
/*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/

import {
    foo,
    bar
} from 'foo-bar';
import {
    baz, qux
} from 'foo-bar';
import {
    foo as f,
    foobar
} from 'foo-bar';

export { foo, bar } from 'foo-bar';
export { foo as f, baz } 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.

兼容性

¥Compatibility

版本

此规则是在 ESLint v2.12.0 中引入。

资源