Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent exception if toggling in unsaved file in Reading mode #1772

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,32 @@ export function findLineNumberOfTaskToToggle(
listItemsCache: ListItemCache[] | MockListItemCache[],
errorLoggingFunction: ErrorLoggingFunction,
) {
const fileLinesCount = fileLines.length;
const { globalFilter } = getSettings();
let taskLineNumber: number | undefined;
let sectionIndex = 0;
for (const listItemCache of listItemsCache) {
if (listItemCache.position.start.line < originalTask.taskLocation.sectionStart) {
const listItemLineNumber = listItemCache.position.start.line;
if (listItemLineNumber >= fileLinesCount) {
// One or more lines has been deleted since the cache was populated,
// so there is at least one list item in the cache that is beyond
// the end of the actual file on disk.
return undefined;
}

if (listItemLineNumber < originalTask.taskLocation.sectionStart) {
continue;
}

if (listItemCache.task === undefined) {
continue;
}

const line = fileLines[listItemCache.position.start.line];
const line = fileLines[listItemLineNumber];
if (line.includes(globalFilter)) {
if (sectionIndex === originalTask.taskLocation.sectionIndex) {
if (line === originalTask.originalMarkdown) {
taskLineNumber = listItemCache.position.start.line;
taskLineNumber = listItemLineNumber;
} else {
errorLoggingFunction(
`Tasks: Unable to find task in file ${originalTask.taskLocation.path}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Could not find line for task
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Could not find line for task
30 changes: 29 additions & 1 deletion tests/File.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ function testFindLineNumberOfTaskToToggle(
);

// Assert
verify(errorString ? errorString : 'Success. Line found OK. No error reported.');
let descriptionOfOutcome = '';
if (errorString !== undefined) {
descriptionOfOutcome = errorString;
} else if (result === undefined) {
descriptionOfOutcome = 'Could not find line for task';
} else {
descriptionOfOutcome = 'Success. Line found OK. No error reported.';
}
verify(descriptionOfOutcome);

if (expectedLineNumber !== undefined) {
expect(result).not.toBeUndefined();
Expand Down Expand Up @@ -97,5 +105,25 @@ describe('replaceTaskWithTasks', () => {
});
});

// --------------------------------------------------------------------------------
// Issue 1680
describe('issue 1680 - Cannot read properties of undefined', () => {
const jsonFileName = '1680_task_line_number_past_end_of_file.json';
const taskLineToToggle = '- [ ] #task Section 2/Task 2';

it.failing('correct behaviour', () => {
// An incorrect line is currently found, so this test fails, due to bug 1680
const expectedLineNumber = 9;
testFindLineNumberOfTaskToToggle(jsonFileName, taskLineToToggle, expectedLineNumber);
});

it('current incorrect behaviour', () => {
// An incorrect line is currently found, due to bug 688.
// It is recognised as an incorrect line, and so line number is returned as undefined.
const expectedLineNumber = undefined;
testFindLineNumberOfTaskToToggle(jsonFileName, taskLineToToggle, expectedLineNumber);
});
});

// --------------------------------------------------------------------------------
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"taskData": {
"originalMarkdown": "- [ ] #task Section 2/Task 2",
"taskLocation": {
"path": "Manual Testing/Task Toggling Scenarios/1680 - Reading Mode line numbers not updated on editing.md",
"lineNumber": 14,
"sectionStart": 13,
"sectionIndex": 1,
"precedingHeader": null
}
},
"fileData": {
"fileLines": [
"## Test Data",
"",
"### Tasks Section 1",
"- [ ] #task Section 1/Task 1",
"- [ ] #task Section 1/Task 2",
"",
"### Tasks Section 2",
"",
"- [ ] #task Section 2/Task 1",
"- [ ] #task Section 2/Task 2",
""
]
},
"cacheData": {
"listItemsCache": [
{
"position": {
"start": {
"line": 8,
"col": 0,
"offset": 39
},
"end": {
"line": 8,
"col": 28,
"offset": 67
}
},
"task": " "
},
{
"position": {
"start": {
"line": 9,
"col": 0,
"offset": 68
},
"end": {
"line": 9,
"col": 28,
"offset": 96
}
},
"task": " "
},
{
"position": {
"start": {
"line": 13,
"col": 0,
"offset": 119
},
"end": {
"line": 13,
"col": 28,
"offset": 147
}
},
"task": " "
},
{
"position": {
"start": {
"line": 14,
"col": 0,
"offset": 148
},
"end": {
"line": 14,
"col": 28,
"offset": 176
}
},
"task": " "
}
]
}
}