diff options
author | Kitson Kelly <me@kitsonkelly.com> | 2020-02-25 06:48:14 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-24 14:48:14 -0500 |
commit | 2b7e28b591a6947f76ddf9a53f49eec8f1accc0f (patch) | |
tree | cfa8f32e1e5171642fc916d77a397937e901fc48 /cli/js | |
parent | 5da7c7df1d8649ddb5628b4dc830aa8c23ccb488 (diff) |
feat: Add Deno.formatDiagnostics (#4032)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/deno.ts | 1 | ||||
-rw-r--r-- | cli/js/dispatch.ts | 1 | ||||
-rw-r--r-- | cli/js/format_error.ts | 9 | ||||
-rw-r--r-- | cli/js/format_error_test.ts | 37 | ||||
-rw-r--r-- | cli/js/lib.deno.ns.d.ts | 7 | ||||
-rw-r--r-- | cli/js/unit_tests.ts | 1 |
6 files changed, 56 insertions, 0 deletions
diff --git a/cli/js/deno.ts b/cli/js/deno.ts index 887f60826..f5f774179 100644 --- a/cli/js/deno.ts +++ b/cli/js/deno.ts @@ -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, diff --git a/cli/js/dispatch.ts b/cli/js/dispatch.ts index fc1c69a62..1b7e23463 100644 --- a/cli/js/dispatch.ts +++ b/cli/js/dispatch.ts @@ -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; diff --git a/cli/js/format_error.ts b/cli/js/format_error.ts index d1d5a73f2..63250473b 100644 --- a/cli/js/format_error.ts +++ b/cli/js/format_error.ts @@ -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 }); +} diff --git a/cli/js/format_error_test.ts b/cli/js/format_error_test.ts new file mode 100644 index 000000000..282c2b274 --- /dev/null +++ b/cli/js/format_error_test.ts @@ -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(); +} diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 0c9430869..4d7822c90 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1903,6 +1903,13 @@ declare namespace Deno { /** 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 * the Deno TypeScript compiler. */ diff --git a/cli/js/unit_tests.ts b/cli/js/unit_tests.ts index ec4505c21..1c8237466 100644 --- a/cli/js/unit_tests.ts +++ b/cli/js/unit_tests.ts @@ -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"; |