Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve tsc diagnostics #7420

Merged
merged 5 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
898 changes: 536 additions & 362 deletions cli/diagnostics.rs

Large diffs are not rendered by default.

54 changes: 24 additions & 30 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,10 @@ declare namespace Deno {

/** The log category for a diagnostic message. */
export enum DiagnosticCategory {
Log = 0,
Debug = 1,
Info = 2,
Error = 3,
Warning = 4,
Suggestion = 5,
Warning = 0,
Error = 1,
Suggestion = 2,
Message = 3,
}

export interface DiagnosticMessageChain {
Expand All @@ -203,37 +201,33 @@ declare namespace Deno {
next?: DiagnosticMessageChain[];
}

export interface DiagnosticItem {
export interface Diagnostic {
/** A string message summarizing the diagnostic. */
message: string;
messageText?: string;
/** An ordered array of further diagnostics. */
messageChain?: DiagnosticMessageChain;
/** Information related to the diagnostic. This is present when there is a
* suggestion or other additional diagnostic information */
relatedInformation?: DiagnosticItem[];
relatedInformation?: Diagnostic[];
/** The text of the source line related to the diagnostic. */
sourceLine?: string;
/** The line number that is related to the diagnostic. */
lineNumber?: number;
/** The name of the script resource related to the diagnostic. */
scriptResourceName?: string;
/** The start position related to the diagnostic. */
startPosition?: number;
/** The end position related to the diagnostic. */
endPosition?: number;
source?: string;
/** The start position of the error. Zero based index. */
start?: {
line: number;
character: number;
};
/** The end position of the error. Zero based index. */
end?: {
line: number;
character: number;
};
/** The filename of the resource related to the diagnostic message. */
fileName?: string;
/** The category of the diagnostic. */
category: DiagnosticCategory;
/** A number identifier. */
code: number;
/** The the start column of the sourceLine related to the diagnostic. */
startColumn?: number;
/** The end column of the sourceLine related to the diagnostic. */
endColumn?: number;
}

export interface Diagnostic {
/** An array of diagnostic items. */
items: DiagnosticItem[];
}

/** **UNSTABLE**: new API, yet to be vetted.
Expand All @@ -247,9 +241,9 @@ declare namespace Deno {
* console.log(Deno.formatDiagnostics(diagnostics)); // User friendly output of diagnostics
* ```
*
* @param items An array of diagnostic items to format
* @param diagnostics An array of diagnostic items to format
*/
export function formatDiagnostics(items: DiagnosticItem[]): string;
export function formatDiagnostics(diagnostics: Diagnostic[]): string;

/** **UNSTABLE**: new API, yet to be vetted.
*
Expand Down Expand Up @@ -530,7 +524,7 @@ declare namespace Deno {
rootName: string,
sources?: Record<string, string>,
options?: CompilerOptions,
): Promise<[DiagnosticItem[] | undefined, Record<string, string>]>;
): Promise<[Diagnostic[] | undefined, Record<string, string>]>;

/** **UNSTABLE**: new API, yet to be vetted.
*
Expand Down Expand Up @@ -573,7 +567,7 @@ declare namespace Deno {
rootName: string,
sources?: Record<string, string>,
options?: CompilerOptions,
): Promise<[DiagnosticItem[] | undefined, string]>;
): Promise<[Diagnostic[] | undefined, string]>;

/** **UNSTABLE**: Should not have same name as `window.location` type. */
interface Location {
Expand Down
4 changes: 2 additions & 2 deletions cli/ops/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::diagnostics::Diagnostic;
use crate::diagnostics::Diagnostics;
use crate::source_maps::get_orig_position;
use crate::source_maps::CachedMaps;
use deno_core::ErrBox;
Expand Down Expand Up @@ -52,6 +52,6 @@ fn op_format_diagnostic(
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, ErrBox> {
let diagnostic = serde_json::from_value::<Diagnostic>(args)?;
let diagnostic: Diagnostics = serde_json::from_value(args)?;
Ok(json!(diagnostic.to_string()))
}
20 changes: 8 additions & 12 deletions cli/rt/40_diagnostics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@

((window) => {
const DiagnosticCategory = {
0: "Log",
1: "Debug",
2: "Info",
3: "Error",
4: "Warning",
5: "Suggestion",
0: "Warning",
1: "Error",
2: "Suggestion",
3: "Message",

Log: 0,
Debug: 1,
Info: 2,
Error: 3,
Warning: 4,
Suggestion: 5,
Warning: 0,
Error: 1,
Suggestion: 2,
Message: 3,
};

window.__bootstrap.diagnostics = {
Expand Down
4 changes: 2 additions & 2 deletions cli/rt/40_error_stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
const internals = window.__bootstrap.internals;
const dispatchJson = window.__bootstrap.dispatchJson;

function opFormatDiagnostics(items) {
return dispatchJson.sendSync("op_format_diagnostic", { items });
function opFormatDiagnostics(diagnostics) {
return dispatchJson.sendSync("op_format_diagnostic", diagnostics);
}

function opApplySourceMap(location) {
Expand Down
30 changes: 18 additions & 12 deletions cli/tests/unit/format_error_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@
import { assert, unitTest } from "./test_util.ts";

unitTest(function formatDiagnosticBasic() {
const fixture: Deno.DiagnosticItem[] = [
const fixture: Deno.Diagnostic[] = [
{
message: "Example error",
category: Deno.DiagnosticCategory.Error,
sourceLine: "abcdefghijklmnopqrstuv",
lineNumber: 1000,
scriptResourceName: "foo.ts",
startColumn: 1,
endColumn: 2,
code: 4000,
start: {
line: 0,
character: 0,
},
end: {
line: 0,
character: 7,
},
fileName: "test.ts",
messageText:
"Cannot find name 'console'. Do you need to change your target library? Try changing the `lib` compiler option to include 'dom'.",
sourceLine: `console.log("a");`,
category: 1,
code: 2584,
},
];
const out = Deno.formatDiagnostics(fixture);
assert(out.includes("Example error"));
assert(out.includes("foo.ts"));
assert(out.includes("Cannot find name"));
assert(out.includes("test.ts"));
});

unitTest(function formatDiagnosticError() {
let thrown = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const bad = ([{ hello: 123 }] as any) as Deno.DiagnosticItem[];
const bad = ([{ hello: 123 }] as any) as Deno.Diagnostic[];
try {
Deno.formatDiagnostics(bad);
} catch (e) {
Expand Down
17 changes: 8 additions & 9 deletions cli/tsc.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::colors;
use crate::diagnostics::Diagnostic;
use crate::diagnostics::DiagnosticItem;
use crate::diagnostics::Diagnostics;
use crate::disk_cache::DiskCache;
use crate::file_fetcher::SourceFile;
use crate::file_fetcher::SourceFileFetcher;
Expand Down Expand Up @@ -406,15 +405,15 @@ struct EmittedSource {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct BundleResponse {
diagnostics: Diagnostic,
diagnostics: Diagnostics,
bundle_output: Option<String>,
stats: Option<Vec<Stat>>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CompileResponse {
diagnostics: Diagnostic,
diagnostics: Diagnostics,
emit_map: HashMap<String, EmittedSource>,
build_info: Option<String>,
stats: Option<Vec<Stat>>,
Expand All @@ -435,14 +434,14 @@ struct TranspileTsOptions {
#[serde(rename_all = "camelCase")]
#[allow(unused)]
struct RuntimeBundleResponse {
diagnostics: Vec<DiagnosticItem>,
diagnostics: Diagnostics,
output: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct RuntimeCompileResponse {
diagnostics: Vec<DiagnosticItem>,
diagnostics: Diagnostics,
emit_map: HashMap<String, EmittedSource>,
}

Expand Down Expand Up @@ -656,7 +655,7 @@ impl TsCompiler {

let compile_response: CompileResponse = serde_json::from_str(&json_str)?;

if !compile_response.diagnostics.items.is_empty() {
if !compile_response.diagnostics.0.is_empty() {
return Err(ErrBox::error(compile_response.diagnostics.to_string()));
}

Expand Down Expand Up @@ -778,7 +777,7 @@ impl TsCompiler {

maybe_log_stats(bundle_response.stats);

if !bundle_response.diagnostics.items.is_empty() {
if !bundle_response.diagnostics.0.is_empty() {
return Err(ErrBox::error(bundle_response.diagnostics.to_string()));
}

Expand Down Expand Up @@ -1295,7 +1294,7 @@ pub async fn runtime_compile(

let response: RuntimeCompileResponse = serde_json::from_str(&json_str)?;

if response.diagnostics.is_empty() && sources.is_none() {
if response.diagnostics.0.is_empty() && sources.is_none() {
compiler.cache_emitted_files(response.emit_map)?;
}

Expand Down
Loading