Repository: obsidian-tasks-group/obsidian-tasks
Pull request #1217 (2022-10-10 20:20:57)

refactor: Store original Markdown line in Task, where possible
<!--- Provide a general summary of your changes in the Title above -->

# Description

Store original Markdown line in Task, where possible

The code is formatted consistently with the originalMarkdown changes that were part of: https://github.com/sytone/obsidian-tasks-x/commit/77270aa8293ae20319cc7ddb3c46166515d0cd3c

Co-Authored-By: @Sytone

## Motivation and Context

This will later be used to add some error-checking to the task-completion code.


## How has this been tested?

- By running all the existing tests
- By writing a new test to confirm that the original line is copied in, when the task is created via `Task.fromLine()`.

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, tests ran to see how -->
<!--- your change affects other areas of the code, etc. -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

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)
- [x] **Tests** (prefix: `test` - additions and improvements to unit tests and the smoke tests)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] My code follows the code style of this project and passes `yarn run lint`.
- [ ] My change requires a change to the documentation.
- [ ] I have [updated the documentation](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md#updating-documentation) accordingly.
- [x] My change has adequate [Unit Test coverage](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md#maintaining-the-tests).

## Terms

<!--
By submitting this pull request, you must agree to follow our
[contributing guide](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md) and
[Code of Conduct](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CODE_OF_CONDUCT.md).
Put an x in the boxes to confirm you agree.
-->

- [x] My contribution follow this project's [contributing guide](https://github.com/obsidian-tasks-group/obsidian-tasks/blob/main/CONTRIBUTING.md)
- [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/CreateOrEdit.ts b/src/Commands/CreateOrEdit.ts
index 93e82105..149b8bd0 100644
--- a/src/Commands/CreateOrEdit.ts
+++ b/src/Commands/CreateOrEdit.ts
@@ -73,6 +73,7 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
             precedingHeader: null,
             blockLink: '',
             tags: [],
+            originalMarkdown: '',
         });
     }
 
@@ -106,5 +107,6 @@ const taskFromLine = ({ line, path }: { line: string; path: string }): Task => {
         sectionIndex: 0,
         precedingHeader: null,
         tags: [],
+        originalMarkdown: '',
     });
 };
diff --git a/src/Task.ts b/src/Task.ts
index d298ff35..effd4b2b 100644
--- a/src/Task.ts
+++ b/src/Task.ts
@@ -149,6 +149,12 @@ export class Task {
     /** The blockLink is a "^" annotation after the dates/recurrence rules. */
     public readonly blockLink: string;
 
+    /** The original line read from file.
+     *
+     * Will be empty if Task was created programmatically
+     * (for example, by Create or Edit Task, or in tests, including via {@link TaskBuilder}). */
+    public readonly originalMarkdown: string;
+
     private _urgency: number | null = null;
 
     constructor({
@@ -168,6 +174,7 @@ export class Task {
         recurrence,
         blockLink,
         tags,
+        originalMarkdown,
     }: {
         status: Status;
         description: string;
@@ -185,6 +192,7 @@ export class Task {
         recurrence: Recurrence | null;
         blockLink: string;
         tags: string[] | [];
+        originalMarkdown: string;
     }) {
         this.status = status;
         this.description = description;
@@ -206,6 +214,7 @@ export class Task {
 
         this.recurrence = recurrence;
         this.blockLink = blockLink;
+        this.originalMarkdown = originalMarkdown;
     }
 
     /**
@@ -407,6 +416,7 @@ export class Task {
             recurrence,
             blockLink,
             tags,
+            originalMarkdown: line,
         });
     }
 


Test patch:
diff --git a/tests/Query.test.ts b/tests/Query.test.ts
index 04b26ade..67a7e130 100644
--- a/tests/Query.test.ts
+++ b/tests/Query.test.ts
@@ -261,6 +261,7 @@ describe('Query', () => {
                     recurrence: null,
                     blockLink: '',
                     tags: [],
+                    originalMarkdown: '',
                 }),
                 new Task({
                     status: Status.TODO,
@@ -279,6 +280,7 @@ describe('Query', () => {
                     recurrence: null,
                     blockLink: '',
                     tags: [],
+                    originalMarkdown: '',
                 }),
             ];
             const input = 'path includes ab/c d';
diff --git a/tests/Task.test.ts b/tests/Task.test.ts
index d93e51d1..5a3ff5b7 100644
--- a/tests/Task.test.ts
+++ b/tests/Task.test.ts
@@ -29,6 +29,7 @@ describe('parsing', () => {
         expect(task!.dueDate!.isSame(moment('2021-09-12', 'YYYY-MM-DD'))).toStrictEqual(true);
         expect(task!.doneDate).not.toBeNull();
         expect(task!.doneDate!.isSame(moment('2021-06-20', 'YYYY-MM-DD'))).toStrictEqual(true);
+        expect(task!.originalMarkdown).toStrictEqual(line);
     });
 
     it('returns null when task does not have global filter', () => {
diff --git a/tests/TestingTools/TaskBuilder.ts b/tests/TestingTools/TaskBuilder.ts
index dbbf8ca6..17a2bfbd 100644
--- a/tests/TestingTools/TaskBuilder.ts
+++ b/tests/TestingTools/TaskBuilder.ts
@@ -72,6 +72,7 @@ export class TaskBuilder {
             recurrence: this._recurrence,
             blockLink: this._blockLink,
             tags: this._tags,
+            originalMarkdown: '',
         });
     }