]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/multiline-ternary.js
first commit
[pve-eslint.git] / eslint / lib / rules / multiline-ternary.js
1 /**
2 * @fileoverview Enforce newlines between operands of ternary expressions
3 * @author Kai Cataldo
4 */
5
6 "use strict";
7
8 const astUtils = require("./utils/ast-utils");
9
10 //------------------------------------------------------------------------------
11 // Rule Definition
12 //------------------------------------------------------------------------------
13
14 module.exports = {
15 meta: {
16 type: "layout",
17
18 docs: {
19 description: "enforce newlines between operands of ternary expressions",
20 category: "Stylistic Issues",
21 recommended: false,
22 url: "https://eslint.org/docs/rules/multiline-ternary"
23 },
24
25 schema: [
26 {
27 enum: ["always", "always-multiline", "never"]
28 }
29 ],
30 messages: {
31 expectedTestCons: "Expected newline between test and consequent of ternary expression.",
32 expectedConsAlt: "Expected newline between consequent and alternate of ternary expression.",
33 unexpectedTestCons: "Unexpected newline between test and consequent of ternary expression.",
34 unexpectedConsAlt: "Unexpected newline between consequent and alternate of ternary expression."
35 }
36 },
37
38 create(context) {
39 const option = context.options[0];
40 const multiline = option !== "never";
41 const allowSingleLine = option === "always-multiline";
42
43 //--------------------------------------------------------------------------
44 // Helpers
45 //--------------------------------------------------------------------------
46
47 /**
48 * Tests whether node is preceded by supplied tokens
49 * @param {ASTNode} node node to check
50 * @param {ASTNode} parentNode parent of node to report
51 * @param {boolean} expected whether newline was expected or not
52 * @returns {void}
53 * @private
54 */
55 function reportError(node, parentNode, expected) {
56 context.report({
57 node,
58 messageId: `${expected ? "expected" : "unexpected"}${node === parentNode.test ? "TestCons" : "ConsAlt"}`
59 });
60 }
61
62 //--------------------------------------------------------------------------
63 // Public
64 //--------------------------------------------------------------------------
65
66 return {
67 ConditionalExpression(node) {
68 const areTestAndConsequentOnSameLine = astUtils.isTokenOnSameLine(node.test, node.consequent);
69 const areConsequentAndAlternateOnSameLine = astUtils.isTokenOnSameLine(node.consequent, node.alternate);
70
71 if (!multiline) {
72 if (!areTestAndConsequentOnSameLine) {
73 reportError(node.test, node, false);
74 }
75
76 if (!areConsequentAndAlternateOnSameLine) {
77 reportError(node.consequent, node, false);
78 }
79 } else {
80 if (allowSingleLine && node.loc.start.line === node.loc.end.line) {
81 return;
82 }
83
84 if (areTestAndConsequentOnSameLine) {
85 reportError(node.test, node, true);
86 }
87
88 if (areConsequentAndAlternateOnSameLine) {
89 reportError(node.consequent, node, true);
90 }
91 }
92 }
93 };
94 }
95 };