Skip to content

fix(material/datepicker): Return union of view component types #23000

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 1 commit into from
Jun 18, 2021

Conversation

rslawik
Copy link
Contributor

@rslawik rslawik commented Jun 16, 2021

TypeScript inferred the return type of _getCurrentViewComponent as the type of the month view only. Tsickle propagates that to the JavaScript compiler which optimizes the code as monthView._init was the only method that could be called. This causes runtime errors.

Fixed #22996

@rslawik rslawik requested a review from mmalerba as a code owner June 16, 2021 21:01
@google-cla google-cla bot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jun 16, 2021
private _getCurrentViewComponent(): MatMonthView<D> | MatYearView<D> | MatMultiYearView<D> {
// The return type must be a union to ensure that the Closure compiler does not optimize the
// calls to _init(). See https://github.com/angular/components/issues/22996.
switch (this.currentView) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this switch required for the Closure compiler fix? The comment sounds like only the return type change would be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only the return type change is necessary.

However, switch makes it clear that we chose the current view component and does not rely on implicit falsyness of class fields that are non-nullable. The code had two ways of selecting the component (ifs and ||), I unified them.

The comment is to ensure that future refactorings do not revert to || and reduce the return type to only one type. IMO, it is an easy mistake to remove the return type of:

returnValue(): A | B {
  return this.a || this.b;
}

Copy link
Member

Choose a reason for hiding this comment

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

It might just be me (also since I wrote the code to begin with), but I think I prefer the || since it's more compact and it doesn't depend on matching class instances to strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, changed back to use || and extended the comment with little more context.

I assume the existing code that did the string to view mapping (L391-L397) does not need to be restored, so I keep them unified.

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

LGTM

@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker labels Jun 17, 2021
@crisbeto
Copy link
Member

@rslawik it looks like your latest commit fails our commit title lint check. It needs to be prefixed with fix(material/datepicker) or squashed into the initial commit.

TypeScript inferred the return type of `_getCurrentViewComponent` as the type of the month view only. Tsickle propagates that to the JavaScript compiler which optimizes the code as `monthView._init` was the only method that could be called. This causes runtime errors.

Fixed angular#22996
@rslawik
Copy link
Contributor Author

rslawik commented Jun 18, 2021

Thank you very much for the help, @crisbeto! It passed all the checks.

@mmalerba mmalerba merged commit fafb24d into angular:master Jun 18, 2021
mmalerba pushed a commit that referenced this pull request Jun 18, 2021
TypeScript inferred the return type of `_getCurrentViewComponent` as the type of the month view only. Tsickle propagates that to the JavaScript compiler which optimizes the code as `monthView._init` was the only method that could be called. This causes runtime errors.

Fixed #22996

(cherry picked from commit fafb24d)
mmalerba pushed a commit that referenced this pull request Jun 18, 2021
TypeScript inferred the return type of `_getCurrentViewComponent` as the type of the month view only. Tsickle propagates that to the JavaScript compiler which optimizes the code as `monthView._init` was the only method that could be called. This causes runtime errors.

Fixed #22996

(cherry picked from commit fafb24d)
@rslawik rslawik deleted the datepicker-init branch June 18, 2021 17:46
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(datepicker): mat-calendar ngOnChanges incorrectly optimized by Closure compiler
3 participants