Skip to content

Add missing arguments to typeToTypeNode. #38336

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

Merged
merged 3 commits into from
May 5, 2020

Conversation

DanielRosenwasser
Copy link
Member

Fixes #38335.

if (!returnTypeNode) {
return undefined;
}

const returnType = checker.getTypeFromTypeNode(returnTypeNode);
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
const promisedTypeNode = checker.typeToTypeNode(promisedType);
const promisedTypeNode = checker.typeToTypeNode(promisedType, /*enclosingDeclaration*/ func, /*flags*/ undefined);
Copy link
Member

Choose a reason for hiding this comment

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

It looks like CFA can't tell func is not undefined here?

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's right - we can tell that returnTypeNode is defined but we don't tie that info back.

Copy link
Member Author

Choose a reason for hiding this comment

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

Technically if we removed the check though, we'd be in a bad situation.

Copy link
Member

Choose a reason for hiding this comment

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

I was just observing that there seemed to be a potential improvement that could be made to CFA, not suggesting any changes to this code.

@DanielRosenwasser DanielRosenwasser merged commit 94c5c3f into master May 5, 2020
@DanielRosenwasser DanielRosenwasser deleted the fixBrokenTypeToTypeNode branch May 5, 2020 02:32
@weswigham
Copy link
Member

weswigham commented May 5, 2020

So, important, after-the-fact question: Why does this quick-fix need to serialize the type at all (in most cases, at least)? Couldn't it just construct a type reference to promise around (a clone of) the existing type node, without re-serializing it? Eg, createTypeReferenceNode(createIdentifier("Promise"), [getDeepSynthesizedClone(returnTypeNode)])?

@amcasey
Copy link
Member

amcasey commented May 5, 2020

@weswigham I could be misreading either your comment or the code, but I think it's removing Promise<>, not adding it. Though I agree that that could also be syntactic in many cases, if that's an important improvement (I think formatting will be lost anyway?).

@weswigham
Copy link
Member

Ah yeah. Even better in that case, yeah, we could just extract the type parameter from the reference and reuse it any time that node's type matches the awaited type. This way we'd preserve any aliases/idiosyncrasies the user used, whenever possible.

@DanielRosenwasser
Copy link
Member Author

I thought about this, but the PR is already in. We'd want to see if the awaited type is the same as the original type and if so just wrap it in Promise<...>. I'd definitely encourage a follow-up issue though if you can write up an example.

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.

master is broken from incorrect call to typeToTypeNode
3 participants