Skip to content

Commit 8b8b7e4

Browse files
committed
Very rough prototype of 2 col type comparison in tsc
1 parent 07fd7bc commit 8b8b7e4

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
"engines": {
2929
"node": ">=4.2.0"
3030
},
31+
"dependencies": {
32+
"prettier": "^2.3.2",
33+
"as-table":"^1.0.55"
34+
},
3135
"devDependencies": {
3236
"@octokit/rest": "latest",
3337
"@types/browserify": "latest",

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17533,7 +17533,9 @@ namespace ts {
1753317533
}
1753417534
}
1753517535
}
17536-
const diag = createDiagnosticForNodeFromMessageChain(errorNode!, errorInfo, relatedInformation);
17536+
const diag: AssignmentDiagnostic = createDiagnosticForNodeFromMessageChain(errorNode!, errorInfo, relatedInformation);
17537+
diag.sourceType = source;
17538+
diag.targetType = target;
1753717539
if (relatedInfo) {
1753817540
addRelatedInfo(diag, ...relatedInfo);
1753917541
}

src/compiler/program.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,62 @@ namespace ts {
484484
return output;
485485
}
486486

487+
export function format2322DiagnosticsWithColorAndContext(diagnostics: readonly AssignmentDiagnostic[], host: FormatDiagnosticsHost): string {
488+
let output = "";
489+
const p = require("prettier");
490+
const table = require("as-table");
491+
for (const diagnostic of diagnostics) {
492+
if (diagnostic.file) {
493+
const { file, start } = diagnostic;
494+
output += formatLocation(file, start!, host); // TODO: GH#18217
495+
output += " - ";
496+
}
497+
output += formatColorAndReset(diagnosticCategoryName(diagnostic), getCategoryFormat(diagnostic.category));
498+
output += formatColorAndReset(` TS${diagnostic.code}: `, ForegroundColorEscapeSequences.Grey);
499+
500+
output += host.getNewLine();
501+
502+
// console.log(diagnostic);
503+
const a = createProgram({ rootNames: [], options: {} });
504+
const t = createTypeChecker(a, false);
505+
506+
const sourceStr = t.typeToString(diagnostic.sourceType!);
507+
const targetStr = t.typeToString(diagnostic.targetType!);
508+
509+
const left = p.format(`type TXM = ${sourceStr}`, { printWidth: 80, filepath: "x.ts" }).replace("type TXM = ", "").trim();
510+
const right = p.format(`type TXM = ${targetStr}`, { printWidth: 80, filepath: "x.ts" }).replace("type TXM = ", "").trim();
511+
512+
output += table([
513+
[ "Type:", "is not assignable to: " ],
514+
["", ""],
515+
[ left, right ],
516+
], { align: [ "l", "l" ] });
517+
output += host.getNewLine();
518+
519+
// output += flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine());
520+
521+
if (diagnostic.file) {
522+
output += host.getNewLine();
523+
output += formatCodeSpan(diagnostic.file, diagnostic.start!, diagnostic.length!, "", getCategoryFormat(diagnostic.category), host); // TODO: GH#18217
524+
}
525+
if (diagnostic.relatedInformation) {
526+
output += host.getNewLine();
527+
for (const { file, start, length, messageText } of diagnostic.relatedInformation) {
528+
if (file) {
529+
output += host.getNewLine();
530+
output += halfIndent + formatLocation(file, start!, host); // TODO: GH#18217
531+
output += formatCodeSpan(file, start!, length!, indent, ForegroundColorEscapeSequences.Cyan, host); // TODO: GH#18217
532+
}
533+
output += host.getNewLine();
534+
output += indent + flattenDiagnosticMessageText(messageText, host.getNewLine());
535+
}
536+
}
537+
output += host.getNewLine();
538+
}
539+
return output;
540+
}
541+
542+
487543
export function flattenDiagnosticMessageText(diag: string | DiagnosticMessageChain | undefined, newLine: string, indent = 0): string {
488544
if (isString(diag)) {
489545
return diag;

src/compiler/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5883,6 +5883,11 @@ namespace ts {
58835883
next?: DiagnosticMessageChain[];
58845884
}
58855885

5886+
export interface AssignmentDiagnostic extends Diagnostic {
5887+
sourceType?: Type
5888+
targetType?: Type
5889+
}
5890+
58865891
export interface Diagnostic extends DiagnosticRelatedInformation {
58875892
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
58885893
reportsUnnecessary?: {};

src/compiler/watch.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ namespace ts {
1818
if (!pretty) {
1919
return diagnostic => system.write(formatDiagnostic(diagnostic, host));
2020
}
21+
2122

2223
const diagnostics: Diagnostic[] = new Array(1);
2324
return diagnostic => {
2425
diagnostics[0] = diagnostic;
25-
system.write(formatDiagnosticsWithColorAndContext(diagnostics, host) + host.getNewLine());
26+
if (diagnostic.code === 2322) {
27+
system.write(format2322DiagnosticsWithColorAndContext(diagnostics, host) + host.getNewLine());
28+
}
29+
else {
30+
system.write(formatDiagnosticsWithColorAndContext(diagnostics, host) + host.getNewLine());
31+
}
2632
diagnostics[0] = undefined!; // TODO: GH#18217
2733
};
2834
}

0 commit comments

Comments
 (0)