-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Added typeToTypeNode with truncation #59332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f6e323c
9092688
44568f7
c1b1d40
52c2ff1
05431a6
5810eab
5891f32
90dade9
3b44569
2cbb9f3
ac4ba37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
addRange, | ||
addRelatedInfo, | ||
addSyntheticLeadingComment, | ||
addSyntheticTrailingComment, | ||
AliasDeclarationNode, | ||
AllAccessorDeclarations, | ||
AmbientModuleDeclaration, | ||
|
@@ -7054,6 +7055,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
|
||
function createTypeNodesFromResolvedType(resolvedType: ResolvedType): TypeElement[] | undefined { | ||
if (checkTruncationLength(context)) { | ||
if (context.flags & NodeBuilderFlags.NoTruncation) { | ||
return [addSyntheticTrailingComment(factory.createNotEmittedTypeElement(), SyntaxKind.MultiLineCommentTrivia, "elided")]; | ||
} | ||
return [factory.createPropertySignature(/*modifiers*/ undefined, "...", /*questionToken*/ undefined, /*type*/ undefined)]; | ||
} | ||
const typeElements: TypeElement[] = []; | ||
|
@@ -7085,7 +7089,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
} | ||
} | ||
if (checkTruncationLength(context) && (i + 2 < properties.length - 1)) { | ||
typeElements.push(factory.createPropertySignature(/*modifiers*/ undefined, `... ${properties.length - i} more ...`, /*questionToken*/ undefined, /*type*/ undefined)); | ||
if (context.flags & NodeBuilderFlags.NoTruncation) { | ||
const typeElement = typeElements.pop()!; | ||
typeElements.push(addSyntheticTrailingComment(typeElement, SyntaxKind.MultiLineCommentTrivia, `... ${properties.length - i} more elided ...`)); | ||
} | ||
else { | ||
typeElements.push(factory.createPropertySignature(/*modifiers*/ undefined, `... ${properties.length - i} more ...`, /*questionToken*/ undefined, /*type*/ undefined)); | ||
} | ||
addPropertyToElementList(properties[properties.length - 1], context, typeElements); | ||
break; | ||
} | ||
|
@@ -7100,7 +7110,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
if (!(context.flags & NodeBuilderFlags.NoTruncation)) { | ||
return factory.createTypeReferenceNode(factory.createIdentifier("..."), /*typeArguments*/ undefined); | ||
} | ||
return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword); | ||
return addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, "elided"); | ||
} | ||
|
||
function shouldUsePlaceholderForProperty(propertySymbol: Symbol, context: NodeBuilderContext) { | ||
|
@@ -7260,12 +7270,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
if (some(types)) { | ||
if (checkTruncationLength(context)) { | ||
if (!isBareList) { | ||
return [factory.createTypeReferenceNode("...", /*typeArguments*/ undefined)]; | ||
return [ | ||
context.flags & NodeBuilderFlags.NoTruncation | ||
? addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, "elided") | ||
: factory.createTypeReferenceNode("...", /*typeArguments*/ undefined), | ||
]; | ||
} | ||
else if (types.length > 2) { | ||
return [ | ||
typeToTypeNodeHelper(types[0], context), | ||
factory.createTypeReferenceNode(`... ${types.length - 2} more ...`, /*typeArguments*/ undefined), | ||
context.flags & NodeBuilderFlags.NoTruncation | ||
? addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, `... ${types.length - 2} more elided ...`) | ||
: factory.createTypeReferenceNode(`... ${types.length - 2} more ...`, /*typeArguments*/ undefined), | ||
typeToTypeNodeHelper(types[types.length - 1], context), | ||
]; | ||
} | ||
|
@@ -7278,7 +7294,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
for (const type of types) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I confused or is this truncating things more when the flag is enabled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not more per se, but it is replacing the previous values with an We kinda have 3 states: when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide to remove all of the types and just have an
We can instead count all of the types that were elided and put that like the example below buy I honestly prefer just having
|
||
i++; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this is discarding all the parts we already actually got to before hitting the truncation limit - shouldn't this all be something more like result.push(context.flags & NodeBuilderFlags.NoTruncation ? addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, `... ${types.length - i} more elided ...`) : factory.createTypeReferenceNode(`... ${types.length - i} more ...`, /*typeArguments*/ undefined)); merged into the line below? This way we still get something like
out? (Part of the reason elision is a bit special here is because it endeavors to retain the last element, since elision only really makes sense in the middle.)
for the whole union, even if there's enough allotted space for a lot of the union. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Including part of types then truncating with an Arguably, adding the types might serve as documentation, but it will be incorrect as we're not including all of the types, just some of them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary for the type arithmetic? Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed as requested but I have some concerns. I understand your point about retaining context, however, I respectfully disagree because the amount of code generated might become too large to be useful. I fear even the comment might go unnoticed. If this happens, including partial types and then truncating with any can lead to confusion and inaccuracies. I feel is more efficient to replace the entire section with i.e take a look at the test |
||
if (checkTruncationLength(context) && (i + 2 < types.length - 1)) { | ||
result.push(factory.createTypeReferenceNode(`... ${types.length - i} more ...`, /*typeArguments*/ undefined)); | ||
result.push( | ||
context.flags & NodeBuilderFlags.NoTruncation | ||
? addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, `... ${types.length - i} more elided ...`) | ||
: factory.createTypeReferenceNode(`... ${types.length - i} more ...`, /*typeArguments*/ undefined), | ||
); | ||
const typeNode = typeToTypeNodeHelper(types[types.length - 1], context); | ||
if (typeNode) { | ||
result.push(typeNode); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,6 @@ var C = /** @class */ (function () { | |
declare class C { | ||
static D: { | ||
new (): {}; | ||
D: any; | ||
D: /*elided*/ any; | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a test that shows what this looks like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you mentioned, there's no test for this scenario. I removed this check, and nothing regressed. I'm not sure how to test it but if it's utterly necessary I can address that another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://app.codecov.io/gh/microsoft/TypeScript/pull/59332?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=checks&utm_campaign=pr+comments&utm_term=microsoft implies that this code is covered somewhere in a test. Maybe add an assert and reverse engineer a specific test from that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After deeper analysis, the file
hugeDeclarationOutputGetsTruncatedWithError.types
access this code.Check line 18, for the last property
zz
. It changes from"zz": { ...; }
tozz: { };
. This is a "type" tests, during emit the "elided" comment gets added:zz: { /*elided*/ };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we add
elided
in type tests?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was working on it. Added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, you're saying that in
*.types
files we will write"zz": { ...; }
but in emit we will writezz: { /*elided*/ };
? The thing I was interested in is whether or not we should do the latter for both, but maybe that's not what you were describing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I mean, before this change files in
*.types
files and during emit we write"zz": { ...; }
.After this change,
*.types
files and emit write"zz": { }
, and.d.ts
files"zz": { /*elided*/ }
respectively.See the test
codeFixClassImplementInterfaceNoTruncationProperties.ts
for the emit example.hugeDeclarationOutputGetsTruncatedWithError.types
shows the*.types
change.