Skip to content

Build your personal knowledge base with Trilium Notes

chore(codemirror/autocomplete): bypass no implicit any

Elian Dorandc07ebe2dba6
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.ts
new file mode 100644
index 0000000000..36a36a8a8f
--- /dev/null
+++ b/packages/codemirror/src/type_completion/index.spec.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it } from "vitest";
+
+import { getScriptDiagnosticCodes, SCRIPT_MIME_BACKEND, SCRIPT_MIME_FRONTEND } from "./index.js";
+
+/** "Parameter 'x' implicitly has an 'any' type." */
+const TS_IMPLICIT_ANY = 7006;
+/** "Property 'x' does not exist on type 'y'." */
+const TS_UNKNOWN_PROPERTY = 2339;
+
+// Loading TypeScript + the bundled lib.*.d.ts the first time is not instant.
+const TIMEOUT = 30_000;
+
+describe("script note diagnostics", () => {
+ it("does not flag untyped parameters as implicit any", async () => {
+ // Loose JS — untyped params are normal in script notes and must not warn.
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_FRONTEND,
+ "function handleClick(event) { return event; }\nconst add = (a, b) => a + b;"
+ );
+ expect(codes).not.toContain(TS_IMPLICIT_ANY);
+ }, TIMEOUT);
+
+ it("does not flag untyped parameters in backend scripts either", async () => {
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_BACKEND,
+ "function process(note, depth) { return note; }"
+ );
+ expect(codes).not.toContain(TS_IMPLICIT_ANY);
+ }, 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.
+ const codes = await getScriptDiagnosticCodes(
+ SCRIPT_MIME_FRONTEND,
+ "api.thisMethodDoesNotExist();"
+ );
+ expect(codes).toContain(TS_UNKNOWN_PROPERTY);
+ }, TIMEOUT);
+});
diff --git a/packages/codemirror/src/type_completion/index.ts b/packages/codemirror/src/type_completion/index.ts
index 72171d8563..d4743d5ecd 100644
--- a/packages/codemirror/src/type_completion/index.ts
+++ b/packages/codemirror/src/type_completion/index.ts
@@ -32,6 +32,11 @@ const COMPILER_OPTIONS = {
// `target`/`lib` are filled in lazily once TypeScript is loaded (needs the ts enums).
allowJs: true,
checkJs: true,
+ // Script notes are loose JS — `@typescript/vfs` forces `strict: true`, which makes
+ // every untyped parameter a TS7006 ("implicitly has an 'any' type") error. Keep the
+ // useful semantic checks (unknown api members, wrong arg counts) but don't nag about
+ // missing type annotations.
+ noImplicitAny: false,
// `@typescript/vfs` defaults moduleResolution to the legacy `node10`, which
// TypeScript 6 rejects unless deprecations are explicitly silenced.
ignoreDeprecations: "6.0"
@@ -105,3 +110,15 @@ export async function buildTypeCompletion(mime: string): Promise<TypeCompletion>
source: tsAutocomplete()
};
}
+
+/**
+ * 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.
+ */
+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);
+}
diff --git a/packages/codemirror/tsconfig.spec.json b/packages/codemirror/tsconfig.spec.json
index 86ad305d93..4d633c5e52 100644
--- a/packages/codemirror/tsconfig.spec.json
+++ b/packages/codemirror/tsconfig.spec.json
@@ -4,7 +4,8 @@
"outDir": "./out-tsc/vitest",
"types": [
"node",
- "vitest"
+ "vitest",
+ "vite/client"
]
},
"include": [