Skip to content

Add related diagnostic to "used before defined" if type is a function that returns a union with undefined #33171

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 6 commits into from
Nov 4, 2019

Conversation

OzairP
Copy link
Contributor

@OzairP OzairP commented Aug 31, 2019

Fixes #32846

This PR conforms to the requirements in #32846 with one distinct difference.
Instead of signaling a distinct error, the error is added as related info to TS2454 (used before defined).

I came to this conclusion after writing this test case.

type T4 = () => void | undefined;
let j: T4;
j; // Error did you mean to paren...

Here it may be so that they meant to parenthesize () => void but considering there may be many lines of code between the type alias and the variable declaration, the origin of the error becomes unclear.
However if the diagnostic is added as related information the origin of the error becomes clear if it was intentional or unintentional.

If is not agreed that this diagnostic should be related information I am good for changing it. 👍

@msftclas
Copy link

msftclas commented Aug 31, 2019

CLA assistant check
All CLA requirements met.

const parenedFuncType = getMutableClone(funcTypeNode);
// Highlight to the end of the second to last constituent of the union
parenedFuncType.end = unionTypes[unionTypes.length - 2].end;
addRelatedInfo(diag, createDiagnosticForNode(parenedFuncType, Diagnostics.Did_you_mean_to_parenthesize_this_function_type));
Copy link
Member

@DanielRosenwasser DanielRosenwasser Sep 3, 2019

Choose a reason for hiding this comment

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

Instead of cloning the node, I believe we should have another function that takes a start/end (createFileDiagnostic?). To get the correct start (that ignores whitespace), there should be a skipTrivia function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm unclear on what "trivia" is exactly.

Copy link
Contributor

Choose a reason for hiding this comment

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

@OzairP
Copy link
Contributor Author

OzairP commented Sep 17, 2019

Will be picking back up on this PR and resolving the comments this week 👍

@m-henderson
Copy link

@OzairP are you going to pick back up on this PR?

@OzairP
Copy link
Contributor Author

OzairP commented Sep 28, 2019

@m-henderson yes I am picking back up on it this weekend.

@orta orta self-assigned this Nov 4, 2019
@orta
Copy link
Contributor

orta commented Nov 4, 2019

Alright, this looks good to me - thanks @OzairP
Great job on your first PR!

@orta orta merged commit be960fa into microsoft:master Nov 4, 2019
orta added a commit that referenced this pull request Dec 18, 2019
…function that returns a union with undefined (#33171)"

This reverts commit be960fa.
orta pushed a commit that referenced this pull request Jan 15, 2020
…function that returns a union with undefined (#33171)" (#35751)

This reverts commit be960fa.
Kingwl pushed a commit to Kingwl/TypeScript that referenced this pull request Mar 4, 2020
…function that returns a union with undefined (microsoft#33171)" (microsoft#35751)

This reverts commit be960fa.
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.

Provide a better error message for "used before defined" on incorrectly annotated functions
5 participants