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
feat(codemirror/autocomplete): handle top-level await based on type of script
Elian Doran5568882b722e
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.tsindex 6a5cc782e9..b34a332431 100644--- a/packages/codemirror/src/type_completion/index.spec.ts+++ b/packages/codemirror/src/type_completion/index.spec.ts@@ -4,6 +4,8 @@ import { getScriptDiagnosticCodes, SCRIPT_MIME_BACKEND, SCRIPT_MIME_FRONTEND } f/** "Property 'x' does not exist on type 'y'." */const TS_UNKNOWN_PROPERTY = 2339;+/** "'await' expressions are only allowed at the top level of a file when that file is a module…" */+const TS_TOP_LEVEL_AWAIT = 1375;// Loading TypeScript + the bundled lib.*.d.ts the first time is not instant.const TIMEOUT = 30_000;@@ -46,6 +48,24 @@ describe("script note diagnostics", () => {expect(codes).toEqual([]);}, TIMEOUT);+ it("supports top-level await in frontend scripts — async wrapper (TS1375)", async () => {+ const codes = await getScriptDiagnosticCodes(+ SCRIPT_MIME_FRONTEND,+ "const value = await Promise.resolve(1);"+ );+ expect(codes).toEqual([]);+ }, TIMEOUT);++ it("flags top-level await in backend scripts — non-async wrapper (TS1375)", async () => {+ // The backend wrapper is a plain function, so top-level await is genuinely+ // invalid there and must stay flagged.+ const codes = await getScriptDiagnosticCodes(+ SCRIPT_MIME_BACKEND,+ "const value = await Promise.resolve(1);"+ );+ expect(codes).toContain(TS_TOP_LEVEL_AWAIT);+ }, TIMEOUT);+it("still surfaces real errors (unknown api member)", async () => {// Guards against "fixing" false positives by disabling all checking:// genuine semantic errors must still be reported.diff --git a/packages/codemirror/src/type_completion/index.ts b/packages/codemirror/src/type_completion/index.tsindex 0e6fd9940c..d6a3baf118 100644--- a/packages/codemirror/src/type_completion/index.ts+++ b/packages/codemirror/src/type_completion/index.ts@@ -104,22 +104,34 @@ export async function buildTypeCompletion(mime: string): Promise<TypeCompletion>extensions: [tsFacet.of({ env, path: SCRIPT_PATH }),tsSync(),- tsLinter({ diagnosticCodesToIgnore: IGNORED_DIAGNOSTIC_CODES }),+ tsLinter({ diagnosticCodesToIgnore: ignoredDiagnosticCodes(mime) }),tsHover()],source: tsAutocomplete()};}+/** TS1108: A 'return' statement can only be used within a function body. */+const TS_RETURN_OUTSIDE_FUNCTION = 1108;+/** TS1375: top-level 'await' is only allowed when the file is a module. */+const TS_TOP_LEVEL_AWAIT = 1375;+/**- * Diagnostic codes suppressed for script notes. Trilium wraps every script in a- * function before executing it (see the client's `bundle.ts` and the server's- * `script.ts`), so grammar that TypeScript only permits inside a function body- * is actually valid at runtime.+ * Diagnostic codes suppressed for the given script MIME type. Trilium wraps every+ * script in a function before executing it (see the client's `bundle.ts` and the+ * server's `script.ts`), so grammar that's only valid inside a function body is+ * fine at runtime:+ * - both wrappers are functions, so a top-level `return` is always valid;+ * - only the frontend wrapper is `async`, so top-level `await` is valid in+ * frontend scripts but genuinely invalid in backend ones.*/-export const IGNORED_DIAGNOSTIC_CODES: number[] = [- 1108 // TS1108: A 'return' statement can only be used within a function body.-];+function ignoredDiagnosticCodes(mime: string): number[] {+ const codes = [TS_RETURN_OUTSIDE_FUNCTION];+ if (mime === SCRIPT_MIME_FRONTEND) {+ codes.push(TS_TOP_LEVEL_AWAIT);+ }+ return codes;+}/*** Returns the TypeScript diagnostic codes the script-note language service@@ -136,7 +148,8 @@ export async function getScriptDiagnosticCodes(mime: string, code: string): Prom...env.languageService.getSyntacticDiagnostics(SCRIPT_PATH),...env.languageService.getSemanticDiagnostics(SCRIPT_PATH)];+ const ignored = ignoredDiagnosticCodes(mime);return diagnostics.map((d) => d.code)- .filter((code) => !IGNORED_DIAGNOSTIC_CODES.includes(code));+ .filter((code) => !ignored.includes(code));}