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