object-curly-newline
在大括号内强制执行一致的换行符(object-curly-newline)
在命令行上的--fix
选项可以自动修复一些被这条规则反映的问题。
许多样式指南要求或不允许对象大括号和其他令牌内部的换行符。
规则细节
此规则在对象文字或解构赋值的大括号内强制执行一致的换行符。
选项
这条规则有一个字符串选项:
"always"
需要大括号内的换行符
"never"
不允许大括号内的换行符
或者一个对象选项:
- 如果属性内或属性之间存在换行符,则
"multiline": true
(默认)需要换行符
- 如果属性的数量至少是给定的整数,则
"minProperties"
需要换行。默认情况下,如果对象包含换行符并且属性少于给定的整数,则也会报告错误。但是,如果consistent
选项设置为,则禁用第二种行为true
"consistent": true
要求无论是花括号还是两者都不直接包含换行符。请注意,启用此选项也会改变选项的行为minProperties
。(minProperties
更多信息见上面)
您可以为对象文字和解构赋值指定不同的选项:
{
"object-curly-newline": ["error", {
"ObjectExpression": "always",
"ObjectPattern": { "multiline": true }
}]
}
"ObjectExpression"
对象文字的配置
"ObjectPattern"
配置解构赋值的对象模式
always
此规则的错误
代码示例包含以下"always"
选项:
/*eslint 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 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
此规则的错误
代码示例包含以下"never"
选项:
/*eslint 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 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
此规则的默认代码错误
代码示例{ "multiline": true }
:
/*eslint 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 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
此规则的错误
代码示例包含以下{ "minProperties": 2 }
选项:
/*eslint 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 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
此规则的错误
代码示例包含以下{ "consistent": true }
选项:
/*eslint 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: 1,
bar: 2};
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 = function() {
dosomething(
}} = obj;
此规则的正确
代码示例包含以下{ "consistent": true }
选项:
/*eslint object-curly-newline: ["error", { "consistent": true }]*/
/*eslint-env es6*/
let a = {};
let b = {foo: 1};
let c = {
foo: 1
};
let d = {
foo: 1, bar: 2
};
let e = {
foo: 1,
bar: 2
};
let f = {foo: function() {dosomething(}};
let g = {
foo: function() {
dosomething(
}
};
let {} = obj;
let {h} = obj;
let {i, j} = obj;
let {
k, l
} = obj;
let {
m,
n
} = obj;
let {o = function() {dosomething(}} = obj;
let {
p = function() {
dosomething(
}
} = obj;
ObjectExpression and ObjectPattern
此规则的代码错误
代码示例{ "ObjectExpression": "always", "ObjectPattern": "never" }
如下:
/*eslint 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 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;
兼容性
JSCS
:requirePaddingNewLinesInObjects和disallowPaddingNewLinesInObjects当不使用 ITIF 你不希望强制在大括号一致换行符,那么它的安全禁用此 rule.Related 规则
- comma-spacing
- key-spacing
- object-curly-spacing
- object-property-newline
版本
该规则在 ESLint 2.12.0中引入。