object-curly-newline
许多风格指南要求或禁止在对象大括号和其他标记内换行。
规则详细信息
此规则要求或禁止在{
与其后的标记之间以及在}
与其之前的标记之间换行,这些标记是对象字面量或解构赋值。
选项
此规则具有字符串选项
"always"
要求在打开大括号之后和关闭大括号之前换行"never"
禁止在打开大括号之后和关闭大括号之前换行
或对象选项
"multiline": true
如果属性内部或属性之间存在换行符,则要求换行符。否则,它禁止换行符。"minProperties"
如果属性数量至少为给定的整数,则要求换行符。默认情况下,如果对象包含换行符并且属性数量少于给定的整数,也会报告错误。但是,如果consistent
选项设置为true
,则第二个行为将被禁用"consistent": true
(默认) 要求要么两个大括号都直接包含换行符,要么都不包含。请注意,启用此选项也会改变minProperties
选项的行为。(有关更多信息,请参见上面的minProperties
)
您可以为对象字面量、解构赋值以及命名导入和导出指定不同的选项。
{
"object-curly-newline": ["error", {
"ObjectExpression": "always",
"ObjectPattern": { "multiline": true },
"ImportDeclaration": "never",
"ExportDeclaration": { "multiline": true, "minProperties": 3 }
}]
}
"ObjectExpression"
配置用于对象字面量。"ObjectPattern"
配置用于解构赋值中的对象模式。"ImportDeclaration"
配置用于命名导入。"ExportDeclaration"
配置用于命名导出。
始终
使用 "always"
选项时,此规则的错误代码示例。
/*eslint @stylistic/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"
选项时,此规则的正确代码示例。
/*eslint @stylistic/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"
选项时,此规则的错误代码示例。
/*eslint @stylistic/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"
选项时,此规则的正确代码示例。
/*eslint @stylistic/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 }
选项时,此规则的错误代码示例。
/*eslint @stylistic/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 }
选项时,此规则的正确代码示例。
/*eslint @stylistic/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 }
选项时,此规则的错误代码示例。
/*eslint @stylistic/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 }
选项时,此规则的正确代码示例。
/*eslint @stylistic/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 }
选项时,此规则的错误代码示例。
/*eslint @stylistic/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 }
选项时,此规则的正确代码示例。
/*eslint @stylistic/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 和 ObjectPattern
使用 { "ObjectExpression": "always", "ObjectPattern": "never" }
选项时,此规则的错误代码示例。
/*eslint @stylistic/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" }
选项时,此规则的正确代码示例。
/*eslint @stylistic/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 和 ExportDeclaration
使用 { "ImportDeclaration": "always", "ExportDeclaration": "never" }
选项时,此规则的错误代码示例。
/*eslint @stylistic/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" }
选项时,此规则的正确代码示例。
/*eslint @stylistic/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';
何时不使用它
如果您不想强制执行在打开和关闭括号后的一致换行符,那么可以安全地禁用此规则。