Repository: obsidian-tasks-group/obsidian-tasks (ts)

Issue:
Bug report: Created date not automatically added when list contains `- [ ]`
### Please check that this issue hasn't been reported before.

- [X] I searched previous [Bug Reports](https://github.com/obsidian-tasks-group/obsidian-tasks/issues?q=is%3Aopen+is%3Aissue+label%3A%22type%3A+bug%22+sort%3Acomments-desc) didn't find any similar reports.

### Expected Behavior

Per my settings to "Set created date on every added task" (see screenshot) and the [documentation on created date](https://publish.obsidian.md/tasks/Getting+Started/Dates#Created+date), I expect a created date to be added automatically when I use the [‘Create or edit Task’ Modal](https://publish.obsidian.md/tasks/Getting+Started/Create+or+edit+Task).
![obsidian-tasks-created-date-settings](https://github.com/obsidian-tasks-group/obsidian-tasks/assets/38968656/0e7a8264-5644-4119-94fa-5e78991ee2b3)


### Current behaviour

✅  The expected behavior occurs when I create a new task on a line that does **not** start with `- [ ]`.
❌  However, the expected behavior **fails** to occurs when I create a new task on a line that starts with `- [ ]`. This occurs when I enter a task below another task.

This video shows the expected and unexpected behavior:
https://www.loom.com/share/29fce02dbc884283af2de94ac65fad63

### Steps to reproduce

See video

### Which Operating Systems are you using?

- [ ] Android
- [ ] iPhone/iPad
- [ ] Linux
- [X] macOS
- [ ] Windows

### Obsidian Version

1.3.5

### Tasks Plugin Version

3.10.0

### Checks

- [ ] I have tried it with all other plugins disabled and the error still occurs

### Possible solution

I believe the issue is that the line already starts with `- [ ]`

Pull request:
fix: Editing a checkbox line with empty description now adds creation date, if enabled
# Description

When a task line has been parsed, check whether the created date shall be appended to it.

In https://github.com/obsidian-tasks-group/obsidian-tasks/issues/2025#issuecomment-1616420079 you wrote:

> So the fix will be to add some code at line 29 that says
> 
> * if it was a task already - with a checkbox
> * and if the description is empty,
> * and it does not already have a creation date....
> * then add the creation date before returning the task on line 31.

## Motivation and Context

Fixes #2025 

## How has this been tested?

New unit tests added.

Smoke test in demo vault:

Global Filter set to `#task`, Add creation date option enabled. On each of the following lines execute `Create or Edit task` command, put something (`blablabla`) into description, click apply:

```
-
- [ ]
```

Both the lines gain the global filter, the description that was set and the created date. The first line gains the checkbox.

Before the fix: only the first line got the created date.

## Types of changes

Changes visible to users:

- [x] **Bug fix** (prefix: `fix` - non-breaking change which fixes an issue)

Internal changes:

- [x] **Tests** (prefix: `test` - additions and improvements to unit tests and the smoke tests)

## Checklist

- [x] My code follows the code style of this project and passes `yarn run lint`.
- [x] My change requires a change to the documentation.
- [x] I have [updated the documentation](https://publish.obsidian.md/tasks-contributing/Documentation/About+Documentation) accordingly.
- [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/Commands/CreateOrEditTaskParser.ts b/src/Commands/CreateOrEditTaskParser.ts
index f4f93d41..f873f54f 100644
--- a/src/Commands/CreateOrEditTaskParser.ts
+++ b/src/Commands/CreateOrEditTaskParser.ts
@@ -27,13 +27,18 @@ export const taskFromLine = ({ line, path }: { line: string; path: string }): Ta
         DateFallback.fromPath(path), // set the scheduled date from the filename, so it can be displayed in the dialog
     );
 
+    const { setCreatedDate } = getSettings();
+    const createdDate = setCreatedDate ? window.moment() : null;
+
     if (task !== null) {
+        if (task.description === '') {
+            if (setCreatedDate && task.createdDate === null) {
+                return new Task({ ...task, createdDate });
+            }
+        }
         return task;
     }
 
-    const { setCreatedDate } = getSettings();
-    const createdDate = setCreatedDate ? window.moment() : null;
-
     // If we are not on a line of a task, we take what we have.
     // The non-task line can still be a checklist, for example if it is lacking the global filter.
     const nonTaskMatch = line.match(TaskRegularExpressions.nonTaskRegex);


Test patch:
diff --git a/tests/Commands/CreateOrEditTaskParser.test.ts b/tests/Commands/CreateOrEditTaskParser.test.ts
index f9c3352c..5a584365 100644
--- a/tests/Commands/CreateOrEditTaskParser.test.ts
+++ b/tests/Commands/CreateOrEditTaskParser.test.ts
@@ -2,6 +2,7 @@
  * @jest-environment jsdom
  */
 import moment from 'moment';
+import { resetSettings, updateSettings } from '../../src/Config/Settings';
 import { Priority } from '../../src/Task';
 import { taskFromLine } from '../../src/Commands/CreateOrEditTaskParser';
 import { GlobalFilter } from '../../src/Config/GlobalFilter';
@@ -96,3 +97,47 @@ describe('CreateOrEditTaskParser - task recognition', () => {
         expect(task.doneDate).toEqualMoment(moment('2023-06-22'));
     });
 });
+
+describe('CreateOrEditTaskParser - created date', () => {
+    beforeEach(() => {
+        jest.useFakeTimers();
+        jest.setSystemTime(new Date('2023-09-17'));
+    });
+
+    afterEach(() => {
+        jest.useRealTimers();
+        resetSettings();
+    });
+
+    it.each([
+        ['- ', '- [ ]  ➕ 2023-09-17', '2023-09-17'], // bullet point only
+        ['- [ ] ', '- [ ]  ➕ 2023-09-17', '2023-09-17'], // bullet point and a checkbox
+        [
+            '- [ ] without global filter and with ➕ 2023-01-20', // with an existing created date
+            '- [ ] without global filter and with ➕ 2023-01-20',
+            '2023-01-20',
+        ],
+    ])(
+        'line loaded into "Create or edit task" command: "%s"',
+        (line: string, expectedTaskLine: string, expectedCreatedDate: string) => {
+            updateSettings({ setCreatedDate: true });
+            const path = 'a/b/c.md';
+
+            const task = taskFromLine({ line, path });
+
+            expect(task.toFileLineString()).toStrictEqual(expectedTaskLine);
+            expect(task.createdDate).toEqualMoment(moment(expectedCreatedDate));
+        },
+    );
+
+    it('should not add created date to a task line with description', () => {
+        updateSettings({ setCreatedDate: true });
+        const path = 'a/b/c.md';
+        const line = '- [ ] hope created date will not be added';
+
+        const task = taskFromLine({ line, path });
+
+        expect(task.toFileLineString()).toStrictEqual('- [ ] hope created date will not be added');
+        expect(task.createdDate).toEqual(null);
+    });
+});
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 39b1524c..b8dce19b 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
@@ -59,11 +59,11 @@ KEY: (globalFilter, set created date)
 
 ('', true)
     '- [ ]' =>
-    '- [ ] simulate user typing text in to empty description field'
+    '- [ ] simulate user typing text in to empty description field ➕ 2023-07-18'
 
 ('', true)
     '- [ ] ' =>
-    '- [ ] simulate user typing text in to empty description field'
+    '- [ ] simulate user typing text in to empty description field ➕ 2023-07-18'
 
 ('', true)
     '- list item, but no checkbox' =>
@@ -139,11 +139,11 @@ KEY: (globalFilter, set created date)
 
 ('#task', true)
     '- [ ]' =>
-    '- [ ] #task simulate user typing text in to empty description field'
+    '- [ ] #task simulate user typing text in to empty description field ➕ 2023-07-18'
 
 ('#task', true)
     '- [ ] ' =>
-    '- [ ] #task simulate user typing text in to empty description field'
+    '- [ ] #task simulate user typing text in to empty description field ➕ 2023-07-18'
 
 ('#task', true)
     '- list item, but no checkbox' =>