Issue a diagnostic when the node builder performs truncation despite the NoTruncation flag being set (#40477)

This commit is contained in:
Wesley Wigham 2020-09-14 12:20:57 -07:00 committed by GitHub
parent d6859c3480
commit 94123d5744
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 0 deletions

View file

@ -4311,6 +4311,9 @@ namespace ts {
approximateLength: 0
};
const resultingNode = cb(context);
if (context.truncating && context.flags & NodeBuilderFlags.NoTruncation) {
context.tracker?.reportTruncationError?.();
}
return context.encounteredError ? undefined : resultingNode;
}

View file

@ -4920,6 +4920,10 @@
"category": "Error",
"code": 7055
},
"The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.": {
"category": "Error",
"code": 7056
},
"You cannot rename this element.": {
"category": "Error",

View file

@ -73,6 +73,7 @@ namespace ts {
reportCyclicStructureError,
reportPrivateInBaseOfClassExpression,
reportLikelyUnsafeImportRequiredError,
reportTruncationError,
moduleResolverHost: host,
trackReferencedAmbientModule,
trackExternalModuleSymbolOfImportTypeNode,
@ -197,6 +198,12 @@ namespace ts {
}
}
function reportTruncationError() {
if (errorNameNode) {
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));
}
}
function reportNonlocalAugmentation(containingFile: SourceFile, parentSymbol: Symbol, symbol: Symbol) {
const primaryDeclaration = find(parentSymbol.declarations, d => getSourceFileOfNode(d) === containingFile)!;
const augmentingDeclarations = filter(symbol.declarations, d => getSourceFileOfNode(d) !== containingFile);

View file

@ -7867,6 +7867,7 @@ namespace ts {
reportInaccessibleUniqueSymbolError?(): void;
reportCyclicStructureError?(): void;
reportLikelyUnsafeImportRequiredError?(specifier: string): void;
reportTruncationError?(): void;
moduleResolverHost?: ModuleSpecifierResolutionHost & { getCommonSourceDirectory(): string };
trackReferencedAmbientModule?(decl: ModuleDeclaration, symbol: Symbol): void;
trackExternalModuleSymbolOfImportTypeNode?(symbol: Symbol): void;

View file

@ -0,0 +1,11 @@
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.
==== tests/cases/compiler/hugeDeclarationOutputGetsTruncatedWithError.ts (1 errors) ====
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";
type manyprops = `${props}${props}`;
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};
~
!!! error TS7056: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.

View file

@ -0,0 +1,12 @@
//// [hugeDeclarationOutputGetsTruncatedWithError.ts]
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";
type manyprops = `${props}${props}`;
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};
//// [hugeDeclarationOutputGetsTruncatedWithError.js]
"use strict";
exports.__esModule = true;
exports.c = void 0;
exports.c = null;

View file

@ -0,0 +1,18 @@
=== tests/cases/compiler/hugeDeclarationOutputGetsTruncatedWithError.ts ===
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";
>props : Symbol(props, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 0))
type manyprops = `${props}${props}`;
>manyprops : Symbol(manyprops, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 167))
>props : Symbol(props, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 0))
>props : Symbol(props, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 0))
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};
>c : Symbol(c, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 12))
>K : Symbol(K, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 34))
>manyprops : Symbol(manyprops, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 167))
>K2 : Symbol(K2, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 53))
>manyprops : Symbol(manyprops, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 0, 167))
>K : Symbol(K, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 34))
>K2 : Symbol(K2, Decl(hugeDeclarationOutputGetsTruncatedWithError.ts, 4, 53))

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,6 @@
// @declaration: true
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";
type manyprops = `${props}${props}`;
export const c = null as any as {[K in manyprops]: {[K2 in manyprops]: `${K}.${K2}`}};