feat: Add Deno.formatDiagnostics (#4032)

This commit is contained in:
Kitson Kelly 2020-02-25 06:48:14 +11:00 committed by GitHub
parent 5da7c7df1d
commit 2b7e28b591
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 83 additions and 7 deletions

View file

@ -43,6 +43,7 @@ export {
OpenOptions,
OpenMode
} from "./files.ts";
export { formatDiagnostics } from "./format_error.ts";
export { FsEvent, fsEvents } from "./fs_events.ts";
export {
EOF,

View file

@ -17,6 +17,7 @@ export let OP_GET_DIR: number;
export let OP_START: number;
export let OP_APPLY_SOURCE_MAP: number;
export let OP_FORMAT_ERROR: number;
export let OP_FORMAT_DIAGNOSTIC: number;
export let OP_CACHE: number;
export let OP_RESOLVE_MODULES: number;
export let OP_FETCH_ASSET: number;

View file

@ -1,4 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { DiagnosticItem } from "./diagnostics.ts";
import * as dispatch from "./dispatch.ts";
import { sendSync } from "./dispatch_json.ts";
@ -7,3 +8,11 @@ export function formatError(errString: string): string {
const res = sendSync(dispatch.OP_FORMAT_ERROR, { error: errString });
return res.error;
}
/**
* Format an array of diagnostic items and return them as a single string.
* @param items An array of diagnostic items to format
*/
export function formatDiagnostics(items: DiagnosticItem[]): string {
return sendSync(dispatch.OP_FORMAT_DIAGNOSTIC, { items });
}

View file

@ -0,0 +1,37 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assert, test } from "./test_util.ts";
test(function formatDiagnosticBasic() {
const fixture: Deno.DiagnosticItem[] = [
{
message: "Example error",
category: Deno.DiagnosticCategory.Error,
sourceLine: "abcdefghijklmnopqrstuv",
lineNumber: 1000,
scriptResourceName: "foo.ts",
startColumn: 1,
endColumn: 2,
code: 4000
}
];
const out = Deno.formatDiagnostics(fixture);
assert(out.includes("Example error"));
assert(out.includes("foo.ts"));
});
test(function formatDiagnosticError() {
let thrown = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const bad = ([{ hello: 123 }] as any) as Deno.DiagnosticItem[];
try {
Deno.formatDiagnostics(bad);
} catch (e) {
assert(e instanceof TypeError);
thrown = true;
}
assert(thrown);
});
if (import.meta.main) {
Deno.runTests();
}

View file

@ -1901,6 +1901,13 @@ declare namespace Deno {
items: DiagnosticItem[];
}
/** UNSTABLE: new API, yet to be vetted.
*
* Format an array of diagnostic items and return them as a single string.
* @param items An array of diagnostic items to format
*/
export function formatDiagnostics(items: DiagnosticItem[]): string;
/** UNSTABLE: new API, yet to be vetted.
*
* A specific subset TypeScript compiler options that can be supported by

View file

@ -23,6 +23,7 @@ import "./fetch_test.ts";
import "./file_test.ts";
import "./files_test.ts";
import "./form_data_test.ts";
import "./format_error_test.ts";
import "./fs_events_test.ts";
import "./get_random_values_test.ts";
import "./globals_test.ts";