Repository: obsidian-tasks-group/obsidian-tasks
Pull request #1221 (2022-10-12 10:30:29)

refactor: Rename Filter type to FilterFunction
<!--- Provide a general summary of your changes in the Title above -->

# Description

Pure automated refactoring, done automatically and safely by WebStorm's refactoring tools.

- Rename Filter type to FilterFunction
- Rename FilterOrErrorMessage.filter to filterFunction

## Motivation and Context

These are in preparation for introducing a Filter class

## How has this been tested?

By running the existing tests.

## 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)

## 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! -->

- [ ] 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/Query/Filter/BooleanField.ts b/src/Query/Filter/BooleanField.ts
index 0dffce71..adbe4047 100644
--- a/src/Query/Filter/BooleanField.ts
+++ b/src/Query/Filter/BooleanField.ts
@@ -5,7 +5,7 @@ import { parseFilter } from '../FilterParser';
 import type { Task } from '../../Task';
 import { Field } from './Field';
 import { FilterOrErrorMessage } from './Filter';
-import type { Filter } from './Filter';
+import type { FilterFunction } from './Filter';
 
 /**
  * BooleanField is a 'container' field type that parses a high-level filtering query of
@@ -26,7 +26,7 @@ export class BooleanField extends Field {
     // Second pattern matches (filter1) - that is, ensures that a single filter is treated as valid
     private readonly basicBooleanRegexp = /(.*(AND|OR|XOR|NOT)\s*[("].*|\(.+\))/g;
     private readonly supportedOperators = ['AND', 'OR', 'XOR', 'NOT'];
-    private subFields: Record<string, Filter> = {};
+    private subFields: Record<string, FilterFunction> = {};
 
     protected filterRegExp(): RegExp {
         return this.basicBooleanRegexp;
@@ -73,8 +73,8 @@ export class BooleanField extends Field {
                         if (parsedField.error) {
                             result.error = `couldn't parse sub-expression '${identifier}': ${parsedField.error}`;
                             return result;
-                        } else if (parsedField.filter) {
-                            this.subFields[identifier] = parsedField.filter;
+                        } else if (parsedField.filterFunction) {
+                            this.subFields[identifier] = parsedField.filterFunction;
                         }
                     }
                 } else if (token.name === 'OPERATOR') {
@@ -93,7 +93,7 @@ export class BooleanField extends Field {
                 }
             }
             // Return the filter function that can run the complete query
-            result.filter = (task: Task) => {
+            result.filterFunction = (task: Task) => {
                 return this.filterTaskWithParsedQuery(task, postfixExpression);
             };
             return result;
diff --git a/src/Query/Filter/DateField.ts b/src/Query/Filter/DateField.ts
index 89f07f06..92f1f346 100644
--- a/src/Query/Filter/DateField.ts
+++ b/src/Query/Filter/DateField.ts
@@ -27,12 +27,12 @@ export abstract class DateField extends Field {
         const result = new FilterOrErrorMessage();
 
         if (line === this.instructionForFieldPresence) {
-            result.filter = (task: Task) => this.date(task) !== null;
+            result.filterFunction = (task: Task) => this.date(task) !== null;
             return result;
         }
 
         if (line === this.instructionForFieldAbsence) {
-            result.filter = (task: Task) => this.date(task) === null;
+            result.filterFunction = (task: Task) => this.date(task) === null;
             return result;
         }
 
@@ -43,17 +43,17 @@ export abstract class DateField extends Field {
                 result.error = 'do not understand ' + this.fieldName() + ' date';
             } else {
                 if (match[1] === 'before') {
-                    result.filter = (task: Task) => {
+                    result.filterFunction = (task: Task) => {
                         const date = this.date(task);
                         return date ? date.isBefore(filterDate) : this.filterResultIfFieldMissing();
                     };
                 } else if (match[1] === 'after') {
-                    result.filter = (task: Task) => {
+                    result.filterFunction = (task: Task) => {
                         const date = this.date(task);
                         return date ? date.isAfter(filterDate) : this.filterResultIfFieldMissing();
                     };
                 } else {
-                    result.filter = (task: Task) => {
+                    result.filterFunction = (task: Task) => {
                         const date = this.date(task);
                         return date ? date.isSame(filterDate) : this.filterResultIfFieldMissing();
                     };
diff --git a/src/Query/Filter/Filter.ts b/src/Query/Filter/Filter.ts
index d6bf9c8f..f4da4a60 100644
--- a/src/Query/Filter/Filter.ts
+++ b/src/Query/Filter/Filter.ts
@@ -4,7 +4,7 @@ import type { Task } from '../../Task';
  * A filtering function, that takes a Task object and returns
  * whether it matches a particular filtering instruction.
  */
-export type Filter = (task: Task) => boolean;
+export type FilterFunction = (task: Task) => boolean;
 
 /**
  * A class which stores one of:
@@ -20,16 +20,16 @@ export type Filter = (task: Task) => boolean;
  * problem line, and perhaps listing allowed options).
  */
 export class FilterOrErrorMessage {
-    filter: Filter | undefined;
+    filterFunction: FilterFunction | undefined;
     error: string | undefined;
 
     /**
      * Construct a FilterOrErrorMessage with the filter.
      * @param filter
      */
-    public static fromFilter(filter: Filter): FilterOrErrorMessage {
+    public static fromFilter(filter: FilterFunction): FilterOrErrorMessage {
         const result = new FilterOrErrorMessage();
-        result.filter = filter;
+        result.filterFunction = filter;
         return result;
     }
 
diff --git a/src/Query/Filter/FilterInstruction.ts b/src/Query/Filter/FilterInstruction.ts
index 040acb17..61acf9ca 100644
--- a/src/Query/Filter/FilterInstruction.ts
+++ b/src/Query/Filter/FilterInstruction.ts
@@ -1,5 +1,5 @@
 import { FilterOrErrorMessage } from './Filter';
-import type { Filter } from './Filter';
+import type { FilterFunction } from './Filter';
 
 /**
  * Implementation of a single instruction for filtering tasks, and its corresponding predicate.
@@ -13,14 +13,14 @@ import type { Filter } from './Filter';
  */
 export class FilterInstruction {
     private readonly _instruction: string;
-    private readonly _filter: Filter;
+    private readonly _filter: FilterFunction;
 
     /**
      * Constructor:
      * @param instruction - Full text of the instruction for the filter: must be matched exactly
      * @param filter
      */
-    constructor(instruction: string, filter: Filter) {
+    constructor(instruction: string, filter: FilterFunction) {
         this._instruction = instruction;
         this._filter = filter;
     }
@@ -33,7 +33,7 @@ export class FilterInstruction {
         const result = new FilterOrErrorMessage();
 
         if (line === this._instruction) {
-            result.filter = this._filter;
+            result.filterFunction = this._filter;
             return result;
         }
 
diff --git a/src/Query/Filter/FilterInstructions.ts b/src/Query/Filter/FilterInstructions.ts
index d1140cd5..0bb82c8b 100644
--- a/src/Query/Filter/FilterInstructions.ts
+++ b/src/Query/Filter/FilterInstructions.ts
@@ -1,6 +1,6 @@
 import { FilterInstruction } from './FilterInstruction';
 import { FilterOrErrorMessage } from './Filter';
-import type { Filter } from './Filter';
+import type { FilterFunction } from './Filter';
 
 /**
  * Implementation of a collection of instructions for filtering tasks.
@@ -14,7 +14,7 @@ import type { Filter } from './Filter';
 export class FilterInstructions {
     private readonly _filters: FilterInstruction[] = [];
 
-    public add(instruction: string, filter: Filter) {
+    public add(instruction: string, filter: FilterFunction) {
         this._filters.push(new FilterInstruction(instruction, filter));
     }
 
diff --git a/src/Query/Filter/HappensDateField.ts b/src/Query/Filter/HappensDateField.ts
index 056aac41..2e889c3e 100644
--- a/src/Query/Filter/HappensDateField.ts
+++ b/src/Query/Filter/HappensDateField.ts
@@ -29,13 +29,13 @@ export class HappensDateField extends Field {
 
         if (line === HappensDateField.instructionForFieldPresence) {
             const result = new FilterOrErrorMessage();
-            result.filter = (task: Task) => this.dates(task).some((date) => date !== null);
+            result.filterFunction = (task: Task) => this.dates(task).some((date) => date !== null);
             return result;
         }
 
         if (line === HappensDateField.instructionForFieldAbsence) {
             const result = new FilterOrErrorMessage();
-            result.filter = (task: Task) => !this.dates(task).some((date) => date !== null);
+            result.filterFunction = (task: Task) => !this.dates(task).some((date) => date !== null);
             return result;
         }
 
@@ -46,15 +46,15 @@ export class HappensDateField extends Field {
                 result.error = 'do not understand happens date';
             } else {
                 if (happensMatch[1] === 'before') {
-                    result.filter = (task: Task) => {
+                    result.filterFunction = (task: Task) => {
                         return this.dates(task).some((date) => date && date.isBefore(filterDate));
                     };
                 } else if (happensMatch[1] === 'after') {
-                    result.filter = (task: Task) => {
+                    result.filterFunction = (task: Task) => {
                         return this.dates(task).some((date) => date && date.isAfter(filterDate));
                     };
                 } else {
-                    result.filter = (task: Task) => {
+                    result.filterFunction = (task: Task) => {
                         return this.dates(task).some((date) => date && date.isSame(filterDate));
                     };
                 }
diff --git a/src/Query/Filter/MultiTextField.ts b/src/Query/Filter/MultiTextField.ts
index 80824f85..e75fa774 100644
--- a/src/Query/Filter/MultiTextField.ts
+++ b/src/Query/Filter/MultiTextField.ts
@@ -1,7 +1,7 @@
 import type { Task } from '../../Task';
 import type { IStringMatcher } from '../Matchers/IStringMatcher';
 import { TextField } from './TextField';
-import type { Filter } from './Filter';
+import type { FilterFunction } from './Filter';
 
 /**
  * MultiTextField is an abstract base class to help implement
@@ -52,7 +52,7 @@ export abstract class MultiTextField extends TextField {
      */
     public abstract values(task: Task): string[];
 
-    protected getFilter(matcher: IStringMatcher, negate: boolean): Filter {
+    protected getFilter(matcher: IStringMatcher, negate: boolean): FilterFunction {
         return (task: Task) => {
             const match = matcher!.matchesAnyOf(this.values(task));
             return negate ? !match : match;
diff --git a/src/Query/Filter/PriorityField.ts b/src/Query/Filter/PriorityField.ts
index 5762cb1a..b2965eda 100644
--- a/src/Query/Filter/PriorityField.ts
+++ b/src/Query/Filter/PriorityField.ts
@@ -41,7 +41,7 @@ export class PriorityField extends Field {
                 filter = (task: Task) => (task.priority ? task.priority === filterPriority : false);
             }
 
-            result.filter = filter;
+            result.filterFunction = filter;
         } else {
             result.error = 'do not understand query filter (priority)';
         }
diff --git a/src/Query/Filter/TextField.ts b/src/Query/Filter/TextField.ts
index 20a45459..b46535ae 100644
--- a/src/Query/Filter/TextField.ts
+++ b/src/Query/Filter/TextField.ts
@@ -3,7 +3,7 @@ import { SubstringMatcher } from '../Matchers/SubstringMatcher';
 import { RegexMatcher } from '../Matchers/RegexMatcher';
 import type { IStringMatcher } from '../Matchers/IStringMatcher';
 import { Field } from './Field';
-import type { Filter } from './Filter';
+import type { FilterFunction } from './Filter';
 import { FilterOrErrorMessage } from './Filter';
 
 /**
@@ -80,7 +80,7 @@ export abstract class TextField extends Field {
      */
     public abstract value(task: Task): string;
 
-    protected getFilter(matcher: IStringMatcher, negate: boolean): Filter {
+    protected getFilter(matcher: IStringMatcher, negate: boolean): FilterFunction {
         return (task: Task) => {
             const match = matcher!.matches(this.value(task));
             return negate ? !match : match;
diff --git a/src/Query/Query.ts b/src/Query/Query.ts
index 5b84b8c8..ec5c175f 100644
--- a/src/Query/Query.ts
+++ b/src/Query/Query.ts
@@ -182,7 +182,7 @@ export class Query implements IQuery {
     private parseFilter(line: string) {
         const filterOrError = parseFilter(line);
         if (filterOrError != null) {
-            if (filterOrError.filter) this._filters.push(filterOrError.filter);
+            if (filterOrError.filterFunction) this._filters.push(filterOrError.filterFunction);
             else this._error = filterOrError.error;
             return true;
         }


Test patch:
diff --git a/tests/CustomMatchers/CustomMatchersForFilters.ts b/tests/CustomMatchers/CustomMatchersForFilters.ts
index 5b410fd7..9c148673 100644
--- a/tests/CustomMatchers/CustomMatchersForFilters.ts
+++ b/tests/CustomMatchers/CustomMatchersForFilters.ts
@@ -82,7 +82,7 @@ declare global {
 }
 
 export function toBeValid(filter: FilterOrErrorMessage) {
-    if (filter.filter === undefined) {
+    if (filter.filterFunction === undefined) {
         return {
             message: () => 'unexpected null filter: check your instruction matches your filter class',
             pass: false,
@@ -103,7 +103,7 @@ export function toBeValid(filter: FilterOrErrorMessage) {
 }
 
 export function toMatchTask(filter: FilterOrErrorMessage, task: Task) {
-    const matches = filter.filter!(task);
+    const matches = filter.filterFunction!(task);
     if (!matches) {
         return {
             message: () => `unexpected failure to match task: ${task.toFileLineString()}`,
diff --git a/tests/Query/Filter/PriorityField.test.ts b/tests/Query/Filter/PriorityField.test.ts
index 55f1e7f4..0d841488 100644
--- a/tests/Query/Filter/PriorityField.test.ts
+++ b/tests/Query/Filter/PriorityField.test.ts
@@ -65,7 +65,7 @@ describe('priority error cases', () => {
     it('priority is something', () => {
         const field = new PriorityField();
         const filter = field.createFilterOrErrorMessage('priority is no-such-priority');
-        expect(filter.filter).toBeUndefined();
+        expect(filter.filterFunction).toBeUndefined();
         expect(filter.error).toBe('do not understand query filter (priority)');
     });
 });
diff --git a/tests/TestingTools/FilterTestHelpers.ts b/tests/TestingTools/FilterTestHelpers.ts
index 44720d19..311b6694 100644
--- a/tests/TestingTools/FilterTestHelpers.ts
+++ b/tests/TestingTools/FilterTestHelpers.ts
@@ -24,9 +24,9 @@ export function testFilter(filter: FilterOrErrorMessage, taskBuilder: TaskBuilde
  * @param expected true if the task should match the filter, and false otherwise.
  */
 export function testTaskFilter(filter: FilterOrErrorMessage, task: Task, expected: boolean) {
-    expect(filter.filter).toBeDefined();
+    expect(filter.filterFunction).toBeDefined();
     expect(filter.error).toBeUndefined();
-    expect(filter.filter!(task)).toEqual(expected);
+    expect(filter.filterFunction!(task)).toEqual(expected);
 }
 
 /**