object-curly-newline
多くのスタイルガイドでは、オブジェクトの中括弧や他のトークンの内側に改行を入れるか、入れないかを規定しています。
ルールの詳細
このルールは、オブジェクトリテラルまたは分割代入の`{`とその次のトークンの間、および`}`とその前のトークンの間に改行を入れるか、入れないかを規定します。
オプション
このルールには、文字列オプションがあります。
"always"
は、開始中括弧の後と閉じ中括弧の前に改行を必須とします。"never"
は、開始中括弧の後と閉じ中括弧の前に改行を禁止します。
またはオブジェクトオプションがあります。
"multiline": true
は、プロパティの内側またはプロパティの間に改行がある場合に改行を必須とします。それ以外の場合は、改行を禁止します。"minProperties"
は、プロパティの数が指定された整数以上の場合に改行を必須とします。デフォルトでは、オブジェクトに改行が含まれていて、指定された整数よりもプロパティが少ない場合にもエラーが報告されます。ただし、`consistent` オプションが `true` に設定されている場合、2 番目の動作は無効になります。"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 (常に)
オプション "always"
を使用した場合のこのルールの**誤った**コードの例
/*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"
を使用した場合のこのルールの**正しい**コードの例
/*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 (しない)
オプション "never"
を使用した場合のこのルールの**誤った**コードの例
/*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"
を使用した場合のこのルールの**正しい**コードの例
/*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 (複数行)
オプション { "multiline": true }
を使用した場合のこのルールの**誤った**コードの例
/*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 }
を使用した場合のこのルールの**正しい**コードの例
/*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 (最小プロパティ数)
オプション { "minProperties": 2 }
を使用した場合のこのルールの**誤った**コードの例
/*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 }
を使用した場合のこのルールの**正しい**コードの例
/*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 (一貫性)
デフォルトのオプション { "consistent": true }
を使用した場合のこのルールの**誤った**コードの例
/*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 }
を使用した場合のこのルールの**正しい**コードの例
/*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 と ObjectPattern
オプション { "ObjectExpression": "always", "ObjectPattern": "never" }
を使用した場合のこのルールの**誤った**コードの例
/*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" }
を使用した場合のこのルールの**正しい**コードの例
/*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 と ExportDeclaration
オプション { "ImportDeclaration": "always", "ExportDeclaration": "never" }
を使用した場合のこのルールの**誤った**コードの例
/*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" }
を使用した場合のこのルールの**正しい**コードの例
/*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';
使用しない場合
開始中括弧の後と閉じ中括弧の前に一貫した改行を強制したくない場合は、このルールを無効にしても問題ありません。
互換性
- **JSCS**: requirePaddingNewLinesInObjects
- **JSCS**: disallowPaddingNewLinesInObjects