Skip to content

Build your personal knowledge base with Trilium Notes

chore(codemirror/autocomplete): bypass return statement in function body

Elian Doran37a1cf801325
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.ts
index 36a36a8a8f..29d6fe7274 100644
--- a/packages/codemirror/src/type_completion/index.spec.ts
+++ b/packages/codemirror/src/type_completion/index.spec.ts
@@ -6,6 +6,8 @@ import { getScriptDiagnosticCodes, SCRIPT_MIME_BACKEND, SCRIPT_MIME_FRONTEND } f
const TS_IMPLICIT_ANY = 7006;
/** "Property 'x' does not exist on type 'y'." */
const TS_UNKNOWN_PROPERTY = 2339;
+/** "A 'return' statement can only be used within a function body." */
+const TS_RETURN_OUTSIDE_FUNCTION = 1108;
// Loading TypeScript + the bundled lib.*.d.ts the first time is not instant.
const TIMEOUT = 30_000;
@@ -28,6 +30,24 @@ describe("script note diagnostics", () => {
expect(codes).not.toContain(TS_IMPLICIT_ANY);
}, TIMEOUT);
+ it("does not flag a top-level return (scripts run inside a function wrapper)", async () => {
+ // Trilium wraps scripts in a function before executing them, so a
+ // top-level `return` is valid at runtime and must not be flagged.
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_FRONTEND,
+ "if (!api.currentNote) { return; }\nreturn api.showMessage('done');"
+ );
+ expect(codes).not.toContain(TS_RETURN_OUTSIDE_FUNCTION);
+ }, TIMEOUT);
+
+ it("does not flag a top-level return in backend scripts either", async () => {
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_BACKEND,
+ "const note = api.getNote('root');\nif (!note) { return; }\nreturn note.title;"
+ );
+ expect(codes).not.toContain(TS_RETURN_OUTSIDE_FUNCTION);
+ }, TIMEOUT);
+
it("still surfaces real errors (unknown api member)", async () => {
// Guards against "fixing" implicit-any 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.ts
index d4743d5ecd..0e6fd9940c 100644
--- a/packages/codemirror/src/type_completion/index.ts
+++ b/packages/codemirror/src/type_completion/index.ts
@@ -104,7 +104,7 @@ export async function buildTypeCompletion(mime: string): Promise<TypeCompletion>
extensions: [
tsFacet.of({ env, path: SCRIPT_PATH }),
tsSync(),
- tsLinter(),
+ tsLinter({ diagnosticCodesToIgnore: IGNORED_DIAGNOSTIC_CODES }),
tsHover()
],
source: tsAutocomplete()
@@ -112,13 +112,31 @@ export async function buildTypeCompletion(mime: string): Promise<TypeCompletion>
}
/**
- * Returns the TypeScript semantic diagnostic codes the script-note language
- * service reports for `code` under the given script MIME type, using the exact
- * compiler options the editor uses. Intended for tests asserting which
- * diagnostics are (and aren't) surfaced.
+ * 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.
+ */
+export const IGNORED_DIAGNOSTIC_CODES: number[] = [
+ 1108 // TS1108: A 'return' statement can only be used within a function body.
+];
+
+/**
+ * Returns the TypeScript diagnostic codes the script-note language service
+ * surfaces for `code` under the given script MIME type, using the exact compiler
+ * options and ignore list the editor uses. Mirrors `@valtown/codemirror-ts`'s
+ * `getLints` (syntactic + semantic, minus the ignored codes), so tests reflect
+ * what the user actually sees. Intended for tests asserting which diagnostics
+ * are (and aren't) surfaced.
*/
export async function getScriptDiagnosticCodes(mime: string, code: string): Promise<number[]> {
const env = await createEnv(mime);
env.updateFile(SCRIPT_PATH, code.length ? code : " ");
- return env.languageService.getSemanticDiagnostics(SCRIPT_PATH).map((d) => d.code);
+ const diagnostics = [
+ ...env.languageService.getSyntacticDiagnostics(SCRIPT_PATH),
+ ...env.languageService.getSemanticDiagnostics(SCRIPT_PATH)
+ ];
+ return diagnostics
+ .map((d) => d.code)
+ .filter((code) => !IGNORED_DIAGNOSTIC_CODES.includes(code));
}