1 | /**
|
---|
2 | * @fileoverview Enforce newlines between operands of ternary expressions
|
---|
3 | * @author Kai Cataldo
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | const astUtils = require("./utils/ast-utils");
|
---|
10 |
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 | // Rule Definition
|
---|
13 | //------------------------------------------------------------------------------
|
---|
14 |
|
---|
15 | /** @type {import('../shared/types').Rule} */
|
---|
16 | module.exports = {
|
---|
17 | meta: {
|
---|
18 | deprecated: true,
|
---|
19 | replacedBy: [],
|
---|
20 | type: "layout",
|
---|
21 |
|
---|
22 | docs: {
|
---|
23 | description: "Enforce newlines between operands of ternary expressions",
|
---|
24 | recommended: false,
|
---|
25 | url: "https://eslint.org/docs/latest/rules/multiline-ternary"
|
---|
26 | },
|
---|
27 |
|
---|
28 | schema: [
|
---|
29 | {
|
---|
30 | enum: ["always", "always-multiline", "never"]
|
---|
31 | }
|
---|
32 | ],
|
---|
33 |
|
---|
34 | messages: {
|
---|
35 | expectedTestCons: "Expected newline between test and consequent of ternary expression.",
|
---|
36 | expectedConsAlt: "Expected newline between consequent and alternate of ternary expression.",
|
---|
37 | unexpectedTestCons: "Unexpected newline between test and consequent of ternary expression.",
|
---|
38 | unexpectedConsAlt: "Unexpected newline between consequent and alternate of ternary expression."
|
---|
39 | },
|
---|
40 |
|
---|
41 | fixable: "whitespace"
|
---|
42 | },
|
---|
43 |
|
---|
44 | create(context) {
|
---|
45 | const sourceCode = context.sourceCode;
|
---|
46 | const option = context.options[0];
|
---|
47 | const multiline = option !== "never";
|
---|
48 | const allowSingleLine = option === "always-multiline";
|
---|
49 |
|
---|
50 | //--------------------------------------------------------------------------
|
---|
51 | // Public
|
---|
52 | //--------------------------------------------------------------------------
|
---|
53 |
|
---|
54 | return {
|
---|
55 | ConditionalExpression(node) {
|
---|
56 | const questionToken = sourceCode.getTokenAfter(node.test, astUtils.isNotClosingParenToken);
|
---|
57 | const colonToken = sourceCode.getTokenAfter(node.consequent, astUtils.isNotClosingParenToken);
|
---|
58 |
|
---|
59 | const firstTokenOfTest = sourceCode.getFirstToken(node);
|
---|
60 | const lastTokenOfTest = sourceCode.getTokenBefore(questionToken);
|
---|
61 | const firstTokenOfConsequent = sourceCode.getTokenAfter(questionToken);
|
---|
62 | const lastTokenOfConsequent = sourceCode.getTokenBefore(colonToken);
|
---|
63 | const firstTokenOfAlternate = sourceCode.getTokenAfter(colonToken);
|
---|
64 |
|
---|
65 | const areTestAndConsequentOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfTest, firstTokenOfConsequent);
|
---|
66 | const areConsequentAndAlternateOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfConsequent, firstTokenOfAlternate);
|
---|
67 |
|
---|
68 | const hasComments = !!sourceCode.getCommentsInside(node).length;
|
---|
69 |
|
---|
70 | if (!multiline) {
|
---|
71 | if (!areTestAndConsequentOnSameLine) {
|
---|
72 | context.report({
|
---|
73 | node: node.test,
|
---|
74 | loc: {
|
---|
75 | start: firstTokenOfTest.loc.start,
|
---|
76 | end: lastTokenOfTest.loc.end
|
---|
77 | },
|
---|
78 | messageId: "unexpectedTestCons",
|
---|
79 | fix(fixer) {
|
---|
80 | if (hasComments) {
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 | const fixers = [];
|
---|
84 | const areTestAndQuestionOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfTest, questionToken);
|
---|
85 | const areQuestionAndConsOnSameLine = astUtils.isTokenOnSameLine(questionToken, firstTokenOfConsequent);
|
---|
86 |
|
---|
87 | if (!areTestAndQuestionOnSameLine) {
|
---|
88 | fixers.push(fixer.removeRange([lastTokenOfTest.range[1], questionToken.range[0]]));
|
---|
89 | }
|
---|
90 | if (!areQuestionAndConsOnSameLine) {
|
---|
91 | fixers.push(fixer.removeRange([questionToken.range[1], firstTokenOfConsequent.range[0]]));
|
---|
92 | }
|
---|
93 |
|
---|
94 | return fixers;
|
---|
95 | }
|
---|
96 | });
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (!areConsequentAndAlternateOnSameLine) {
|
---|
100 | context.report({
|
---|
101 | node: node.consequent,
|
---|
102 | loc: {
|
---|
103 | start: firstTokenOfConsequent.loc.start,
|
---|
104 | end: lastTokenOfConsequent.loc.end
|
---|
105 | },
|
---|
106 | messageId: "unexpectedConsAlt",
|
---|
107 | fix(fixer) {
|
---|
108 | if (hasComments) {
|
---|
109 | return null;
|
---|
110 | }
|
---|
111 | const fixers = [];
|
---|
112 | const areConsAndColonOnSameLine = astUtils.isTokenOnSameLine(lastTokenOfConsequent, colonToken);
|
---|
113 | const areColonAndAltOnSameLine = astUtils.isTokenOnSameLine(colonToken, firstTokenOfAlternate);
|
---|
114 |
|
---|
115 | if (!areConsAndColonOnSameLine) {
|
---|
116 | fixers.push(fixer.removeRange([lastTokenOfConsequent.range[1], colonToken.range[0]]));
|
---|
117 | }
|
---|
118 | if (!areColonAndAltOnSameLine) {
|
---|
119 | fixers.push(fixer.removeRange([colonToken.range[1], firstTokenOfAlternate.range[0]]));
|
---|
120 | }
|
---|
121 |
|
---|
122 | return fixers;
|
---|
123 | }
|
---|
124 | });
|
---|
125 | }
|
---|
126 | } else {
|
---|
127 | if (allowSingleLine && node.loc.start.line === node.loc.end.line) {
|
---|
128 | return;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (areTestAndConsequentOnSameLine) {
|
---|
132 | context.report({
|
---|
133 | node: node.test,
|
---|
134 | loc: {
|
---|
135 | start: firstTokenOfTest.loc.start,
|
---|
136 | end: lastTokenOfTest.loc.end
|
---|
137 | },
|
---|
138 | messageId: "expectedTestCons",
|
---|
139 | fix: fixer => (hasComments ? null : (
|
---|
140 | fixer.replaceTextRange(
|
---|
141 | [
|
---|
142 | lastTokenOfTest.range[1],
|
---|
143 | questionToken.range[0]
|
---|
144 | ],
|
---|
145 | "\n"
|
---|
146 | )
|
---|
147 | ))
|
---|
148 | });
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (areConsequentAndAlternateOnSameLine) {
|
---|
152 | context.report({
|
---|
153 | node: node.consequent,
|
---|
154 | loc: {
|
---|
155 | start: firstTokenOfConsequent.loc.start,
|
---|
156 | end: lastTokenOfConsequent.loc.end
|
---|
157 | },
|
---|
158 | messageId: "expectedConsAlt",
|
---|
159 | fix: (fixer => (hasComments ? null : (
|
---|
160 | fixer.replaceTextRange(
|
---|
161 | [
|
---|
162 | lastTokenOfConsequent.range[1],
|
---|
163 | colonToken.range[0]
|
---|
164 | ],
|
---|
165 | "\n"
|
---|
166 | )
|
---|
167 | )))
|
---|
168 | });
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | };
|
---|
173 | }
|
---|
174 | };
|
---|