Skip to content
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

Clone signature return type nodes if they are present and yield the same type as the signature return type #23296

Closed
wants to merge 3 commits into from

Conversation

weswigham
Copy link
Member

@weswigham weswigham commented Apr 10, 2018

In #23280 @andy-ms brought up that we never preserve aliases used to create inferred types (which makes sense, given that we immediately collapse type aliases down to their target types). With this PR, we preserve and clone return types of signatures (and thereby their aliases) provided they still yield the same type in the new context. This won't solve the issue in the OP of #23280 (as the alias is annotated within the function body and not as the signature return type node itself), but does make @andy-ms's simplified example work as one may hope.

I'm not 100% sold on this always being a good thing (checkout the type baselines for all the places where any became a copy of the erroneous declaration that generated any), but it does produce nice results in many places (including preserving input style).

@weswigham weswigham changed the title Clone signature return type nodes if they are present and yield the same type Clone signature return type nodes if they are present and yield the same type as the signature return type Apr 10, 2018
@weswigham weswigham requested review from rbuckton, a user and mhegazy April 10, 2018 00:15
@rbuckton
Copy link
Member

@weswigham can you look at the failing test in the build?

@@ -2797,7 +2797,7 @@ namespace ts {
}
}

function isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult {
function resolveNameFromEntityName(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node) {
Copy link
Member

Choose a reason for hiding this comment

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

Don't we already have a resolveEntityName function?

Copy link
Member Author

Choose a reason for hiding this comment

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

This skips a lot of the work resolveEntityName does to verify namespaceyness and also generates an appropriate meaning from the entityName's parent. So they're a bit different, unfortunately.

Copy link
Member

Choose a reason for hiding this comment

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

We either should have only one function with options to control behavior (preferred), or should clearly document the difference between the two.

function getDeepSynthesizedCloneAndPaintVisibility<T extends Node>(node: T | undefined): T | undefined {
if (isEntityName(node) || isEntityNameExpression(node)) {
// Paint visible aliases required for copied nodes (so required imports are not elided)
isEntityNameVisible(node, context.enclosingDeclaration);
Copy link
Member

Choose a reason for hiding this comment

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

Calling isEntityNameVisible without using the result is odd. I understand what this is doing, but perhaps its better to split isEntityNameVisible into two functions: one that tracks visibility and one that uses the result.

Copy link
Member Author

Choose a reason for hiding this comment

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

That'd just be hasVisibleDeclarations(resolveNameFromEntityName(entityName, enclosingDeclaration), /*shouldComputeAliasToMakeVisible*/ true), I suppose?

@typescript-bot
Copy link
Collaborator

Thanks for your contribution. This PR has not been updated in a while and cannot be automatically merged at the time being. For housekeeping purposes we are closing stale PRs. If you'd still like to continue working on this PR, please leave a message and one of the maintainers can reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants