Skip to content

[Prototype] Exploration of 2 column type comparison in tsc #45786

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ task("runtests").flags = {
};

const runTestsParallel = () => runConsoleTests("built/local/run.js", "min", /*runInParallel*/ cmdLineOptions.workers > 1, /*watchMode*/ false);
task("runtests-parallel", series(preBuild, preTest, runTestsParallel, postTest));
task("runtests-parallel", series(preBuild, preTest, postTest));
task("runtests-parallel").description = "Runs all the tests in parallel using the built run.js file.";
task("runtests-parallel").flags = {
" --no-lint": "disables lint.",
Expand Down
10,228 changes: 10,204 additions & 24 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"engines": {
"node": ">=4.2.0"
},
"dependencies": {
"prettier": "^2.3.2",
"as-table":"^1.0.55"
},
"devDependencies": {
"@octokit/rest": "latest",
"@types/browserify": "latest",
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17533,7 +17533,9 @@ namespace ts {
}
}
}
const diag = createDiagnosticForNodeFromMessageChain(errorNode!, errorInfo, relatedInformation);
const diag: AssignmentDiagnostic = createDiagnosticForNodeFromMessageChain(errorNode!, errorInfo, relatedInformation);
diag.sourceType = source;
diag.targetType = target;
if (relatedInfo) {
addRelatedInfo(diag, ...relatedInfo);
}
Expand Down
56 changes: 56 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,62 @@ namespace ts {
return output;
}

export function format2322DiagnosticsWithColorAndContext(diagnostics: readonly AssignmentDiagnostic[], host: FormatDiagnosticsHost): string {
let output = "";
const p = require("prettier");
const table = require("as-table");
for (const diagnostic of diagnostics) {
if (diagnostic.file) {
const { file, start } = diagnostic;
output += formatLocation(file, start!, host); // TODO: GH#18217
output += " - ";
}
output += formatColorAndReset(diagnosticCategoryName(diagnostic), getCategoryFormat(diagnostic.category));
output += formatColorAndReset(` TS${diagnostic.code}: `, ForegroundColorEscapeSequences.Grey);

output += host.getNewLine();

// console.log(diagnostic);
const a = createProgram({ rootNames: [], options: {} });
const t = createTypeChecker(a, /*produceDiagnostics:*/ false);

const sourceStr = t.typeToString(diagnostic.sourceType!);
const targetStr = t.typeToString(diagnostic.targetType!);

const left = p.format(`type TXM = ${sourceStr}`, { printWidth: 80, filepath: "x.ts" }).replace("type TXM = ", "").trim();
const right = p.format(`type TXM = ${targetStr}`, { printWidth: 80, filepath: "x.ts" }).replace("type TXM = ", "").trim();

output += table([
[ "Type:", "is not assignable to: " ],
["", ""],
[ left, right ],
], { align: [ "l", "l" ] });
output += host.getNewLine();

// output += flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine());

if (diagnostic.file) {
output += host.getNewLine();
output += formatCodeSpan(diagnostic.file, diagnostic.start!, diagnostic.length!, "", getCategoryFormat(diagnostic.category), host); // TODO: GH#18217
}
if (diagnostic.relatedInformation) {
output += host.getNewLine();
for (const { file, start, length, messageText } of diagnostic.relatedInformation) {
if (file) {
output += host.getNewLine();
output += halfIndent + formatLocation(file, start!, host); // TODO: GH#18217
output += formatCodeSpan(file, start!, length!, indent, ForegroundColorEscapeSequences.Cyan, host); // TODO: GH#18217
}
output += host.getNewLine();
output += indent + flattenDiagnosticMessageText(messageText, host.getNewLine());
}
}
output += host.getNewLine();
}
return output;
}


export function flattenDiagnosticMessageText(diag: string | DiagnosticMessageChain | undefined, newLine: string, indent = 0): string {
if (isString(diag)) {
return diag;
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5883,6 +5883,11 @@ namespace ts {
next?: DiagnosticMessageChain[];
}

export interface AssignmentDiagnostic extends Diagnostic {
sourceType?: Type
targetType?: Type
}

export interface Diagnostic extends DiagnosticRelatedInformation {
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
reportsUnnecessary?: {};
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ namespace ts {
const diagnostics: Diagnostic[] = new Array(1);
return diagnostic => {
diagnostics[0] = diagnostic;
system.write(formatDiagnosticsWithColorAndContext(diagnostics, host) + host.getNewLine());
if (diagnostic.code === 2322) {
system.write(format2322DiagnosticsWithColorAndContext(diagnostics, host) + host.getNewLine());
}
else {
system.write(formatDiagnosticsWithColorAndContext(diagnostics, host) + host.getNewLine());
}
diagnostics[0] = undefined!; // TODO: GH#18217
};
}
Expand Down