Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce newlines between operands of ternary expressions if the expression spans multiple lines (multiline-ternary) #1558

Closed
feross opened this issue Oct 21, 2020 · 4 comments

Comments

@feross
Copy link
Member

feross commented Oct 21, 2020

https://eslint.org/docs/rules/multiline-ternary

Desired config:

/*eslint multiline-ternary: ["error", "always-multiline"]*/

JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.

For example:

var foo = bar > baz ? value1 : value2;

The above can be rewritten as the following to improve readability and more clearly delineate the operands:

var foo = bar > baz ?
    value1 :
    value2;

Rule Details

This rule enforces or disallows newlines between operands of a ternary expression.
Note: The location of the operators is not enforced by this rule. Please see the operator-linebreak rule if you are interested in enforcing the location of the operators themselves.

always-multiline

Examples of incorrect code for this rule with the "always-multiline" option:

/*eslint multiline-ternary: ["error", "always-multiline"]*/

foo > bar ? value1 :
    value2;

foo > bar ?
    value1 : value2;

foo > bar &&
    bar > baz ? value1 : value2;

Examples of correct code for this rule with the "always-multiline" option:

/*eslint multiline-ternary: ["error", "always-multiline"]*/

foo > bar ? value1 : value2;

foo > bar ?
    value1 :
    value2;

foo > bar ?
    (baz > qux ? value1 : value2) :
    value3;

foo > bar ?
    (baz > qux ?
        value1 :
        value2) :
    value3;

foo > bar &&
    bar > baz ?
        value1 :
        value2;
@feross
Copy link
Member Author

feross commented Oct 22, 2020

This rule seems to have a bug, which I've filed against ESLint here: eslint/eslint#13782

@feross
Copy link
Member Author

feross commented Oct 22, 2020

Nevermind, I was misunderstanding the rule.

@feross
Copy link
Member Author

feross commented Oct 22, 2020

3% ecosystem impact.

1..210
# tests 210
# pass  204
# fail  6

PRs need to be sent to these packages:

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 26, 2021
@standard standard unlocked this conversation Sep 1, 2022
@LinusU
Copy link
Member

LinusU commented Sep 1, 2022

Sorry for dragging up such an old issue. My team just noticed this since we upgraded to ts-standard 11 today 😅

Just wanted to share some unfortunate diffs that this generated:

   </head>
   <body>
     <div id="slip">
-      ${printerName == null ? '' : `
+      ${printerName == null
+        ? ''
+        : `
         <div class="printer-name">
           ${escapeHtml(printerName)}
         </div>
       `}
 
-      ${!isCopy ? '' : `
+      ${!isCopy
+        ? ''
+        : `
         <div class="copy-header">
           <div class="copy-title">KOPIA</div>
           <div class="copy-date">${escapeHtml(formatDateTime(now, 'yyyy-MM-dd HH:mm:ss', timeZone))}</div>
         _created_at: requestTime,
         _id: id,
         _updated_at: requestTime,
-        employee: employee == null || employeeGroups == null ? null : {
-          _created_at: employee._created_at,
-          _id: employee._id,
-          _updated_at: employee._updated_at,
-          name: employee.name,
-          groups: employeeGroups
-        },
+        employee: employee == null || employeeGroups == null
+          ? null
+          : {
+              _created_at: employee._created_at,
+              _id: employee._id,
+              _updated_at: employee._updated_at,
+              name: employee.name,
+              groups: employeeGroups
+            },
         orderIntentId,
         paymentMethodId: paymentMethod._id,
       const html = await renderSlipFromOrder(restaurant, orders, printerQueue ?? null, isCopy ?? false, dataloader)
 
-      return html == null ? null : {
-        html,
-        needsPrinting: null,
-        printerQueueId: printerQueue?._id ?? null,
-        restaurantId: restaurant._id
-      }
+      return html == null
+        ? null
+        : {
+            html,
+            needsPrinting: null,
+            printerQueueId: printerQueue?._id ?? null,
+            restaurantId: restaurant._id
+          }
     }
   }
 }
-          ${servingLocation?.tableName == null || servingLocation.tableName === '' ? '' : `
+          ${servingLocation?.tableName == null || servingLocation.tableName === ''
+            ? ''
+            : `
             <div class="footer-text">Utlämnas till: ${escapeHtml(servingLocation.tableName)}</div>
           `}
         {
           $set: {
-            alternativeItems: patch.alternativeItems == null ? undefined : ({
-              alternativeItems: patch.alternativeItems?.map(item => ({
-                countableUsage: item.countableUsage ?? undefined,
-                isDefault: item.isDefault ?? false,
-                name: item.name,
-                productId: item.addonProductId ?? undefined
-              }))
-            }),
+            alternativeItems: patch.alternativeItems == null
+              ? undefined
+              : ({
+                  alternativeItems: patch.alternativeItems?.map(item => ({
+                    countableUsage: item.countableUsage ?? undefined,
+                    isDefault: item.isDefault ?? false,
+                    name: item.name,
+                    productId: item.addonProductId ?? undefined
+                  }))
+                }),
             forceChoice: patch.forceChoice ?? undefined,
             isCountable: patch.isCountable ?? undefined,
             maxChoices: patch.maxChoices ?? undefined,

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

2 participants