From 0d100a6dc048389a95e357e7eb4a39db98c7c2c9 Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Thu, 21 Sep 2023 13:45:38 +0100 Subject: [PATCH 1/6] test: Add a failing test for #2112 --- tests/Commands/CreateOrEditTaskParser.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Commands/CreateOrEditTaskParser.test.ts b/tests/Commands/CreateOrEditTaskParser.test.ts index 547fc52f08..e7ba4d175e 100644 --- a/tests/Commands/CreateOrEditTaskParser.test.ts +++ b/tests/Commands/CreateOrEditTaskParser.test.ts @@ -107,6 +107,7 @@ describe('CreateOrEditTaskParser - created date', () => { afterEach(() => { jest.useRealTimers(); resetSettings(); + GlobalFilter.reset(); }); it.each([ @@ -151,4 +152,17 @@ describe('CreateOrEditTaskParser - created date', () => { expect(task.toFileLineString()).toStrictEqual('- [ ] hope created date will not be added'); expect(task.createdDate).toEqual(null); }); + + it.failing('should add created date if adding the global filter', () => { + updateSettings({ setCreatedDate: true }); + GlobalFilter.set('#task'); + const path = 'a/b/c.md'; + const line = '- [ ] did not have the global filter'; + + const task = taskFromLine({ line, path }); + + // The global filter doesn't get added until the Modal rewrites the line + expect(task.toFileLineString()).toStrictEqual('- [ ] did not have the global filter ➕ 2023-09-17'); + expect(task.createdDate).toEqual(null); + }); }); From 951a774093107178723584771603b45e2a8b8722 Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Thu, 21 Sep 2023 13:57:30 +0100 Subject: [PATCH 2/6] refactor: . Negate !== to === --- src/Commands/CreateOrEditTaskParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/CreateOrEditTaskParser.ts b/src/Commands/CreateOrEditTaskParser.ts index 78cba913f9..f6c124fdb3 100644 --- a/src/Commands/CreateOrEditTaskParser.ts +++ b/src/Commands/CreateOrEditTaskParser.ts @@ -23,7 +23,7 @@ function shouldUpdateCreatedDateForTask(task: Task) { return false; } - if (task.description !== '') { + if (!(task.description === '')) { // The task already had a description, so had been created previously: don't change it. return false; } From 2b418b6427459f17710308c64a7f3fdeec2c8842 Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Thu, 21 Sep 2023 13:58:39 +0100 Subject: [PATCH 3/6] refactor: . Extract variable descriptionIsEmpty --- src/Commands/CreateOrEditTaskParser.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Commands/CreateOrEditTaskParser.ts b/src/Commands/CreateOrEditTaskParser.ts index f6c124fdb3..94ad030823 100644 --- a/src/Commands/CreateOrEditTaskParser.ts +++ b/src/Commands/CreateOrEditTaskParser.ts @@ -23,7 +23,8 @@ function shouldUpdateCreatedDateForTask(task: Task) { return false; } - if (!(task.description === '')) { + const descriptionIsEmpty = task.description === ''; + if (!descriptionIsEmpty) { // The task already had a description, so had been created previously: don't change it. return false; } From 727e4811e76cd54a77107d233b051ffc349eec3c Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Thu, 21 Sep 2023 14:15:37 +0100 Subject: [PATCH 4/6] refactor: . Simplify 'if' statement --- src/Commands/CreateOrEditTaskParser.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Commands/CreateOrEditTaskParser.ts b/src/Commands/CreateOrEditTaskParser.ts index 94ad030823..f3dfa66146 100644 --- a/src/Commands/CreateOrEditTaskParser.ts +++ b/src/Commands/CreateOrEditTaskParser.ts @@ -23,13 +23,10 @@ function shouldUpdateCreatedDateForTask(task: Task) { return false; } + // If the task already had a description, treat it as new and add a creation date. const descriptionIsEmpty = task.description === ''; - if (!descriptionIsEmpty) { - // The task already had a description, so had been created previously: don't change it. - return false; - } - return true; + return descriptionIsEmpty; } /** From 625bfc17766fac76fd713efbc42834d33cb6f310 Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Thu, 21 Sep 2023 14:26:12 +0100 Subject: [PATCH 5/6] fix: If editing a task adds global filter, then add Created date, if enabled When editing a task via the 'Edit task modal', if it didn't yet have the global filter, then treat it as a new task and add the Created date, if enabled. This fixes https://github.com/obsidian-tasks-group/obsidian-tasks/issues/2112 --- src/Commands/CreateOrEditTaskParser.ts | 9 ++++++++- tests/Commands/CreateOrEditTaskParser.test.ts | 4 ++-- ...austive_editing_Edit_and_save_All_inputs.approved.txt | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Commands/CreateOrEditTaskParser.ts b/src/Commands/CreateOrEditTaskParser.ts index f3dfa66146..56416ccc64 100644 --- a/src/Commands/CreateOrEditTaskParser.ts +++ b/src/Commands/CreateOrEditTaskParser.ts @@ -4,6 +4,7 @@ import { DateFallback } from '../DateFallback'; import { StatusRegistry } from '../StatusRegistry'; import { TaskLocation } from '../TaskLocation'; import { getSettings } from '../Config/Settings'; +import { GlobalFilter } from '../Config/GlobalFilter'; function getDefaultCreatedDate() { const { setCreatedDate } = getSettings(); @@ -26,7 +27,13 @@ function shouldUpdateCreatedDateForTask(task: Task) { // If the task already had a description, treat it as new and add a creation date. const descriptionIsEmpty = task.description === ''; - return descriptionIsEmpty; + // If the global filter will be added when the task is saved, treat it as new and add a creation date. + // See issue #2112. + const globalFilterEnabled = !GlobalFilter.isEmpty(); + const taskDoesNotContainGlobalFilter = !GlobalFilter.includedIn(task.description); + const needsGlobalFilterToBeAdded = globalFilterEnabled && taskDoesNotContainGlobalFilter; + + return descriptionIsEmpty || needsGlobalFilterToBeAdded; } /** diff --git a/tests/Commands/CreateOrEditTaskParser.test.ts b/tests/Commands/CreateOrEditTaskParser.test.ts index e7ba4d175e..a4a7488122 100644 --- a/tests/Commands/CreateOrEditTaskParser.test.ts +++ b/tests/Commands/CreateOrEditTaskParser.test.ts @@ -153,7 +153,7 @@ describe('CreateOrEditTaskParser - created date', () => { expect(task.createdDate).toEqual(null); }); - it.failing('should add created date if adding the global filter', () => { + it('should add created date if adding the global filter', () => { updateSettings({ setCreatedDate: true }); GlobalFilter.set('#task'); const path = 'a/b/c.md'; @@ -163,6 +163,6 @@ describe('CreateOrEditTaskParser - created date', () => { // The global filter doesn't get added until the Modal rewrites the line expect(task.toFileLineString()).toStrictEqual('- [ ] did not have the global filter ➕ 2023-09-17'); - expect(task.createdDate).toEqual(null); + expect(task.createdDate).toEqualMoment(moment('2023-09-17')); }); }); diff --git a/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt b/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt index b8dce19b58..18062dd7fb 100644 --- a/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt +++ b/tests/EditTask.test.Exhaustive_editing_Edit_and_save_All_inputs.approved.txt @@ -151,7 +151,7 @@ KEY: (globalFilter, set created date) ('#task', true) '- [ ] checkbox with initial description' => - '- [ ] #task checkbox with initial description' + '- [ ] #task checkbox with initial description ➕ 2023-07-18' ('#task', true) '- [ ] checkbox with initial description and created date ➕ 2023-01-01' => From 57b341f6aec534bebc242825f1c7bd6ffc4d3ba9 Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Thu, 21 Sep 2023 14:50:57 +0100 Subject: [PATCH 6/6] comment: Fix an incorrect comment, noticed in code review --- src/Commands/CreateOrEditTaskParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/CreateOrEditTaskParser.ts b/src/Commands/CreateOrEditTaskParser.ts index 56416ccc64..f21823dc37 100644 --- a/src/Commands/CreateOrEditTaskParser.ts +++ b/src/Commands/CreateOrEditTaskParser.ts @@ -24,7 +24,7 @@ function shouldUpdateCreatedDateForTask(task: Task) { return false; } - // If the task already had a description, treat it as new and add a creation date. + // If the description was empty, treat it as new and add a creation date. const descriptionIsEmpty = task.description === ''; // If the global filter will be added when the task is saved, treat it as new and add a creation date.