Skip to content

Build your personal knowledge base with Trilium Notes

fix(safe_template): flag a method called on a value without it

Elian Doran0c32e4c05fcd
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 02284ae329..9b094a0b06 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -959,6 +959,7 @@
"error_unknown_variable": "There is no variable named “{{name}}”.",
"error_property_not_allowed": "The property “{{name}}” cannot be used in a template.",
"error_method_not_allowed": "The method “{{name}}” cannot be used in a template.",
+ "error_method_not_on_value": "The value before “{{name}}” has no such method.",
"variable_note": "The note being renamed",
"member_title": "The note's title",
"member_note_id": "The note's ID",
diff --git a/apps/client/src/widgets/bulk_actions/TemplateField.tsx b/apps/client/src/widgets/bulk_actions/TemplateField.tsx
index 7896053ed4..2893ea9121 100644
--- a/apps/client/src/widgets/bulk_actions/TemplateField.tsx
+++ b/apps/client/src/widgets/bulk_actions/TemplateField.tsx
@@ -4,7 +4,7 @@ import "./TemplateField.css";
import { hostedCompletion, type HostedCompletionState } from "@triliumnext/codemirror/src/extensions/hosted_completion";
import { triliumTemplate } from "@triliumnext/codemirror/src/extensions/trilium_template";
import { createFieldEditor, type FieldEditor } from "@triliumnext/codemirror/src/field_editor";
-import { dayjs, evaluateTemplate, type TemplateVariables } from "@triliumnext/commons";
+import { dayjs, evaluateTemplate, type TemplateVariables, type TemplateVariableTypes } from "@triliumnext/commons";
import clsx from "clsx";
import { createContext } from "preact";
import { useContext, useEffect, useRef, useState } from "preact/hooks";
@@ -15,7 +15,7 @@ import froca from "../../services/froca";
import { t } from "../../services/i18n";
import { CommandMentionList, createHostedList, filterCommandEntries } from "../react/NoteAutocomplete";
import { SEARCH_FIELD_EDITOR_CLASS } from "../search_field_editor";
-import { type TemplateCompletion, templateCompletionAt, type TemplateEntry, type TemplateVariableTypes } from "./template_completions";
+import { type TemplateCompletion, templateCompletionAt, type TemplateEntry } from "./template_completions";
/** The note a template is previewed against, such as the first of the notes a bulk action runs on. */
export const TemplatePreviewNoteContext = createContext<string | undefined>(undefined);
@@ -51,7 +51,7 @@ export default function TemplateField({ currentValue, onChange, variables, place
singleLine: true,
onChange: (value) => onChangeRef.current(value),
extensions: [
- triliumTemplate(Object.keys(variables), templateLintMessages()),
+ triliumTemplate(variables, templateLintMessages()),
hostedCompletion({
match: (before) => templateCompletionAt(before, variables),
list: createTemplateCompletionList
@@ -78,7 +78,8 @@ function templateLintMessages() {
unsupported: t("template_field.error_unsupported"),
unknownVariable: (name: string) => t("template_field.error_unknown_variable", { name }),
propertyNotAllowed: (name: string) => t("template_field.error_property_not_allowed", { name }),
- methodNotAllowed: (name: string) => t("template_field.error_method_not_allowed", { name })
+ methodNotAllowed: (name: string) => t("template_field.error_method_not_allowed", { name }),
+ methodNotOnValue: (name: string) => t("template_field.error_method_not_on_value", { name })
};
}
diff --git a/apps/client/src/widgets/bulk_actions/template_completions.ts b/apps/client/src/widgets/bulk_actions/template_completions.ts
index 94c2775424..a337f92455 100644
--- a/apps/client/src/widgets/bulk_actions/template_completions.ts
+++ b/apps/client/src/widgets/bulk_actions/template_completions.ts
@@ -1,4 +1,5 @@
import type { HostedCompletionInsert } from "@triliumnext/codemirror/src/extensions/hosted_completion";
+import type { TemplateValueType, TemplateVariableTypes } from "@triliumnext/commons";
import { t } from "../../services/i18n";
import type { CommandEntry } from "../react/NoteAutocomplete";
@@ -9,12 +10,6 @@ import type { CommandEntry } from "../react/NoteAutocomplete";
* and that give a readable value on the server's `BNote`, are offered.
*/
-/** What a template variable or member holds, which decides the members offered after it. */
-export type TemplateValueType = "note" | "date" | "text";
-
-/** The variables a template can read, by name, and what each holds. */
-export type TemplateVariableTypes = Readonly<Record<string, TemplateValueType>>;
-
export interface TemplateMember {
name: string;
kind: "property" | "method";
diff --git a/packages/codemirror/src/extensions/trilium_template.spec.ts b/packages/codemirror/src/extensions/trilium_template.spec.ts
index 8ad7d7bbf2..0e25401555 100644
--- a/packages/codemirror/src/extensions/trilium_template.spec.ts
+++ b/packages/codemirror/src/extensions/trilium_template.spec.ts
@@ -7,7 +7,8 @@ const MESSAGES: TemplateLintMessages = {
unsupported: "unsupported",
unknownVariable: (name) => `variable:${name}`,
propertyNotAllowed: (name) => `property:${name}`,
- methodNotAllowed: (name) => `method:${name}`
+ methodNotAllowed: (name) => `method:${name}`,
+ methodNotOnValue: (name) => `not-on-value:${name}`
};
function tokensOf(text: string) {
@@ -35,16 +36,20 @@ describe("tokenizeTemplate", () => {
describe("diagnoseTemplate", () => {
it("underlines each refused expression with the consumer's wording", () => {
- const diagnostics = diagnoseTemplate("${note.title} ${note.constructor} ${other} ${note.title", [ "note" ], MESSAGES);
+ const diagnostics = diagnoseTemplate(
+ "${note.title} ${note.constructor} ${other} ${note.title.format('Y')} ${note.title",
+ { note: "note" },
+ MESSAGES
+ );
expect(diagnostics.map((d) => d.message)).toStrictEqual([
- "property:constructor", "variable:other", "unclosed"
+ "property:constructor", "variable:other", "not-on-value:format", "unclosed"
]);
expect(diagnostics.every((d) => d.severity === "error")).toBe(true);
expect([ diagnostics[0].from, diagnostics[0].to ]).toStrictEqual([ 14, 33 ]);
});
it("has nothing to say about a valid template", () => {
- expect(diagnoseTemplate("NEW: ${note.title}", [ "note" ], MESSAGES)).toStrictEqual([]);
+ expect(diagnoseTemplate("NEW: ${note.title}", { note: "note" }, MESSAGES)).toStrictEqual([]);
});
});
diff --git a/packages/codemirror/src/extensions/trilium_template.ts b/packages/codemirror/src/extensions/trilium_template.ts
index 24adea2758..5e800c395c 100644
--- a/packages/codemirror/src/extensions/trilium_template.ts
+++ b/packages/codemirror/src/extensions/trilium_template.ts
@@ -1,7 +1,7 @@
import { type Diagnostic, linter } from "@codemirror/lint";
import { type EditorState, type Extension, RangeSetBuilder, StateField } from "@codemirror/state";
import { Decoration, type DecorationSet, EditorView } from "@codemirror/view";
-import { findTemplateIssues } from "@triliumnext/commons";
+import { findTemplateIssues, type TemplateVariableTypes } from "@triliumnext/commons";
/**
* Highlighting and linting for the templates `evaluateTemplate()` in `@triliumnext/commons` fills
@@ -83,11 +83,12 @@ export interface TemplateLintMessages {
unknownVariable(name: string): string;
propertyNotAllowed(name: string): string;
methodNotAllowed(name: string): string;
+ methodNotOnValue(name: string): string;
}
/** Marks the expressions `evaluateTemplate()` refuses, given the variables it is called with. */
-export function diagnoseTemplate(text: string, variableNames: readonly string[], messages: TemplateLintMessages): Diagnostic[] {
- return findTemplateIssues(text, variableNames).map(({ kind, from, to, name }) => ({
+export function diagnoseTemplate(text: string, variables: TemplateVariableTypes, messages: TemplateLintMessages): Diagnostic[] {
+ return findTemplateIssues(text, variables).map(({ kind, from, to, name }) => ({
from,
to,
severity: "error",
@@ -130,12 +131,12 @@ function buildTemplateDecorations(state: EditorState): DecorationSet {
/**
* The extension to register on an editor holding a template whose expressions can read
- * `variableNames`. It colours the expressions and underlines the ones the evaluator refuses.
+ * `variables`. It colours the expressions and underlines the ones the evaluator refuses.
*/
-export function triliumTemplate(variableNames: readonly string[], messages: TemplateLintMessages): Extension {
+export function triliumTemplate(variables: TemplateVariableTypes, messages: TemplateLintMessages): Extension {
return [
templateHighlightField,
templateHighlightTheme,
- linter((view) => diagnoseTemplate(view.state.doc.toString(), variableNames, messages), { delay: 250 })
+ linter((view) => diagnoseTemplate(view.state.doc.toString(), variables, messages), { delay: 250 })
];
}
diff --git a/packages/commons/src/lib/safe_template.spec.ts b/packages/commons/src/lib/safe_template.spec.ts
index 60df3caca1..472c4796de 100644
--- a/packages/commons/src/lib/safe_template.spec.ts
+++ b/packages/commons/src/lib/safe_template.spec.ts
@@ -195,7 +195,7 @@ describe("safe_template - rejected expressions", () => {
expect(() => evaluateTemplate(`\${${expression}}`, { A: {} })).toThrow(
/is not a supported expression/
);
- expect(findTemplateIssues(`\${${expression}}`, [ "A" ])).toMatchObject([
+ expect(findTemplateIssues(`\${${expression}}`, { A: "text" })).toMatchObject([
{ kind: "unsupported" }
]);
expect(performance.now() - start).toBeLessThan(1000);
@@ -203,7 +203,7 @@ describe("safe_template - rejected expressions", () => {
});
describe("safe_template - findTemplateIssues", () => {
- const variables = [ "note" ];
+ const variables = { note: "note" } as const;
it("accepts what evaluateTemplate accepts", () => {
expect(findTemplateIssues("NEW: ${note.title}", variables)).toStrictEqual([]);
@@ -227,14 +227,28 @@ describe("safe_template - findTemplateIssues", () => {
}
});
+ it("reports a method called on a value that does not have it", () => {
+ const template = "${note.title.format('YYYY')} ${note.format('YYYY')} ${note.dateCreatedObj.getLabelValue('x')}";
+ expect(findTemplateIssues(template, variables).map(({ kind, name }) => ({ kind, name }))).toStrictEqual([
+ { kind: "methodNotOnValue", name: "format" },
+ { kind: "methodNotOnValue", name: "format" },
+ { kind: "methodNotOnValue", name: "getLabelValue" }
+ ]);
+ expect(findTemplateIssues("${now.format('YYYY')} ${now.title.format('YYYY')}", { now: "date" })).toStrictEqual([]);
+ });
+
it("reports a ${ that is never closed, which evaluateTemplate leaves as text", () => {
const issues = findTemplateIssues("NEW: ${note.title", variables);
expect(issues).toStrictEqual([ { kind: "unclosed", from: 5, to: 17, name: undefined } ]);
});
it("agrees with evaluateTemplate on every case", () => {
- const note = fakeNote();
- for (const template of [ "${note.title}", "${note.nope}", "${note.eval('x')}", "${note['title']}", "${other}" ]) {
+ const note = fakeNote({ format: undefined, dateCreatedObj: { format: (fmt: string) => fmt } });
+ for (const template of [
+ "${note.title}", "${note.nope}", "${note.eval('x')}", "${note['title']}", "${other}", "${constructor}",
+ "${note.dateCreatedObj.format('YYYY')}", "${note.title.format('YYYY')}", "${note.format('YYYY')}",
+ "${note.dateCreatedObj.getLabelValue('x')}", "${note.dateCreatedObj.title.format('YYYY')}"
+ ]) {
const throws = (() => {
try {
evaluateTemplate(template, { note });
diff --git a/packages/commons/src/lib/safe_template.ts b/packages/commons/src/lib/safe_template.ts
index b9c02e6465..c6ed765b35 100644
--- a/packages/commons/src/lib/safe_template.ts
+++ b/packages/commons/src/lib/safe_template.ts
@@ -46,8 +46,23 @@ export interface TemplateVariables {
[key: string]: unknown;
}
+/** What a template value holds: a `BNote`, a `dayjs` date, or a string or other plain value. */
+export type TemplateValueType = "note" | "date" | "text";
+
+/** The variables a template can read, by name, and what each holds. */
+export type TemplateVariableTypes = Readonly<Record<string, TemplateValueType>>;
+
+const TEMPLATE_METHODS_OF: Readonly<Record<TemplateValueType, ReadonlySet<string>>> = {
+ note: new Set([ "getLabelValue", "getLabel", "getLabelValues", "getRelationValue", "getAttributeValue" ]),
+ date: new Set([ "format" ]),
+ text: new Set()
+};
+
+const NOTE_DATE_PROPERTIES: ReadonlySet<string> = new Set([ "dateCreatedObj", "utcDateCreatedObj" ]);
+
/** Why {@link findTemplateIssues} refuses an expression, which `evaluateTemplate()` would throw for. */
-export type TemplateIssueKind = "unclosed" | "unsupported" | "unknownVariable" | "propertyNotAllowed" | "methodNotAllowed";
+export type TemplateIssueKind =
+ | "unclosed" | "unsupported" | "unknownVariable" | "propertyNotAllowed" | "methodNotAllowed" | "methodNotOnValue";
export interface TemplateIssue {
kind: TemplateIssueKind;
@@ -82,13 +97,12 @@ export function evaluateTemplate(template: string, variables: TemplateVariables)
}
/**
- * Lists the expressions of `template` that `evaluateTemplate()` refuses when given `variableNames`,
- * without evaluating anything, so an editor can mark them as they are typed. Also lists a `${` that
- * is never closed, which `evaluateTemplate()` leaves in the result as text.
+ * Lists the expressions of `template` that `evaluateTemplate()` refuses when given values of
+ * `variables`, without evaluating anything, so an editor can mark them as they are typed. Also lists
+ * a `${` that is never closed, which `evaluateTemplate()` leaves in the result as text.
*/
-export function findTemplateIssues(template: string, variableNames: readonly string[]): TemplateIssue[] {
+export function findTemplateIssues(template: string, variables: TemplateVariableTypes): TemplateIssue[] {
const issues: TemplateIssue[] = [];
- const known = new Set(variableNames);
let closedUpTo = 0;
for (const match of template.matchAll(EXPRESSION)) {
@@ -97,7 +111,7 @@ export function findTemplateIssues(template: string, variableNames: readonly str
reportUnclosed(template.slice(closedUpTo, from), closedUpTo, issues);
closedUpTo = to;
- const problem = checkExpression(parseExpression(match[1].trim()), known);
+ const problem = checkExpression(parseExpression(match[1].trim()), variables);
if (problem) {
issues.push({ ...problem, from, to });
}
@@ -130,8 +144,8 @@ function parseExpression(expr: string): ParsedExpression | null {
return PROPERTY_CHAIN.test(expr) ? { chain: expr.split(".") } : null;
}
-/** What `evaluateExpression()` throws for `parsed`, short of what depends on the values. */
-function checkExpression(parsed: ParsedExpression | null, known: ReadonlySet<string>): Omit<TemplateIssue, "from" | "to"> | null {
+/** What `evaluateExpression()` throws for `parsed` when the variables hold values of `variables`. */
+function checkExpression(parsed: ParsedExpression | null, variables: TemplateVariableTypes): Omit<TemplateIssue, "from" | "to"> | null {
if (!parsed) {
return { kind: "unsupported", name: undefined };
}
@@ -140,12 +154,33 @@ function checkExpression(parsed: ParsedExpression | null, known: ReadonlySet<str
}
const [ root, ...properties ] = parsed.chain;
- if (!known.has(root)) {
+ if (!Object.hasOwn(variables, root)) {
return { kind: "unknownVariable", name: root };
}
const refused = properties.find((property) => !TEMPLATE_ALLOWED_PROPERTIES.has(property));
- return refused ? { kind: "propertyNotAllowed", name: refused } : null;
+ if (refused) {
+ return { kind: "propertyNotAllowed", name: refused };
+ }
+
+ // A method on a missing value gives an empty result, so only a value of a known type is checked.
+ const target = resolveChainType(variables[root], properties);
+ if (parsed.method && target && !TEMPLATE_METHODS_OF[target].has(parsed.method.name)) {
+ return { kind: "methodNotOnValue", name: parsed.method.name };
+ }
+ return null;
+}
+
+/** What reading `properties` off a value of `type` gives, or `undefined` when it gives nothing. */
+function resolveChainType(type: TemplateValueType, properties: readonly string[]): TemplateValueType | undefined {
+ let current: TemplateValueType | undefined = type;
+ for (const property of properties) {
+ if (current !== "note") {
+ return undefined;
+ }
+ current = NOTE_DATE_PROPERTIES.has(property) ? "date" : "text";
+ }
+ return current;
}
/**
@@ -197,7 +232,7 @@ function evaluateExpression(expr: string, variables: TemplateVariables): unknown
function resolvePropertyChain(parts: string[], variables: TemplateVariables): unknown {
const rootName = parts[0];
- if (!(rootName in variables)) {
+ if (!Object.hasOwn(variables, rootName)) {
throw new Error(`Unknown variable '${rootName}' in template expression`);
}