Skip to content

Commit e319f25

Browse files
authored
Merge pull request #3390 from ilandikov/refactor-null-from-list-item-line
refactor: `ListItem.fromListItemLine()` now correctly returns `null`
2 parents 366a8a1 + 7a7c207 commit e319f25

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

src/Task/ListItem.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,22 @@ export class ListItem {
6363
taskLocation: TaskLocation,
6464
): ListItem | null {
6565
const nonTaskMatch = RegExp(TaskRegularExpressions.nonTaskRegex).exec(originalMarkdown);
66-
let indentation = '';
67-
let listMarker = '';
68-
let statusCharacter = null;
69-
let description = '';
70-
if (nonTaskMatch) {
71-
indentation = nonTaskMatch[1];
72-
listMarker = nonTaskMatch[2];
73-
statusCharacter = nonTaskMatch[4] ?? null;
74-
description = nonTaskMatch[5].trim();
66+
if (!nonTaskMatch) {
67+
// In practice we never reach here, because the regexp matches any text even '', but the compiler doesn't know that.
68+
return null;
69+
}
70+
71+
const listMarker = nonTaskMatch[2];
72+
if (listMarker === undefined) {
73+
return null;
7574
}
7675

7776
return new ListItem({
7877
originalMarkdown,
79-
indentation,
78+
indentation: nonTaskMatch[1],
8079
listMarker,
81-
statusCharacter,
82-
description,
80+
statusCharacter: nonTaskMatch[4] ?? null,
81+
description: nonTaskMatch[5].trim(),
8382
taskLocation,
8483
parent,
8584
});

tests/Task/ListItem.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ const taskLocation = TaskLocation.fromUnknownPosition(new TasksFile('anything.md
1616

1717
describe('list item tests', () => {
1818
it('should create list item with empty children and absent parent', () => {
19-
const listItem = ListItem.fromListItemLine('', null, taskLocation)!;
19+
const listItem = ListItem.fromListItemLine('- list item', null, taskLocation)!;
2020
expect(listItem).toBeDefined();
2121
expect(listItem.children).toEqual([]);
2222
expect(listItem.parent).toEqual(null);
2323
expect(listItem.taskLocation).toBe(taskLocation);
2424
});
2525

2626
it('should create a list item with 2 children', () => {
27-
const listItem = ListItem.fromListItemLine('', null, taskLocation)!;
28-
const childItem1 = ListItem.fromListItemLine('', listItem, taskLocation)!;
29-
const childItem2 = ListItem.fromListItemLine('', listItem, taskLocation)!;
27+
const listItem = ListItem.fromListItemLine('- list item', null, taskLocation)!;
28+
const childItem1 = ListItem.fromListItemLine('- list item', listItem, taskLocation)!;
29+
const childItem2 = ListItem.fromListItemLine('- list item', listItem, taskLocation)!;
3030
expect(listItem).toBeDefined();
3131
expect(childItem1.parent).toEqual(listItem);
3232
expect(childItem2.parent).toEqual(listItem);
3333
expect(listItem.children).toEqual([childItem1, childItem2]);
3434
});
3535

3636
it('should create a list item with a parent', () => {
37-
const parentItem = ListItem.fromListItemLine('', null, taskLocation)!;
38-
const listItem = ListItem.fromListItemLine('', parentItem, taskLocation)!;
37+
const parentItem = ListItem.fromListItemLine('- list item', null, taskLocation)!;
38+
const listItem = ListItem.fromListItemLine('- list item', parentItem, taskLocation)!;
3939
expect(listItem).toBeDefined();
4040
expect(listItem.parent).toEqual(parentItem);
4141
expect(parentItem.children).toEqual([listItem]);
@@ -149,14 +149,10 @@ describe('list item parsing', () => {
149149
expect(ListItem.fromListItemLine('2. xxx', null, taskLocation)!.listMarker).toEqual('2.');
150150
});
151151

152-
it('should accept a non list item', () => {
153-
// we tried making the constructor throw if given a non list item
154-
// but it broke lots of normal Task uses in the tests (TaskBuilder)
152+
it('should detect a non-list item', () => {
155153
const item = ListItem.fromListItemLine('# Heading', null, taskLocation)!;
156154

157-
expect(item.description).toEqual('# Heading');
158-
expect(item.originalMarkdown).toEqual('# Heading');
159-
expect(item.statusCharacter).toEqual(null);
155+
expect(item).toBeNull();
160156
});
161157
});
162158

0 commit comments

Comments
 (0)