1 | /**
|
---|
2 | * @fileoverview Rule to enforce placing object properties on separate lines.
|
---|
3 | * @author Vitor Balocco
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | //------------------------------------------------------------------------------
|
---|
10 | // Rule Definition
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 |
|
---|
13 | /** @type {import('../shared/types').Rule} */
|
---|
14 | module.exports = {
|
---|
15 | meta: {
|
---|
16 | deprecated: true,
|
---|
17 | replacedBy: [],
|
---|
18 | type: "layout",
|
---|
19 |
|
---|
20 | docs: {
|
---|
21 | description: "Enforce placing object properties on separate lines",
|
---|
22 | recommended: false,
|
---|
23 | url: "https://eslint.org/docs/latest/rules/object-property-newline"
|
---|
24 | },
|
---|
25 |
|
---|
26 | schema: [
|
---|
27 | {
|
---|
28 | type: "object",
|
---|
29 | properties: {
|
---|
30 | allowAllPropertiesOnSameLine: {
|
---|
31 | type: "boolean",
|
---|
32 | default: false
|
---|
33 | },
|
---|
34 | allowMultiplePropertiesPerLine: { // Deprecated
|
---|
35 | type: "boolean",
|
---|
36 | default: false
|
---|
37 | }
|
---|
38 | },
|
---|
39 | additionalProperties: false
|
---|
40 | }
|
---|
41 | ],
|
---|
42 |
|
---|
43 | fixable: "whitespace",
|
---|
44 |
|
---|
45 | messages: {
|
---|
46 | propertiesOnNewlineAll: "Object properties must go on a new line if they aren't all on the same line.",
|
---|
47 | propertiesOnNewline: "Object properties must go on a new line."
|
---|
48 | }
|
---|
49 | },
|
---|
50 |
|
---|
51 | create(context) {
|
---|
52 | const allowSameLine = context.options[0] && (
|
---|
53 | (context.options[0].allowAllPropertiesOnSameLine || context.options[0].allowMultiplePropertiesPerLine /* Deprecated */)
|
---|
54 | );
|
---|
55 | const messageId = allowSameLine
|
---|
56 | ? "propertiesOnNewlineAll"
|
---|
57 | : "propertiesOnNewline";
|
---|
58 |
|
---|
59 | const sourceCode = context.sourceCode;
|
---|
60 |
|
---|
61 | return {
|
---|
62 | ObjectExpression(node) {
|
---|
63 | if (allowSameLine) {
|
---|
64 | if (node.properties.length > 1) {
|
---|
65 | const firstTokenOfFirstProperty = sourceCode.getFirstToken(node.properties[0]);
|
---|
66 | const lastTokenOfLastProperty = sourceCode.getLastToken(node.properties[node.properties.length - 1]);
|
---|
67 |
|
---|
68 | if (firstTokenOfFirstProperty.loc.end.line === lastTokenOfLastProperty.loc.start.line) {
|
---|
69 |
|
---|
70 | // All keys and values are on the same line
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | for (let i = 1; i < node.properties.length; i++) {
|
---|
77 | const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.properties[i - 1]);
|
---|
78 | const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.properties[i]);
|
---|
79 |
|
---|
80 | if (lastTokenOfPreviousProperty.loc.end.line === firstTokenOfCurrentProperty.loc.start.line) {
|
---|
81 | context.report({
|
---|
82 | node,
|
---|
83 | loc: firstTokenOfCurrentProperty.loc,
|
---|
84 | messageId,
|
---|
85 | fix(fixer) {
|
---|
86 | const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty);
|
---|
87 | const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]];
|
---|
88 |
|
---|
89 | // Don't perform a fix if there are any comments between the comma and the next property.
|
---|
90 | if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) {
|
---|
91 | return null;
|
---|
92 | }
|
---|
93 |
|
---|
94 | return fixer.replaceTextRange(rangeAfterComma, "\n");
|
---|
95 | }
|
---|
96 | });
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 | };
|
---|
101 | }
|
---|
102 | };
|
---|