Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(safe_template): match a method call's whitespace in linear time
Elian Dorande0bdf9b24ea
diff --git a/packages/commons/src/lib/safe_template.spec.ts b/packages/commons/src/lib/safe_template.spec.tsindex c792e90a56..60df3caca1 100644--- a/packages/commons/src/lib/safe_template.spec.ts+++ b/packages/commons/src/lib/safe_template.spec.ts@@ -188,6 +188,18 @@ describe("safe_template - rejected expressions", () => {})).toThrow(/is not a supported expression/);});++ it("rejects an unclosed method call padded with whitespace in linear time", () => {+ const expression = `A.A(${"\t".repeat(100_000)}x`;+ const start = performance.now();+ expect(() => evaluateTemplate(`\${${expression}}`, { A: {} })).toThrow(+ /is not a supported expression/+ );+ expect(findTemplateIssues(`\${${expression}}`, [ "A" ])).toMatchObject([+ { kind: "unsupported" }+ ]);+ expect(performance.now() - start).toBeLessThan(1000);+ });});describe("safe_template - findTemplateIssues", () => {diff --git a/packages/commons/src/lib/safe_template.ts b/packages/commons/src/lib/safe_template.tsindex 1433a4cbb7..b9c02e6465 100644--- a/packages/commons/src/lib/safe_template.ts+++ b/packages/commons/src/lib/safe_template.ts@@ -60,7 +60,7 @@ export interface TemplateIssue {// `{` is excluded too, so a `${` without a closing brace scans only to the next brace.const EXPRESSION = /\$\{([^{}]+)\}/g;-const METHOD_CALL = /^([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)\.([a-zA-Z_]\w*)\(\s*(?:'([^']*)'|"([^"]*)")?\s*\)$/;+const METHOD_CALL = /^([a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*)\.([a-zA-Z_]\w*)\(\s*(?:(?:'([^']*)'|"([^"]*)")\s*)?\)$/;const PROPERTY_CHAIN = /^[a-zA-Z_]\w*(?:\.[a-zA-Z_]\w*)*$/;/**@@ -92,7 +92,7 @@ export function findTemplateIssues(template: string, variableNames: readonly strlet closedUpTo = 0;for (const match of template.matchAll(EXPRESSION)) {- const from = match.index ?? 0;+ const from = match.index;const to = from + match[0].length;reportUnclosed(template.slice(closedUpTo, from), closedUpTo, issues);closedUpTo = to;