Skip to content

Commit

Permalink
Merge pull request #3257 from obsidian-tasks-group/fix-issue-3256
Browse files Browse the repository at this point in the history
fix: Remove console error when line is deleted by 'Toggle task done' command
  • Loading branch information
claremacrae authored Dec 31, 2024
2 parents 083fdbd + ccd292b commit a4cbe90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Commands/ToggleDone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export const toggleLine = (line: string, path: string): EditorInsertion => {
});
if (task !== null) {
const lines = task.toggleWithRecurrenceInUsersOrder().map((t) => t.toFileLineString());
return { text: lines.join('\n'), moveTo: { line: lines.length - 1 } };
const newLineNumber = lines.length > 0 ? lines.length - 1 : 0;
return { text: lines.join('\n'), moveTo: { line: newLineNumber } };
} else {
// If the task is null this means that we have one of:
// 1. a regular checklist item
Expand Down
34 changes: 34 additions & 0 deletions tests/Commands/ToggleDone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,40 @@ describe('ToggleDone', () => {
);
});

describe('on completion', () => {
it('should delete a self-deleting task - cursor at start of line', () => {
// Issue #3256 - traceback occurred.
testToggleLine(
// Force linebreak
'|- [ ] #task Delete me 🏁 delete',
'|',
);
});

it('should delete a self-deleting task - cursor at end of line', () => {
// Issue #3256 - traceback occurred.
testToggleLine(
// Force linebreak
'- [ ] #task Delete me 🏁 delete|',
'|',
);
});

it('should discard completed recurring task - cursor at start of line', () => {
testToggleLine(
'|- [ ] #task Delete my completed task πŸ” every day 🏁 delete ⏳ 2024-12-31',
'|- [ ] #task Delete my completed task πŸ” every day 🏁 delete ⏳ 2025-01-01',
);
});

it('should discard completed recurring task - cursor at end of line', () => {
testToggleLine(
'- [ ] #task Delete my completed task πŸ” every day 🏁 delete ⏳ 2024-12-31|',
'- [ ] #task Delete my completed task πŸ” every day 🏁 delete ⏳ 2025-01-01|',
);
});
});

describe('should honour next status character', () => {
afterEach(() => {
GlobalFilter.getInstance().reset();
Expand Down

0 comments on commit a4cbe90

Please sign in to comment.