Skip to content

Commit 94123d5

Browse files
authored
Issue a diagnostic when the node builder performs truncation despite the NoTruncation flag being set (#40477)
1 parent d6859c3 commit 94123d5

9 files changed

+75
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4311,6 +4311,9 @@ namespace ts {
43114311
approximateLength: 0
43124312
};
43134313
const resultingNode = cb(context);
4314+
if (context.truncating && context.flags & NodeBuilderFlags.NoTruncation) {
4315+
context.tracker?.reportTruncationError?.();
4316+
}
43144317
return context.encounteredError ? undefined : resultingNode;
43154318
}
43164319

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4920,6 +4920,10 @@
49204920
"category": "Error",
49214921
"code": 7055
49224922
},
4923+
"The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.": {
4924+
"category": "Error",
4925+
"code": 7056
4926+
},
49234927

49244928
"You cannot rename this element.": {
49254929
"category": "Error",

src/compiler/transformers/declarations.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ namespace ts {
7373
reportCyclicStructureError,
7474
reportPrivateInBaseOfClassExpression,
7575
reportLikelyUnsafeImportRequiredError,
76+
reportTruncationError,
7677
moduleResolverHost: host,
7778
trackReferencedAmbientModule,
7879
trackExternalModuleSymbolOfImportTypeNode,
@@ -197,6 +198,12 @@ namespace ts {
197198
}
198199
}
199200

201+
function reportTruncationError() {
202+
if (errorNameNode) {
203+
context.addDiagnostic(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_this_node_exceeds_the_maximum_length_the_compiler_will_serialize_An_explicit_type_annotation_is_needed));
204+
}
205+
}
206+
200207
function reportNonlocalAugmentation(containingFile: SourceFile, parentSymbol: Symbol, symbol: Symbol) {
201208
const primaryDeclaration = find(parentSymbol.declarations, d => getSourceFileOfNode(d) === containingFile)!;
202209
const augmentingDeclarations = filter(symbol.declarations, d => getSourceFileOfNode(d) !== containingFile);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7867,6 +7867,7 @@ namespace ts {
78677867
reportInaccessibleUniqueSymbolError?(): void;
78687868
reportCyclicStructureError?(): void;
78697869
reportLikelyUnsafeImportRequiredError?(specifier: string): void;
7870+
reportTruncationError?(): void;
78707871
moduleResolverHost?: ModuleSpecifierResolutionHost & { getCommonSourceDirectory(): string };
78717872
trackReferencedAmbientModule?(decl: ModuleDeclaration, symbol: Symbol): void;
78727873
trackExternalModuleSymbolOfImportTypeNode?(symbol: Symbol): void;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/hugeDeclarationOutputGetsTruncatedWithError.ts(5,14): error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.
2+
3+
4+
==== tests/cases/compiler/hugeDeclarationOutputGetsTruncatedWithError.ts (1 errors) ====
5+
type props = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
6+
7+
type manyprops = `${props}${props}`;
8+
9+
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};
10+
~
11+
!!! error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [hugeDeclarationOutputGetsTruncatedWithError.ts]
2+
type props = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
3+
4+
type manyprops = `${props}${props}`;
5+
6+
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};
7+
8+
//// [hugeDeclarationOutputGetsTruncatedWithError.js]
9+
"use strict";
10+
exports.__esModule = true;
11+
exports.c = void 0;
12+
exports.c = null;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/hugeDeclarationOutputGetsTruncatedWithError.ts ===
2+
type props = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
3+
>props : Symbol(props, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 0))
4+
5+
type manyprops = `${props}${props}`;
6+
>manyprops : Symbol(manyprops, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 167))
7+
>props : Symbol(props, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 0))
8+
>props : Symbol(props, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 0))
9+
10+
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};
11+
>c : Symbol(c, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 12))
12+
>K : Symbol(K, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 34))
13+
>manyprops : Symbol(manyprops, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 167))
14+
>K2 : Symbol(K2, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 53))
15+
>manyprops : Symbol(manyprops, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 167))
16+
>K : Symbol(K, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 34))
17+
>K2 : Symbol(K2, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 53))
18+

tests/baselines/reference/hugeDeclarationOutputGetsTruncatedWithError.types

Lines changed: 13 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @declaration: true
2+
type props = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
3+
4+
type manyprops = `${props}${props}`;
5+
6+
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};

0 commit comments

Comments
 (0)