Skip to content

Build your personal knowledge base with Trilium Notes

fix(codemirror): show the whole message chain of a script error

Elian Doran9010a581e5d5
diff --git a/packages/codemirror/src/type_completion/codemirror-ts/lint/utils.ts b/packages/codemirror/src/type_completion/codemirror-ts/lint/utils.ts
index d168029b39..bacaae0699 100644
--- a/packages/codemirror/src/type_completion/codemirror-ts/lint/utils.ts
+++ b/packages/codemirror/src/type_completion/codemirror-ts/lint/utils.ts
@@ -49,11 +49,7 @@ export function isDiagnosticWithLocation(
export function tsDiagnosticMessage(
diagnostic: Pick<ts.Diagnostic, "messageText">,
): string {
- if (typeof diagnostic.messageText === "string") {
- return diagnostic.messageText;
- }
- // TODO: go through linked list
- return diagnostic.messageText.messageText;
+ return ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
}
/**
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.ts
index 17cb9d516c..68a904c177 100644
--- a/packages/codemirror/src/type_completion/index.spec.ts
+++ b/packages/codemirror/src/type_completion/index.spec.ts
@@ -4,6 +4,7 @@ import {
buildTypeCompletion,
getScriptCompletions,
getScriptDiagnosticCodes,
+ getScriptLints,
renderHoverTooltip,
SCRIPT_MIME_BACKEND,
SCRIPT_MIME_FRONTEND,
@@ -458,6 +459,16 @@ describe("JSX component typing", () => {
);
expect(codes).toContain(TS_TYPE_NOT_ASSIGNABLE);
}, TIMEOUT);
+
+ it("names the missing props in the message, not only the unassignable type", async () => {
+ const [ lint ] = await getScriptLints(
+ SCRIPT_MIME_JSX,
+ "import { Modal } from \"trilium:preact\";\nexport default () => <Modal show={true}>hi</Modal>;"
+ );
+ expect(lint?.severity).toBe("error");
+ expect(lint?.message).toMatch(/^Type .* is not assignable to type /);
+ expect(lint?.message).toMatch(/\n\s+Type .* is missing the following properties from type 'ModalProps': .*\bsize\b/);
+ }, TIMEOUT);
});
describe("buildTypeCompletion", () => {
diff --git a/packages/codemirror/src/type_completion/index.ts b/packages/codemirror/src/type_completion/index.ts
index f92f010ab3..00b5abc8c7 100644
--- a/packages/codemirror/src/type_completion/index.ts
+++ b/packages/codemirror/src/type_completion/index.ts
@@ -359,6 +359,19 @@ export async function getScriptDiagnosticCodes(mime: string, code: string, conte
.filter((code) => !ignored.includes(code));
}
+/**
+ * Returns the diagnostics the editor's `tsLinter` shows for `code` under the given
+ * script MIME type, as CodeMirror diagnostics. Intended for tests asserting how a
+ * diagnostic reads.
+ */
+export async function getScriptLints(mime: string, code: string, context: ScriptApiContext = {}) {
+ const env = await createEnv(mime, context);
+ const path = scriptPath(mime);
+ env.updateFile(path, code.length ? code : " ");
+ const { getLints } = await import("./codemirror-ts/lint/getLints.js");
+ return getLints({ env, path, diagnosticCodesToIgnore: ignoredDiagnosticCodes(mime) });
+}
+
/**
* Returns the completion entry names the language service offers at `offset` in
* `code` for the given script MIME type — the same `getCompletionsAtPosition`