Skip to content

Commit ac36806

Browse files
authored
Merge pull request #3380 from obsidian-tasks-group/prevent-unused-placeholders-in-placeholders
fix: Prevent use of missing properties in placeholders (#3249)
2 parents 6fad70b + 7ff9d08 commit ac36806

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/Scripting/ExpandPlaceholders.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ function evaluateAnyFunctionCalls(template: string, view: any) {
7272
const functionOrError = parseExpression(paramsArgs, reconstructed);
7373
if (functionOrError.isValid()) {
7474
const result = evaluateExpression(functionOrError.queryComponent!, paramsArgs);
75+
if (result === null) {
76+
throw Error(
77+
`Invalid placeholder result 'null'.
78+
Check for missing file property in this expression:
79+
{{${reconstructed}}}`,
80+
);
81+
}
7582
if (result !== undefined) {
7683
return result;
7784
}

tests/Query/Query.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -892,19 +892,21 @@ group by folder
892892
`);
893893
});
894894

895-
it('visualise the need to guard against undefined query properties in placeholders', () => {
895+
it('visualise the guard against undefined query properties in placeholders', () => {
896896
const source = "{{query.file.property('no_such_property')}}";
897897
const query = new Query(source, file);
898898

899899
expect(query.error).not.toBeUndefined();
900-
// The 'null' value is because we are embedding regardless of whether the property
901-
// exists in the query file.
902900
expect(query.error).toMatchInlineSnapshot(`
903-
"do not understand query
904-
Problem statement:
905-
{{query.file.property('no_such_property')}} =>
906-
null
907-
"
901+
"There was an error expanding one or more placeholders.
902+
903+
The error message was:
904+
Invalid placeholder result 'null'.
905+
Check for missing file property in this expression:
906+
{{query.file.property('no_such_property')}}
907+
908+
The problem is in:
909+
{{query.file.property('no_such_property')}}"
908910
`);
909911
});
910912

tests/Scripting/ExpandPlaceholders.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,22 @@ The problem is in:
9090
);
9191
});
9292

93-
it.failing('should not treat absent property values as string ', () => {
94-
// TODO We really must prevent use of null - and probably undefined - as strings from property placeholders
93+
it('should not treat absent property values as string, but report the error ', () => {
9594
const rawString = '{{ query.file.property("non-existent")}}';
9695

9796
const queryContext = makeQueryContext(tasksFile);
98-
const expanded = expandPlaceholders(rawString, queryContext);
99-
expect(expanded).not.toContain('null');
97+
98+
expect(() => expandPlaceholders(rawString, queryContext)).toThrow(
99+
`There was an error expanding one or more placeholders.
100+
101+
The error message was:
102+
Invalid placeholder result 'null'.
103+
Check for missing file property in this expression:
104+
{{ query.file.property("non-existent")}}
105+
106+
The problem is in:
107+
{{ query.file.property("non-existent")}}`,
108+
);
100109
});
101110
});
102111

0 commit comments

Comments
 (0)