Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed elided comment
  • Loading branch information
Armando Aguirre Sepulveda committed Aug 21, 2024
commit 90dade912a092601f823c8eae994c5567c1cbc31
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7292,7 +7292,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
i++;
if (checkTruncationLength(context) && (i + 2 < types.length - 1)) {
if (context.flags & NodeBuilderFlags.NoTruncation) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 any

We kinda have 3 states: when NoTruncation is disabled, we truncate up to defaultMaximumTruncationLength, if NoTruncation is enabled it truncates up to noTruncationMaximumTruncationLength, anything after that is an any.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A ... ${types.length - i} more elided ... comment would be more in-line with the non-grammatical node this replaces when NoTruncation is set.

Copy link
Copy Markdown
Contributor Author

@armanio123 armanio123 Aug 21, 2024

Choose a reason for hiding this comment

The 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 any it will not make sense to have this message as the code will look like:

 foo(bar: /*... 516 more elided ...*/ any): /*elided*/ any {
        throw new Error("Method not implemented.");
    }
    get bar(): /*... 516 more elided ...*/ any {
        throw new Error("Method not implemented.");
    }
    set bar(value: /*... 516 more elided ...*/ any) {
        throw new Error("Method not implemented.");
    }
    baz<V extends /*... 516 more elided ...*/ any>(value: V): V {
        throw new Error("Method not implemented.");
    }

We can instead count all of the types that were elided and put that like the example below buy I honestly prefer just having /*elided*/ for something short and descriptive enough IMO.

 foo(bar: /*... 800 elided ...*/ any): /*... 800 elided ...*/ any {
       throw new Error("Method not implemented.");
 }

return [addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, "elided")];
return [addSyntheticLeadingComment(factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), SyntaxKind.MultiLineCommentTrivia, `... ${types.length - i} more elided ...`)];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

A | B | C | /* ... 6 more elided ... */ any | undefined

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.)
As written, this is just going to output

/* elided */ any

for the whole union, even if there's enough allotted space for a lot of the union.

Copy link
Copy Markdown
Contributor Author

@armanio123 armanio123 Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including part of types then truncating with an any seems to me like it would be incorrect and unnecessary, even for the last type. Instead, I would argue that if we are truncating and putting any anyway, we might as well remove the whole thing and replace it for an any.

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.

Copy link
Copy Markdown
Member

@weswigham weswigham Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary for the type arithmetic? Yes. A | B | any is just any. Unnecessary for the human that has to go and fill in the blank after the quickfix runs? Probably not! The more context we retain, the easier it is for the human that has to go and fix this fix's output with what's supposed to go there!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 any to maintain clarity and correctness. This way, we avoid the risk of misinterpretation and ensure the code remains manageable.

i.e take a look at the test codeFixClassImplementInterfaceNoTruncation.ts in the latest change, finding where the parameter type ends is already complicated. A user noticing the comment, or the introduction of any seems likely to be missed.

}
result.push(factory.createTypeReferenceNode(`... ${types.length - i} more ...`, /*typeArguments*/ undefined));
const typeNode = typeToTypeNodeHelper(types[types.length - 1], context);
Expand Down