Skip to content

Commit 831be5d

Browse files
authored
Introduce truncation into node builder and symbol display part writer (#24258)
* Introduce truncation into node builder and symbol display part writer * Change default maxa truncation length to 240 * Fancy truncation + higher hard limit
1 parent 547856a commit 831be5d

File tree

7 files changed

+751
-62
lines changed

7 files changed

+751
-62
lines changed

src/compiler/checker.ts

Lines changed: 142 additions & 57 deletions
Large diffs are not rendered by default.

src/compiler/transformers/declarations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ namespace ts {
1111

1212
const declarationEmitNodeBuilderFlags =
1313
NodeBuilderFlags.MultilineObjectLiterals |
14-
TypeFormatFlags.WriteClassExpressionAsTypeLiteral |
14+
NodeBuilderFlags.WriteClassExpressionAsTypeLiteral |
1515
NodeBuilderFlags.UseTypeOfFunction |
1616
NodeBuilderFlags.UseStructuralFallback |
1717
NodeBuilderFlags.AllowEmptyTuple |
18-
NodeBuilderFlags.GenerateNamesForShadowedTypeParams;
18+
NodeBuilderFlags.GenerateNamesForShadowedTypeParams |
19+
NodeBuilderFlags.NoTruncation;
1920

2021
/**
2122
* Transforms a ts file into a .d.ts file

src/compiler/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace ts {
2121

2222
export const externalHelpersModuleNameText = "tslib";
2323

24+
export const defaultMaximumTruncationLength = 160;
25+
2426
export function getDeclarationOfKind<T extends Declaration>(symbol: Symbol, kind: T["kind"]): T | undefined {
2527
const declarations = symbol.declarations;
2628
if (declarations) {

src/services/utilities.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,14 +1435,25 @@ namespace ts {
14351435

14361436
const displayPartWriter = getDisplayPartWriter();
14371437
function getDisplayPartWriter(): DisplayPartsSymbolWriter {
1438+
const absoluteMaximumLength = defaultMaximumTruncationLength * 10; // A hard cutoff to avoid overloading the messaging channel in worst-case scenarios
14381439
let displayParts: SymbolDisplayPart[];
14391440
let lineStart: boolean;
14401441
let indent: number;
1442+
let length: number;
14411443

14421444
resetWriter();
14431445
const unknownWrite = (text: string) => writeKind(text, SymbolDisplayPartKind.text);
14441446
return {
1445-
displayParts: () => displayParts,
1447+
displayParts: () => {
1448+
const finalText = displayParts.length && displayParts[displayParts.length - 1].text;
1449+
if (length > absoluteMaximumLength && finalText && finalText !== "...") {
1450+
if (!isWhiteSpaceLike(finalText.charCodeAt(finalText.length - 1))) {
1451+
displayParts.push(displayPart(" ", SymbolDisplayPartKind.space));
1452+
}
1453+
displayParts.push(displayPart("...", SymbolDisplayPartKind.punctuation));
1454+
}
1455+
return displayParts;
1456+
},
14461457
writeKeyword: text => writeKind(text, SymbolDisplayPartKind.keyword),
14471458
writeOperator: text => writeKind(text, SymbolDisplayPartKind.operator),
14481459
writePunctuation: text => writeKind(text, SymbolDisplayPartKind.punctuation),
@@ -1472,26 +1483,34 @@ namespace ts {
14721483
};
14731484

14741485
function writeIndent() {
1486+
if (length > absoluteMaximumLength) return;
14751487
if (lineStart) {
14761488
const indentString = getIndentString(indent);
14771489
if (indentString) {
1490+
length += indentString.length;
14781491
displayParts.push(displayPart(indentString, SymbolDisplayPartKind.space));
14791492
}
14801493
lineStart = false;
14811494
}
14821495
}
14831496

14841497
function writeKind(text: string, kind: SymbolDisplayPartKind) {
1498+
if (length > absoluteMaximumLength) return;
14851499
writeIndent();
1500+
length += text.length;
14861501
displayParts.push(displayPart(text, kind));
14871502
}
14881503

14891504
function writeSymbol(text: string, symbol: Symbol) {
1505+
if (length > absoluteMaximumLength) return;
14901506
writeIndent();
1507+
length += text.length;
14911508
displayParts.push(symbolPart(text, symbol));
14921509
}
14931510

14941511
function writeLine() {
1512+
if (length > absoluteMaximumLength) return;
1513+
length += 1;
14951514
displayParts.push(lineBreakPart());
14961515
lineStart = true;
14971516
}
@@ -1500,6 +1519,7 @@ namespace ts {
15001519
displayParts = [];
15011520
lineStart = true;
15021521
indent = 0;
1522+
length = 0;
15031523
}
15041524
}
15051525

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6031,6 +6031,7 @@ declare namespace ts {
60316031
const emptyMap: ReadonlyMap<never>;
60326032
const emptyUnderscoreEscapedMap: ReadonlyUnderscoreEscapedMap<never>;
60336033
const externalHelpersModuleNameText = "tslib";
6034+
const defaultMaximumTruncationLength = 160;
60346035
function getDeclarationOfKind<T extends Declaration>(symbol: Symbol, kind: T["kind"]): T | undefined;
60356036
/** Create a new escaped identifier map. */
60366037
function createUnderscoreEscapedMap<T>(): UnderscoreEscapedMap<T>;

tests/baselines/reference/errorWithTruncatedType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/errorWithTruncatedType.ts(10,5): error TS2322: Type '{ propertyWithAnExceedinglyLongName1: string; propertyWithAnExceedinglyLongName2: string; propert...' is not assignable to type 'string'.
1+
tests/cases/compiler/errorWithTruncatedType.ts(10,5): error TS2322: Type '{ propertyWithAnExceedinglyLongName1: string; propertyWithAnExceedinglyLongName2: string; propertyWithAnExceedinglyLongName3: string; propertyWithAnExceedinglyLongName4: string; propertyWithAnExceedinglyLongName5: string; }' is not assignable to type 'string'.
22

33

44
==== tests/cases/compiler/errorWithTruncatedType.ts (1 errors) ====
@@ -13,5 +13,5 @@ tests/cases/compiler/errorWithTruncatedType.ts(10,5): error TS2322: Type '{ prop
1313
// String representation of type of 'x' should be truncated in error message
1414
var s: string = x;
1515
~
16-
!!! error TS2322: Type '{ propertyWithAnExceedinglyLongName1: string; propertyWithAnExceedinglyLongName2: string; propert...' is not assignable to type 'string'.
16+
!!! error TS2322: Type '{ propertyWithAnExceedinglyLongName1: string; propertyWithAnExceedinglyLongName2: string; propertyWithAnExceedinglyLongName3: string; propertyWithAnExceedinglyLongName4: string; propertyWithAnExceedinglyLongName5: string; }' is not assignable to type 'string'.
1717

0 commit comments

Comments
 (0)