Skip to content

Commit 6260f7a

Browse files
Armando AguirreArmando Aguirre Sepulveda
Armando Aguirre
and
Armando Aguirre Sepulveda
authored
Added typeToTypeNode with truncation (#59332)
Co-authored-by: Armando Aguirre Sepulveda <[email protected]>
1 parent 0e292c4 commit 6260f7a

19 files changed

+160
-45
lines changed

src/compiler/checker.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
addRange,
77
addRelatedInfo,
88
addSyntheticLeadingComment,
9+
addSyntheticTrailingComment,
910
AliasDeclarationNode,
1011
AllAccessorDeclarations,
1112
AmbientModuleDeclaration,
@@ -7054,6 +7055,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
70547055

70557056
function createTypeNodesFromResolvedType(resolvedType: ResolvedType): TypeElement[] | undefined {
70567057
if (checkTruncationLength(context)) {
7058+
if (context.flags & NodeBuilderFlags.NoTruncation) {
7059+
return [addSyntheticTrailingComment(factory.createNotEmittedTypeElement(), SyntaxKind.MultiLineCommentTrivia, "elided")];
7060+
}
70577061
return [factory.createPropertySignature(/*modifiers*/ undefined, "...", /*questionToken*/ undefined, /*type*/ undefined)];
70587062
}
70597063
const typeElements: TypeElement[] = [];
@@ -7085,7 +7089,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
70857089
}
70867090
}
70877091
if (checkTruncationLength(context) && (i + 2 < properties.length - 1)) {
7088-
typeElements.push(factory.createPropertySignature(/*modifiers*/ undefined, `... ${properties.length - i} more ...`, /*questionToken*/ undefined, /*type*/ undefined));
7092+
if (context.flags & NodeBuilderFlags.NoTruncation) {
7093+
const typeElement = typeElements.pop()!;
7094+
typeElements.push(addSyntheticTrailingComment(typeElement, SyntaxKind.MultiLineCommentTrivia, `... ${properties.length - i} more elided ...`));
7095+
}
7096+
else {
7097+
typeElements.push(factory.createPropertySignature(/*modifiers*/ undefined, `... ${properties.length - i} more ...`, /*questionToken*/ undefined, /*type*/ undefined));
7098+
}
70897099
addPropertyToElementList(properties[properties.length - 1], context, typeElements);
70907100
break;
70917101
}
@@ -7100,7 +7110,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
71007110
if (!(context.flags & NodeBuilderFlags.NoTruncation)) {
71017111
return factory.createTypeReferenceNode(factory.createIdentifier("..."), /*typeArguments*/ undefined);
71027112
}
7103-
return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
7113+
return addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, "elided");
71047114
}
71057115

71067116
function shouldUsePlaceholderForProperty(propertySymbol: Symbol, context: NodeBuilderContext) {
@@ -7260,12 +7270,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
72607270
if (some(types)) {
72617271
if (checkTruncationLength(context)) {
72627272
if (!isBareList) {
7263-
return [factory.createTypeReferenceNode("...", /*typeArguments*/ undefined)];
7273+
return [
7274+
context.flags & NodeBuilderFlags.NoTruncation
7275+
? addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, "elided")
7276+
: factory.createTypeReferenceNode("...", /*typeArguments*/ undefined),
7277+
];
72647278
}
72657279
else if (types.length > 2) {
72667280
return [
72677281
typeToTypeNodeHelper(types[0], context),
7268-
factory.createTypeReferenceNode(`... ${types.length - 2} more ...`, /*typeArguments*/ undefined),
7282+
context.flags & NodeBuilderFlags.NoTruncation
7283+
? addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, `... ${types.length - 2} more elided ...`)
7284+
: factory.createTypeReferenceNode(`... ${types.length - 2} more ...`, /*typeArguments*/ undefined),
72697285
typeToTypeNodeHelper(types[types.length - 1], context),
72707286
];
72717287
}
@@ -7278,7 +7294,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
72787294
for (const type of types) {
72797295
i++;
72807296
if (checkTruncationLength(context) && (i + 2 < types.length - 1)) {
7281-
result.push(factory.createTypeReferenceNode(`... ${types.length - i} more ...`, /*typeArguments*/ undefined));
7297+
result.push(
7298+
context.flags & NodeBuilderFlags.NoTruncation
7299+
? addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, `... ${types.length - i} more elided ...`)
7300+
: factory.createTypeReferenceNode(`... ${types.length - i} more ...`, /*typeArguments*/ undefined),
7301+
);
72827302
const typeNode = typeToTypeNodeHelper(types[types.length - 1], context);
72837303
if (typeNode) {
72847304
result.push(typeNode);

src/compiler/emitter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
18801880

18811881
// Transformation nodes
18821882
case SyntaxKind.NotEmittedStatement:
1883+
case SyntaxKind.NotEmittedTypeElement:
18831884
return;
18841885
}
18851886
if (isExpression(node)) {

src/compiler/factory/nodeFactory.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ import {
332332
NonNullExpression,
333333
NoSubstitutionTemplateLiteral,
334334
NotEmittedStatement,
335+
NotEmittedTypeElement,
335336
NullLiteral,
336337
nullNodeConverters,
337338
nullParenthesizerRules,
@@ -1007,6 +1008,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
10071008
createSyntheticExpression,
10081009
createSyntaxList,
10091010
createNotEmittedStatement,
1011+
createNotEmittedTypeElement,
10101012
createPartiallyEmittedExpression,
10111013
updatePartiallyEmittedExpression,
10121014
createCommaListExpression,
@@ -6263,6 +6265,11 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
62636265
: node;
62646266
}
62656267

6268+
// @api
6269+
function createNotEmittedTypeElement() {
6270+
return createBaseNode<NotEmittedTypeElement>(SyntaxKind.NotEmittedTypeElement);
6271+
}
6272+
62666273
function flattenCommaElements(node: Expression): Expression | readonly Expression[] {
62676274
if (nodeIsSynthesized(node) && !isParseTreeNode(node) && !node.original && !node.emitNode && !node.id) {
62686275
if (isCommaListExpression(node)) {

src/compiler/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ export const enum SyntaxKind {
445445

446446
// Transformation nodes
447447
NotEmittedStatement,
448+
NotEmittedTypeElement,
448449
PartiallyEmittedExpression,
449450
CommaListExpression,
450451
SyntheticReferenceExpression,
@@ -3310,6 +3311,10 @@ export interface NotEmittedStatement extends Statement {
33103311
readonly kind: SyntaxKind.NotEmittedStatement;
33113312
}
33123313

3314+
export interface NotEmittedTypeElement extends TypeElement {
3315+
readonly kind: SyntaxKind.NotEmittedTypeElement;
3316+
}
3317+
33133318
/**
33143319
* A list of comma-separated expressions. This node is only created by transformations.
33153320
*/
@@ -9165,6 +9170,7 @@ export interface NodeFactory {
91659170
//
91669171

91679172
createNotEmittedStatement(original: Node): NotEmittedStatement;
9173+
createNotEmittedTypeElement(): NotEmittedTypeElement;
91689174
createPartiallyEmittedExpression(expression: Expression, original?: Node): PartiallyEmittedExpression;
91699175
updatePartiallyEmittedExpression(node: PartiallyEmittedExpression, expression: Expression): PartiallyEmittedExpression;
91709176
/** @internal */ createSyntheticReferenceExpression(expression: Expression, thisArg: Expression): SyntheticReferenceExpression;

src/compiler/utilitiesPublic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,8 @@ export function isTypeElement(node: Node): node is TypeElement {
17671767
|| kind === SyntaxKind.MethodSignature
17681768
|| kind === SyntaxKind.IndexSignature
17691769
|| kind === SyntaxKind.GetAccessor
1770-
|| kind === SyntaxKind.SetAccessor;
1770+
|| kind === SyntaxKind.SetAccessor
1771+
|| kind === SyntaxKind.NotEmittedTypeElement;
17711772
}
17721773

17731774
export function isClassOrTypeElement(node: Node): node is ClassElement | TypeElement {

src/services/codefixes/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ export function addNewNodeForMemberSymbol(
217217
const optional = !!(symbol.flags & SymbolFlags.Optional);
218218
const ambient = !!(enclosingDeclaration.flags & NodeFlags.Ambient) || isAmbient;
219219
const quotePreference = getQuotePreference(sourceFile, preferences);
220+
const flags = NodeBuilderFlags.NoTruncation
221+
| (quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : NodeBuilderFlags.None);
220222

221223
switch (kind) {
222224
case SyntaxKind.PropertySignature:
223225
case SyntaxKind.PropertyDeclaration:
224-
let flags = NodeBuilderFlags.NoTruncation;
225-
flags |= quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : 0;
226226
let typeNode = checker.typeToTypeNode(type, enclosingDeclaration, flags, InternalNodeBuilderFlags.AllowUnresolvedNames, getNoopSymbolTrackerWithResolver(context));
227227
if (importAdder) {
228228
const importableReference = tryGetAutoImportableReferenceFromTypeNode(typeNode, scriptTarget);
@@ -242,7 +242,7 @@ export function addNewNodeForMemberSymbol(
242242
case SyntaxKind.GetAccessor:
243243
case SyntaxKind.SetAccessor: {
244244
Debug.assertIsDefined(declarations);
245-
let typeNode = checker.typeToTypeNode(type, enclosingDeclaration, /*flags*/ undefined, /*internalFlags*/ undefined, getNoopSymbolTrackerWithResolver(context));
245+
let typeNode = checker.typeToTypeNode(type, enclosingDeclaration, flags, /*internalFlags*/ undefined, getNoopSymbolTrackerWithResolver(context));
246246
const allAccessors = getAllAccessorDeclarations(declarations, declaration as AccessorDeclaration);
247247
const orderedAccessors = allAccessors.secondAccessor
248248
? [allAccessors.firstAccessor, allAccessors.secondAccessor]

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3983,10 +3983,11 @@ declare namespace ts {
39833983
JSDocImportTag = 351,
39843984
SyntaxList = 352,
39853985
NotEmittedStatement = 353,
3986-
PartiallyEmittedExpression = 354,
3987-
CommaListExpression = 355,
3988-
SyntheticReferenceExpression = 356,
3989-
Count = 357,
3986+
NotEmittedTypeElement = 354,
3987+
PartiallyEmittedExpression = 355,
3988+
CommaListExpression = 356,
3989+
SyntheticReferenceExpression = 357,
3990+
Count = 358,
39903991
FirstAssignment = 64,
39913992
LastAssignment = 79,
39923993
FirstCompoundAssignment = 65,
@@ -5212,6 +5213,9 @@ declare namespace ts {
52125213
interface NotEmittedStatement extends Statement {
52135214
readonly kind: SyntaxKind.NotEmittedStatement;
52145215
}
5216+
interface NotEmittedTypeElement extends TypeElement {
5217+
readonly kind: SyntaxKind.NotEmittedTypeElement;
5218+
}
52155219
/**
52165220
* A list of comma-separated expressions. This node is only created by transformations.
52175221
*/
@@ -7813,6 +7817,7 @@ declare namespace ts {
78137817
createSourceFile(statements: readonly Statement[], endOfFileToken: EndOfFileToken, flags: NodeFlags): SourceFile;
78147818
updateSourceFile(node: SourceFile, statements: readonly Statement[], isDeclarationFile?: boolean, referencedFiles?: readonly FileReference[], typeReferences?: readonly FileReference[], hasNoDefaultLib?: boolean, libReferences?: readonly FileReference[]): SourceFile;
78157819
createNotEmittedStatement(original: Node): NotEmittedStatement;
7820+
createNotEmittedTypeElement(): NotEmittedTypeElement;
78167821
createPartiallyEmittedExpression(expression: Expression, original?: Node): PartiallyEmittedExpression;
78177822
updatePartiallyEmittedExpression(node: PartiallyEmittedExpression, expression: Expression): PartiallyEmittedExpression;
78187823
createCommaListExpression(elements: readonly Expression[]): CommaListExpression;

tests/baselines/reference/classExpressionInClassStaticDeclarations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ var C = /** @class */ (function () {
3939
declare class C {
4040
static D: {
4141
new (): {};
42-
D: any;
42+
D: /*elided*/ any;
4343
};
4444
}

tests/baselines/reference/commonJSImportNestedClassTypeReference.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function f(k) {
4141
//// [mod1.d.ts]
4242
export var K: {
4343
new (): {
44-
values(): any;
44+
values(): /*elided*/ any;
4545
};
4646
};
4747
//// [main.d.ts]

tests/baselines/reference/declFileTypeofFunction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ declare function b1(): typeof b1;
7070
declare function foo(): typeof foo;
7171
declare var foo1: typeof foo;
7272
declare var foo2: typeof foo;
73-
declare var foo3: () => any;
74-
declare var x: () => any;
73+
declare var foo3: () => /*elided*/ any;
74+
declare var x: () => /*elided*/ any;
7575
declare function foo5(x: number): (x: number) => number;

tests/baselines/reference/declarationEmitInferredTypeAlias4.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ function f() {
1616

1717
//// [declarationEmitInferredTypeAlias4.d.ts]
1818
declare function f<A>(): A[] | {
19-
x: A[] | any;
19+
x: A[] | /*elided*/ any;
2020
};

tests/baselines/reference/declarationsWithRecursiveInternalTypesProduceUniqueTypeParams.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void p3.result.three;
9696
//// [declarationsWithRecursiveInternalTypesProduceUniqueTypeParams.d.ts]
9797
export type Key<U> = keyof U;
9898
export type Value<K extends Key<U>, U> = U[K];
99-
export declare const updateIfChanged: <T>(t: T) => (<K extends keyof T>(key: K) => (<K_1 extends keyof Value<K, T>>(key: K_1) => (<K_2 extends keyof Value<K_1, Value<K, T>>>(key: K_2) => (<K_3 extends keyof Value<K_2, Value<K_1, Value<K, T>>>>(key: K_3) => (<K_4 extends keyof Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>(key: K_4) => (<K_5 extends keyof Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>(key: K_5) => (<K_6 extends keyof Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>(key: K_6) => (<K_7 extends keyof Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>(key: K_7) => (<K_8 extends keyof Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>(key: K_8) => (<K_9 extends keyof Value<K_8, Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>>(key: K_9) => (<K_10 extends keyof Value<K_9, Value<K_8, Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>>>(key: K_10) => any & {
99+
export declare const updateIfChanged: <T>(t: T) => (<K extends keyof T>(key: K) => (<K_1 extends keyof Value<K, T>>(key: K_1) => (<K_2 extends keyof Value<K_1, Value<K, T>>>(key: K_2) => (<K_3 extends keyof Value<K_2, Value<K_1, Value<K, T>>>>(key: K_3) => (<K_4 extends keyof Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>(key: K_4) => (<K_5 extends keyof Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>(key: K_5) => (<K_6 extends keyof Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>(key: K_6) => (<K_7 extends keyof Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>(key: K_7) => (<K_8 extends keyof Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>(key: K_8) => (<K_9 extends keyof Value<K_8, Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>>(key: K_9) => (<K_10 extends keyof Value<K_9, Value<K_8, Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>>>(key: K_10) => /*elided*/ any & {
100100
map: (updater: (u: Value<K_10, Value<K_9, Value<K_8, Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>>>) => Value<K_10, Value<K_9, Value<K_8, Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>>>) => T;
101101
set: (newU: Value<K_10, Value<K_9, Value<K_8, Value<K_7, Value<K_6, Value<K_5, Value<K_4, Value<K_3, Value<K_2, Value<K_1, Value<K, T>>>>>>>>>>>) => T;
102102
}) & {
@@ -155,7 +155,7 @@ export declare const testRecFun: <T extends Object>(parent: T) => {
155155
result: T & U & U_1 & U_2 & U_3 & U_4 & U_5 & U_6 & U_7 & U_8;
156156
deeper: <U_9 extends Object>(child: U_9) => {
157157
result: T & U & U_1 & U_2 & U_3 & U_4 & U_5 & U_6 & U_7 & U_8 & U_9;
158-
deeper: <U_10 extends Object>(child: U_10) => any;
158+
deeper: <U_10 extends Object>(child: U_10) => /*elided*/ any;
159159
};
160160
};
161161
};

tests/baselines/reference/emitClassExpressionInDeclarationFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ export declare var simpleExample: {
107107
};
108108
export declare var circularReference: {
109109
new (): {
110-
tags(c: any): any;
110+
tags(c: /*elided*/ any): /*elided*/ any;
111111
};
112112
getTags(c: {
113-
tags(c: any): any;
113+
tags(c: /*elided*/ any): /*elided*/ any;
114114
}): {
115-
tags(c: any): any;
115+
tags(c: /*elided*/ any): /*elided*/ any;
116116
};
117117
};
118118
export declare class FooItem {

tests/baselines/reference/functionExpressionReturningItself.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ var x = function somefn() { return somefn; };
88

99

1010
//// [functionExpressionReturningItself.d.ts]
11-
declare var x: () => any;
11+
declare var x: () => /*elided*/ any;

0 commit comments

Comments
 (0)