Repository: obsidian-tasks-group/obsidian-tasks
Pull request #2490 (2023-12-09 11:39:39)

refactor: rename variables in postpone code
# Description

Rename variables with the following logic:
- `update` -> `postpone`
- `dateType` -> `dateField`
- `newTasks` -> `postponedTask`

Plus remove one type assertion since it is not needed by design (`HappensDate` type ensures the type).

## Motivation and Context

Improve code readability.

## How has this been tested?

Unit tests. Only automated refactorings used.

## Types of changes

Internal changes:

- [x] **Refactor** (prefix: `refactor` - non-breaking change which only improves the design or structure of existing code, and making no changes to its external behaviour)

## Checklist

- [x] My code follows the code style of this project and passes `yarn run lint`.
- [x] My change has adequate [Unit Test coverage](https://publish.obsidian.md/tasks-contributing/Testing/About+Testing).

## Terms

- [x] My contribution follow this project's [contributing guide](https://publish.obsidian.md/tasks-contributing)
- [x] I agree to follow this project's [Code of Conduct](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CODE_OF_CONDUCT.md)



Patch:
diff --git a/src/QueryRenderer.ts b/src/QueryRenderer.ts
index 2b84791d..d825635c 100644
--- a/src/QueryRenderer.ts
+++ b/src/QueryRenderer.ts
@@ -480,21 +480,21 @@ class QueryRenderChild extends MarkdownRenderChild {
         button: HTMLButtonElement,
         task: Task,
         amount: number,
-        timeUnit: moment.unitOfTime.DurationConstructor,
+        timeUnit: unitOfTime.DurationConstructor,
     ) {
-        const dateTypeToUpdate = getDateFieldToPostpone(task);
-        if (dateTypeToUpdate === null) {
+        const dateFieldToPostpone = getDateFieldToPostpone(task);
+        if (dateFieldToPostpone === null) {
             const errorMessage = '⚠️ Postponement requires a date: due, scheduled or start.';
             return new Notice(errorMessage, 10000);
         }
 
-        const { postponedDate, newTasks } = createPostponedTask(task, dateTypeToUpdate, timeUnit, amount);
+        const { postponedDate, postponedTask } = createPostponedTask(task, dateFieldToPostpone, timeUnit, amount);
 
         await replaceTaskWithTasks({
             originalTask: task,
-            newTasks,
+            newTasks: postponedTask,
         });
-        this.postponeSuccessCallback(button, dateTypeToUpdate, postponedDate);
+        this.postponeSuccessCallback(button, dateFieldToPostpone, postponedDate);
     }
 
     private postponeSuccessCallback(button: HTMLButtonElement, updatedDateType: HappensDate, postponedDate: Moment) {
diff --git a/src/Scripting/Postponer.ts b/src/Scripting/Postponer.ts
index a0c95417..4c3c4ec8 100644
--- a/src/Scripting/Postponer.ts
+++ b/src/Scripting/Postponer.ts
@@ -36,18 +36,18 @@ export function getDateFieldToPostpone(task: Task): HappensDate | null {
 
 export function createPostponedTask(
     task: Task,
-    dateTypeToUpdate: HappensDate,
+    dateFieldToPostpone: HappensDate,
     timeUnit: unitOfTime.DurationConstructor,
     amount: number,
 ) {
-    const dateToUpdate = task[dateTypeToUpdate] as Moment;
-    const postponedDate = new TasksDate(dateToUpdate).postpone(timeUnit, amount);
-    const newTasks = new Task({ ...task, [dateTypeToUpdate]: postponedDate });
-    return { postponedDate, newTasks };
+    const dateToPostpone = task[dateFieldToPostpone];
+    const postponedDate = new TasksDate(dateToPostpone).postpone(timeUnit, amount);
+    const postponedTask = new Task({ ...task, [dateFieldToPostpone]: postponedDate });
+    return { postponedDate, postponedTask };
 }
 
-export function postponementSuccessMessage(postponedDate: Moment, updatedDateType: HappensDate) {
+export function postponementSuccessMessage(postponedDate: Moment, dateFieldToPostpone: HappensDate) {
     // TODO all logic for invalid dates
     const postponedDateString = postponedDate?.format('DD MMM YYYY');
-    return `Task's ${updatedDateType} postponed until ${postponedDateString}`;
+    return `Task's ${dateFieldToPostpone} postponed until ${postponedDateString}`;
 }


Test patch:
diff --git a/tests/Scripting/Postponer.test.ts b/tests/Scripting/Postponer.test.ts
index 3735604e..3172a37f 100644
--- a/tests/Scripting/Postponer.test.ts
+++ b/tests/Scripting/Postponer.test.ts
@@ -157,15 +157,15 @@ describe('postpone - new task creation', () => {
     });
 
     function testPostponedTaskAndDate(task: Task, expectedDateField: HappensDate, expectedPostponedDate: string) {
-        const { postponedDate, newTasks } = createPostponedTask(task, expectedDateField, 'day', 1);
+        const { postponedDate, postponedTask } = createPostponedTask(task, expectedDateField, 'day', 1);
         expect(postponedDate.format('YYYY-MM-DD')).toEqual(expectedPostponedDate);
-        expect(newTasks[expectedDateField]?.format('YYYY-MM-DD')).toEqual(expectedPostponedDate);
+        expect(postponedTask[expectedDateField]?.format('YYYY-MM-DD')).toEqual(expectedPostponedDate);
 
         // If the scheduled date was inferred from the filename, and it is the scheduledDate that was postponed,
         // we must ensure that the 'inferred' flag has been reset to false.
         // Otherwise, the new scheduled date will be ignored in some locations, like rendering of dates.
         if (task.scheduledDateIsInferred && expectedDateField === 'scheduledDate') {
-            expect(newTasks.scheduledDateIsInferred).toEqual(false);
+            expect(postponedTask.scheduledDateIsInferred).toEqual(false);
         }
     }