跳至内容

@stylistic/js/

object-curly-newline

许多风格指南要求或禁止在对象花括号和其他标记内换行。

规则详情

此规则要求或禁止在对象字面量或解构赋值的 { 和其后的标记之间,以及 } 和其前的标记之间换行。

选项

此规则有一个字符串选项

  • "always" 要求在开括号后和闭括号前换行
  • "never" 禁止在开括号后和闭括号前换行

或一个对象选项

  • "multiline": true 如果属性内部或属性之间存在换行,则要求换行。否则,它禁止换行。
  • "minProperties" 如果属性数量至少为给定的整数,则要求换行。默认情况下,如果对象包含换行并且属性数量少于给定的整数,也会报告错误。但是,如果 consistent 选项设置为 true,则第二个行为将被禁用
  • "consistent": true (默认) 要求花括号要么都直接包含换行,要么都不包含。请注意,启用此选项也会改变 minProperties 选项的行为。(有关更多信息,请参见上面的 minProperties)

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

json
{
    "object-curly-newline": ["error", {
        "ObjectExpression": "always",
        "ObjectPattern": { "multiline": true },
        "ImportDeclaration": "never",
        "ExportDeclaration": { "multiline": true, "minProperties": 3 }
    }]
}
  • "ObjectExpression" 对象字面量的配置
  • "ObjectPattern" 解构赋值的对象模式配置
  • "ImportDeclaration" 配置用于命名导入
  • "ExportDeclaration" 配置用于命名导出

始终

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

js
/*eslint @stylistic/js/object-curly-newline: ["error", "always"]*/
/*eslint-env es6*/

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" 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", "always"]*/
/*eslint-env es6*/

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" 选项时,此规则的错误代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", "never"]*/
/*eslint-env es6*/

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" 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", "never"]*/
/*eslint-env es6*/

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 } 选项时,此规则的错误代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "multiline": true }]*/
/*eslint-env es6*/

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 } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "multiline": true }]*/
/*eslint-env es6*/

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 } 选项时,此规则的错误代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "minProperties": 2 }]*/
/*eslint-env es6*/

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 } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "minProperties": 2 }]*/
/*eslint-env es6*/

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": true } 选项时,此规则的错误代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "consistent": true }]*/
/*eslint-env es6*/

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 } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "consistent": true }]*/
/*eslint-env es6*/

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": "always", "ObjectPattern": "never" } 选项时,此规则的错误代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
/*eslint-env es6*/

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" } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
/*eslint-env es6*/

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": "always", "ExportDeclaration": "never" } 选项时,此规则的错误代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
/*eslint-env es6*/

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" } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
/*eslint-env es6*/

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';
正确

何时不使用它

如果您不想强制执行在打开和关闭括号后的一致换行符,那么可以安全地禁用此规则。

兼容性