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
44 const allowSameLine = context.options[0] && (
45 (context.options[0].allowAllPropertiesOnSameLine || context.options[0].allowMultiplePropertiesPerLine /* Deprecated */)
47 const errorMessage = allowSameLine
48 ? "Object properties must go on a new line if they aren't all on the same line."
49 : "Object properties must go on a new line.";
51 const sourceCode = context.getSourceCode();
54 ObjectExpression(node) {
56 if (node.properties.length > 1) {
57 const firstTokenOfFirstProperty = sourceCode.getFirstToken(node.properties[0]);
58 const lastTokenOfLastProperty = sourceCode.getLastToken(node.properties[node.properties.length - 1]);
60 if (firstTokenOfFirstProperty.loc.end.line === lastTokenOfLastProperty.loc.start.line) {
62 // All keys and values are on the same line
68 for (let i = 1; i < node.properties.length; i++) {
69 const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.properties[i - 1]);
70 const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.properties[i]);
72 if (lastTokenOfPreviousProperty.loc.end.line === firstTokenOfCurrentProperty.loc.start.line) {
75 loc: firstTokenOfCurrentProperty.loc.start,
76 message: errorMessage,
78 const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty);
79 const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]];
81 // Don't perform a fix if there are any comments between the comma and the next property.
82 if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) {
86 return fixer.replaceTextRange(rangeAfterComma, "\n");