]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20327 Reduce cognitive complexity of function
authorDavid Cho-Lerat <david.cho-lerat@sonarsource.com>
Thu, 7 Sep 2023 15:28:36 +0000 (17:28 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 12 Sep 2023 20:02:41 +0000 (20:02 +0000)
server/sonar-web/src/main/js/components/issue/components/IssueChangelogDiff.tsx

index 165008932099f16e3ea3249db9273119c5a6b685..78e044e86e37f765f68f28f6f23da4369536d22a 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
+
 import * as React from 'react';
 import { translate, translateWithParameters } from '../../../helpers/l10n';
 import { formatMeasure } from '../../../helpers/measures';
@@ -26,66 +27,79 @@ export interface IssueChangelogDiffProps {
   diff: TypeIssueChangelogDiff;
 }
 
-export default function IssueChangelogDiff({ diff }: IssueChangelogDiffProps) {
+export default function IssueChangelogDiff(props: IssueChangelogDiffProps) {
+  const { diff } = props;
+
+  const diffComputedValues = {
+    newValue: diff.newValue ?? '',
+    oldValue: diff.oldValue ?? '',
+  };
+
   if (diff.key === 'file') {
     return (
       <p>
         {translateWithParameters(
           'issue.change.file_move',
-          diff.oldValue ?? '',
-          diff.newValue ?? '',
+          diffComputedValues.oldValue,
+          diffComputedValues.newValue,
         )}
       </p>
     );
-  } else if (['from_long_branch', 'from_branch'].includes(diff.key)) {
+  }
+
+  if (['from_long_branch', 'from_branch'].includes(diff.key)) {
     return (
       <p>
         {translateWithParameters(
           'issue.change.from_branch',
-          diff.oldValue ?? '',
-          diff.newValue ?? '',
+          diffComputedValues.oldValue,
+          diffComputedValues.newValue,
         )}
       </p>
     );
-  } else if (diff.key === 'from_short_branch') {
+  }
+
+  if (diff.key === 'from_short_branch') {
     // Applies to both legacy short lived branch and pull request
     return (
       <p>
         {translateWithParameters(
           'issue.change.from_non_branch',
-          diff.oldValue ?? '',
-          diff.newValue ?? '',
+          diffComputedValues.oldValue,
+          diffComputedValues.newValue,
         )}
       </p>
     );
-  } else if (diff.key === 'line') {
-    return <p>{translateWithParameters('issue.changelog.line_removed_X', diff.oldValue ?? '')}</p>;
   }
 
-  let message;
-  if (diff.newValue != null) {
-    let { newValue } = diff;
-    if (diff.key === 'effort') {
-      newValue = formatMeasure(diff.newValue, 'WORK_DUR');
-    }
-    message = translateWithParameters(
-      'issue.changelog.changed_to',
-      translate('issue.changelog.field', diff.key),
-      newValue,
-    );
-  } else {
-    message = translateWithParameters(
-      'issue.changelog.removed',
-      translate('issue.changelog.field', diff.key),
+  if (diff.key === 'line') {
+    return (
+      <p>
+        {translateWithParameters('issue.changelog.line_removed_X', diffComputedValues.oldValue)}
+      </p>
     );
   }
 
-  if (diff.oldValue != null) {
-    let { oldValue } = diff;
-    if (diff.key === 'effort') {
-      oldValue = formatMeasure(diff.oldValue, 'WORK_DUR');
-    }
-    message += ` (${translateWithParameters('issue.changelog.was', oldValue)})`;
+  if (diff.key === 'effort') {
+    diffComputedValues.newValue = formatMeasure(diff.newValue, 'WORK_DUR');
+    diffComputedValues.oldValue = formatMeasure(diff.oldValue, 'WORK_DUR');
   }
+
+  let message =
+    diff.newValue !== undefined
+      ? translateWithParameters(
+          'issue.changelog.changed_to',
+          translate('issue.changelog.field', diff.key),
+          diffComputedValues.newValue,
+        )
+      : translateWithParameters(
+          'issue.changelog.removed',
+          translate('issue.changelog.field', diff.key),
+        );
+
+  if (diff.oldValue !== undefined) {
+    message += ` (${translateWithParameters('issue.changelog.was', diffComputedValues.oldValue)})`;
+  }
+
   return <p>{message}</p>;
 }