Repository: obsidian-tasks-group/obsidian-tasks
Pull request #2280 (2023-09-23 14:23:24)

refactor: remove unused return value in `createTasksList()`
# Description

Just a tiny PR to clean the code up. The `taskCount` in the return was never used, so remove it.

## Motivation and Context

Cleaner code.

## How has this been tested?

Smoke tests.

## Types of changes

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)

## Checklist

- [x] My code follows the code style of this project and passes `yarn run lint`.
- [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/Config/GlobalFilter.ts b/src/Config/GlobalFilter.ts
index 9158235a..e5c29332 100644
--- a/src/Config/GlobalFilter.ts
+++ b/src/Config/GlobalFilter.ts
@@ -1,20 +1,30 @@
 import * as RegExpTools from '../lib/RegExpTools';
-import { getSettings, updateSettings } from './Settings';
 
+/**
+ * GlobalFilter has its own data, independent of {@link Settings.globalFilter} value in {@link Settings}.
+ *
+ * See https://publish.obsidian.md/tasks/Getting+Started/Global+Filter
+ *
+ * Limitations:
+ * - All methods are static, so it is a collection of multiple static things
+ *     - This is in contrast to {@link GlobalQuery} what has just the one static method, {@link GlobalQuery.getInstance}.
+ *     - These static methods will be made non-static in a future change.
+ */
 export class GlobalFilter {
     static empty = '';
+    static _globalFilter = '';
+    static _removeGlobalFilter = false;
 
     static get(): string {
-        const { globalFilter } = getSettings();
-        return globalFilter;
+        return GlobalFilter._globalFilter;
     }
 
     static set(value: string) {
-        updateSettings({ globalFilter: value });
+        GlobalFilter._globalFilter = value;
     }
 
     static reset() {
-        updateSettings({ globalFilter: GlobalFilter.empty });
+        GlobalFilter.set(GlobalFilter.empty);
     }
 
     static isEmpty(): boolean {
@@ -35,7 +45,7 @@ export class GlobalFilter {
     }
 
     static removeAsWordFromDependingOnSettings(description: string): string {
-        const { removeGlobalFilter } = getSettings();
+        const removeGlobalFilter = GlobalFilter.getRemoveGlobalFilter();
         if (removeGlobalFilter) {
             return GlobalFilter.removeAsWordFrom(description);
         }
@@ -43,6 +53,20 @@ export class GlobalFilter {
         return description;
     }
 
+    /**
+     * @see setRemoveGlobalFilter
+     */
+    static getRemoveGlobalFilter() {
+        return GlobalFilter._removeGlobalFilter;
+    }
+
+    /**
+     * @see getRemoveGlobalFilter
+     */
+    static setRemoveGlobalFilter(removeGlobalFilter: boolean) {
+        GlobalFilter._removeGlobalFilter = removeGlobalFilter;
+    }
+
     /**
      * Search for the global filter for the purpose of removing it from the description, but do so only
      * if it is a separate word (preceding the beginning of line or a space and followed by the end of line
diff --git a/src/Config/SettingsTab.ts b/src/Config/SettingsTab.ts
index 6d4dc6f9..d12152bd 100644
--- a/src/Config/SettingsTab.ts
+++ b/src/Config/SettingsTab.ts
@@ -103,6 +103,7 @@ export class SettingsTab extends PluginSettingTab {
                 text.setPlaceholder('e.g. #task or TODO')
                     .setValue(GlobalFilter.get())
                     .onChange(async (value) => {
+                        updateSettings({ globalFilter: value });
                         GlobalFilter.set(value);
                         await this.plugin.saveSettings();
                     });
@@ -118,7 +119,7 @@ export class SettingsTab extends PluginSettingTab {
 
                 toggle.setValue(settings.removeGlobalFilter).onChange(async (value) => {
                     updateSettings({ removeGlobalFilter: value });
-
+                    GlobalFilter.setRemoveGlobalFilter(value);
                     await this.plugin.saveSettings();
                 });
             });
diff --git a/src/QueryRenderer.ts b/src/QueryRenderer.ts
index 08c2baa0..c5c905ed 100644
--- a/src/QueryRenderer.ts
+++ b/src/QueryRenderer.ts
@@ -213,17 +213,14 @@ class QueryRenderChild extends MarkdownRenderChild {
     }: {
         tasks: Task[];
         content: HTMLDivElement;
-    }): Promise<{ taskList: HTMLUListElement; tasksCount: number }> {
-        const tasksCount = tasks.length;
-
+    }): Promise<HTMLUListElement> {
         const layout = new TaskLayout(this.query.layoutOptions);
         const taskList = content.createEl('ul');
         taskList.addClasses(['contains-task-list', 'plugin-tasks-query-result']);
         taskList.addClasses(layout.taskListHiddenClasses);
         const groupingAttribute = this.getGroupingAttribute();
         if (groupingAttribute && groupingAttribute.length > 0) taskList.dataset.taskGroupBy = groupingAttribute;
-        for (let i = 0; i < tasksCount; i++) {
-            const task = tasks[i];
+        for (const [i, task] of tasks.entries()) {
             const isFilenameUnique = this.isFilenameUnique({ task });
 
             const listItem = await task.toLi({
@@ -239,8 +236,6 @@ class QueryRenderChild extends MarkdownRenderChild {
             const footnotes = listItem.querySelectorAll('[data-footnote-id]');
             footnotes.forEach((footnote) => footnote.remove());
 
-            const shortMode = this.query.layoutOptions.shortMode;
-
             const extrasSpan = listItem.createSpan('task-extras');
 
             if (!this.query.layoutOptions.hideUrgency) {
@@ -248,6 +243,7 @@ class QueryRenderChild extends MarkdownRenderChild {
             }
 
             if (!this.query.layoutOptions.hideBacklinks) {
+                const shortMode = this.query.layoutOptions.shortMode;
                 this.addBacklinks(extrasSpan, task, shortMode, isFilenameUnique);
             }
 
@@ -258,7 +254,7 @@ class QueryRenderChild extends MarkdownRenderChild {
             taskList.appendChild(listItem);
         }
 
-        return { taskList, tasksCount };
+        return taskList;
     }
 
     private addEditButton(listItem: HTMLElement, task: Task) {
@@ -296,7 +292,7 @@ class QueryRenderChild extends MarkdownRenderChild {
             // will be empty, and no headings will be added.
             this.addGroupHeadings(content, group.groupHeadings);
 
-            const { taskList } = await this.createTasksList({
+            const taskList = await this.createTasksList({
                 tasks: group.tasks,
                 content: content,
             });
diff --git a/src/main.ts b/src/main.ts
index aee6a0dd..f5326f2e 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -16,6 +16,7 @@ import { EditorSuggestor } from './Suggestor/EditorSuggestorPopup';
 import { StatusSettings } from './Config/StatusSettings';
 import type { Task } from './Task';
 import { tasksApiV1 } from './Api';
+import { GlobalFilter } from './Config/GlobalFilter';
 
 export default class TasksPlugin extends Plugin {
     private cache: Cache | undefined;
@@ -69,6 +70,8 @@ export default class TasksPlugin extends Plugin {
     async loadSettings() {
         const newSettings = await this.loadData();
         updateSettings(newSettings);
+        GlobalFilter.set(newSettings.globalFilter);
+        GlobalFilter.setRemoveGlobalFilter(newSettings.removeGlobalFilter);
         GlobalQuery.getInstance().set(newSettings.globalQuery);
         await this.loadTaskStatuses();
     }


Test patch:
diff --git a/tests/GlobalFilter.test.ts b/tests/GlobalFilter.test.ts
index 5b8838dd..35546b67 100644
--- a/tests/GlobalFilter.test.ts
+++ b/tests/GlobalFilter.test.ts
@@ -1,4 +1,4 @@
-import { resetSettings, updateSettings } from '../src/Config/Settings';
+import { resetSettings } from '../src/Config/Settings';
 import { GlobalFilter } from '../src/Config/GlobalFilter';
 
 describe('Global Filter tests', () => {
@@ -50,6 +50,16 @@ describe('Global Filter tests', () => {
         expect(GlobalFilter.includedIn('Important #task inside')).toEqual(true);
     });
 
+    it('Should check equality correctly', () => {
+        // Arrange
+        GlobalFilter.set('#task');
+
+        // Assert
+        expect(GlobalFilter.equals('#task')).toEqual(true);
+        expect(GlobalFilter.equals('#tasks')).toEqual(false);
+        expect(GlobalFilter.equals('#TODO')).toEqual(false);
+    });
+
     it('Should not match a string without Global Filter', () => {
         // Arrange
         GlobalFilter.set('testValue');
@@ -58,6 +68,14 @@ describe('Global Filter tests', () => {
         expect(GlobalFilter.includedIn('Without Global Filter')).toEqual(false);
     });
 
+    it('Should control whether to remove the global filter from displayed tasks', () => {
+        GlobalFilter.setRemoveGlobalFilter(false);
+        expect(GlobalFilter.getRemoveGlobalFilter()).toEqual(false);
+
+        GlobalFilter.setRemoveGlobalFilter(true);
+        expect(GlobalFilter.getRemoveGlobalFilter()).toEqual(true);
+    });
+
     it('Should remove Global Filter from the beginning of a string', () => {
         // Arrange
         const testValue = '#end';
@@ -102,7 +120,7 @@ describe('Global Filter tests with Remove Global Filter Setting', () => {
     it('Should remove Global Filter from a string when Setting is set to false', () => {
         // Arrange
         GlobalFilter.set('todo');
-        updateSettings({ removeGlobalFilter: false });
+        GlobalFilter.setRemoveGlobalFilter(false);
 
         // Assert
         expect(GlobalFilter.removeAsWordFromDependingOnSettings('This is absolutely todo')).toEqual(
@@ -113,7 +131,7 @@ describe('Global Filter tests with Remove Global Filter Setting', () => {
     it('Should remove Global Filter from a string when Setting is set to true', () => {
         // Arrange
         GlobalFilter.set('todo');
-        updateSettings({ removeGlobalFilter: true });
+        GlobalFilter.setRemoveGlobalFilter(true);
 
         // Assert
         expect(GlobalFilter.removeAsWordFromDependingOnSettings('This is absolutely todo')).toEqual(
@@ -134,7 +152,7 @@ describe('Global Filter tests with Remove Global Filter Setting', () => {
     ])(
         'should not remove Global Filter (%s) if it is in a substring for a task "- [ ] %s"',
         async (globalFilter: string, description: string) => {
-            updateSettings({ removeGlobalFilter: true });
+            GlobalFilter.setRemoveGlobalFilter(true);
             GlobalFilter.set(globalFilter);
 
             expect(GlobalFilter.removeAsWordFrom(description)).toEqual(description);
diff --git a/tests/Task.test.ts b/tests/Task.test.ts
index 45059eb6..348816c8 100644
--- a/tests/Task.test.ts
+++ b/tests/Task.test.ts
@@ -684,6 +684,7 @@ describe('to string', () => {
         const expectedLine = 'This is a task with #t as a global filter and also #t/some tags';
         expect(task.toString()).toStrictEqual(expectedLine);
         resetSettings();
+        GlobalFilter.reset();
     });
 });
 
@@ -1158,6 +1159,7 @@ describe('created dates on recurring task', () => {
     afterEach(() => {
         jest.useRealTimers();
         resetSettings();
+        GlobalFilter.reset();
     });
 
     it('should not set created date with disabled setting', () => {
@@ -1214,11 +1216,13 @@ describe('order of recurring tasks', () => {
         jest.useFakeTimers();
         jest.setSystemTime(new Date(2023, 5 - 1, 16));
         resetSettings();
+        GlobalFilter.reset();
     });
 
     afterAll(() => {
         jest.useRealTimers();
         resetSettings();
+        GlobalFilter.reset();
     });
 
     it('should put new task before old, by default', () => {
diff --git a/tests/TaskLineRenderer.test.ts b/tests/TaskLineRenderer.test.ts
index 4cf398f9..650d20e8 100644
--- a/tests/TaskLineRenderer.test.ts
+++ b/tests/TaskLineRenderer.test.ts
@@ -67,6 +67,8 @@ function getOtherLayoutComponents(parentElement: HTMLElement): string[] {
 describe('task line rendering', () => {
     afterEach(() => {
         resetSettings();
+        GlobalFilter.reset();
+        GlobalFilter.setRemoveGlobalFilter(false);
     });
 
     it('creates the correct span structure for a basic task', async () => {
@@ -118,7 +120,7 @@ describe('task line rendering', () => {
     };
 
     it('should render Global Filter when the Remove Global Filter is off', async () => {
-        updateSettings({ removeGlobalFilter: false });
+        GlobalFilter.setRemoveGlobalFilter(false);
         GlobalFilter.set('#global');
 
         const taskLine = '- [ ] This is a simple task with a #global filter';
@@ -128,7 +130,7 @@ describe('task line rendering', () => {
     });
 
     it('should not render Global Filter when the Remove Global Filter is on', async () => {
-        updateSettings({ removeGlobalFilter: true });
+        GlobalFilter.setRemoveGlobalFilter(true);
         GlobalFilter.set('#global');
 
         const taskLine = '- [ ] #global/subtag-shall-stay This is a simple task with a #global filter';