feat(45679): support 'did you mean' diagnostics for string literal union#45723
Conversation
|
Looks pretty solid to me. I think for the codefix you should be able to use Then you should have enough information to re-run |
|
Thanks for the comment and tips! So, unfortunately, implementing this properly would be probably beyond my capacity... |
sandersn
left a comment
There was a problem hiding this comment.
I like what you have here. Two minor requests.
I looked at the other spelling codefixes. They all work on right-hand sides (rhs) for which the value is misspelled, not the type. It's possible to inspect the assignment's rhs and offer the fix only in the case when it is a value with a unit type. But I don't think that needs to happen in this PR. Let me know whether you'd like to try that.
|
|
||
| function getSuggestedTypeForNonexistentStringLiteralType(source: StringLiteralType, target: UnionType): StringLiteralType | undefined { | ||
| const candidates = target.types.filter((type): type is StringLiteralType => !!(type.flags & TypeFlags.StringLiteral)); | ||
| return getSpellingSuggestion(source.value, candidates, (type) => type.value); |
There was a problem hiding this comment.
very minor: no parens around arrow parameter
| return getSpellingSuggestion(source.value, candidates, (type) => type.value); | |
| return getSpellingSuggestion(source.value, candidates, type => type.value); |
| if (source.flags & TypeFlags.StringLiteral && target.flags & TypeFlags.Union) { | ||
| const suggestedType = getSuggestedTypeForNonexistentStringLiteralType(source as StringLiteralType, target as UnionType); | ||
| if (suggestedType) { | ||
| reportError(Diagnostics.Type_0_is_not_assignable_to_type_1_Did_you_mean_2, sourceType, targetType, typeToString(suggestedType)); |
There was a problem hiding this comment.
Currently, generalizedSourceType must === sourceType in this case, but it would be better to use generalizedSourceType here, like the other reportErrors, in case we add some additional processing at the beginning of the function.
| reportError(Diagnostics.Type_0_is_not_assignable_to_type_1_Did_you_mean_2, sourceType, targetType, typeToString(suggestedType)); | |
| reportError(Diagnostics.Type_0_is_not_assignable_to_type_1_Did_you_mean_2, generalizedSourceType, targetType, typeToString(suggestedType)); |
|
@sandersn Thanks for the review! I addressed your feedback in 3dba734. Thanks for suggestion about codefix as well. But for the codefix feature, I cannot really estimate how it's feasible for me to implement right now, so as you mention, I'd like it to be separate PR from this one if I would even try it. Other contributors might be able to pick it up, so maybe it's better in that way. If there's anything else I can do on this PR, please let me know. Thank you so much! |
Fixes #45679
This PR adds a new diagnostic message for assignment type error with "did you mean" suggestion.
I made a new if-branch in
reportRelationErrorfor the case whensourceisStringLiteralTypeandtargetisUnionTypeand it tries to find suggestion with the existing utility functiongetSpellingSuggestion.So far I only tested with simple examples I can think of, but if more complicated cases are desired, I'm happy to add more.
So please let me know if that's the case.
Thanks a lot!
EDIT:
I thought I could also try to implement codefix service for this "did you mean" suggestion, but it doesn't look so easy since there seems no way of obtaining
targettype information at the time ofgetCodeActionsin fixSpelling.ts. I might be missing something here, so I would appreciate if there's some advice on this.