2 * @fileoverview Rule to enforce placing object properties on separate lines.
3 * @author Vitor Balocco
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description
: "enforce placing object properties on separate lines",
18 category
: "Stylistic Issues",
20 url
: "https://eslint.org/docs/rules/object-property-newline"
27 allowAllPropertiesOnSameLine
: {
31 allowMultiplePropertiesPerLine
: { // Deprecated
36 additionalProperties
: false
40 fixable
: "whitespace",
43 propertiesOnNewlineAll
: "Object properties must go on a new line if they aren't all on the same line.",
44 propertiesOnNewline
: "Object properties must go on a new line."
49 const allowSameLine
= context
.options
[0] && (
50 (context
.options
[0].allowAllPropertiesOnSameLine
|| context
.options
[0].allowMultiplePropertiesPerLine
/* Deprecated */)
52 const messageId
= allowSameLine
53 ? "propertiesOnNewlineAll"
54 : "propertiesOnNewline";
56 const sourceCode
= context
.getSourceCode();
59 ObjectExpression(node
) {
61 if (node
.properties
.length
> 1) {
62 const firstTokenOfFirstProperty
= sourceCode
.getFirstToken(node
.properties
[0]);
63 const lastTokenOfLastProperty
= sourceCode
.getLastToken(node
.properties
[node
.properties
.length
- 1]);
65 if (firstTokenOfFirstProperty
.loc
.end
.line
=== lastTokenOfLastProperty
.loc
.start
.line
) {
67 // All keys and values are on the same line
73 for (let i
= 1; i
< node
.properties
.length
; i
++) {
74 const lastTokenOfPreviousProperty
= sourceCode
.getLastToken(node
.properties
[i
- 1]);
75 const firstTokenOfCurrentProperty
= sourceCode
.getFirstToken(node
.properties
[i
]);
77 if (lastTokenOfPreviousProperty
.loc
.end
.line
=== firstTokenOfCurrentProperty
.loc
.start
.line
) {
80 loc
: firstTokenOfCurrentProperty
.loc
.start
,
83 const comma
= sourceCode
.getTokenBefore(firstTokenOfCurrentProperty
);
84 const rangeAfterComma
= [comma
.range
[1], firstTokenOfCurrentProperty
.range
[0]];
86 // Don't perform a fix if there are any comments between the comma and the next property.
87 if (sourceCode
.text
.slice(rangeAfterComma
[0], rangeAfterComma
[1]).trim()) {
91 return fixer
.replaceTextRange(rangeAfterComma
, "\n");